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

Commit 339468f

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 d8fca15 commit 339468f

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
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: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,9 +2449,9 @@ describe('`afterEach` clean-up', function() {
24492449
// We want to verify the subsequent call, made by `angular-mocks`
24502450
expect(prevCleanDataSpy.callCount).toBe(2);
24512451

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

@@ -2500,10 +2500,32 @@ describe('`afterEach` clean-up', function() {
25002500
// We want to verify the subsequent call, made by `angular-mocks`
25012501
expect(prevCleanDataSpy.callCount).toBe(2);
25022502

2503-
var cleanUpElems = prevCleanDataSpy.calls[1].args[0];
2504-
expect(cleanUpElems.length).toBe(2);
2505-
expect(cleanUpElems[0]).toBe(prevOriginalRootElement[0]);
2506-
expect(cleanUpElems[1]).toBe(prevRootElement[0]);
2503+
var cleanUpNodes = prevCleanDataSpy.calls[1].args[0];
2504+
expect(cleanUpNodes.length).toBe(2);
2505+
expect(cleanUpNodes[0]).toBe(prevOriginalRootElement[0]);
2506+
expect(cleanUpNodes[1]).toBe(prevRootElement[0]);
2507+
});
2508+
});
2509+
2510+
2511+
describe('uninstantiated or falsy `$rootElement`', function() {
2512+
it('should not break if `$rootElement` was never instantiated', function() {
2513+
// Just an empty test to verify that `angular-mocks` doesn't break,
2514+
// when trying to clean up `$rootElement`, if `$rootElement` was never injected in the test
2515+
// (and thus never instantiated/created)
2516+
2517+
// Ensure the `$injector` is created - if there is no `$injector`, no clean-up takes places
2518+
inject(function() {});
2519+
});
2520+
2521+
2522+
it('should not break if the decorated `$rootElement` is falsy (e.g. `null`)', function() {
2523+
module(function($provide) {
2524+
$provide.value('$rootElement', null);
2525+
});
2526+
2527+
// Ensure the `$injector` is created - if there is no `$injector`, no clean-up takes places
2528+
inject(function() {});
25072529
});
25082530
});
25092531

0 commit comments

Comments
 (0)