Skip to content

Commit 8efa08a

Browse files
committed
test(jqLite): test not firing $destroy on jqLite.cleanData with jQuery UI
So far it wasn't tested that Angular's logic for skipping it triggering the $destroy event on jQuery.cleanData in the replaceWith internal function works correctly when Angular is not the last one to patch the cleanData method (e.g. if jQuery UI does the patching later). This commits adds the relevant test. Ref angular#8486
1 parent 38f8c97 commit 8efa08a

File tree

1 file changed

+73
-41
lines changed

1 file changed

+73
-41
lines changed

test/ng/compileSpec.js

+73-41
Original file line numberDiff line numberDiff line change
@@ -2080,17 +2080,49 @@ describe('$compile', function() {
20802080
));
20812081

20822082

2083-
it('should work when directive is in a repeater', inject(
2084-
function($compile, $httpBackend, $rootScope) {
2085-
$httpBackend.expect('GET', 'hello.html').
2083+
describe('when directive is in a repeater', function() {
2084+
var is;
2085+
beforeEach(function() {
2086+
is = [1, 2];
2087+
});
2088+
2089+
function runTest() {
2090+
inject(function($compile, $httpBackend, $rootScope) {
2091+
$httpBackend.expect('GET', 'hello.html').
20862092
respond('<span>i=<span ng-transclude></span>;</span>');
2087-
element = jqLite('<div><b class=hello ng-repeat="i in [1,2]">{{i}}</b></div>');
2088-
$compile(element)($rootScope);
2093+
element = jqLite('<div><b class=hello ng-repeat="i in [' + is + ']">{{i}}</b></div>');
2094+
$compile(element)($rootScope);
20892095

2090-
$httpBackend.flush();
2091-
expect(element.text()).toEqual('i=1;i=2;');
2096+
$httpBackend.flush();
2097+
expect(element.text()).toEqual('i=' + is.join(';i=') + ';');
2098+
});
20922099
}
2093-
));
2100+
2101+
it('should work in jqLite and jQuery with jQuery.cleanData last patched by Angular', runTest);
2102+
2103+
if (jQuery) {
2104+
it('should work with another library patching jQuery.cleanData after Angular', function() {
2105+
var cleanedCount = 0;
2106+
var currentCleanData = jqLite.cleanData;
2107+
jqLite.cleanData = function(elems) {
2108+
cleanedCount += elems.length;
2109+
// Don't return the output and explicitly pass only the first parameter
2110+
// so that we're sure we're not relying on either of them. jQuery UI patch
2111+
// behaves in this way.
2112+
currentCleanData(elems);
2113+
};
2114+
2115+
runTest();
2116+
2117+
// The initial ng-repeat div is dumped after parsing hence we expect cleanData
2118+
// count to be one larger than size of the iterated array.
2119+
expect(cleanedCount).toBe(is.length + 1);
2120+
2121+
// Restore the previous cleanData.
2122+
jqLite.cleanData = currentCleanData;
2123+
});
2124+
}
2125+
});
20942126

20952127
describe('replace and not exactly one root element', function() {
20962128

@@ -8573,46 +8605,46 @@ describe('$compile', function() {
85738605
});
85748606
});
85758607

8576-
if (jQuery) {
8577-
describe('cleaning up after a replaced element', function() {
8578-
var $compile, xs;
8579-
beforeEach(inject(function(_$compile_) {
8580-
$compile = _$compile_;
8581-
xs = [0, 1];
8582-
}));
8608+
describe('cleaning up after a replaced element', function() {
8609+
var $compile, xs;
8610+
beforeEach(inject(function(_$compile_) {
8611+
$compile = _$compile_;
8612+
xs = [0, 1];
8613+
}));
85838614

8584-
function testCleanup() {
8585-
var privateData, firstRepeatedElem;
8615+
function testCleanup() {
8616+
var privateData, firstRepeatedElem;
85868617

8587-
element = $compile('<div><div ng-repeat="x in xs" ng-click="noop()">{{x}}</div></div>')($rootScope);
8618+
element = $compile('<div><div ng-repeat="x in xs" ng-click="noop()">{{x}}</div></div>')($rootScope);
85888619

8589-
$rootScope.$apply('xs = [' + xs + ']');
8590-
firstRepeatedElem = element.children('.ng-scope').eq(0);
8620+
$rootScope.$apply('xs = [' + xs + ']');
8621+
firstRepeatedElem = element.children('.ng-scope').eq(0);
85918622

8592-
expect(firstRepeatedElem.data('$scope')).toBeDefined();
8593-
privateData = jQuery._data(firstRepeatedElem[0]);
8594-
expect(privateData.events).toBeDefined();
8595-
expect(privateData.events.click).toBeDefined();
8596-
expect(privateData.events.click[0]).toBeDefined();
8623+
expect(firstRepeatedElem.data('$scope')).toBeDefined();
8624+
privateData = jqLite._data(firstRepeatedElem[0]);
8625+
expect(privateData.events).toBeDefined();
8626+
expect(privateData.events.click).toBeDefined();
8627+
expect(privateData.events.click[0]).toBeDefined();
85978628

8598-
//Ensure the AngularJS $destroy event is still sent
8599-
var destroyCount = 0;
8600-
element.find('div').on('$destroy', function() { destroyCount++; });
8629+
// Ensure the AngularJS $destroy event is still sent
8630+
var destroyCount = 0;
8631+
element.find('div').on('$destroy', function() { destroyCount++; });
86018632

8602-
$rootScope.$apply('xs = null');
8633+
$rootScope.$apply('xs = null');
86038634

8604-
expect(destroyCount).toBe(2);
8605-
expect(firstRepeatedElem.data('$scope')).not.toBeDefined();
8606-
privateData = jQuery._data(firstRepeatedElem[0]);
8607-
expect(privateData && privateData.events).not.toBeDefined();
8608-
}
8635+
expect(destroyCount).toBe(2);
8636+
expect(firstRepeatedElem.data('$scope')).not.toBeDefined();
8637+
privateData = jqLite._data(firstRepeatedElem[0]);
8638+
expect(privateData && privateData.events).not.toBeDefined();
8639+
}
86098640

8610-
it('should work without external libraries (except jQuery)', testCleanup);
8641+
it('should work without external libraries (except jQuery)', testCleanup);
86118642

8643+
if (jQuery) {
86128644
it('should work with another library patching jQuery.cleanData after AngularJS', function() {
86138645
var cleanedCount = 0;
8614-
var currentCleanData = jQuery.cleanData;
8615-
jQuery.cleanData = function(elems) {
8646+
var currentCleanData = jqLite.cleanData;
8647+
jqLite.cleanData = function(elems) {
86168648
cleanedCount += elems.length;
86178649
// Don't return the output and explicitly pass only the first parameter
86188650
// so that we're sure we're not relying on either of them. jQuery UI patch
@@ -8626,11 +8658,11 @@ describe('$compile', function() {
86268658
// and each clone of the ng-repeat template is also removed (xs.length)
86278659
expect(cleanedCount).toBe(xs.length + 1);
86288660

8629-
// Restore the previous jQuery.cleanData.
8630-
jQuery.cleanData = currentCleanData;
8661+
// Restore the previous cleanData.
8662+
jqLite.cleanData = currentCleanData;
86318663
});
8632-
});
8633-
}
8664+
}
8665+
});
86348666

86358667

86368668
it('should add a $$transcluded property onto the transcluded scope', function() {

0 commit comments

Comments
 (0)