From 02531a6c33ad4a28429c55d11532c324327e1d0b Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Mon, 6 Dec 2021 13:08:03 +0100 Subject: [PATCH 1/2] test: add test case for #269 --- .../src/app/issues/issue-269.spec.ts | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 apps/example-app/src/app/issues/issue-269.spec.ts diff --git a/apps/example-app/src/app/issues/issue-269.spec.ts b/apps/example-app/src/app/issues/issue-269.spec.ts new file mode 100644 index 00000000..fd9c591d --- /dev/null +++ b/apps/example-app/src/app/issues/issue-269.spec.ts @@ -0,0 +1,34 @@ +import { render, screen } from '@testing-library/angular'; +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'stripHTML', +}) +class StripHTMLPipe implements PipeTransform { + transform(stringValueWithHTML: string): string { + return stringValueWithHTML.replace(/<[^>]*>?/gm, ''); + } +} + +const STRING_WITH_HTML = + 'Some database field
with stripped HTML
'; + +test('should strip the HTML from a string', async () => { + await expect(() => + render(`

{{ '${STRING_WITH_HTML}' | stripHTML }}

`, { + declarations: [StripHTMLPipe], + }), + ).rejects.toThrow(); +}); + +test('workaround should strip the HTML from a string', async () => { + await render(`

{{ stringWithHtml | stripHTML }}

`, { + componentProperties: { + stringWithHtml: STRING_WITH_HTML, + }, + declarations: [StripHTMLPipe], + }); + + const testControl = screen.getByTestId('test'); + expect(testControl).toHaveTextContent('Some database field with stripped HTML'); +}); From d4aaeb6f4782470d7125acf779686da45a3ef6b8 Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Tue, 7 Dec 2021 17:37:41 +0100 Subject: [PATCH 2/2] update as example --- .../18-html-as-input.spec.ts} | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) rename apps/example-app/src/app/{issues/issue-269.spec.ts => examples/18-html-as-input.spec.ts} (67%) diff --git a/apps/example-app/src/app/issues/issue-269.spec.ts b/apps/example-app/src/app/examples/18-html-as-input.spec.ts similarity index 67% rename from apps/example-app/src/app/issues/issue-269.spec.ts rename to apps/example-app/src/app/examples/18-html-as-input.spec.ts index fd9c591d..5a56b412 100644 --- a/apps/example-app/src/app/issues/issue-269.spec.ts +++ b/apps/example-app/src/app/examples/18-html-as-input.spec.ts @@ -13,22 +13,24 @@ class StripHTMLPipe implements PipeTransform { const STRING_WITH_HTML = 'Some database field
with stripped HTML
'; -test('should strip the HTML from a string', async () => { - await expect(() => - render(`

{{ '${STRING_WITH_HTML}' | stripHTML }}

`, { - declarations: [StripHTMLPipe], - }), - ).rejects.toThrow(); -}); - -test('workaround should strip the HTML from a string', async () => { - await render(`

{{ stringWithHtml | stripHTML }}

`, { +// https://github.com/testing-library/angular-testing-library/pull/271 +test('passes HTML as component properties', async () => { + await render(`

{{ stringWithHtml | stripHTML }}

`, { componentProperties: { stringWithHtml: STRING_WITH_HTML, }, declarations: [StripHTMLPipe], }); - const testControl = screen.getByTestId('test'); - expect(testControl).toHaveTextContent('Some database field with stripped HTML'); + expect(screen.getByText('Some database field with stripped HTML')).toBeInTheDocument(); +}); + + +test('throws when passed HTML is passed in directly', async () => { + await expect(() => + render(`

{{ '${STRING_WITH_HTML}' | stripHTML }}

`, { + declarations: [StripHTMLPipe], + }), + ).rejects.toThrow(); }); +