Thanks to visit codestin.com
Credit goes to github.com

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

Commit 223fb52

Browse files
committed
fixup 4
Ensure that `angular.element.cleanData()`: 1. has been called with the expected arguments (not necessary, since the next test will verify that) 2. has indeed cleaned up the element's data (e.g. there was a bug in `testabilityPatch.js`, where `cleanData()` was called with an array of jq-wrapped elements, instead of "raw" nodes, which silently failed to clean up)
1 parent db92324 commit 223fb52

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

test/ngMock/angular-mocksSpec.js

Lines changed: 29 additions & 2 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
});
@@ -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
});
@@ -2526,4 +2526,31 @@ describe('`afterEach` clean-up', function() {
25262526
inject(function() {});
25272527
});
25282528
});
2529+
2530+
2531+
// Helpers
2532+
function mockCleanData(expectedElementsPerCall) {
2533+
var jq = angular.element;
2534+
var callIdx = -1;
2535+
2536+
var cleanData_ = jq.cleanData;
2537+
var cleanDataSpy = spyOn(jq, 'cleanData').andCallFake(function(cleanUpNodes) {
2538+
var expectedElements = expectedElementsPerCall[++callIdx];
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+
}
25292556
});

0 commit comments

Comments
 (0)