From 4c81ed93528fd67fbaf9bdc46444f9e2354fc13c Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Fri, 26 Nov 2021 21:52:28 +0100 Subject: [PATCH] feat: remove RenderDirectiveOptions (#265) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: '', componentProperties: { sendValue: sendSpy, }, }); ``` AFTER: ```ts // 👇 Move the template in the first argument await render('', { // 👇 Add the component to declarations declarations: [InputOutputComponent], componentProperties: { sendValue: sendSpy, }, }); ``` --- .../src/app/examples/11-ng-content.spec.ts | 2 +- projects/testing-library/src/lib/models.ts | 37 ------------------- .../src/lib/testing-library.ts | 33 ++++------------- .../tests/render-template.spec.ts | 9 ----- 4 files changed, 8 insertions(+), 73 deletions(-) diff --git a/apps/example-app/src/app/examples/11-ng-content.spec.ts b/apps/example-app/src/app/examples/11-ng-content.spec.ts index 2dedd809..f061e862 100644 --- a/apps/example-app/src/app/examples/11-ng-content.spec.ts +++ b/apps/example-app/src/app/examples/11-ng-content.spec.ts @@ -4,7 +4,7 @@ import { CellComponent } from './11-ng-content'; test('it is possible to test ng-content without selector', async () => { const projection = 'it should be showed into a p element!'; - + await render(`${projection}`, { declarations: [CellComponent] }); diff --git a/projects/testing-library/src/lib/models.ts b/projects/testing-library/src/lib/models.ts index bda39543..7f5a02a7 100644 --- a/projects/testing-library/src/lib/models.ts +++ b/projects/testing-library/src/lib/models.ts @@ -256,43 +256,6 @@ export interface RenderComponentOptions - extends RenderComponentOptions { - /** - * @description - * The template to render the directive. - * This template will override the template from the WrapperComponent. - * - * @example - * const component = await render(SpoilerDirective, { - * template: `
` - * }) - * - * @deprecated Use `render(template, { declarations: [SomeDirective] })` instead. - */ - template: string; - /** - * @description - * An Angular component to wrap the component in. - * The template will be overridden with the `template` option. - * - * @default - * `WrapperComponent`, an empty component that strips the `ng-version` attribute - * - * @example - * const component = await render(SpoilerDirective, { - * template: `
` - * wrapper: CustomWrapperComponent - * }) - */ - wrapper?: Type; - componentProperties?: Partial; -} - // eslint-disable-next-line @typescript-eslint/ban-types export interface RenderTemplateOptions extends RenderComponentOptions { diff --git a/projects/testing-library/src/lib/testing-library.ts b/projects/testing-library/src/lib/testing-library.ts index bfbe7f93..6d66e933 100644 --- a/projects/testing-library/src/lib/testing-library.ts +++ b/projects/testing-library/src/lib/testing-library.ts @@ -26,7 +26,7 @@ import { getQueriesForElement, queries as dtlQueries, } from '@testing-library/dom'; -import { RenderComponentOptions, RenderDirectiveOptions, RenderTemplateOptions, RenderResult } from './models'; +import { RenderComponentOptions, RenderTemplateOptions, RenderResult } from './models'; import { getConfig } from './config'; const mountedFixtures = new Set>(); @@ -36,13 +36,6 @@ export async function render( component: Type, renderOptions?: RenderComponentOptions, ): Promise>; -/** - * @deprecated Use `render(template, { declarations: [DirectiveType] })` instead. - */ -export async function render( - component: Type, - renderOptions?: RenderDirectiveOptions, -): Promise>; export async function render( template: string, renderOptions?: RenderTemplateOptions, @@ -50,10 +43,7 @@ export async function render( export async function render( sut: Type | string, - renderOptions: - | RenderComponentOptions - | RenderDirectiveOptions - | RenderTemplateOptions = {}, + renderOptions: RenderComponentOptions | RenderTemplateOptions = {}, ): Promise> { const { dom: domConfig, ...globalConfig } = getConfig(); const { @@ -86,7 +76,6 @@ export async function render( declarations: addAutoDeclarations(sut, { declarations, excludeComponentDeclaration, - template, wrapper, }), imports: addAutoImports({ @@ -106,7 +95,7 @@ export async function render( TestBed.overrideProvider(provide, provider); }); - const componentContainer = createComponentFixture(sut, { template, wrapper }); + const componentContainer = createComponentFixture(sut, { wrapper }); let fixture: ComponentFixture; let detectChanges: () => void; @@ -227,22 +216,18 @@ async function createComponent(component: Type): Promise( sut: Type | string, - { template, wrapper }: Pick, 'template' | 'wrapper'>, + { wrapper }: Pick, 'wrapper'>, ): Type { if (typeof sut === 'string') { TestBed.overrideTemplate(wrapper, sut); return wrapper; } - if (template) { - TestBed.overrideTemplate(wrapper, template); - return wrapper; - } return sut; } function setComponentProperties( fixture: ComponentFixture, - { componentProperties = {} }: Pick, 'componentProperties'>, + { componentProperties = {} }: Pick, 'componentProperties'>, ) { for (const key of Object.keys(componentProperties)) { const descriptor: PropertyDescriptor = Object.getOwnPropertyDescriptor( @@ -293,19 +278,15 @@ function addAutoDeclarations( { declarations, excludeComponentDeclaration, - template, wrapper, - }: Pick, 'declarations' | 'excludeComponentDeclaration' | 'template' | 'wrapper'>, + }: Pick, 'declarations' | 'excludeComponentDeclaration' | 'wrapper'>, ) { if (typeof sut === 'string') { return [...declarations, wrapper]; } - const wrappers = () => (template ? [wrapper] : []); - const components = () => (excludeComponentDeclaration ? [] : [sut]); - - return [...declarations, ...wrappers(), ...components()]; + return [...declarations, ...components()]; } function addAutoImports({ imports, routes }: Pick, 'imports' | 'routes'>) { diff --git a/projects/testing-library/tests/render-template.spec.ts b/projects/testing-library/tests/render-template.spec.ts index 60c2a074..9dd20d01 100644 --- a/projects/testing-library/tests/render-template.spec.ts +++ b/projects/testing-library/tests/render-template.spec.ts @@ -62,15 +62,6 @@ test('the component renders', async () => { expect(screen.getByText('Hello Angular!')).toBeInTheDocument(); }); -test('the directive renders (compatibility with the deprecated signature)', async () => { - const view = await render(OnOffDirective, { - template: '
', - }); - - // eslint-disable-next-line testing-library/no-container - expect(view.container.querySelector('[onoff]')).toBeInTheDocument(); -}); - test('uses the default props', async () => { await render('
', { declarations: [OnOffDirective],