mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-07 23:16:52 +08:00
Tests organization and cleanup
Moved hacking tests to test folder, got rid of ionic=window.ionic for wrapper functions in JS files.
This commit is contained in:
60
test/js/controllers/navController.unit.js
Normal file
60
test/js/controllers/navController.unit.js
Normal file
@ -0,0 +1,60 @@
|
||||
describe('NavController', function() {
|
||||
var ctrl, navBarEl, contentEl;
|
||||
|
||||
var content = function(title) {
|
||||
return {
|
||||
el: document.createElement('div'),
|
||||
title: title,
|
||||
detach: function() {
|
||||
this.el.parentNode && this.el.parentNode.removeChild(this.el);
|
||||
},
|
||||
attach: function() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
beforeEach(function() {
|
||||
navBarEl = document.createElement('div');
|
||||
contentEl = document.createElement('div');
|
||||
|
||||
ctrl = new ionic.controllers.NavController({
|
||||
navBar: new ionic.views.NavBar({el: navBarEl }),
|
||||
content: { el: contentEl }
|
||||
});
|
||||
});
|
||||
|
||||
it('Should load controllers', function() {
|
||||
ctrl = new ionic.controllers.NavController({
|
||||
navBar: new ionic.views.NavBar({el: navBarEl }),
|
||||
content: { el: contentEl },
|
||||
controllers: [{}]
|
||||
});
|
||||
expect(ctrl.getControllers().length).toEqual(1);
|
||||
});
|
||||
|
||||
it('Should push controller', function() {
|
||||
ctrl.push(content('Page 1'));
|
||||
expect(ctrl.getControllers().length).toEqual(1);
|
||||
ctrl.push(content('Page 2'));
|
||||
expect(ctrl.getControllers().length).toEqual(2);
|
||||
var last = ctrl.pop();
|
||||
expect(ctrl.getControllers().length).toEqual(1);
|
||||
expect(last.title).toEqual('Page 2');
|
||||
});
|
||||
|
||||
it('Should change top view controller', function() {
|
||||
expect(ctrl.getTopController()).toBe(undefined);
|
||||
var c1 = content('Page 1');
|
||||
var c2 = content('Page 2');
|
||||
ctrl.push(c1);
|
||||
expect(ctrl.getTopController()).toEqual(c1);
|
||||
ctrl.push(c2);
|
||||
expect(ctrl.getTopController()).toEqual(c2);
|
||||
ctrl.pop();
|
||||
expect(ctrl.getTopController()).toEqual(c1);
|
||||
|
||||
// Make sure we can't pop the first one off
|
||||
ctrl.pop();
|
||||
expect(ctrl.getTopController()).toEqual(c1);
|
||||
});
|
||||
});
|
||||
158
test/js/controllers/sideMenuController.unit.js
Normal file
158
test/js/controllers/sideMenuController.unit.js
Normal file
@ -0,0 +1,158 @@
|
||||
describe('SideMenuController', function() {
|
||||
var ctrl, l, r, c;
|
||||
|
||||
var Controller = function(opts) {
|
||||
this.el = opts.el;
|
||||
this.animateClass = opts.animateClass;
|
||||
};
|
||||
Controller.prototype = {
|
||||
getTranslateX: function() {
|
||||
var r = /translate3d\((-?.+)px/;
|
||||
var d = r.exec(this.el.style.webkitTransform);
|
||||
|
||||
if(d && d.length > 0) {
|
||||
return parseFloat(d[1]);
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
setTranslateX: function(amount) {
|
||||
this.el.style.webkitTransform = 'translate3d(' + amount + 'px, 0, 0)';
|
||||
},
|
||||
enableAnimation: function() {
|
||||
this.el.classList.add(this.animateClass);
|
||||
},
|
||||
disableAnimation: function() {
|
||||
this.el.classList.remove(this.animateClass);
|
||||
}
|
||||
};
|
||||
|
||||
beforeEach(function() {
|
||||
l = new ionic.views.SideMenu({ el: document.createElement('div'), width: 270 });
|
||||
r = new ionic.views.SideMenu({ el: document.createElement('div'), width: 270 });
|
||||
c = new Controller({ el: document.createElement('div') });
|
||||
|
||||
ctrl = new ionic.controllers.SideMenuController({
|
||||
left: l,
|
||||
right: r,
|
||||
content: c
|
||||
});
|
||||
});
|
||||
|
||||
// Init
|
||||
it('Should init', function() {
|
||||
expect(ctrl.left).toBe(l);
|
||||
expect(ctrl.right).toBe(r);
|
||||
expect(ctrl.content).toBe(c);
|
||||
});
|
||||
|
||||
// Menu enable/disable
|
||||
it('Should set enabled/disabled', function() {
|
||||
var left = ctrl.left;
|
||||
var right = ctrl.right;
|
||||
left.setIsEnabled(false);
|
||||
right.setIsEnabled(false);
|
||||
expect(left.isEnabled).toEqual(false);
|
||||
expect(right.isEnabled).toEqual(false);
|
||||
left.setIsEnabled(true);
|
||||
right.setIsEnabled(true);
|
||||
expect(left.isEnabled).toEqual(true);
|
||||
expect(right.isEnabled).toEqual(true);
|
||||
});
|
||||
|
||||
// Menu widths
|
||||
it('Should init widths', function() {
|
||||
var left = ctrl.left;
|
||||
var right = ctrl.right;
|
||||
expect(left.width).toEqual(270);
|
||||
expect(right.width).toEqual(270);
|
||||
});
|
||||
|
||||
it('Should have amount and percentage correct', function() {
|
||||
ctrl.openAmount(l.width/2);
|
||||
expect(ctrl.getOpenAmount()).toEqual(l.width/2);
|
||||
expect(ctrl.getOpenPercentage()).toEqual(50);
|
||||
|
||||
ctrl.openAmount(l.width/4);
|
||||
expect(ctrl.getOpenAmount()).toEqual(l.width/4);
|
||||
expect(ctrl.getOpenPercentage()).toEqual(25);
|
||||
|
||||
ctrl.openAmount(-r.width/2);
|
||||
expect(ctrl.getOpenAmount()).toEqual(-r.width/2);
|
||||
expect(ctrl.getOpenPercentage()).toEqual(-50);
|
||||
});
|
||||
|
||||
// Open
|
||||
it('Should toggle left and right', function() {
|
||||
ctrl.toggleLeft();
|
||||
expect(ctrl.getOpenPercentage()).toEqual(100);
|
||||
ctrl.toggleRight();
|
||||
expect(ctrl.getOpenPercentage()).toEqual(-100);
|
||||
});
|
||||
|
||||
// Snap
|
||||
it('Should snap', function() {
|
||||
|
||||
// Center to right, Going right, less than half, too slow (snap back)
|
||||
ctrl.openAmount(10);
|
||||
ctrl.snapToRest({
|
||||
gesture: {
|
||||
velocityX: 0,
|
||||
direction: 'right'
|
||||
}
|
||||
});
|
||||
expect(ctrl.getOpenPercentage()).toEqual(0);
|
||||
|
||||
// Right to left, Going left, more than half, too slow (snap back)
|
||||
ctrl.openPercentage(51);
|
||||
ctrl.snapToRest({
|
||||
gesture: {
|
||||
velocityX: 0,
|
||||
direction: 'left'
|
||||
}
|
||||
});
|
||||
expect(ctrl.getOpenPercentage()).toEqual(100);
|
||||
|
||||
// Right to left, Going left, less than half, too slow (snap back)
|
||||
ctrl.openAmount(10);
|
||||
ctrl.snapToRest({
|
||||
gesture: {
|
||||
velocityX: 0,
|
||||
direction: 'left'
|
||||
}
|
||||
});
|
||||
expect(ctrl.getOpenPercentage()).toEqual(0);
|
||||
|
||||
// Left to right, Going right, more than half, too slow (snap back)
|
||||
ctrl.openPercentage(-51);
|
||||
ctrl.snapToRest({
|
||||
gesture: {
|
||||
velocityX: 0,
|
||||
direction: 'right'
|
||||
}
|
||||
});
|
||||
expect(ctrl.getOpenPercentage()).toEqual(-100);
|
||||
|
||||
// Going right, more than half, or quickly (snap open)
|
||||
ctrl.openPercentage(-51);
|
||||
ctrl.snapToRest({
|
||||
gesture: {
|
||||
velocityX: 1,
|
||||
direction: 'right'
|
||||
}
|
||||
});
|
||||
expect(ctrl.getOpenPercentage()).toEqual(0);
|
||||
|
||||
// Going left, more than half, or quickly (snap open)
|
||||
ctrl.openPercentage(-51);
|
||||
ctrl.snapToRest({
|
||||
gesture: {
|
||||
velocityX: 1,
|
||||
direction: 'left'
|
||||
}
|
||||
});
|
||||
expect(ctrl.getOpenPercentage()).toEqual(-100);
|
||||
});
|
||||
|
||||
it('Should test content drag events', function() {
|
||||
});
|
||||
});
|
||||
85
test/js/controllers/tabBarController.unit.js
Normal file
85
test/js/controllers/tabBarController.unit.js
Normal file
@ -0,0 +1,85 @@
|
||||
describe('TabBarController', function() {
|
||||
var ctrl;
|
||||
|
||||
beforeEach(function() {
|
||||
var tabEl = $('<div class="tabs"></div>');
|
||||
ctrl = new ionic.controllers.TabBarController({
|
||||
tabBar: new ionic.views.TabBar({
|
||||
el: tabEl.get(0)
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
it('Should add Controllers', function() {
|
||||
ctrl.addController({
|
||||
title: 'Item 1',
|
||||
icon: 'icon-home',
|
||||
});
|
||||
|
||||
expect(ctrl.getController(0).title).toEqual('Item 1');
|
||||
|
||||
var items = ctrl.tabBar.getItems();
|
||||
expect(items.length).toEqual(1);
|
||||
|
||||
expect(items[0].getTitle()).toEqual('Item 1');
|
||||
expect(items[0].getIcon()).toEqual('icon-home');
|
||||
|
||||
});
|
||||
|
||||
it('Should set Controllers', function() {
|
||||
var Controllers = [
|
||||
{ title: 'Item 1' },
|
||||
{ title: 'Item 2' },
|
||||
{ title: 'Item 3' },
|
||||
];
|
||||
ctrl.setControllers(Controllers);
|
||||
|
||||
expect(ctrl.getControllers()).toBe(Controllers);
|
||||
});
|
||||
|
||||
it('Should select Controller', function() {
|
||||
Controller = {
|
||||
title: 'Item 1'
|
||||
};
|
||||
|
||||
ctrl.addController(Controller);
|
||||
|
||||
ctrl.selectController(0);
|
||||
|
||||
expect(ctrl.getSelectedController()).toEqual(Controller);
|
||||
});
|
||||
|
||||
it('Should trigger lifecycle methods', function() {
|
||||
Controller = {
|
||||
title: 'Item 1'
|
||||
};
|
||||
|
||||
spyOn(ctrl, 'controllerWillChange');
|
||||
spyOn(ctrl, 'controllerChanged');
|
||||
|
||||
ctrl.addController(Controller);
|
||||
ctrl.selectController(0);
|
||||
expect(ctrl.controllerWillChange).toHaveBeenCalled();
|
||||
expect(ctrl.controllerChanged).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('Should allow cancelling Controller switch', function() {
|
||||
var tabEl = $('<div class="tabs"></div>');
|
||||
ctrl = new ionic.controllers.TabBarController({
|
||||
tabBar: new ionic.views.TabBar({ el: tabEl.get(0) }),
|
||||
controllerWillChange: function(Controller) { return false; }
|
||||
});
|
||||
|
||||
ctrl.addController({
|
||||
title: 'Item 1'
|
||||
});
|
||||
ctrl.addController({
|
||||
title: 'Item 2'
|
||||
});
|
||||
ctrl.selectController(1);
|
||||
|
||||
// Make sure the Controller didn't switch
|
||||
expect(ctrl.getSelectedController()).toBe(ctrl.getController(0));
|
||||
});
|
||||
|
||||
})
|
||||
Reference in New Issue
Block a user