Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 21f5b6c

Browse files
committed
test(ngAnimateSwap): add tests regarding creating/removing scopes
1 parent a245483 commit 21f5b6c

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/ngAnimate/ngAnimateSwap.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ var ngAnimateSwapDirective = ['$animate', function($animate) {
104104
previousScope = null;
105105
}
106106
if (value || value === 0) {
107-
previousScope = scope.$new();
108-
$transclude(previousScope, function(element) {
109-
previousElement = element;
110-
$animate.enter(element, null, $element);
107+
$transclude(function(clone, childScope) {
108+
previousElement = clone;
109+
previousScope = childScope;
110+
$animate.enter(clone, null, $element);
111111
});
112112
}
113113
});

test/ngAnimate/ngAnimateSwapSpec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,51 @@ describe('ngAnimateSwap', function() {
132132
expect(two).toBeTruthy();
133133
}));
134134

135+
it('should create a new (non-isolate) scope for each inserted clone', inject(function() {
136+
var parentScope = $rootScope.$new();
137+
element = $compile('<div><div ng-animate-swap="value">{{ value }}</div></div>')(parentScope);
138+
139+
$rootScope.$apply('value = 1');
140+
var scopeOne = element.find('div').eq(0).scope();
141+
expect(scopeOne.$parent).toBe(parentScope);
142+
143+
$rootScope.$apply('value = 2');
144+
var scopeTwo = element.find('div').eq(0).scope();
145+
expect(scopeTwo.$parent).toBe(parentScope);
146+
147+
expect(scopeOne).not.toBe(scopeTwo);
148+
}));
149+
150+
it('should destroy the previous scope when removing the element', inject(function() {
151+
element = $compile('<div><div ng-animate-swap="value">{{ value }}</div></div>')($rootScope);
152+
153+
$rootScope.$apply('value = 1');
154+
var scopeOne = element.find('div').eq(0).scope();
155+
expect(scopeOne.$$destroyed).toBe(false);
156+
157+
// Swapping the old element with a new one.
158+
$rootScope.$apply('value = 2');
159+
expect(scopeOne.$$destroyed).toBe(true);
160+
161+
var scopeTwo = element.find('div').eq(0).scope();
162+
expect(scopeTwo.$$destroyed).toBe(false);
163+
164+
// Removing the old element (without inserting a new one).
165+
$rootScope.$apply('value = null');
166+
expect(scopeTwo.$$destroyed).toBe(true);
167+
}));
168+
169+
it('should destroy the previous scope before when swapping elements', inject(function() {
170+
element = $compile('<div><div ng-animate-swap="value">{{ value }}</div></div>')($rootScope);
171+
172+
$rootScope.$apply('value = 1');
173+
var scopeOne = element.find('div').eq(0).scope();
174+
expect(scopeOne.$$destroyed).toBe(false);
175+
176+
$rootScope.$apply('value = 2');
177+
expect(scopeOne.$$destroyed).toBe(true);
178+
}));
179+
135180

136181
describe('animations', function() {
137182
it('should trigger a leave animation followed by an enter animation upon swap',

0 commit comments

Comments
 (0)