Skip to content

Commit 4c81ed9

Browse files
feat: remove RenderDirectiveOptions (#265)
BREAKING CHANGE: The template property is removed from the render options. Instead, you can pass it as the first argument of `render. BEFORE: ```ts await render(InputOutputComponent, { // 👇 this is deprecated template: '<app-fixture [value]="47" (sendValue)="sendValue($event)" (clicked)="clicked()"></app-fixture>', componentProperties: { sendValue: sendSpy, }, }); ``` AFTER: ```ts // 👇 Move the template in the first argument await render('<app-fixture [value]="47" (sendValue)="sendValue($event)" (clicked)="clicked()"></app-fixture>', { // 👇 Add the component to declarations declarations: [InputOutputComponent], componentProperties: { sendValue: sendSpy, }, }); ```
1 parent 224e550 commit 4c81ed9

File tree

4 files changed

+8
-73
lines changed

4 files changed

+8
-73
lines changed

Diff for: apps/example-app/src/app/examples/11-ng-content.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CellComponent } from './11-ng-content';
44

55
test('it is possible to test ng-content without selector', async () => {
66
const projection = 'it should be showed into a p element!';
7-
7+
88
await render(`<app-fixture data-testid="one-cell-with-ng-content">${projection}</app-fixture>`, {
99
declarations: [CellComponent]
1010
});

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

-37
Original file line numberDiff line numberDiff line change
@@ -256,43 +256,6 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
256256
removeAngularAttributes?: boolean;
257257
}
258258

259-
/**
260-
* @deprecated Use `render(template, { declarations: [SomeDirective] })` instead.
261-
*/
262-
// eslint-disable-next-line @typescript-eslint/ban-types
263-
export interface RenderDirectiveOptions<WrapperType, Properties extends object = {}, Q extends Queries = typeof queries>
264-
extends RenderComponentOptions<Properties, Q> {
265-
/**
266-
* @description
267-
* The template to render the directive.
268-
* This template will override the template from the WrapperComponent.
269-
*
270-
* @example
271-
* const component = await render(SpoilerDirective, {
272-
* template: `<div spoiler message='SPOILER'></div>`
273-
* })
274-
*
275-
* @deprecated Use `render(template, { declarations: [SomeDirective] })` instead.
276-
*/
277-
template: string;
278-
/**
279-
* @description
280-
* An Angular component to wrap the component in.
281-
* The template will be overridden with the `template` option.
282-
*
283-
* @default
284-
* `WrapperComponent`, an empty component that strips the `ng-version` attribute
285-
*
286-
* @example
287-
* const component = await render(SpoilerDirective, {
288-
* template: `<div spoiler message='SPOILER'></div>`
289-
* wrapper: CustomWrapperComponent
290-
* })
291-
*/
292-
wrapper?: Type<WrapperType>;
293-
componentProperties?: Partial<WrapperType & Properties>;
294-
}
295-
296259
// eslint-disable-next-line @typescript-eslint/ban-types
297260
export interface RenderTemplateOptions<WrapperType, Properties extends object = {}, Q extends Queries = typeof queries>
298261
extends RenderComponentOptions<Properties, Q> {

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

+7-26
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
getQueriesForElement,
2727
queries as dtlQueries,
2828
} from '@testing-library/dom';
29-
import { RenderComponentOptions, RenderDirectiveOptions, RenderTemplateOptions, RenderResult } from './models';
29+
import { RenderComponentOptions, RenderTemplateOptions, RenderResult } from './models';
3030
import { getConfig } from './config';
3131

3232
const mountedFixtures = new Set<ComponentFixture<any>>();
@@ -36,24 +36,14 @@ export async function render<ComponentType>(
3636
component: Type<ComponentType>,
3737
renderOptions?: RenderComponentOptions<ComponentType>,
3838
): Promise<RenderResult<ComponentType, ComponentType>>;
39-
/**
40-
* @deprecated Use `render(template, { declarations: [DirectiveType] })` instead.
41-
*/
42-
export async function render<DirectiveType, WrapperType = WrapperComponent>(
43-
component: Type<DirectiveType>,
44-
renderOptions?: RenderDirectiveOptions<WrapperType>,
45-
): Promise<RenderResult<WrapperType>>;
4639
export async function render<WrapperType = WrapperComponent>(
4740
template: string,
4841
renderOptions?: RenderTemplateOptions<WrapperType>,
4942
): Promise<RenderResult<WrapperType>>;
5043

5144
export async function render<SutType, WrapperType = SutType>(
5245
sut: Type<SutType> | string,
53-
renderOptions:
54-
| RenderComponentOptions<SutType>
55-
| RenderDirectiveOptions<WrapperType>
56-
| RenderTemplateOptions<WrapperType> = {},
46+
renderOptions: RenderComponentOptions<SutType> | RenderTemplateOptions<WrapperType> = {},
5747
): Promise<RenderResult<SutType>> {
5848
const { dom: domConfig, ...globalConfig } = getConfig();
5949
const {
@@ -86,7 +76,6 @@ export async function render<SutType, WrapperType = SutType>(
8676
declarations: addAutoDeclarations(sut, {
8777
declarations,
8878
excludeComponentDeclaration,
89-
template,
9079
wrapper,
9180
}),
9281
imports: addAutoImports({
@@ -106,7 +95,7 @@ export async function render<SutType, WrapperType = SutType>(
10695
TestBed.overrideProvider(provide, provider);
10796
});
10897

109-
const componentContainer = createComponentFixture(sut, { template, wrapper });
98+
const componentContainer = createComponentFixture(sut, { wrapper });
11099

111100
let fixture: ComponentFixture<SutType>;
112101
let detectChanges: () => void;
@@ -227,22 +216,18 @@ async function createComponent<SutType>(component: Type<SutType>): Promise<Compo
227216

228217
function createComponentFixture<SutType>(
229218
sut: Type<SutType> | string,
230-
{ template, wrapper }: Pick<RenderDirectiveOptions<any>, 'template' | 'wrapper'>,
219+
{ wrapper }: Pick<RenderTemplateOptions<SutType>, 'wrapper'>,
231220
): Type<any> {
232221
if (typeof sut === 'string') {
233222
TestBed.overrideTemplate(wrapper, sut);
234223
return wrapper;
235224
}
236-
if (template) {
237-
TestBed.overrideTemplate(wrapper, template);
238-
return wrapper;
239-
}
240225
return sut;
241226
}
242227

243228
function setComponentProperties<SutType>(
244229
fixture: ComponentFixture<SutType>,
245-
{ componentProperties = {} }: Pick<RenderDirectiveOptions<SutType, any>, 'componentProperties'>,
230+
{ componentProperties = {} }: Pick<RenderTemplateOptions<SutType, any>, 'componentProperties'>,
246231
) {
247232
for (const key of Object.keys(componentProperties)) {
248233
const descriptor: PropertyDescriptor = Object.getOwnPropertyDescriptor(
@@ -293,19 +278,15 @@ function addAutoDeclarations<SutType>(
293278
{
294279
declarations,
295280
excludeComponentDeclaration,
296-
template,
297281
wrapper,
298-
}: Pick<RenderDirectiveOptions<any>, 'declarations' | 'excludeComponentDeclaration' | 'template' | 'wrapper'>,
282+
}: Pick<RenderTemplateOptions<any>, 'declarations' | 'excludeComponentDeclaration' | 'wrapper'>,
299283
) {
300284
if (typeof sut === 'string') {
301285
return [...declarations, wrapper];
302286
}
303287

304-
const wrappers = () => (template ? [wrapper] : []);
305-
306288
const components = () => (excludeComponentDeclaration ? [] : [sut]);
307-
308-
return [...declarations, ...wrappers(), ...components()];
289+
return [...declarations, ...components()];
309290
}
310291

311292
function addAutoImports({ imports, routes }: Pick<RenderComponentOptions<any>, 'imports' | 'routes'>) {

Diff for: projects/testing-library/tests/render-template.spec.ts

-9
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ test('the component renders', async () => {
6262
expect(screen.getByText('Hello Angular!')).toBeInTheDocument();
6363
});
6464

65-
test('the directive renders (compatibility with the deprecated signature)', async () => {
66-
const view = await render(OnOffDirective, {
67-
template: '<div onOff></div>',
68-
});
69-
70-
// eslint-disable-next-line testing-library/no-container
71-
expect(view.container.querySelector('[onoff]')).toBeInTheDocument();
72-
});
73-
7465
test('uses the default props', async () => {
7566
await render('<div onOff></div>', {
7667
declarations: [OnOffDirective],

0 commit comments

Comments
 (0)