mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-06-18 03:58:15 +08:00
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
This commit is contained in:
24
src/ngMock/angular-mocks.js
vendored
24
src/ngMock/angular-mocks.js
vendored
@@ -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,
|
||||
@@ -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];
|
||||
|
||||
@@ -161,6 +161,7 @@
|
||||
"provideLog": false,
|
||||
"spyOnlyCallsWithArgs": false,
|
||||
"createMockStyleSheet": false,
|
||||
"browserTrigger": false
|
||||
"browserTrigger": false,
|
||||
"jqLiteCacheSize": false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ beforeEach(function() {
|
||||
|
||||
// reset to jQuery or default to us.
|
||||
bindJQuery();
|
||||
jqLiteCacheSizeInit();
|
||||
}
|
||||
|
||||
angular.element(document.body).empty().removeData();
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
|
||||
function calcCacheSize() {
|
||||
var size = 0;
|
||||
for(var key in jqLite.cache) { size++; }
|
||||
return size;
|
||||
}
|
||||
|
||||
describe('$compile', function() {
|
||||
function isUnknownElement(el) {
|
||||
return !!el.toString().match(/Unknown/);
|
||||
@@ -334,26 +328,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);
|
||||
});
|
||||
|
||||
|
||||
@@ -4242,29 +4236,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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4274,32 +4262,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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4842,30 +4826,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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
66
test/ngMock/angular-mocksSpec.js
vendored
66
test/ngMock/angular-mocksSpec.js
vendored
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user