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

Commit 517a673

Browse files
committed
chore(mocks): remove helper fn angular.mocks.clearData
we don't need this any more because Karma reloads the iframe after each test run Related to #8532 Closes #8618
1 parent 1a05daf commit 517a673

File tree

6 files changed

+39
-143
lines changed

6 files changed

+39
-143
lines changed

src/ngMock/angular-mocks.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,28 +2001,6 @@ angular.mock.e2e.$httpBackendDecorator =
20012001
['$rootScope', '$delegate', '$browser', createHttpBackendMock];
20022002

20032003

2004-
angular.mock.clearDataCache = function() {
2005-
// jQuery 2.x doesn't expose data attached to elements. We could use jQuery.cleanData
2006-
// to clean up after elements but we'd first need to know which elements to clean up after.
2007-
// Skip it then.
2008-
if (window.jQuery) {
2009-
return;
2010-
}
2011-
2012-
var key,
2013-
cache = angular.element.cache;
2014-
2015-
for(key in cache) {
2016-
if (Object.prototype.hasOwnProperty.call(cache,key)) {
2017-
var handle = cache[key].handle;
2018-
2019-
handle && angular.element(handle.elem).off();
2020-
delete cache[key];
2021-
}
2022-
}
2023-
};
2024-
2025-
20262004
if(window.jasmine || window.mocha) {
20272005

20282006
var currentSpec = null,
@@ -2053,8 +2031,6 @@ if(window.jasmine || window.mocha) {
20532031
injector.get('$browser').pollFns.length = 0;
20542032
}
20552033

2056-
angular.mock.clearDataCache();
2057-
20582034
// clean up jquery's fragment cache
20592035
angular.forEach(angular.element.fragments, function(val, key) {
20602036
delete angular.element.fragments[key];

test/.jshintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
"provideLog": false,
161161
"spyOnlyCallsWithArgs": false,
162162
"createMockStyleSheet": false,
163-
"browserTrigger": false
163+
"browserTrigger": false,
164+
"jqLiteCacheSize": false
164165
}
165166
}

test/helpers/testabilityPatch.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ beforeEach(function() {
2828

2929
// reset to jQuery or default to us.
3030
bindJQuery();
31+
jqLiteCacheSizeInit();
3132
}
3233

3334
angular.element(document.body).empty().removeData();
@@ -128,6 +129,19 @@ function dealoc(obj) {
128129
}
129130
}
130131

132+
133+
function jqLiteCacheSize() {
134+
var size = 0;
135+
forEach(jqLite.cache, function() { size++; });
136+
return size - jqLiteCacheSize.initSize;
137+
}
138+
jqLiteCacheSize.initSize = 0;
139+
140+
function jqLiteCacheSizeInit() {
141+
jqLiteCacheSize.initSize = jqLiteCacheSize.initSize + jqLiteCacheSize();
142+
}
143+
144+
131145
/**
132146
* @param {DOMElement} element
133147
* @param {boolean=} showNgClass

test/jqLiteSpec.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -393,18 +393,12 @@ describe('jqLite', function() {
393393

394394

395395
it('should not add to the cache if the node is a comment or text node', function() {
396-
var calcCacheSize = function() {
397-
var count = 0;
398-
for (var k in jqLite.cache) { ++count; }
399-
return count;
400-
};
401-
402396
var nodes = jqLite('<!-- some comment --> and some text');
403-
expect(calcCacheSize()).toEqual(0);
397+
expect(jqLiteCacheSize()).toEqual(0);
404398
nodes.data('someKey');
405-
expect(calcCacheSize()).toEqual(0);
399+
expect(jqLiteCacheSize()).toEqual(0);
406400
nodes.data('someKey', 'someValue');
407-
expect(calcCacheSize()).toEqual(0);
401+
expect(jqLiteCacheSize()).toEqual(0);
408402
});
409403

410404

test/ng/compileSpec.js

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
'use strict';
22

33

4-
function calcCacheSize() {
5-
var size = 0;
6-
for(var key in jqLite.cache) { size++; }
7-
return size;
8-
}
9-
10-
114
describe('$compile', function() {
125
var element, directive, $compile, $rootScope;
136

@@ -173,26 +166,26 @@ describe('$compile', function() {
173166
element = jqLite('<div><div></div></div>');
174167
$compile(element.contents())($rootScope);
175168
element.empty();
176-
expect(calcCacheSize()).toEqual(0);
169+
expect(jqLiteCacheSize()).toEqual(0);
177170

178171
// Next with non-empty text nodes at the top level
179172
// (in this case the compiler will wrap them in a <span>)
180173
element = jqLite('<div>xxx</div>');
181174
$compile(element.contents())($rootScope);
182175
element.empty();
183-
expect(calcCacheSize()).toEqual(0);
176+
expect(jqLiteCacheSize()).toEqual(0);
184177

185178
// Next with comment nodes at the top level
186179
element = jqLite('<div><!-- comment --></div>');
187180
$compile(element.contents())($rootScope);
188181
element.empty();
189-
expect(calcCacheSize()).toEqual(0);
182+
expect(jqLiteCacheSize()).toEqual(0);
190183

191184
// Finally with empty text nodes at the top level
192185
element = jqLite('<div> \n<div></div> </div>');
193186
$compile(element.contents())($rootScope);
194187
element.empty();
195-
expect(calcCacheSize()).toEqual(0);
188+
expect(jqLiteCacheSize()).toEqual(0);
196189
});
197190

198191

@@ -4056,29 +4049,23 @@ describe('$compile', function() {
40564049
return;
40574050
}
40584051

4059-
var calcCacheSize = function() {
4060-
var size = 0;
4061-
forEach(jqLite.cache, function(item, key) { size++; });
4062-
return size;
4063-
};
4064-
40654052
inject(function($compile, $rootScope) {
4066-
expect(calcCacheSize()).toEqual(0);
4053+
expect(jqLiteCacheSize()).toEqual(0);
40674054

40684055
element = $compile('<div><div ng-repeat="x in xs" ng-if="x==1">{{x}}</div></div>')($rootScope);
4069-
expect(calcCacheSize()).toEqual(1);
4056+
expect(jqLiteCacheSize()).toEqual(1);
40704057

40714058
$rootScope.$apply('xs = [0,1]');
4072-
expect(calcCacheSize()).toEqual(2);
4059+
expect(jqLiteCacheSize()).toEqual(2);
40734060

40744061
$rootScope.$apply('xs = [0]');
4075-
expect(calcCacheSize()).toEqual(1);
4062+
expect(jqLiteCacheSize()).toEqual(1);
40764063

40774064
$rootScope.$apply('xs = []');
4078-
expect(calcCacheSize()).toEqual(1);
4065+
expect(jqLiteCacheSize()).toEqual(1);
40794066

40804067
element.remove();
4081-
expect(calcCacheSize()).toEqual(0);
4068+
expect(jqLiteCacheSize()).toEqual(0);
40824069
});
40834070
});
40844071

@@ -4088,32 +4075,28 @@ describe('$compile', function() {
40884075
// jQuery 2.x doesn't expose the cache storage.
40894076
return;
40904077
}
4091-
var calcCacheSize = function() {
4092-
var size = 0;
4093-
forEach(jqLite.cache, function(item, key) { size++; });
4094-
return size;
4095-
};
4078+
40964079
inject(function($compile, $rootScope) {
4097-
expect(calcCacheSize()).toEqual(0);
4080+
expect(jqLiteCacheSize()).toEqual(0);
40984081
element = $compile('<div><div ng-repeat="x in xs" ng-if="val">{{x}}</div></div>')($rootScope);
40994082

41004083
$rootScope.$apply('xs = [0,1]');
41014084
// At this point we have a bunch of comment placeholders but no real transcluded elements
41024085
// So the cache only contains the root element's data
4103-
expect(calcCacheSize()).toEqual(1);
4086+
expect(jqLiteCacheSize()).toEqual(1);
41044087

41054088
$rootScope.$apply('val = true');
41064089
// Now we have two concrete transcluded elements plus some comments so two more cache items
4107-
expect(calcCacheSize()).toEqual(3);
4090+
expect(jqLiteCacheSize()).toEqual(3);
41084091

41094092
$rootScope.$apply('val = false');
41104093
// Once again we only have comments so no transcluded elements and the cache is back to just
41114094
// the root element
4112-
expect(calcCacheSize()).toEqual(1);
4095+
expect(jqLiteCacheSize()).toEqual(1);
41134096

41144097
element.remove();
41154098
// Now we've even removed the root element along with its cache
4116-
expect(calcCacheSize()).toEqual(0);
4099+
expect(jqLiteCacheSize()).toEqual(0);
41174100
});
41184101
});
41194102

@@ -4656,30 +4639,24 @@ describe('$compile', function() {
46564639

46574640

46584641
it('should not leak memory with nested transclusion', function() {
4659-
var calcCacheSize = function() {
4660-
var count = 0;
4661-
for (var k in jqLite.cache) { ++count; }
4662-
return count;
4663-
};
4664-
46654642
inject(function($compile, $rootScope) {
46664643
var size;
46674644

4668-
expect(calcCacheSize()).toEqual(0);
4645+
expect(jqLiteCacheSize()).toEqual(0);
46694646

46704647
element = jqLite('<div><ul><li ng-repeat="n in nums">{{n}} => <i ng-if="0 === n%2">Even</i><i ng-if="1 === n%2">Odd</i></li></ul></div>');
46714648
$compile(element)($rootScope.$new());
46724649

46734650
$rootScope.nums = [0,1,2];
46744651
$rootScope.$apply();
4675-
size = calcCacheSize();
4652+
size = jqLiteCacheSize();
46764653

46774654
$rootScope.nums = [3,4,5];
46784655
$rootScope.$apply();
4679-
expect(calcCacheSize()).toEqual(size);
4656+
expect(jqLiteCacheSize()).toEqual(size);
46804657

46814658
element.remove();
4682-
expect(calcCacheSize()).toEqual(0);
4659+
expect(jqLiteCacheSize()).toEqual(0);
46834660
});
46844661
});
46854662
});

test/ngMock/angular-mocksSpec.js

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -705,72 +705,6 @@ describe('ngMock', function() {
705705
});
706706

707707

708-
describe('angular.mock.clearDataCache', function() {
709-
if (window.jQuery) {
710-
// jQuery 2.x doesn't expose the cache storage.
711-
return;
712-
}
713-
714-
function keys(obj) {
715-
var keysArr = [];
716-
for(var key in obj) {
717-
if (obj.hasOwnProperty(key)) keysArr.push(key);
718-
}
719-
return keysArr.sort();
720-
}
721-
722-
function browserTrigger(element, eventType) {
723-
element = element[0];
724-
if (document.createEvent) {
725-
var event = document.createEvent('MouseEvents');
726-
event.initMouseEvent(eventType, true, true, window, 0, 0, 0, 0, 0, false, false,
727-
false, false, 0, element);
728-
element.dispatchEvent(event);
729-
} else {
730-
element.fireEvent('on' + eventType);
731-
}
732-
}
733-
734-
it('should remove data', function() {
735-
expect(angular.element.cache).toEqual({});
736-
var div = angular.element('<div></div>');
737-
div.data('name', 'angular');
738-
expect(keys(angular.element.cache)).not.toEqual([]);
739-
angular.mock.clearDataCache();
740-
expect(keys(angular.element.cache)).toEqual([]);
741-
});
742-
743-
it('should deregister event handlers', function() {
744-
expect(keys(angular.element.cache)).toEqual([]);
745-
var log = '';
746-
var div = angular.element('<div></div>');
747-
748-
// crazy IE9 requires div to be connected to render DOM for click event to work
749-
// mousemove works even when not connected. This is a heisen-bug since stepping
750-
// through the code makes the test pass. Viva IE!!!
751-
angular.element(document.body).append(div);
752-
753-
div.on('click', function() { log += 'click1;'; });
754-
div.on('click', function() { log += 'click2;'; });
755-
div.on('mousemove', function() { log += 'mousemove;'; });
756-
757-
browserTrigger(div, 'click');
758-
browserTrigger(div, 'mousemove');
759-
expect(log).toEqual('click1;click2;mousemove;');
760-
log = '';
761-
762-
angular.mock.clearDataCache();
763-
764-
browserTrigger(div, 'click');
765-
browserTrigger(div, 'mousemove');
766-
expect(log).toEqual('');
767-
expect(keys(angular.element.cache)).toEqual([]);
768-
769-
div.remove();
770-
});
771-
});
772-
773-
774708
describe('jasmine module and inject', function(){
775709
var log;
776710

0 commit comments

Comments
 (0)