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

Commit db92324

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 db92324

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

src/ngMock/angular-mocks.js

+9-4
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

+29-7
Original file line numberDiff line numberDiff line change
@@ -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

@@ -2498,10 +2498,32 @@ 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]);
2505+
});
2506+
});
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() {});
25052527
});
25062528
});
25072529
});

0 commit comments

Comments
 (0)