@@ -1596,6 +1596,10 @@ describe('ngMock', function() {
1596
1596
it ( 'should create mock application root' , inject ( function ( $rootElement ) {
1597
1597
expect ( $rootElement . text ( ) ) . toEqual ( '' ) ;
1598
1598
} ) ) ;
1599
+
1600
+ it ( 'should attach the `$injector` to `$rootElement`' , inject ( function ( $injector , $rootElement ) {
1601
+ expect ( $rootElement . injector ( ) ) . toBe ( $injector ) ;
1602
+ } ) ) ;
1599
1603
} ) ;
1600
1604
1601
1605
@@ -2113,9 +2117,122 @@ describe('ngMockE2E', function() {
2113
2117
} ) ;
2114
2118
} ) ;
2115
2119
2120
+
2116
2121
describe ( 'make sure that we can create an injector outside of tests' , function ( ) {
2117
2122
//since some libraries create custom injectors outside of tests,
2118
2123
//we want to make sure that this is not breaking the internals of
2119
2124
//how we manage annotated function cleanup during tests. See #10967
2120
2125
angular . injector ( [ function ( $injector ) { } ] ) ;
2121
2126
} ) ;
2127
+
2128
+
2129
+ describe ( '`afterEach` clean-up' , function ( ) {
2130
+ describe ( 'undecorated `$rootElement`' , function ( ) {
2131
+ var prevRootElement ;
2132
+ var prevCleanDataSpy ;
2133
+
2134
+
2135
+ it ( 'should set up spies so the next test can verify `$rootElement` was cleaned up' , function ( ) {
2136
+ module ( function ( $provide ) {
2137
+ $provide . decorator ( '$rootElement' , function ( $delegate ) {
2138
+ prevRootElement = $delegate ;
2139
+
2140
+ // Spy on `angular.element.cleanData()`, so the next test can verify
2141
+ // that it has been called as necessary
2142
+ prevCleanDataSpy = spyOn ( angular . element , 'cleanData' ) . andCallThrough ( ) ;
2143
+
2144
+ return $delegate ;
2145
+ } ) ;
2146
+ } ) ;
2147
+
2148
+ // Inject the `$rootElement` to ensure it has been created
2149
+ inject ( function ( $rootElement ) {
2150
+ expect ( $rootElement . injector ( ) ) . toBeDefined ( ) ;
2151
+ } ) ;
2152
+ } ) ;
2153
+
2154
+
2155
+ it ( 'should clean up `$rootElement` after each test' , function ( ) {
2156
+ // One call is made by `testabilityPatch`'s `dealoc()`
2157
+ // We want to verify the subsequent call, made by `angular-mocks`
2158
+ expect ( prevCleanDataSpy . callCount ) . toBe ( 2 ) ;
2159
+
2160
+ var cleanUpNodes = prevCleanDataSpy . calls [ 1 ] . args [ 0 ] ;
2161
+ expect ( cleanUpNodes . length ) . toBe ( 1 ) ;
2162
+ expect ( cleanUpNodes [ 0 ] ) . toBe ( prevRootElement [ 0 ] ) ;
2163
+ } ) ;
2164
+ } ) ;
2165
+
2166
+
2167
+ describe ( 'decorated `$rootElement`' , function ( ) {
2168
+ var prevOriginalRootElement ;
2169
+ var prevRootElement ;
2170
+ var prevCleanDataSpy ;
2171
+
2172
+
2173
+ it ( 'should set up spies so the next text can verify `$rootElement` was cleaned up' , function ( ) {
2174
+ module ( function ( $provide ) {
2175
+ $provide . decorator ( '$rootElement' , function ( $delegate ) {
2176
+ prevOriginalRootElement = $delegate ;
2177
+
2178
+ // Mock `$rootElement` to be able to verify that the correct object is cleaned up
2179
+ prevRootElement = angular . element ( '<div></div>' ) ;
2180
+
2181
+ // Spy on `angular.element.cleanData()`, so the next test can verify
2182
+ // that it has been called as necessary
2183
+ prevCleanDataSpy = spyOn ( angular . element , 'cleanData' ) . andCallThrough ( ) ;
2184
+
2185
+ return prevRootElement ;
2186
+ } ) ;
2187
+ } ) ;
2188
+
2189
+ // Inject the `$rootElement` to ensure it has been created
2190
+ inject ( function ( $rootElement ) {
2191
+ expect ( $rootElement ) . toBe ( prevRootElement ) ;
2192
+ expect ( prevOriginalRootElement . injector ( ) ) . toBeDefined ( ) ;
2193
+ expect ( prevRootElement . injector ( ) ) . toBeUndefined ( ) ;
2194
+
2195
+ // If we don't clean up `prevOriginalRootElement`-related data now, `testabilityPatch` will
2196
+ // complain about a memory leak, because it doesn't clean up after the original
2197
+ // `$rootElement`
2198
+ // This is a false alarm, because `angular-mocks` would have cleaned up in a subsequent
2199
+ // `afterEach` block
2200
+ prevOriginalRootElement . removeData ( ) ;
2201
+ } ) ;
2202
+ } ) ;
2203
+
2204
+
2205
+ it ( 'should clean up `$rootElement` (both original and decorated) after each test' , function ( ) {
2206
+ // One call is made by `testabilityPatch`'s `dealoc()`
2207
+ // We want to verify the subsequent call, made by `angular-mocks`
2208
+ expect ( prevCleanDataSpy . callCount ) . toBe ( 2 ) ;
2209
+
2210
+ var cleanUpNodes = prevCleanDataSpy . calls [ 1 ] . args [ 0 ] ;
2211
+ expect ( cleanUpNodes . length ) . toBe ( 2 ) ;
2212
+ expect ( cleanUpNodes [ 0 ] ) . toBe ( prevOriginalRootElement [ 0 ] ) ;
2213
+ expect ( cleanUpNodes [ 1 ] ) . toBe ( prevRootElement [ 0 ] ) ;
2214
+ } ) ;
2215
+ } ) ;
2216
+
2217
+
2218
+ describe ( 'uninstantiated or falsy `$rootElement`' , function ( ) {
2219
+ it ( 'should not break if `$rootElement` was never instantiated' , function ( ) {
2220
+ // Just an empty test to verify that `angular-mocks` doesn't break,
2221
+ // when trying to clean up `$rootElement`, if `$rootElement` was never injected in the test
2222
+ // (and thus never instantiated/created)
2223
+
2224
+ // Ensure the `$injector` is created - if there is no `$injector`, no clean-up takes places
2225
+ inject ( function ( ) { } ) ;
2226
+ } ) ;
2227
+
2228
+
2229
+ it ( 'should not break if the decorated `$rootElement` is falsy (e.g. `null`)' , function ( ) {
2230
+ module ( function ( $provide ) {
2231
+ $provide . value ( '$rootElement' , null ) ;
2232
+ } ) ;
2233
+
2234
+ // Ensure the `$injector` is created - if there is no `$injector`, no clean-up takes places
2235
+ inject ( function ( ) { } ) ;
2236
+ } ) ;
2237
+ } ) ;
2238
+ } ) ;
0 commit comments