1
1
import { Component , CUSTOM_ELEMENTS_SCHEMA , NgModule } from '@angular/core' ;
2
- import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ;
2
+ import { ComponentFixture , TestBed } from '@angular/core/testing' ;
3
3
4
4
import { LazyElementsModule } from '../lazy-elements.module' ;
5
5
import { LazyElementsLoaderService } from '../lazy-elements-loader.service' ;
@@ -92,7 +92,6 @@ describe('LazyElementDirective', () => {
92
92
let fixture : ComponentFixture < TestHostComponent > ;
93
93
let appendChildSpy : jest . SpyInstance ;
94
94
let whenDefinedSpy : jest . SpyInstance ;
95
- let requestAnimationFrameSpy : jest . SpyInstance ;
96
95
97
96
function getAppendChildFirstScript ( ) : HTMLScriptElement {
98
97
return appendChildSpy . mock . calls [ 0 ] [ 0 ] ;
@@ -102,8 +101,8 @@ describe('LazyElementDirective', () => {
102
101
return appendChildSpy . mock . calls [ 1 ] [ 0 ] ;
103
102
}
104
103
105
- beforeEach ( waitForAsync ( ( ) => {
106
- TestBed . configureTestingModule ( {
104
+ beforeEach ( async ( ) => {
105
+ await TestBed . configureTestingModule ( {
107
106
imports : [
108
107
TestModule ,
109
108
LazyElementsModule . forRoot ( {
@@ -119,9 +118,9 @@ describe('LazyElementDirective', () => {
119
118
schemas : [ CUSTOM_ELEMENTS_SCHEMA ] ,
120
119
declarations : [ TestHostComponent ] ,
121
120
} ) . compileComponents ( ) ;
122
- } ) ) ;
121
+ } ) ;
123
122
124
- beforeEach ( ( ) => {
123
+ beforeEach ( async ( ) => {
125
124
fixture = TestBed . createComponent ( TestHostComponent ) ;
126
125
testHostComponent = fixture . componentInstance ;
127
126
appendChildSpy = jest . spyOn ( document . body , 'appendChild' ) ;
@@ -130,20 +129,13 @@ describe('LazyElementDirective', () => {
130
129
. mockReturnValue (
131
130
Promise . resolve ( class DummyElement extends HTMLElement { } )
132
131
) ;
133
- requestAnimationFrameSpy = jest
134
- . spyOn ( window , 'requestAnimationFrame' )
135
- . mockImplementation ( ( callback ) => {
136
- const time = 0 ;
137
- callback ( time ) ;
138
- return time ;
139
- } ) ;
140
132
fixture . detectChanges ( ) ;
133
+ await fixture . whenRenderingDone ( ) ;
141
134
} ) ;
142
135
143
136
afterEach ( ( ) => {
144
137
appendChildSpy . mockRestore ( ) ;
145
138
whenDefinedSpy . mockRestore ( ) ;
146
- requestAnimationFrameSpy . mockRestore ( ) ;
147
139
} ) ;
148
140
149
141
it ( 'should create' , ( ) => {
@@ -167,10 +159,12 @@ describe('LazyElementDirective', () => {
167
159
) ;
168
160
} ) ;
169
161
170
- it ( 'adds multiple script tags if elements have different bundle url' , ( ) => {
162
+ it ( 'adds multiple script tags if elements have different bundle url' , async ( ) => {
171
163
testHostComponent . addOtherElement = true ;
172
164
fixture . detectChanges ( ) ;
173
165
166
+ await fixture . whenStable ( ) ;
167
+
174
168
expect ( appendChildSpy ) . toHaveBeenCalledTimes ( 2 ) ;
175
169
expect ( getAppendChildFirstScript ( ) . src ) . toBe (
176
170
'http://elements.com/some-element'
@@ -180,11 +174,12 @@ describe('LazyElementDirective', () => {
180
174
) ;
181
175
} ) ;
182
176
183
- it ( 'renders loading template' , ( ) => {
177
+ it ( 'renders loading template' , async ( ) => {
184
178
expect ( document . querySelector ( '.loading' ) ) . toBe ( null ) ;
185
179
186
180
testHostComponent . useLoadingTemplate = true ;
187
181
fixture . detectChanges ( ) ;
182
+ await fixture . whenStable ( ) ;
188
183
189
184
expect ( document . querySelector ( '.loading' ) . textContent ) . toBe ( 'Loading...' ) ;
190
185
} ) ;
@@ -194,6 +189,7 @@ describe('LazyElementDirective', () => {
194
189
195
190
testHostComponent . useLoadingTemplate = true ;
196
191
fixture . detectChanges ( ) ;
192
+ await fixture . whenStable ( ) ;
197
193
198
194
expect ( document . querySelector ( '.loading' ) . textContent ) . toBe ( 'Loading...' ) ;
199
195
@@ -212,6 +208,7 @@ describe('LazyElementDirective', () => {
212
208
213
209
testHostComponent . useErrorTemplate = true ;
214
210
fixture . detectChanges ( ) ;
211
+ await fixture . whenStable ( ) ;
215
212
216
213
expect ( document . querySelector ( '.loading' ) . textContent ) . toBe ( 'Loading...' ) ;
217
214
expect ( document . querySelector ( '.error' ) ) . toBe ( null ) ;
@@ -231,7 +228,7 @@ describe('LazyElementDirective', () => {
231
228
consoleErrorSpy . mockRestore ( ) ;
232
229
} ) ;
233
230
234
- it ( 'uses type module on script tag when specified' , ( ) => {
231
+ it ( 'uses type module on script tag when specified' , async ( ) => {
235
232
fixture . detectChanges ( ) ;
236
233
237
234
expect ( appendChildSpy ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -242,6 +239,7 @@ describe('LazyElementDirective', () => {
242
239
243
240
testHostComponent . useModule = true ;
244
241
fixture . detectChanges ( ) ;
242
+ await fixture . whenStable ( ) ;
245
243
246
244
expect ( appendChildSpy ) . toHaveBeenCalledTimes ( 2 ) ;
247
245
expect ( getAppendChildSecondScript ( ) . src ) . toBe (
@@ -273,14 +271,15 @@ describe('LazyElementDirective', () => {
273
271
) ;
274
272
} ) ;
275
273
276
- it ( 'uses elementConfig for the tag' , ( ) => {
274
+ it ( 'uses elementConfig for the tag' , async ( ) => {
277
275
testHostComponent . useElementConfig = true ;
278
276
fixture . detectChanges ( ) ;
277
+ await fixture . whenStable ( ) ;
279
278
280
279
expect ( document . querySelector ( '.loading' ) . textContent ) . toBe ( 'Spinner...' ) ;
281
280
} ) ;
282
281
283
- it ( 'should load another element when the `url` binding changes' , ( ) => {
282
+ it ( 'should load another element when the `url` binding changes' , async ( ) => {
284
283
// Arrange
285
284
const elementsLoaderService = TestBed . inject ( LazyElementsLoaderService ) ;
286
285
const loadElementSpy = jest . spyOn ( elementsLoaderService , 'loadElement' ) ;
@@ -290,10 +289,12 @@ describe('LazyElementDirective', () => {
290
289
testHostComponent . url =
291
290
'http://elements.com/some-configured-element-module' ;
292
291
fixture . detectChanges ( ) ;
292
+ await fixture . whenStable ( ) ;
293
293
294
294
testHostComponent . url =
295
295
'http://elements.com/some-configured-element-module-es2015' ;
296
296
fixture . detectChanges ( ) ;
297
+ await fixture . whenStable ( ) ;
297
298
298
299
// Assert
299
300
expect ( loadElementSpy ) . toHaveBeenCalledTimes ( 2 ) ;
0 commit comments