11import { 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' ;
33
44import { LazyElementsModule } from '../lazy-elements.module' ;
55import { LazyElementsLoaderService } from '../lazy-elements-loader.service' ;
@@ -92,7 +92,6 @@ describe('LazyElementDirective', () => {
9292 let fixture : ComponentFixture < TestHostComponent > ;
9393 let appendChildSpy : jest . SpyInstance ;
9494 let whenDefinedSpy : jest . SpyInstance ;
95- let requestAnimationFrameSpy : jest . SpyInstance ;
9695
9796 function getAppendChildFirstScript ( ) : HTMLScriptElement {
9897 return appendChildSpy . mock . calls [ 0 ] [ 0 ] ;
@@ -102,8 +101,8 @@ describe('LazyElementDirective', () => {
102101 return appendChildSpy . mock . calls [ 1 ] [ 0 ] ;
103102 }
104103
105- beforeEach ( waitForAsync ( ( ) => {
106- TestBed . configureTestingModule ( {
104+ beforeEach ( async ( ) => {
105+ await TestBed . configureTestingModule ( {
107106 imports : [
108107 TestModule ,
109108 LazyElementsModule . forRoot ( {
@@ -119,9 +118,9 @@ describe('LazyElementDirective', () => {
119118 schemas : [ CUSTOM_ELEMENTS_SCHEMA ] ,
120119 declarations : [ TestHostComponent ] ,
121120 } ) . compileComponents ( ) ;
122- } ) ) ;
121+ } ) ;
123122
124- beforeEach ( ( ) => {
123+ beforeEach ( async ( ) => {
125124 fixture = TestBed . createComponent ( TestHostComponent ) ;
126125 testHostComponent = fixture . componentInstance ;
127126 appendChildSpy = jest . spyOn ( document . body , 'appendChild' ) ;
@@ -130,20 +129,13 @@ describe('LazyElementDirective', () => {
130129 . mockReturnValue (
131130 Promise . resolve ( class DummyElement extends HTMLElement { } )
132131 ) ;
133- requestAnimationFrameSpy = jest
134- . spyOn ( window , 'requestAnimationFrame' )
135- . mockImplementation ( ( callback ) => {
136- const time = 0 ;
137- callback ( time ) ;
138- return time ;
139- } ) ;
140132 fixture . detectChanges ( ) ;
133+ await fixture . whenRenderingDone ( ) ;
141134 } ) ;
142135
143136 afterEach ( ( ) => {
144137 appendChildSpy . mockRestore ( ) ;
145138 whenDefinedSpy . mockRestore ( ) ;
146- requestAnimationFrameSpy . mockRestore ( ) ;
147139 } ) ;
148140
149141 it ( 'should create' , ( ) => {
@@ -167,10 +159,12 @@ describe('LazyElementDirective', () => {
167159 ) ;
168160 } ) ;
169161
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 ( ) => {
171163 testHostComponent . addOtherElement = true ;
172164 fixture . detectChanges ( ) ;
173165
166+ await fixture . whenStable ( ) ;
167+
174168 expect ( appendChildSpy ) . toHaveBeenCalledTimes ( 2 ) ;
175169 expect ( getAppendChildFirstScript ( ) . src ) . toBe (
176170 'http://elements.com/some-element'
@@ -180,11 +174,12 @@ describe('LazyElementDirective', () => {
180174 ) ;
181175 } ) ;
182176
183- it ( 'renders loading template' , ( ) => {
177+ it ( 'renders loading template' , async ( ) => {
184178 expect ( document . querySelector ( '.loading' ) ) . toBe ( null ) ;
185179
186180 testHostComponent . useLoadingTemplate = true ;
187181 fixture . detectChanges ( ) ;
182+ await fixture . whenStable ( ) ;
188183
189184 expect ( document . querySelector ( '.loading' ) . textContent ) . toBe ( 'Loading...' ) ;
190185 } ) ;
@@ -194,6 +189,7 @@ describe('LazyElementDirective', () => {
194189
195190 testHostComponent . useLoadingTemplate = true ;
196191 fixture . detectChanges ( ) ;
192+ await fixture . whenStable ( ) ;
197193
198194 expect ( document . querySelector ( '.loading' ) . textContent ) . toBe ( 'Loading...' ) ;
199195
@@ -212,6 +208,7 @@ describe('LazyElementDirective', () => {
212208
213209 testHostComponent . useErrorTemplate = true ;
214210 fixture . detectChanges ( ) ;
211+ await fixture . whenStable ( ) ;
215212
216213 expect ( document . querySelector ( '.loading' ) . textContent ) . toBe ( 'Loading...' ) ;
217214 expect ( document . querySelector ( '.error' ) ) . toBe ( null ) ;
@@ -231,7 +228,7 @@ describe('LazyElementDirective', () => {
231228 consoleErrorSpy . mockRestore ( ) ;
232229 } ) ;
233230
234- it ( 'uses type module on script tag when specified' , ( ) => {
231+ it ( 'uses type module on script tag when specified' , async ( ) => {
235232 fixture . detectChanges ( ) ;
236233
237234 expect ( appendChildSpy ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -242,6 +239,7 @@ describe('LazyElementDirective', () => {
242239
243240 testHostComponent . useModule = true ;
244241 fixture . detectChanges ( ) ;
242+ await fixture . whenStable ( ) ;
245243
246244 expect ( appendChildSpy ) . toHaveBeenCalledTimes ( 2 ) ;
247245 expect ( getAppendChildSecondScript ( ) . src ) . toBe (
@@ -273,14 +271,15 @@ describe('LazyElementDirective', () => {
273271 ) ;
274272 } ) ;
275273
276- it ( 'uses elementConfig for the tag' , ( ) => {
274+ it ( 'uses elementConfig for the tag' , async ( ) => {
277275 testHostComponent . useElementConfig = true ;
278276 fixture . detectChanges ( ) ;
277+ await fixture . whenStable ( ) ;
279278
280279 expect ( document . querySelector ( '.loading' ) . textContent ) . toBe ( 'Spinner...' ) ;
281280 } ) ;
282281
283- it ( 'should load another element when the `url` binding changes' , ( ) => {
282+ it ( 'should load another element when the `url` binding changes' , async ( ) => {
284283 // Arrange
285284 const elementsLoaderService = TestBed . inject ( LazyElementsLoaderService ) ;
286285 const loadElementSpy = jest . spyOn ( elementsLoaderService , 'loadElement' ) ;
@@ -290,10 +289,12 @@ describe('LazyElementDirective', () => {
290289 testHostComponent . url =
291290 'http://elements.com/some-configured-element-module' ;
292291 fixture . detectChanges ( ) ;
292+ await fixture . whenStable ( ) ;
293293
294294 testHostComponent . url =
295295 'http://elements.com/some-configured-element-module-es2015' ;
296296 fixture . detectChanges ( ) ;
297+ await fixture . whenStable ( ) ;
297298
298299 // Assert
299300 expect ( loadElementSpy ) . toHaveBeenCalledTimes ( 2 ) ;
0 commit comments