|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +describe("ngAnimateSwap", function() { |
| 4 | + |
| 5 | + beforeEach(module('ngAnimate')); |
| 6 | + beforeEach(module('ngAnimateMock')); |
| 7 | + |
| 8 | + var element; |
| 9 | + afterEach(function() { |
| 10 | + dealoc(element); |
| 11 | + }); |
| 12 | + |
| 13 | + var $rootScope, $compile, $animate; |
| 14 | + beforeEach(inject(function(_$rootScope_, _$animate_, _$compile_) { |
| 15 | + $rootScope = _$rootScope_; |
| 16 | + $animate = _$animate_; |
| 17 | + $compile = _$compile_; |
| 18 | + |
| 19 | + $animate.enabled(false); |
| 20 | + })); |
| 21 | + |
| 22 | + |
| 23 | + it('should render a new container when the expression changes', inject(function() { |
| 24 | + element = $compile('<div><div ng-animate-swap="exp">{{ exp }}</div></div>')($rootScope); |
| 25 | + $rootScope.$digest(); |
| 26 | + |
| 27 | + var first = element.find('div')[0]; |
| 28 | + expect(first).toBeFalsy(); |
| 29 | + |
| 30 | + $rootScope.exp = 'yes'; |
| 31 | + $rootScope.$digest(); |
| 32 | + |
| 33 | + var second = element.find('div')[0]; |
| 34 | + expect(second.textContent).toBe('yes'); |
| 35 | + |
| 36 | + $rootScope.exp = 'super'; |
| 37 | + $rootScope.$digest(); |
| 38 | + |
| 39 | + var third = element.find('div')[0]; |
| 40 | + expect(third.textContent).toBe('super'); |
| 41 | + expect(third).not.toEqual(second); |
| 42 | + expect(second.parentNode).toBeFalsy(); |
| 43 | + })); |
| 44 | + |
| 45 | + it('should render a new container only when the expression property changes', inject(function() { |
| 46 | + element = $compile('<div><div ng-animate-swap="exp.prop">{{ exp.value }}</div></div>')($rootScope); |
| 47 | + $rootScope.exp = { |
| 48 | + prop: 'hello', |
| 49 | + value: 'world' |
| 50 | + }; |
| 51 | + $rootScope.$digest(); |
| 52 | + |
| 53 | + var one = element.find('div')[0]; |
| 54 | + expect(one.textContent).toBe('world'); |
| 55 | + |
| 56 | + $rootScope.exp.value = 'planet'; |
| 57 | + $rootScope.$digest(); |
| 58 | + |
| 59 | + var two = element.find('div')[0]; |
| 60 | + expect(two.textContent).toBe('planet'); |
| 61 | + expect(two).toBe(one); |
| 62 | + |
| 63 | + $rootScope.exp.prop = 'goodbye'; |
| 64 | + $rootScope.$digest(); |
| 65 | + |
| 66 | + var three = element.find('div')[0]; |
| 67 | + expect(three.textContent).toBe('planet'); |
| 68 | + expect(three).not.toBe(two); |
| 69 | + })); |
| 70 | + |
| 71 | + it('should watch the expression as a collection', inject(function() { |
| 72 | + element = $compile('<div><div ng-animate-swap="exp">{{ exp.a }} {{ exp.b }} {{ exp.c }}</div></div>')($rootScope); |
| 73 | + $rootScope.exp = { |
| 74 | + a: 1, |
| 75 | + b: 2 |
| 76 | + }; |
| 77 | + $rootScope.$digest(); |
| 78 | + |
| 79 | + var one = element.find('div')[0]; |
| 80 | + expect(one.textContent.trim()).toBe('1 2'); |
| 81 | + |
| 82 | + $rootScope.exp.a++; |
| 83 | + $rootScope.$digest(); |
| 84 | + |
| 85 | + var two = element.find('div')[0]; |
| 86 | + expect(two.textContent.trim()).toBe('2 2'); |
| 87 | + expect(two).not.toEqual(one); |
| 88 | + |
| 89 | + $rootScope.exp.c = 3; |
| 90 | + $rootScope.$digest(); |
| 91 | + |
| 92 | + var three = element.find('div')[0]; |
| 93 | + expect(three.textContent.trim()).toBe('2 2 3'); |
| 94 | + expect(three).not.toEqual(two); |
| 95 | + |
| 96 | + $rootScope.exp = { c: 4 }; |
| 97 | + $rootScope.$digest(); |
| 98 | + |
| 99 | + var four = element.find('div')[0]; |
| 100 | + expect(four.textContent.trim()).toBe('4'); |
| 101 | + expect(four).not.toEqual(three); |
| 102 | + })); |
| 103 | + |
| 104 | + they('should consider $prop as a falsy value', [false, undefined, null], function(value) { |
| 105 | + inject(function() { |
| 106 | + element = $compile('<div><div ng-animate-swap="value">{{ value }}</div></div>')($rootScope); |
| 107 | + $rootScope.value = true; |
| 108 | + $rootScope.$digest(); |
| 109 | + |
| 110 | + var one = element.find('div')[0]; |
| 111 | + expect(one).toBeTruthy(); |
| 112 | + |
| 113 | + $rootScope.value = value; |
| 114 | + $rootScope.$digest(); |
| 115 | + |
| 116 | + var two = element.find('div')[0]; |
| 117 | + expect(two).toBeFalsy(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should consider "0" as a truthy value', inject(function() { |
| 122 | + element = $compile('<div><div ng-animate-swap="value">{{ value }}</div></div>')($rootScope); |
| 123 | + $rootScope.$digest(); |
| 124 | + |
| 125 | + var one = element.find('div')[0]; |
| 126 | + expect(one).toBeFalsy(); |
| 127 | + |
| 128 | + $rootScope.value = 0; |
| 129 | + $rootScope.$digest(); |
| 130 | + |
| 131 | + var two = element.find('div')[0]; |
| 132 | + expect(two).toBeTruthy(); |
| 133 | + })); |
| 134 | + |
| 135 | + |
| 136 | + describe("animations", function() { |
| 137 | + it('should trigger a leave animation followed by an enter animation upon swap', |
| 138 | + inject(function() { |
| 139 | + |
| 140 | + element = $compile('<div><div ng-animate-swap="exp">{{ exp }}</div></div>')($rootScope); |
| 141 | + $rootScope.exp = 1; |
| 142 | + $rootScope.$digest(); |
| 143 | + |
| 144 | + var first = $animate.queue.shift(); |
| 145 | + expect(first.event).toBe('enter'); |
| 146 | + expect($animate.queue.length).toBe(0); |
| 147 | + |
| 148 | + $rootScope.exp = 2; |
| 149 | + $rootScope.$digest(); |
| 150 | + |
| 151 | + var second = $animate.queue.shift(); |
| 152 | + expect(second.event).toBe('leave'); |
| 153 | + |
| 154 | + var third = $animate.queue.shift(); |
| 155 | + expect(third.event).toBe('enter'); |
| 156 | + expect($animate.queue.length).toBe(0); |
| 157 | + |
| 158 | + $rootScope.exp = false; |
| 159 | + $rootScope.$digest(); |
| 160 | + |
| 161 | + var forth = $animate.queue.shift(); |
| 162 | + expect(forth.event).toBe('leave'); |
| 163 | + expect($animate.queue.length).toBe(0); |
| 164 | + })); |
| 165 | + }); |
| 166 | +}); |
0 commit comments