From cad4d9b0d584425a8ca9391e2f63f8a6ee0165e1 Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Fri, 13 Sep 2013 09:48:48 -0500 Subject: [PATCH] Test for tab bar controller --- hacking/TabBar.js | 54 ++++++++++++++++++++++++++++++++ hacking/TabBar.unit.js | 49 +++++++++++++++++++++++++++++ hacking/TabBarController.js | 7 +---- hacking/TabBarController.unit.js | 8 +++-- hacking/TestRunner.html | 43 ------------------------- ionic.conf.js | 6 ++-- 6 files changed, 112 insertions(+), 55 deletions(-) create mode 100644 hacking/TabBar.js create mode 100644 hacking/TabBar.unit.js delete mode 100644 hacking/TestRunner.html diff --git a/hacking/TabBar.js b/hacking/TabBar.js new file mode 100644 index 0000000000..d00d6953be --- /dev/null +++ b/hacking/TabBar.js @@ -0,0 +1,54 @@ +TabBarItem = function(el) { + this.el = el; +}; +TabBarItem.prototype = { + setSelected: function(isSelected) { + this.isSelected = isSelected; + if(isSelected) { + this.el.classList.add('active'); + } else { + this.el.classList.remove('active'); + } + } +}; + +TabBar = function(opts) { + this.el = opts.el; + this._buildItems(); +}; + +TabBar.prototype = { + getSelectedItem: function() { + return this.selectedItem; + }, + setSelectedItem: function(index) { + this.selectedItem = this.items[index]; + + // Deselect all + for(var i = 0, j = this.items.length; i < j; i += 1) { + this.items[i].setSelected(false); + } + + // Select the new item + this.selectedItem && this.selectedItem.setSelected(true); + }, + + getItems: function() { + return this.items; + }, + + _buildItems: function() { + this.items = []; + + var items = this.el.children; + + for(var i = 0, j = items.length; i < j; i += 1) { + this.items[i] = new TabBarItem(items[i]); + } + + if(this.items.length > 0) { + this.selectedItem = this.items[0]; + } + } +}; + diff --git a/hacking/TabBar.unit.js b/hacking/TabBar.unit.js new file mode 100644 index 0000000000..52599cc853 --- /dev/null +++ b/hacking/TabBar.unit.js @@ -0,0 +1,49 @@ +describe('TabBar view', function() { + var element, tabBar, items; + + beforeEach(function() { + element = $('
' + + 'Tab 1' + + 'Tab 2' + + 'Tab 3'); + + tabBar = new TabBar({ + el: element.get(0) + }); + + }); + it('Should read tabs', function() { + + items = tabBar.getItems(); + + expect(items.length).toEqual(3); + expect(items[0].el.innerText).toEqual('Tab 1'); + expect(items[1].el.innerText).toEqual('Tab 2'); + expect(items[2].el.innerText).toEqual('Tab 3'); + }); + + it('Should select', function() { + items = tabBar.getItems(); + + // Track selection object + tabBar.setSelectedItem(1); + expect(tabBar.getSelectedItem().el.innerText).toEqual('Tab 2'); + tabBar.setSelectedItem(0); + expect(tabBar.getSelectedItem().el.innerText).toEqual('Tab 1'); + tabBar.setSelectedItem(2); + expect(tabBar.getSelectedItem().el.innerText).toEqual('Tab 3'); + + // Track class change + expect(tabBar.getSelectedItem().el.classList.contains('active')).toEqual(true); + + // Make sure the other ones have theirs cleared + expect(items[0].el.classList.contains('active')).toEqual(false); + expect(items[1].el.classList.contains('active')).toEqual(false); + expect(items[2].el.classList.contains('active')).toEqual(true); + + tabBar.setSelectedItem(0); + expect(items[0].el.classList.contains('active')).toEqual(true); + expect(items[1].el.classList.contains('active')).toEqual(false); + expect(items[2].el.classList.contains('active')).toEqual(false); + }); +}); diff --git a/hacking/TabBarController.js b/hacking/TabBarController.js index 019c9e3ec6..29dea4abda 100644 --- a/hacking/TabBarController.js +++ b/hacking/TabBarController.js @@ -1,8 +1,3 @@ -TabBar = function(element) { - this.element = element; -} -TabBar.prototype = {}; - TabBarController = function(options) { this.tabBar = options.tabBar; @@ -73,4 +68,4 @@ TabBarController.prototype = { this._clearSelected(); this.selectController(0); } -} \ No newline at end of file +} diff --git a/hacking/TabBarController.unit.js b/hacking/TabBarController.unit.js index 138943f59d..bfb503fdca 100644 --- a/hacking/TabBarController.unit.js +++ b/hacking/TabBarController.unit.js @@ -2,8 +2,9 @@ describe('TabBarController', function() { var ctrl; beforeEach(function() { + var tabEl = $('
'); ctrl = new TabBarController({ - tabBar: new TabBar() + tabBar: new TabBar({ el: tabEl }) }); }) @@ -53,8 +54,9 @@ describe('TabBarController', function() { }); it('Should allow cancelling Controller switch', function() { + var tabEl = $('
'); ctrl = new TabBarController({ - tabBar: new TabBar(), + tabBar: new TabBar({ el: tabEl }), controllerWillChange: function(Controller) { return false; } }); @@ -70,4 +72,4 @@ describe('TabBarController', function() { expect(ctrl.getSelectedController()).toBe(ctrl.getController(0)); }); -}) \ No newline at end of file +}) diff --git a/hacking/TestRunner.html b/hacking/TestRunner.html deleted file mode 100644 index 590d912029..0000000000 --- a/hacking/TestRunner.html +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/ionic.conf.js b/ionic.conf.js index 8062afc518..c167c50efa 100644 --- a/ionic.conf.js +++ b/ionic.conf.js @@ -19,10 +19,10 @@ module.exports = function(config) { 'https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', 'vendor/angular/1.2.0rc1/*', - 'ext/angular/src/**/*.js', - 'ext/angular/test/**/*.js', + //'ext/angular/src/**/*.js', + //'ext/angular/test/**/*.js', 'hacking/**/*.js', - 'test/**/*.js' + //'test/**/*.js' ],