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

Commit 1a2caad

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 ffbd276 commit 1a2caad

File tree

6 files changed

+39
-142
lines changed

6 files changed

+39
-142
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
@@ -161,6 +161,7 @@
161161
"provideLog": false,
162162
"spyOnlyCallsWithArgs": false,
163163
"createMockStyleSheet": false,
164-
"browserTrigger": false
164+
"browserTrigger": false,
165+
"jqLiteCacheSize": false
165166
}
166167
}

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 & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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-
104
describe('$compile', function() {
115
function isUnknownElement(el) {
126
return !!el.toString().match(/Unknown/);
@@ -334,26 +328,26 @@ describe('$compile', function() {
334328
element = jqLite('<div><div></div></div>');
335329
$compile(element.contents())($rootScope);
336330
element.empty();
337-
expect(calcCacheSize()).toEqual(0);
331+
expect(jqLiteCacheSize()).toEqual(0);
338332

339333
// Next with non-empty text nodes at the top level
340334
// (in this case the compiler will wrap them in a <span>)
341335
element = jqLite('<div>xxx</div>');
342336
$compile(element.contents())($rootScope);
343337
element.empty();
344-
expect(calcCacheSize()).toEqual(0);
338+
expect(jqLiteCacheSize()).toEqual(0);
345339

346340
// Next with comment nodes at the top level
347341
element = jqLite('<div><!-- comment --></div>');
348342
$compile(element.contents())($rootScope);
349343
element.empty();
350-
expect(calcCacheSize()).toEqual(0);
344+
expect(jqLiteCacheSize()).toEqual(0);
351345

352346
// Finally with empty text nodes at the top level
353347
element = jqLite('<div> \n<div></div> </div>');
354348
$compile(element.contents())($rootScope);
355349
element.empty();
356-
expect(calcCacheSize()).toEqual(0);
350+
expect(jqLiteCacheSize()).toEqual(0);
357351
});
358352

359353

@@ -4242,29 +4236,23 @@ describe('$compile', function() {
42424236
return;
42434237
}
42444238

4245-
var calcCacheSize = function() {
4246-
var size = 0;
4247-
forEach(jqLite.cache, function(item, key) { size++; });
4248-
return size;
4249-
};
4250-
42514239
inject(function($compile, $rootScope) {
4252-
expect(calcCacheSize()).toEqual(0);
4240+
expect(jqLiteCacheSize()).toEqual(0);
42534241

42544242
element = $compile('<div><div ng-repeat="x in xs" ng-if="x==1">{{x}}</div></div>')($rootScope);
4255-
expect(calcCacheSize()).toEqual(1);
4243+
expect(jqLiteCacheSize()).toEqual(1);
42564244

42574245
$rootScope.$apply('xs = [0,1]');
4258-
expect(calcCacheSize()).toEqual(2);
4246+
expect(jqLiteCacheSize()).toEqual(2);
42594247

42604248
$rootScope.$apply('xs = [0]');
4261-
expect(calcCacheSize()).toEqual(1);
4249+
expect(jqLiteCacheSize()).toEqual(1);
42624250

42634251
$rootScope.$apply('xs = []');
4264-
expect(calcCacheSize()).toEqual(1);
4252+
expect(jqLiteCacheSize()).toEqual(1);
42654253

42664254
element.remove();
4267-
expect(calcCacheSize()).toEqual(0);
4255+
expect(jqLiteCacheSize()).toEqual(0);
42684256
});
42694257
});
42704258

@@ -4274,32 +4262,28 @@ describe('$compile', function() {
42744262
// jQuery 2.x doesn't expose the cache storage.
42754263
return;
42764264
}
4277-
var calcCacheSize = function() {
4278-
var size = 0;
4279-
forEach(jqLite.cache, function(item, key) { size++; });
4280-
return size;
4281-
};
4265+
42824266
inject(function($compile, $rootScope) {
4283-
expect(calcCacheSize()).toEqual(0);
4267+
expect(jqLiteCacheSize()).toEqual(0);
42844268
element = $compile('<div><div ng-repeat="x in xs" ng-if="val">{{x}}</div></div>')($rootScope);
42854269

42864270
$rootScope.$apply('xs = [0,1]');
42874271
// At this point we have a bunch of comment placeholders but no real transcluded elements
42884272
// So the cache only contains the root element's data
4289-
expect(calcCacheSize()).toEqual(1);
4273+
expect(jqLiteCacheSize()).toEqual(1);
42904274

42914275
$rootScope.$apply('val = true');
42924276
// Now we have two concrete transcluded elements plus some comments so two more cache items
4293-
expect(calcCacheSize()).toEqual(3);
4277+
expect(jqLiteCacheSize()).toEqual(3);
42944278

42954279
$rootScope.$apply('val = false');
42964280
// Once again we only have comments so no transcluded elements and the cache is back to just
42974281
// the root element
4298-
expect(calcCacheSize()).toEqual(1);
4282+
expect(jqLiteCacheSize()).toEqual(1);
42994283

43004284
element.remove();
43014285
// Now we've even removed the root element along with its cache
4302-
expect(calcCacheSize()).toEqual(0);
4286+
expect(jqLiteCacheSize()).toEqual(0);
43034287
});
43044288
});
43054289

@@ -4842,30 +4826,24 @@ describe('$compile', function() {
48424826

48434827

48444828
it('should not leak memory with nested transclusion', function() {
4845-
var calcCacheSize = function() {
4846-
var count = 0;
4847-
for (var k in jqLite.cache) { ++count; }
4848-
return count;
4849-
};
4850-
48514829
inject(function($compile, $rootScope) {
48524830
var size;
48534831

4854-
expect(calcCacheSize()).toEqual(0);
4832+
expect(jqLiteCacheSize()).toEqual(0);
48554833

48564834
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>');
48574835
$compile(element)($rootScope.$new());
48584836

48594837
$rootScope.nums = [0,1,2];
48604838
$rootScope.$apply();
4861-
size = calcCacheSize();
4839+
size = jqLiteCacheSize();
48624840

48634841
$rootScope.nums = [3,4,5];
48644842
$rootScope.$apply();
4865-
expect(calcCacheSize()).toEqual(size);
4843+
expect(jqLiteCacheSize()).toEqual(size);
48664844

48674845
element.remove();
4868-
expect(calcCacheSize()).toEqual(0);
4846+
expect(jqLiteCacheSize()).toEqual(0);
48694847
});
48704848
});
48714849
});

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)