|
1 | 1 | import React from "react"; |
2 | | -import { render } from "@testing-library/react"; |
| 2 | +import {render, RenderResult} from "@testing-library/react"; |
3 | 3 |
|
4 | 4 | import "@testing-library/jest-dom"; |
5 | 5 |
|
6 | 6 | import { Markdown, TextReducer } from "./../../"; |
7 | 7 | import { Default as TextReducerStory } from "./TextReducer.stories"; |
8 | 8 |
|
9 | 9 | describe("TextReducer", () => { |
| 10 | + const textMustExist = (queryByText: RenderResult["queryByText"], text: string) => { |
| 11 | + expect(queryByText(text, { exact: false })).not.toBeNull(); |
| 12 | + } |
| 13 | + const textMustNotExist = (queryByText: RenderResult["queryByText"], text: string) => { |
| 14 | + expect(queryByText(text, { exact: false })).toBeNull(); |
| 15 | + } |
10 | 16 | it("should display encoded HTML entities by default if they are used in the transformed markup", () => { |
11 | 17 | const { queryByText } = render(<TextReducer {...TextReducerStory.args} />); |
12 | | - expect(queryByText("'entities' & "quotes"", { exact: false })).not.toBeNull(); |
13 | | - expect(queryByText(`'entities' & "quotes"`, { exact: false })).toBeNull(); |
| 18 | + textMustExist(queryByText, "'entities' & "quotes""); |
| 19 | + textMustNotExist(queryByText, `'entities' & "quotes"`); |
14 | 20 | }); |
15 | 21 | it("should not display encoded HTML entities if `decodeHtmlEntities` is enabled", () => { |
16 | 22 | const { queryByText } = render(<TextReducer {...TextReducerStory.args} decodeHtmlEntities />); |
17 | | - expect(queryByText("'entities' & "quotes"", { exact: false })).toBeNull(); |
18 | | - expect(queryByText(`'entities' & "quotes"`, { exact: false })).not.toBeNull(); |
| 23 | + textMustNotExist(queryByText, "'entities' & "quotes""); |
| 24 | + textMustExist(queryByText, `'entities' & "quotes"`); |
19 | 25 | }); |
20 | 26 | it("should only decode if correct encoded HTML entities are found (strict mode)", () => { |
21 | 27 | const { queryByText } = render( |
22 | 28 | <TextReducer decodeHtmlEntities> |
23 | 29 | <Markdown>&</Markdown>& foo&bar |
24 | 30 | </TextReducer> |
25 | 31 | ); |
26 | | - expect(queryByText("& & foo&bar", { exact: false })).not.toBeNull(); |
27 | | - expect(queryByText("& & foo&bar", { exact: false })).toBeNull(); |
| 32 | + textMustExist(queryByText, "& & foo&bar"); |
| 33 | + textMustNotExist(queryByText, "& & foo&bar"); |
28 | 34 | }); |
29 | 35 | it("should allow decoding non-strict encoded HTML entities", () => { |
30 | 36 | const { queryByText } = render( |
31 | 37 | <TextReducer decodeHtmlEntities decodeHtmlEntitiesOptions={{ strict: false }}> |
32 | 38 | <Markdown>&</Markdown>& foo&bar |
33 | 39 | </TextReducer> |
34 | 40 | ); |
35 | | - expect(queryByText("& & foo&bar", { exact: false })).toBeNull(); |
36 | | - expect(queryByText("& & foo&bar", { exact: false })).not.toBeNull(); |
| 41 | + textMustNotExist(queryByText, "& & foo&bar"); |
| 42 | + textMustExist(queryByText, "& & foo&bar"); |
37 | 43 | }); |
38 | 44 | }); |
0 commit comments