|
1 |
| -describe('tabs', function() { |
| 1 | +ddescribe('tabs', function() { |
2 | 2 |
|
3 | 3 | describe('miscellaneous', function() {
|
4 | 4 | beforeEach(module('ionic', function($provide) {
|
@@ -261,12 +261,103 @@ describe('tabs', function() {
|
261 | 261 | });
|
262 | 262 | });
|
263 | 263 |
|
264 |
| - describe('ionTab directive', function() { |
| 264 | + describe('ionicTab controller', function() { |
265 | 265 | beforeEach(module('ionic'));
|
266 |
| - var tabsCtrl, tabsEl, scope; |
| 266 | + function setup(attrs) { |
| 267 | + var ctrl; |
| 268 | + inject(function($controller, $rootScope) { |
| 269 | + ctrl = $controller('ionicTab', { |
| 270 | + $scope: $rootScope.$new(), |
| 271 | + $attrs: attrs || {} |
| 272 | + }); |
| 273 | + }); |
| 274 | + return ctrl; |
| 275 | + } |
| 276 | + |
| 277 | + it('.hrefMatchesState', inject(function($location) { |
| 278 | + spyOn($location, 'path').andReturn('/a/b/c'); |
| 279 | + var attr = {}; |
| 280 | + var ctrl = setup(attr); |
| 281 | + |
| 282 | + expect(ctrl.hrefMatchesState()).toBeFalsy(); |
| 283 | + |
| 284 | + attr.href = 'a'; |
| 285 | + expect(ctrl.hrefMatchesState()).toBe(false); |
| 286 | + |
| 287 | + attr.href = '/a'; |
| 288 | + expect(ctrl.hrefMatchesState()).toBe(true); |
| 289 | + |
| 290 | + attr.href = '#/a'; |
| 291 | + expect(ctrl.hrefMatchesState()).toBe(true); |
| 292 | + |
| 293 | + attr.href = '#/a/b/c'; |
| 294 | + expect(ctrl.hrefMatchesState()).toBe(true); |
| 295 | + |
| 296 | + attr.href = '#/a/b/c/'; |
| 297 | + expect(ctrl.hrefMatchesState()).toBe(true); |
| 298 | + |
| 299 | + attr.href = '/a/b/c/'; |
| 300 | + expect(ctrl.hrefMatchesState()).toBe(true); |
| 301 | + |
| 302 | + attr.href = '/a/b/c/d'; |
| 303 | + expect(ctrl.hrefMatchesState()).toBe(false); |
| 304 | + |
| 305 | + attr.href = 'something'; |
| 306 | + expect(ctrl.hrefMatchesState()).toBe(false); |
| 307 | + })); |
| 308 | + |
| 309 | + it('.srefMatchesState', inject(function($state) { |
| 310 | + spyOn($state, 'includes').andReturn(111); |
| 311 | + var attr = {}; |
| 312 | + var ctrl = setup(attr); |
| 313 | + |
| 314 | + expect(ctrl.srefMatchesState()).toBeFalsy(); |
| 315 | + expect($state.includes).not.toHaveBeenCalled(); |
| 316 | + |
| 317 | + //We won't unit test $state.includes, only that it was called |
| 318 | + attr.uiSref = 'abc'; |
| 319 | + expect(ctrl.srefMatchesState()).toBe(111); |
| 320 | + expect($state.includes).toHaveBeenCalledWith('abc'); |
| 321 | + |
| 322 | + $state.includes.reset(); |
| 323 | + attr.uiSref = 'def({ param: "value" })'; |
| 324 | + ctrl.srefMatchesState(); |
| 325 | + expect($state.includes).toHaveBeenCalledWith('def'); |
| 326 | + })); |
| 327 | + |
| 328 | + it('.navNameMatchesState', inject(function($ionicViewService) { |
| 329 | + spyOn($ionicViewService, 'isCurrentStateNavView').andReturn(123); |
| 330 | + |
| 331 | + var ctrl = setup(); |
| 332 | + expect(ctrl.navNameMatchesState()).toBeFalsy(); |
| 333 | + |
| 334 | + ctrl.navViewName = 'foo'; |
| 335 | + expect(ctrl.navNameMatchesState()).toBe(123); |
| 336 | + expect($ionicViewService.isCurrentStateNavView).toHaveBeenCalledWith('foo'); |
| 337 | + })); |
| 338 | + }); |
| 339 | + |
| 340 | + describe('ionTab directive', function() { |
| 341 | + var tabDoesMatch; |
| 342 | + beforeEach(module('ionic', function($controllerProvider) { |
| 343 | + $controllerProvider.register('ionicTab', function($scope) { |
| 344 | + this.$scope = $scope; |
| 345 | + this.tabMatchesState = jasmine.createSpy('tabMatchesState') |
| 346 | + .andCallFake(function() { |
| 347 | + return tabDoesMatch; |
| 348 | + }); |
| 349 | + }); |
| 350 | + })); |
| 351 | + |
| 352 | + var tabsCtrl, tabsEl, scope, tabEl; |
267 | 353 | function setup(attrs, content) {
|
268 | 354 | inject(function($compile, $rootScope) {
|
269 |
| - tabsEl = angular.element('<ion-tabs><ion-tab '+(attrs||'')+'>'+(content||'')+'</ion-tab></ion-tabs>'); |
| 355 | + tabsEl = angular.element( |
| 356 | + '<ion-tabs>' + |
| 357 | + '<ion-tab '+(attrs||'')+'>'+(content||'')+'</ion-tab>' + |
| 358 | + '</ion-tabs>' |
| 359 | + ); |
| 360 | + tabEl = tabsEl.find('ion-tab'); |
270 | 361 |
|
271 | 362 | $compile(tabsEl)($rootScope.$new());
|
272 | 363 | $rootScope.$apply();
|
@@ -323,32 +414,48 @@ describe('tabs', function() {
|
323 | 414 | expect(navItem.parent().length).toBe(0);
|
324 | 415 | });
|
325 | 416 |
|
326 |
| - it('should not set navViewName if no child nav-view', function() { |
| 417 | + it('should not set navViewName by default', function() { |
327 | 418 | setup();
|
328 |
| - expect(tabsCtrl.tabs[0].navViewName).toBeUndefined(); |
| 419 | + expect(tabEl.controller('ionTab').navViewName).toBeUndefined(); |
329 | 420 | });
|
330 | 421 |
|
331 | 422 | angular.forEach(['ion-nav-view', 'data-ion-nav-view'], function(directive) {
|
332 |
| - it('should set navViewName and select when necessary if a child '+directive, inject(function($ionicViewService, $rootScope) { |
333 |
| - var isCurrent = false; |
334 |
| - spyOn($ionicViewService, 'isCurrentStateNavView').andCallFake(function(name) { |
335 |
| - return isCurrent; |
336 |
| - }); |
337 |
| - |
| 423 | + it('should set navViewName if a child '+directive, inject(function($ionicViewService, $rootScope) { |
338 | 424 | setup('', '<' + directive + ' name="banana"></' + directive + '>');
|
339 | 425 | spyOn(tabsCtrl, 'select');
|
340 | 426 | var tab = tabsCtrl.tabs[0];
|
341 | 427 |
|
342 |
| - expect(tab.navViewName).toBe('banana'); |
343 |
| - expect($ionicViewService.isCurrentStateNavView).toHaveBeenCalledWith('banana'); |
| 428 | + expect(tabEl.controller('ionTab').navViewName).toBe('banana'); |
| 429 | + })); |
| 430 | + }); |
344 | 431 |
|
345 |
| - $ionicViewService.isCurrentStateNavView.reset(); |
346 |
| - isCurrent = true; |
347 |
| - $rootScope.$broadcast('$stateChangeSuccess'); |
| 432 | + it('should call tabMatchesState on compile and if match select', function() { |
| 433 | + setup(); |
| 434 | + expect(tabEl.controller('ionTab').tabMatchesState).toHaveBeenCalled(); |
348 | 435 |
|
349 |
| - expect($ionicViewService.isCurrentStateNavView).toHaveBeenCalledWith('banana'); |
350 |
| - expect(tabsCtrl.select).toHaveBeenCalledWith(tab); |
351 |
| - })); |
| 436 | + tabDoesMatch = true; |
| 437 | + setup(); |
| 438 | + expect(tabEl.controller('ionTab').tabMatchesState).toHaveBeenCalled(); |
| 439 | + }); |
| 440 | + |
| 441 | + it('should call selectIfMatchesState on $stateChangeSuccess', function() { |
| 442 | + setup(); |
| 443 | + var tabMatchesState = tabEl.controller('ionTab').tabMatchesState; |
| 444 | + |
| 445 | + tabMatchesState.reset(); |
| 446 | + spyOn(tabsCtrl, 'select'); |
| 447 | + tabDoesMatch = false; |
| 448 | + |
| 449 | + tabEl.scope().$broadcast('$stateChangeSuccess'); |
| 450 | + expect(tabMatchesState).toHaveBeenCalled(); |
| 451 | + expect(tabsCtrl.select).not.toHaveBeenCalled(); |
| 452 | + |
| 453 | + tabMatchesState.reset(); |
| 454 | + tabDoesMatch = true; |
| 455 | + |
| 456 | + tabEl.scope().$broadcast('$stateChangeSuccess'); |
| 457 | + expect(tabMatchesState).toHaveBeenCalled(); |
| 458 | + expect(tabsCtrl.select).toHaveBeenCalledWith(tabEl.scope()); |
352 | 459 | });
|
353 | 460 |
|
354 | 461 | it('should transclude on $tabSelected=true', function() {
|
|
0 commit comments