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

Commit 352e2a9

Browse files
committed
fixup 3
- Reset `originalRootElement` for each test to avoid. - Fix clean-up break, if `$rootElement` was never initialized. - Fix clean-up break, if decorated `$rootElement` was falsy (e.g. `null`). - (Rename `cleanUpElems` to `cleanUpNodes` - since it's "raw" nodes, not jq-wrapped elements.)
1 parent 0a4fe53 commit 352e2a9

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

src/ngMock/angular-mocks.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,7 @@ if (window.jasmine || window.mocha) {
25792579

25802580

25812581
(window.beforeEach || window.setup)(function() {
2582+
originalRootElement = null;
25822583
annotatedFunctions = [];
25832584
currentSpec = this;
25842585
});
@@ -2602,10 +2603,14 @@ if (window.jasmine || window.mocha) {
26022603
currentSpec = null;
26032604

26042605
if (injector) {
2605-
var cleanUpElems = [originalRootElement[0]];
2606-
var rootNode = injector.get('$rootElement')[0];
2607-
if (rootNode && (rootNode !== originalRootElement[0])) cleanUpElems.push(rootNode);
2608-
angular.element.cleanData(cleanUpElems);
2606+
// Ensure `$rootElement` is instantiated, before checking `originalRootElement`
2607+
var $rootElement = injector.get('$rootElement');
2608+
var rootNode = $rootElement && $rootElement[0];
2609+
var cleanUpNodes = !originalRootElement ? [] : [originalRootElement[0]];
2610+
if (rootNode && (!originalRootElement || rootNode !== originalRootElement[0])) {
2611+
cleanUpNodes.push(rootNode);
2612+
}
2613+
angular.element.cleanData(cleanUpNodes);
26092614

26102615
injector.get('$rootScope').$destroy();
26112616
}

test/ngMock/angular-mocksSpec.js

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,7 +2430,7 @@ describe('`afterEach` clean-up', function() {
24302430

24312431
// Spy on `angular.element.cleanData()`, so the next test can verify
24322432
// that it has been called as necessary
2433-
prevCleanDataSpy = spyOn(angular.element, 'cleanData').andCallThrough();
2433+
prevCleanDataSpy = mockCleanData([null, [prevRootElement]]);
24342434

24352435
return $delegate;
24362436
});
@@ -2448,9 +2448,9 @@ describe('`afterEach` clean-up', function() {
24482448
// We want to verify the subsequent call, made by `angular-mocks`
24492449
expect(prevCleanDataSpy.callCount).toBe(2);
24502450

2451-
var cleanUpElems = prevCleanDataSpy.calls[1].args[0];
2452-
expect(cleanUpElems.length).toBe(1);
2453-
expect(cleanUpElems[0]).toBe(prevRootElement[0]);
2451+
var cleanUpNodes = prevCleanDataSpy.calls[1].args[0];
2452+
expect(cleanUpNodes.length).toBe(1);
2453+
expect(cleanUpNodes[0]).toBe(prevRootElement[0]);
24542454
});
24552455
});
24562456

@@ -2471,7 +2471,7 @@ describe('`afterEach` clean-up', function() {
24712471

24722472
// Spy on `angular.element.cleanData()`, so the next test can verify
24732473
// that it has been called as necessary
2474-
prevCleanDataSpy = spyOn(angular.element, 'cleanData').andCallThrough();
2474+
prevCleanDataSpy = mockCleanData([null, [prevOriginalRootElement, prevRootElement]]);
24752475

24762476
return prevRootElement;
24772477
});
@@ -2498,10 +2498,59 @@ describe('`afterEach` clean-up', function() {
24982498
// We want to verify the subsequent call, made by `angular-mocks`
24992499
expect(prevCleanDataSpy.callCount).toBe(2);
25002500

2501-
var cleanUpElems = prevCleanDataSpy.calls[1].args[0];
2502-
expect(cleanUpElems.length).toBe(2);
2503-
expect(cleanUpElems[0]).toBe(prevOriginalRootElement[0]);
2504-
expect(cleanUpElems[1]).toBe(prevRootElement[0]);
2501+
var cleanUpNodes = prevCleanDataSpy.calls[1].args[0];
2502+
expect(cleanUpNodes.length).toBe(2);
2503+
expect(cleanUpNodes[0]).toBe(prevOriginalRootElement[0]);
2504+
expect(cleanUpNodes[1]).toBe(prevRootElement[0]);
25052505
});
25062506
});
2507+
2508+
2509+
describe('uninstantiated or falsy `$rootElement`', function() {
2510+
it('should not break if `$rootElement` was never instantiated', function() {
2511+
// Just an empty test to verify that `angular-mocks` doesn't break,
2512+
// when trying to clean up `$rootElement`, if `$rootElement` was never injected in the test
2513+
// (and thus never instantiated/created)
2514+
2515+
// Ensure the `$injector` is created - if there is no `$injector`, no clean-up takes places
2516+
inject(function() {});
2517+
});
2518+
2519+
2520+
it('should not break if the decorated `$rootElement` is falsy (e.g. `null`)', function() {
2521+
module(function($provide) {
2522+
$provide.value('$rootElement', null);
2523+
});
2524+
2525+
// Ensure the `$injector` is created - if there is no `$injector`, no clean-up takes places
2526+
inject(function() {});
2527+
});
2528+
});
2529+
2530+
2531+
// Helpers
2532+
function mockCleanData(expectedElementsPerCall) {
2533+
var jq = angular.element;
2534+
var currentCallIdx = -1;
2535+
2536+
var cleanData_ = jq.cleanData;
2537+
var cleanDataSpy = spyOn(jq, 'cleanData').andCallFake(function(cleanUpNodes) {
2538+
var expectedElements = expectedElementsPerCall[++currentCallIdx];
2539+
2540+
if (!expectedElements) {
2541+
cleanData_.apply(jq, arguments);
2542+
} else {
2543+
var expectedNodes = expectedElements.map(function(elem) { return elem[0]; });
2544+
expect(cleanUpNodes).toEqual(expectedNodes);
2545+
2546+
cleanData_.apply(jq, arguments);
2547+
2548+
expectedNodes.forEach(function(node) {
2549+
expect(jq.hasData(node)).toBe(false);
2550+
});
2551+
}
2552+
});
2553+
2554+
return cleanDataSpy;
2555+
}
25072556
});

0 commit comments

Comments
 (0)