|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +describe('ngIf', function () { |
| 4 | + var $scope, $compile, element; |
| 5 | + |
| 6 | + beforeEach(inject(function ($rootScope, _$compile_) { |
| 7 | + $scope = $rootScope.$new(); |
| 8 | + $compile = _$compile_; |
| 9 | + element = $compile('<div></div>')($scope); |
| 10 | + })); |
| 11 | + |
| 12 | + afterEach(function () { |
| 13 | + dealoc(element); |
| 14 | + }); |
| 15 | + |
| 16 | + function makeIf(expr) { |
| 17 | + element.append($compile('<div class="my-class" ng-if="' + expr + '"><div>Hi</div></div>')($scope)); |
| 18 | + $scope.$apply(); |
| 19 | + } |
| 20 | + |
| 21 | + it('should immediately remove element if condition is false', function () { |
| 22 | + makeIf('false'); |
| 23 | + expect(element.children().length).toBe(0); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should leave the element if condition is true', function () { |
| 27 | + makeIf('true'); |
| 28 | + expect(element.children().length).toBe(1); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should create then remove the element if condition changes', function () { |
| 32 | + $scope.hello = true; |
| 33 | + makeIf('hello'); |
| 34 | + expect(element.children().length).toBe(1); |
| 35 | + $scope.$apply('hello = false'); |
| 36 | + expect(element.children().length).toBe(0); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should create a new scope', function () { |
| 40 | + $scope.$apply('value = true'); |
| 41 | + element.append($compile( |
| 42 | + '<div ng-if="value"><span ng-init="value=false"></span></div>' |
| 43 | + )($scope)); |
| 44 | + $scope.$apply(); |
| 45 | + expect(element.children('div').length).toBe(1); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should play nice with other elements beside it', function () { |
| 49 | + $scope.values = [1, 2, 3, 4]; |
| 50 | + element.append($compile( |
| 51 | + '<div ng-repeat="i in values"></div>' + |
| 52 | + '<div ng-if="values.length==4"></div>' + |
| 53 | + '<div ng-repeat="i in values"></div>' |
| 54 | + )($scope)); |
| 55 | + $scope.$apply(); |
| 56 | + expect(element.children().length).toBe(9); |
| 57 | + $scope.$apply('values.splice(0,1)'); |
| 58 | + expect(element.children().length).toBe(6); |
| 59 | + $scope.$apply('values.push(1)'); |
| 60 | + expect(element.children().length).toBe(9); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should restore the element to its compiled state', function() { |
| 64 | + $scope.value = true; |
| 65 | + makeIf('value'); |
| 66 | + expect(element.children().length).toBe(1); |
| 67 | + jqLite(element.children()[0]).removeClass('my-class'); |
| 68 | + expect(element.children()[0].className).not.toContain('my-class'); |
| 69 | + $scope.$apply('value = false'); |
| 70 | + expect(element.children().length).toBe(0); |
| 71 | + $scope.$apply('value = true'); |
| 72 | + expect(element.children().length).toBe(1); |
| 73 | + expect(element.children()[0].className).toContain('my-class'); |
| 74 | + }); |
| 75 | + |
| 76 | +}); |
| 77 | + |
| 78 | +describe('ngIf ngAnimate', function () { |
| 79 | + var vendorPrefix, window; |
| 80 | + var body, element; |
| 81 | + |
| 82 | + function html(html) { |
| 83 | + body.html(html); |
| 84 | + element = body.children().eq(0); |
| 85 | + return element; |
| 86 | + } |
| 87 | + |
| 88 | + beforeEach(function() { |
| 89 | + // we need to run animation on attached elements; |
| 90 | + body = jqLite(document.body); |
| 91 | + }); |
| 92 | + |
| 93 | + afterEach(function(){ |
| 94 | + dealoc(body); |
| 95 | + dealoc(element); |
| 96 | + }); |
| 97 | + |
| 98 | + beforeEach(module(function($animationProvider, $provide) { |
| 99 | + $provide.value('$window', window = angular.mock.createMockWindow()); |
| 100 | + return function($sniffer, $animator) { |
| 101 | + vendorPrefix = '-' + $sniffer.vendorPrefix + '-'; |
| 102 | + $animator.enabled(true); |
| 103 | + }; |
| 104 | + })); |
| 105 | + |
| 106 | + it('should fire off the enter animation + add and remove the css classes', |
| 107 | + inject(function($compile, $rootScope, $sniffer) { |
| 108 | + var $scope = $rootScope.$new(); |
| 109 | + var style = vendorPrefix + 'transition: 1s linear all'; |
| 110 | + element = $compile(html( |
| 111 | + '<div>' + |
| 112 | + '<div ng-if="value" style="' + style + '" ng-animate="{enter: \'custom-enter\', leave: \'custom-leave\'}"><div>Hi</div></div>' + |
| 113 | + '</div>' |
| 114 | + ))($scope); |
| 115 | + |
| 116 | + $rootScope.$digest(); |
| 117 | + $scope.$apply('value = true'); |
| 118 | + |
| 119 | + |
| 120 | + expect(element.children().length).toBe(1); |
| 121 | + var first = element.children()[0]; |
| 122 | + |
| 123 | + if ($sniffer.supportsTransitions) { |
| 124 | + expect(first.className).toContain('custom-enter-setup'); |
| 125 | + window.setTimeout.expect(1).process(); |
| 126 | + expect(first.className).toContain('custom-enter-start'); |
| 127 | + window.setTimeout.expect(1000).process(); |
| 128 | + } else { |
| 129 | + expect(window.setTimeout.queue).toEqual([]); |
| 130 | + } |
| 131 | + |
| 132 | + expect(first.className).not.toContain('custom-enter-setup'); |
| 133 | + expect(first.className).not.toContain('custom-enter-start'); |
| 134 | + })); |
| 135 | + |
| 136 | + it('should fire off the leave animation + add and remove the css classes', |
| 137 | + inject(function ($compile, $rootScope, $sniffer) { |
| 138 | + var $scope = $rootScope.$new(); |
| 139 | + var style = vendorPrefix + 'transition: 1s linear all'; |
| 140 | + element = $compile(html( |
| 141 | + '<div>' + |
| 142 | + '<div ng-if="value" style="' + style + '" ng-animate="{enter: \'custom-enter\', leave: \'custom-leave\'}"><div>Hi</div></div>' + |
| 143 | + '</div>' |
| 144 | + ))($scope); |
| 145 | + $scope.$apply('value = true'); |
| 146 | + |
| 147 | + expect(element.children().length).toBe(1); |
| 148 | + var first = element.children()[0]; |
| 149 | + |
| 150 | + if ($sniffer.supportsTransitions) { |
| 151 | + window.setTimeout.expect(1).process(); |
| 152 | + window.setTimeout.expect(1000).process(); |
| 153 | + } else { |
| 154 | + expect(window.setTimeout.queue).toEqual([]); |
| 155 | + } |
| 156 | + |
| 157 | + $scope.$apply('value = false'); |
| 158 | + expect(element.children().length).toBe($sniffer.supportsTransitions ? 1 : 0); |
| 159 | + |
| 160 | + if ($sniffer.supportsTransitions) { |
| 161 | + expect(first.className).toContain('custom-leave-setup'); |
| 162 | + window.setTimeout.expect(1).process(); |
| 163 | + expect(first.className).toContain('custom-leave-start'); |
| 164 | + window.setTimeout.expect(1000).process(); |
| 165 | + } else { |
| 166 | + expect(window.setTimeout.queue).toEqual([]); |
| 167 | + } |
| 168 | + |
| 169 | + expect(element.children().length).toBe(0); |
| 170 | + })); |
| 171 | + |
| 172 | + it('should catch and use the correct duration for animation', |
| 173 | + inject(function ($compile, $rootScope, $sniffer) { |
| 174 | + var $scope = $rootScope.$new(); |
| 175 | + var style = vendorPrefix + 'transition: 0.5s linear all'; |
| 176 | + element = $compile(html( |
| 177 | + '<div>' + |
| 178 | + '<div ng-if="value" style="' + style + '" ng-animate="{enter: \'custom-enter\', leave: \'custom-leave\'}"><div>Hi</div></div>' + |
| 179 | + '</div>' |
| 180 | + ))($scope); |
| 181 | + $scope.$apply('value = true'); |
| 182 | + |
| 183 | + if ($sniffer.supportsTransitions) { |
| 184 | + window.setTimeout.expect(1).process(); |
| 185 | + window.setTimeout.expect(500).process(); |
| 186 | + } else { |
| 187 | + expect(window.setTimeout.queue).toEqual([]); |
| 188 | + } |
| 189 | + })); |
| 190 | + |
| 191 | +}); |
0 commit comments