@@ -97,53 +97,30 @@ export async function render<SutType, WrapperType = SutType>(
9797 schemas : [ ...schemas ] ,
9898 } ) ;
9999
100- if ( componentProviders ) {
101- componentProviders
102- . reduce ( ( acc , provider ) => acc . concat ( provider ) , [ ] )
103- . forEach ( ( p ) => {
104- const { provide, ...provider } = p ;
105- TestBed . overrideProvider ( provide , provider ) ;
106- } ) ;
107- }
108-
109- const fixture = await createComponentFixture ( sut , { template, wrapper } ) ;
110- setComponentProperties ( fixture , { componentProperties } ) ;
111-
112- if ( removeAngularAttributes ) {
113- fixture . nativeElement . removeAttribute ( 'ng-version' ) ;
114- const idAttribute = fixture . nativeElement . getAttribute ( 'id' ) ;
115- if ( idAttribute && idAttribute . startsWith ( 'root' ) ) {
116- fixture . nativeElement . removeAttribute ( 'id' ) ;
117- }
118- }
119-
120- mountedFixtures . add ( fixture ) ;
121-
122100 await TestBed . compileComponents ( ) ;
123101
124- let isAlive = true ;
125- fixture . componentRef . onDestroy ( ( ) => ( isAlive = false ) ) ;
102+ componentProviders
103+ . reduce ( ( acc , provider ) => acc . concat ( provider ) , [ ] )
104+ . forEach ( ( p ) => {
105+ const { provide, ...provider } = p ;
106+ TestBed . overrideProvider ( provide , provider ) ;
107+ } ) ;
126108
127- function detectChanges ( ) {
128- if ( isAlive ) {
129- fixture . detectChanges ( ) ;
130- }
131- }
109+ const componentContainer = createComponentFixture ( sut , { template, wrapper } ) ;
132110
133- // Call ngOnChanges on initial render
134- if ( hasOnChangesHook ( fixture . componentInstance ) ) {
135- const changes = getChangesObj ( null , componentProperties ) ;
136- fixture . componentInstance . ngOnChanges ( changes ) ;
137- }
111+ let fixture : ComponentFixture < SutType > ;
112+ let detectChanges : ( ) => void ;
138113
139- if ( detectChangesOnRender ) {
140- detectChanges ( ) ;
141- }
114+ await renderFixture ( componentProperties ) ;
115+
116+ const rerender = async ( rerenderedProperties : Partial < SutType > ) => {
117+ await renderFixture ( rerenderedProperties ) ;
118+ } ;
142119
143- const rerender = ( rerenderedProperties : Partial < SutType > ) => {
144- const changes = getChangesObj ( fixture . componentInstance , rerenderedProperties ) ;
120+ const change = ( changedProperties : Partial < SutType > ) => {
121+ const changes = getChangesObj ( fixture . componentInstance , changedProperties ) ;
145122
146- setComponentProperties ( fixture , { componentProperties : rerenderedProperties } ) ;
123+ setComponentProperties ( fixture , { componentProperties : changedProperties } ) ;
147124
148125 if ( hasOnChangesHook ( fixture . componentInstance ) ) {
149126 fixture . componentInstance . ngOnChanges ( changes ) ;
@@ -192,9 +169,10 @@ export async function render<SutType, WrapperType = SutType>(
192169
193170 return {
194171 fixture,
195- detectChanges,
172+ detectChanges : ( ) => detectChanges ( ) ,
196173 navigate,
197174 rerender,
175+ change,
198176 debugElement : typeof sut === 'string' ? fixture . debugElement : fixture . debugElement . query ( By . directive ( sut ) ) ,
199177 container : fixture . nativeElement ,
200178 debug : ( element = fixture . nativeElement , maxLength , options ) =>
@@ -203,6 +181,42 @@ export async function render<SutType, WrapperType = SutType>(
203181 : console . log ( dtlPrettyDOM ( element , maxLength , options ) ) ,
204182 ...replaceFindWithFindAndDetectChanges ( dtlGetQueriesForElement ( fixture . nativeElement , queries ) ) ,
205183 } ;
184+
185+ async function renderFixture ( properties : Partial < SutType > ) {
186+ if ( fixture ) {
187+ cleanupAtFixture ( fixture ) ;
188+ }
189+
190+ fixture = await createComponent ( componentContainer ) ;
191+ setComponentProperties ( fixture , { componentProperties : properties } ) ;
192+
193+ if ( removeAngularAttributes ) {
194+ fixture . nativeElement . removeAttribute ( 'ng-version' ) ;
195+ const idAttribute = fixture . nativeElement . getAttribute ( 'id' ) ;
196+ if ( idAttribute && idAttribute . startsWith ( 'root' ) ) {
197+ fixture . nativeElement . removeAttribute ( 'id' ) ;
198+ }
199+ }
200+ mountedFixtures . add ( fixture ) ;
201+
202+ let isAlive = true ;
203+ fixture . componentRef . onDestroy ( ( ) => ( isAlive = false ) ) ;
204+
205+ if ( hasOnChangesHook ( fixture . componentInstance ) ) {
206+ const changes = getChangesObj ( null , componentProperties ) ;
207+ fixture . componentInstance . ngOnChanges ( changes ) ;
208+ }
209+
210+ detectChanges = ( ) => {
211+ if ( isAlive ) {
212+ fixture . detectChanges ( ) ;
213+ }
214+ } ;
215+
216+ if ( detectChangesOnRender ) {
217+ detectChanges ( ) ;
218+ }
219+ }
206220}
207221
208222async function createComponent < SutType > ( component : Type < SutType > ) : Promise < ComponentFixture < SutType > > {
@@ -211,19 +225,19 @@ async function createComponent<SutType>(component: Type<SutType>): Promise<Compo
211225 return TestBed . createComponent ( component ) ;
212226}
213227
214- async function createComponentFixture < SutType > (
228+ function createComponentFixture < SutType > (
215229 sut : Type < SutType > | string ,
216230 { template, wrapper } : Pick < RenderDirectiveOptions < any > , 'template' | 'wrapper' > ,
217- ) : Promise < ComponentFixture < SutType > > {
231+ ) : Type < any > {
218232 if ( typeof sut === 'string' ) {
219233 TestBed . overrideTemplate ( wrapper , sut ) ;
220- return createComponent ( wrapper ) ;
234+ return wrapper ;
221235 }
222236 if ( template ) {
223237 TestBed . overrideTemplate ( wrapper , template ) ;
224- return createComponent ( wrapper ) ;
238+ return wrapper ;
225239 }
226- return createComponent ( sut ) ;
240+ return sut ;
227241}
228242
229243function setComponentProperties < SutType > (
0 commit comments