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

chore(mocks): remove helper fn angular.mocks.clearData #8618

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2001,28 +2001,6 @@ angular.mock.e2e.$httpBackendDecorator =
['$rootScope', '$delegate', '$browser', createHttpBackendMock];


angular.mock.clearDataCache = function() {
// jQuery 2.x doesn't expose data attached to elements. We could use jQuery.cleanData
// to clean up after elements but we'd first need to know which elements to clean up after.
// Skip it then.
if (window.jQuery) {
return;
}

var key,
cache = angular.element.cache;

for(key in cache) {
if (Object.prototype.hasOwnProperty.call(cache,key)) {
var handle = cache[key].handle;

handle && angular.element(handle.elem).off();
delete cache[key];
}
}
};


if(window.jasmine || window.mocha) {

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

angular.mock.clearDataCache();

// clean up jquery's fragment cache
angular.forEach(angular.element.fragments, function(val, key) {
delete angular.element.fragments[key];
Expand Down
3 changes: 2 additions & 1 deletion test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
"provideLog": false,
"spyOnlyCallsWithArgs": false,
"createMockStyleSheet": false,
"browserTrigger": false
"browserTrigger": false,
"jqLiteCacheSize": false
}
}
14 changes: 14 additions & 0 deletions test/helpers/testabilityPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ beforeEach(function() {

// reset to jQuery or default to us.
bindJQuery();
jqLiteCacheSizeInit();
}

angular.element(document.body).empty().removeData();
Expand Down Expand Up @@ -128,6 +129,19 @@ function dealoc(obj) {
}
}


function jqLiteCacheSize() {
var size = 0;
forEach(jqLite.cache, function() { size++; });
return size - jqLiteCacheSize.initSize;
}
jqLiteCacheSize.initSize = 0;

function jqLiteCacheSizeInit() {
jqLiteCacheSize.initSize = jqLiteCacheSize.initSize + jqLiteCacheSize();
}


/**
* @param {DOMElement} element
* @param {boolean=} showNgClass
Expand Down
12 changes: 3 additions & 9 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,18 +393,12 @@ describe('jqLite', function() {


it('should not add to the cache if the node is a comment or text node', function() {
var calcCacheSize = function() {
var count = 0;
for (var k in jqLite.cache) { ++count; }
return count;
};

var nodes = jqLite('<!-- some comment --> and some text');
expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);
nodes.data('someKey');
expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);
nodes.data('someKey', 'someValue');
expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);
});


Expand Down
63 changes: 20 additions & 43 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
'use strict';


function calcCacheSize() {
var size = 0;
for(var key in jqLite.cache) { size++; }
return size;
}


describe('$compile', function() {
var element, directive, $compile, $rootScope;

Expand Down Expand Up @@ -173,26 +166,26 @@ describe('$compile', function() {
element = jqLite('<div><div></div></div>');
$compile(element.contents())($rootScope);
element.empty();
expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);

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

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

// Finally with empty text nodes at the top level
element = jqLite('<div> \n<div></div> </div>');
$compile(element.contents())($rootScope);
element.empty();
expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);
});


Expand Down Expand Up @@ -4056,29 +4049,23 @@ describe('$compile', function() {
return;
}

var calcCacheSize = function() {
var size = 0;
forEach(jqLite.cache, function(item, key) { size++; });
return size;
};

inject(function($compile, $rootScope) {
expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);

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

$rootScope.$apply('xs = [0,1]');
expect(calcCacheSize()).toEqual(2);
expect(jqLiteCacheSize()).toEqual(2);

$rootScope.$apply('xs = [0]');
expect(calcCacheSize()).toEqual(1);
expect(jqLiteCacheSize()).toEqual(1);

$rootScope.$apply('xs = []');
expect(calcCacheSize()).toEqual(1);
expect(jqLiteCacheSize()).toEqual(1);

element.remove();
expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);
});
});

Expand All @@ -4088,32 +4075,28 @@ describe('$compile', function() {
// jQuery 2.x doesn't expose the cache storage.
return;
}
var calcCacheSize = function() {
var size = 0;
forEach(jqLite.cache, function(item, key) { size++; });
return size;
};

inject(function($compile, $rootScope) {
expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);
element = $compile('<div><div ng-repeat="x in xs" ng-if="val">{{x}}</div></div>')($rootScope);

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

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

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

element.remove();
// Now we've even removed the root element along with its cache
expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);
});
});

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


it('should not leak memory with nested transclusion', function() {
var calcCacheSize = function() {
var count = 0;
for (var k in jqLite.cache) { ++count; }
return count;
};

inject(function($compile, $rootScope) {
var size;

expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);

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

$rootScope.nums = [0,1,2];
$rootScope.$apply();
size = calcCacheSize();
size = jqLiteCacheSize();

$rootScope.nums = [3,4,5];
$rootScope.$apply();
expect(calcCacheSize()).toEqual(size);
expect(jqLiteCacheSize()).toEqual(size);

element.remove();
expect(calcCacheSize()).toEqual(0);
expect(jqLiteCacheSize()).toEqual(0);
});
});
});
Expand Down
66 changes: 0 additions & 66 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,72 +705,6 @@ describe('ngMock', function() {
});


describe('angular.mock.clearDataCache', function() {
if (window.jQuery) {
// jQuery 2.x doesn't expose the cache storage.
return;
}

function keys(obj) {
var keysArr = [];
for(var key in obj) {
if (obj.hasOwnProperty(key)) keysArr.push(key);
}
return keysArr.sort();
}

function browserTrigger(element, eventType) {
element = element[0];
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initMouseEvent(eventType, true, true, window, 0, 0, 0, 0, 0, false, false,
false, false, 0, element);
element.dispatchEvent(event);
} else {
element.fireEvent('on' + eventType);
}
}

it('should remove data', function() {
expect(angular.element.cache).toEqual({});
var div = angular.element('<div></div>');
div.data('name', 'angular');
expect(keys(angular.element.cache)).not.toEqual([]);
angular.mock.clearDataCache();
expect(keys(angular.element.cache)).toEqual([]);
});

it('should deregister event handlers', function() {
expect(keys(angular.element.cache)).toEqual([]);
var log = '';
var div = angular.element('<div></div>');

// crazy IE9 requires div to be connected to render DOM for click event to work
// mousemove works even when not connected. This is a heisen-bug since stepping
// through the code makes the test pass. Viva IE!!!
angular.element(document.body).append(div);

div.on('click', function() { log += 'click1;'; });
div.on('click', function() { log += 'click2;'; });
div.on('mousemove', function() { log += 'mousemove;'; });

browserTrigger(div, 'click');
browserTrigger(div, 'mousemove');
expect(log).toEqual('click1;click2;mousemove;');
log = '';

angular.mock.clearDataCache();

browserTrigger(div, 'click');
browserTrigger(div, 'mousemove');
expect(log).toEqual('');
expect(keys(angular.element.cache)).toEqual([]);

div.remove();
});
});


describe('jasmine module and inject', function(){
var log;

Expand Down