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