|
| 1 | +ddescribe('ionNavButtons directive', function() { |
| 2 | + |
| 3 | + beforeEach(module('ionic', function($compileProvider) { |
| 4 | + $compileProvider.directive('needsScroll', function() { |
| 5 | + return { |
| 6 | + //Test if the buttons are 'children of ionScroll' when compiled |
| 7 | + require: '^$ionicScroll', |
| 8 | + link: function(scope, element, attrs, ctrl) { |
| 9 | + element.data('scrollCtrl', ctrl); |
| 10 | + } |
| 11 | + }; |
| 12 | + }); |
| 13 | + })); |
| 14 | + beforeEach(function() { |
| 15 | + ionic.requestAnimationFrame = function(cb) { cb(); }; |
| 16 | + }); |
| 17 | + |
| 18 | + function setup(side, tpl) { |
| 19 | + var el; |
| 20 | + inject(function($compile, $rootScope) { |
| 21 | + el = $compile('<div>' + |
| 22 | + '<ion-nav-bar></ion-nav-bar>' + |
| 23 | + '<ion-view>' + |
| 24 | + '<ion-content>' + |
| 25 | + '<ion-nav-buttons side="'+(side)+'">' + |
| 26 | + (tpl || '') + |
| 27 | + '</ion-nav-buttons>' + |
| 28 | + '</ion-content>' + |
| 29 | + '</ion-view>' + |
| 30 | + '</div>')($rootScope.$new()); |
| 31 | + $rootScope.$apply(); |
| 32 | + }); |
| 33 | + return el; |
| 34 | + } |
| 35 | + |
| 36 | + it('should add buttons to left side by default', function() { |
| 37 | + var el = setup(null, '<button id="my-btn">'); |
| 38 | + expect(el[0].querySelector('.left-buttons #my-btn')).toBeTruthy(); |
| 39 | + expect(el[0].querySelector('.right-buttons #my-btn')).toBeFalsy(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should add buttons to left side', function() { |
| 43 | + var el = setup('left', '<button id="my-btn">'); |
| 44 | + expect(el[0].querySelector('.left-buttons #my-btn')).toBeTruthy(); |
| 45 | + expect(el[0].querySelector('.right-buttons #my-btn')).toBeFalsy(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should add buttons to right side', function() { |
| 49 | + var el = setup('right', '<button id="my-btn">'); |
| 50 | + expect(el[0].querySelector('.left-buttons #my-btn')).toBeFalsy(); |
| 51 | + expect(el[0].querySelector('.right-buttons #my-btn')).toBeTruthy(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should remove buttons on content destroy', function() { |
| 55 | + var el = setup('', '<button id="my-btn">'); |
| 56 | + expect(el[0].querySelector('.left-buttons #my-btn')).toBeTruthy(); |
| 57 | + el.find('ion-nav-buttons').scope().$destroy(); |
| 58 | + el.scope().$apply(); |
| 59 | + expect(el[0].querySelector('.left-buttons #my-btn')).toBeFalsy(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should compile buttons with same scope & access the same data on compile', function() { |
| 63 | + var el = setup('left', '<button needs-scroll>Hello!</button>'); |
| 64 | + expect(jqLite(el[0].querySelector('ion-content')).children().scope().$id) |
| 65 | + .toBe(jqLite(el[0].querySelector('.left-buttons button')).scope().$id); |
| 66 | + |
| 67 | + //Test if the button was compiled able to access the parents of ion-nav-buttons |
| 68 | + var scrollCtrl = el.find('ion-content').controller('$ionicScroll'); |
| 69 | + expect(scrollCtrl).toBeTruthy(); |
| 70 | + expect(el.find('button[needs-scroll]').data('scrollCtrl')).toBe(scrollCtrl); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should not enter if button is destroyed before raf', function() { |
| 74 | + var rafCb; |
| 75 | + ionic.requestAnimationFrame = function(cb) { rafCb = cb; }; |
| 76 | + var el = setup('left', '<button id="my-btn">'); |
| 77 | + el.find('ion-content').scope().$destroy(); |
| 78 | + el.scope().$apply(); |
| 79 | + rafCb(); |
| 80 | + expect(el[0].querySelector('.left-buttons #my-btn')).toBeFalsy(); |
| 81 | + }); |
| 82 | + |
| 83 | +}); |
0 commit comments