|
9 | 9 | ApplicationInitStatus, |
10 | 10 | isStandalone, |
11 | 11 | } from '@angular/core'; |
12 | | -import { ComponentFixture, DeferBlockState, TestBed, tick } from '@angular/core/testing'; |
| 12 | +import { ComponentFixture, TestBed, tick } from '@angular/core/testing'; |
13 | 13 | import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations'; |
14 | 14 | import { NavigationExtras, Router } from '@angular/router'; |
15 | 15 | import { RouterTestingModule } from '@angular/router/testing'; |
@@ -65,7 +65,6 @@ export async function render<SutType, WrapperType = SutType>( |
65 | 65 | removeAngularAttributes = false, |
66 | 66 | defaultImports = [], |
67 | 67 | initialRoute = '', |
68 | | - deferBlockStates = undefined, |
69 | 68 | configureTestBed = () => { |
70 | 69 | /* noop*/ |
71 | 70 | }, |
@@ -161,19 +160,10 @@ export async function render<SutType, WrapperType = SutType>( |
161 | 160 | } |
162 | 161 | } |
163 | 162 |
|
| 163 | + let fixture: ComponentFixture<SutType>; |
164 | 164 | let detectChanges: () => void; |
165 | 165 |
|
166 | | - const fixture = await renderFixture(componentProperties, componentInputs, componentOutputs); |
167 | | - |
168 | | - if (deferBlockStates) { |
169 | | - if (Array.isArray(deferBlockStates)) { |
170 | | - for (const deferBlockState of deferBlockStates) { |
171 | | - await renderDeferBlock(fixture, deferBlockState.deferBlockState, deferBlockState.deferBlockIndex); |
172 | | - } |
173 | | - } else { |
174 | | - await renderDeferBlock(fixture, deferBlockStates); |
175 | | - } |
176 | | - } |
| 166 | + await renderFixture(componentProperties, componentInputs, componentOutputs); |
177 | 167 |
|
178 | 168 | let renderedPropKeys = Object.keys(componentProperties); |
179 | 169 | let renderedInputKeys = Object.keys(componentInputs); |
@@ -220,61 +210,60 @@ export async function render<SutType, WrapperType = SutType>( |
220 | 210 | }; |
221 | 211 |
|
222 | 212 | return { |
| 213 | + // @ts-ignore: fixture assigned |
223 | 214 | fixture, |
224 | 215 | detectChanges: () => detectChanges(), |
225 | 216 | navigate, |
226 | 217 | rerender, |
227 | | - renderDeferBlock: async (deferBlockState: DeferBlockState, deferBlockIndex?: number) => { |
228 | | - await renderDeferBlock(fixture, deferBlockState, deferBlockIndex); |
229 | | - }, |
| 218 | + // @ts-ignore: fixture assigned |
230 | 219 | debugElement: fixture.debugElement, |
| 220 | + // @ts-ignore: fixture assigned |
231 | 221 | container: fixture.nativeElement, |
232 | 222 | debug: (element = fixture.nativeElement, maxLength, options) => |
233 | 223 | Array.isArray(element) |
234 | 224 | ? element.forEach((e) => console.log(dtlPrettyDOM(e, maxLength, options))) |
235 | 225 | : console.log(dtlPrettyDOM(element, maxLength, options)), |
| 226 | + // @ts-ignore: fixture assigned |
236 | 227 | ...replaceFindWithFindAndDetectChanges(dtlGetQueriesForElement(fixture.nativeElement, queries)), |
237 | 228 | }; |
238 | 229 |
|
239 | | - async function renderFixture( |
240 | | - properties: Partial<SutType>, |
241 | | - inputs: Partial<SutType>, |
242 | | - outputs: Partial<SutType>, |
243 | | - ): Promise<ComponentFixture<SutType>> { |
244 | | - const createdFixture = await createComponent(componentContainer); |
245 | | - setComponentProperties(createdFixture, properties); |
246 | | - setComponentInputs(createdFixture, inputs); |
247 | | - setComponentOutputs(createdFixture, outputs); |
| 230 | + async function renderFixture(properties: Partial<SutType>, inputs: Partial<SutType>, outputs: Partial<SutType>) { |
| 231 | + if (fixture) { |
| 232 | + cleanupAtFixture(fixture); |
| 233 | + } |
| 234 | + |
| 235 | + fixture = await createComponent(componentContainer); |
| 236 | + setComponentProperties(fixture, properties); |
| 237 | + setComponentInputs(fixture, inputs); |
| 238 | + setComponentOutputs(fixture, outputs); |
248 | 239 |
|
249 | 240 | if (removeAngularAttributes) { |
250 | | - createdFixture.nativeElement.removeAttribute('ng-version'); |
251 | | - const idAttribute = createdFixture.nativeElement.getAttribute('id'); |
| 241 | + fixture.nativeElement.removeAttribute('ng-version'); |
| 242 | + const idAttribute = fixture.nativeElement.getAttribute('id'); |
252 | 243 | if (idAttribute && idAttribute.startsWith('root')) { |
253 | | - createdFixture.nativeElement.removeAttribute('id'); |
| 244 | + fixture.nativeElement.removeAttribute('id'); |
254 | 245 | } |
255 | 246 | } |
256 | 247 |
|
257 | | - mountedFixtures.add(createdFixture); |
| 248 | + mountedFixtures.add(fixture); |
258 | 249 |
|
259 | 250 | let isAlive = true; |
260 | | - createdFixture.componentRef.onDestroy(() => (isAlive = false)); |
| 251 | + fixture.componentRef.onDestroy(() => (isAlive = false)); |
261 | 252 |
|
262 | | - if (hasOnChangesHook(createdFixture.componentInstance) && Object.keys(properties).length > 0) { |
| 253 | + if (hasOnChangesHook(fixture.componentInstance) && Object.keys(properties).length > 0) { |
263 | 254 | const changes = getChangesObj(null, componentProperties); |
264 | | - createdFixture.componentInstance.ngOnChanges(changes); |
| 255 | + fixture.componentInstance.ngOnChanges(changes); |
265 | 256 | } |
266 | 257 |
|
267 | 258 | detectChanges = () => { |
268 | 259 | if (isAlive) { |
269 | | - createdFixture.detectChanges(); |
| 260 | + fixture.detectChanges(); |
270 | 261 | } |
271 | 262 | }; |
272 | 263 |
|
273 | 264 | if (detectChangesOnRender) { |
274 | 265 | detectChanges(); |
275 | 266 | } |
276 | | - |
277 | | - return createdFixture; |
278 | 267 | } |
279 | 268 | } |
280 | 269 |
|
@@ -440,30 +429,6 @@ function addAutoImports<SutType>( |
440 | 429 | return [...imports, ...components(), ...animations(), ...routing()]; |
441 | 430 | } |
442 | 431 |
|
443 | | -async function renderDeferBlock<SutType>( |
444 | | - fixture: ComponentFixture<SutType>, |
445 | | - deferBlockState: DeferBlockState, |
446 | | - deferBlockIndex?: number, |
447 | | -) { |
448 | | - const deferBlockFixtures = await fixture.getDeferBlocks(); |
449 | | - |
450 | | - if (deferBlockIndex !== undefined) { |
451 | | - if (deferBlockIndex < 0) { |
452 | | - throw new Error('deferBlockIndex must be a positive number'); |
453 | | - } |
454 | | - |
455 | | - const deferBlockFixture = deferBlockFixtures[deferBlockIndex]; |
456 | | - if (!deferBlockFixture) { |
457 | | - throw new Error(`Could not find a deferrable block with index '${deferBlockIndex}'`); |
458 | | - } |
459 | | - await deferBlockFixture.render(deferBlockState); |
460 | | - } else { |
461 | | - for (const deferBlockFixture of deferBlockFixtures) { |
462 | | - await deferBlockFixture.render(deferBlockState); |
463 | | - } |
464 | | - } |
465 | | -} |
466 | | - |
467 | 432 | /** |
468 | 433 | * Wrap waitFor to invoke the Angular change detection cycle before invoking the callback |
469 | 434 | */ |
|
0 commit comments