Skip to content

Commit 0582c04

Browse files
fix: revert methods to test deferrable views
This reverts commit 40661ea, which should be a breaking change and will be released in v15.
1 parent 40661ea commit 0582c04

File tree

5 files changed

+25
-182
lines changed

5 files changed

+25
-182
lines changed

Diff for: apps/example-app/src/app/examples/21-deferable-view.component.ts

-25
This file was deleted.

Diff for: apps/example-app/src/app/examples/21-deferable-view.spec.ts

-23
This file was deleted.

Diff for: projects/testing-library/src/lib/models.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Type, DebugElement } from '@angular/core';
2-
import { ComponentFixture, DeferBlockState, TestBed } from '@angular/core/testing';
2+
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { Routes } from '@angular/router';
44
import { BoundFunction, Queries, queries, Config as dtlConfig, PrettyDOMOptions } from '@testing-library/dom';
55

@@ -63,11 +63,6 @@ export interface RenderResult<ComponentType, WrapperType = ComponentType> extend
6363
'componentProperties' | 'componentInputs' | 'componentOutputs' | 'detectChangesOnRender'
6464
>,
6565
) => Promise<void>;
66-
/**
67-
* @description
68-
* Set the state of a deferrable block.
69-
*/
70-
renderDeferBlock: (deferBlockState: DeferBlockState, deferBlockIndex?: number) => Promise<void>;
7166
}
7267

7368
export interface RenderComponentOptions<ComponentType, Q extends Queries = typeof queries> {
@@ -368,8 +363,6 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
368363
* })
369364
*/
370365
configureTestBed?: (testbed: TestBed) => void;
371-
372-
deferBlockStates?: DeferBlockState | { deferBlockState: DeferBlockState; deferBlockIndex: number }[];
373366
}
374367

375368
export interface ComponentOverride<T> {

Diff for: projects/testing-library/src/lib/testing-library.ts

+24-59
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
ApplicationInitStatus,
1010
isStandalone,
1111
} from '@angular/core';
12-
import { ComponentFixture, DeferBlockState, TestBed, tick } from '@angular/core/testing';
12+
import { ComponentFixture, TestBed, tick } from '@angular/core/testing';
1313
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
1414
import { NavigationExtras, Router } from '@angular/router';
1515
import { RouterTestingModule } from '@angular/router/testing';
@@ -65,7 +65,6 @@ export async function render<SutType, WrapperType = SutType>(
6565
removeAngularAttributes = false,
6666
defaultImports = [],
6767
initialRoute = '',
68-
deferBlockStates = undefined,
6968
configureTestBed = () => {
7069
/* noop*/
7170
},
@@ -161,19 +160,10 @@ export async function render<SutType, WrapperType = SutType>(
161160
}
162161
}
163162

163+
let fixture: ComponentFixture<SutType>;
164164
let detectChanges: () => void;
165165

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);
177167

178168
let renderedPropKeys = Object.keys(componentProperties);
179169
let renderedInputKeys = Object.keys(componentInputs);
@@ -220,61 +210,60 @@ export async function render<SutType, WrapperType = SutType>(
220210
};
221211

222212
return {
213+
// @ts-ignore: fixture assigned
223214
fixture,
224215
detectChanges: () => detectChanges(),
225216
navigate,
226217
rerender,
227-
renderDeferBlock: async (deferBlockState: DeferBlockState, deferBlockIndex?: number) => {
228-
await renderDeferBlock(fixture, deferBlockState, deferBlockIndex);
229-
},
218+
// @ts-ignore: fixture assigned
230219
debugElement: fixture.debugElement,
220+
// @ts-ignore: fixture assigned
231221
container: fixture.nativeElement,
232222
debug: (element = fixture.nativeElement, maxLength, options) =>
233223
Array.isArray(element)
234224
? element.forEach((e) => console.log(dtlPrettyDOM(e, maxLength, options)))
235225
: console.log(dtlPrettyDOM(element, maxLength, options)),
226+
// @ts-ignore: fixture assigned
236227
...replaceFindWithFindAndDetectChanges(dtlGetQueriesForElement(fixture.nativeElement, queries)),
237228
};
238229

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);
248239

249240
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');
252243
if (idAttribute && idAttribute.startsWith('root')) {
253-
createdFixture.nativeElement.removeAttribute('id');
244+
fixture.nativeElement.removeAttribute('id');
254245
}
255246
}
256247

257-
mountedFixtures.add(createdFixture);
248+
mountedFixtures.add(fixture);
258249

259250
let isAlive = true;
260-
createdFixture.componentRef.onDestroy(() => (isAlive = false));
251+
fixture.componentRef.onDestroy(() => (isAlive = false));
261252

262-
if (hasOnChangesHook(createdFixture.componentInstance) && Object.keys(properties).length > 0) {
253+
if (hasOnChangesHook(fixture.componentInstance) && Object.keys(properties).length > 0) {
263254
const changes = getChangesObj(null, componentProperties);
264-
createdFixture.componentInstance.ngOnChanges(changes);
255+
fixture.componentInstance.ngOnChanges(changes);
265256
}
266257

267258
detectChanges = () => {
268259
if (isAlive) {
269-
createdFixture.detectChanges();
260+
fixture.detectChanges();
270261
}
271262
};
272263

273264
if (detectChangesOnRender) {
274265
detectChanges();
275266
}
276-
277-
return createdFixture;
278267
}
279268
}
280269

@@ -440,30 +429,6 @@ function addAutoImports<SutType>(
440429
return [...imports, ...components(), ...animations(), ...routing()];
441430
}
442431

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-
467432
/**
468433
* Wrap waitFor to invoke the Angular change detection cycle before invoking the callback
469434
*/

Diff for: projects/testing-library/tests/defer-blocks.spec.ts

-67
This file was deleted.

0 commit comments

Comments
 (0)