@@ -2430,7 +2430,7 @@ describe('`afterEach` clean-up', function() {
2430
2430
2431
2431
// Spy on `angular.element.cleanData()`, so the next test can verify
2432
2432
// that it has been called as necessary
2433
- prevCleanDataSpy = spyOn ( angular . element , 'cleanData' ) . andCallThrough ( ) ;
2433
+ prevCleanDataSpy = mockCleanData ( [ null , [ prevRootElement ] ] ) ;
2434
2434
2435
2435
return $delegate ;
2436
2436
} ) ;
@@ -2448,9 +2448,9 @@ describe('`afterEach` clean-up', function() {
2448
2448
// We want to verify the subsequent call, made by `angular-mocks`
2449
2449
expect ( prevCleanDataSpy . callCount ) . toBe ( 2 ) ;
2450
2450
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 ] ) ;
2454
2454
} ) ;
2455
2455
} ) ;
2456
2456
@@ -2471,7 +2471,7 @@ describe('`afterEach` clean-up', function() {
2471
2471
2472
2472
// Spy on `angular.element.cleanData()`, so the next test can verify
2473
2473
// that it has been called as necessary
2474
- prevCleanDataSpy = spyOn ( angular . element , 'cleanData' ) . andCallThrough ( ) ;
2474
+ prevCleanDataSpy = mockCleanData ( [ null , [ prevOriginalRootElement , prevRootElement ] ] ) ;
2475
2475
2476
2476
return prevRootElement ;
2477
2477
} ) ;
@@ -2498,10 +2498,59 @@ describe('`afterEach` clean-up', function() {
2498
2498
// We want to verify the subsequent call, made by `angular-mocks`
2499
2499
expect ( prevCleanDataSpy . callCount ) . toBe ( 2 ) ;
2500
2500
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
2505
} ) ;
2506
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 ( ) { } ) ;
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
+ }
2507
2556
} ) ;
0 commit comments