Skip to content

Commit 2ee960f

Browse files
committed
fix overwriting default options for decoding and add tests
1 parent 329461b commit 2ee960f

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/common/utils/reduceToText.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export const reduceToText: ReduceToTextFuncType = (input, options) => {
5858
.split(" ")
5959
.map((value) => {
6060
try {
61-
return utils.decodeHtmlEntities(value, { ...decodeDefaultOptions });
61+
return utils.decodeHtmlEntities(value, {
62+
...decodeDefaultOptions,
63+
...options?.decodeHtmlEntitiesOptions,
64+
});
6265
} catch {
6366
decodeErrors++;
6467
return value;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from "react";
2+
import { render } 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+
it("should display encoded HTML entities by default if they are used in the transformed markup", () => {
11+
const { queryByText } = render(<TextReducer {...TextReducerStory.args} />);
12+
expect(queryByText("&#x27;entities&#x27; &amp; &quot;quotes&quot;", { exact: false })).not.toBeNull();
13+
expect(queryByText(`'entities' & "quotes"`, { exact: false })).toBeNull();
14+
});
15+
it("should not display encoded HTML entities if `decodeHtmlEntities` is enabled", () => {
16+
const { queryByText } = render(<TextReducer {...TextReducerStory.args} decodeHtmlEntities />);
17+
expect(queryByText("&#x27;entities&#x27; &amp; &quot;quotes&quot;", { exact: false })).toBeNull();
18+
expect(queryByText(`'entities' & "quotes"`, { exact: false })).not.toBeNull();
19+
});
20+
it("should only decode if correct encoded HTML entities are found (strict mode)", () => {
21+
const { queryByText } = render(
22+
<TextReducer decodeHtmlEntities>
23+
<Markdown>&</Markdown>&amp foo&ampbar
24+
</TextReducer>
25+
);
26+
expect(queryByText("& &amp foo&ampbar", { exact: false })).not.toBeNull();
27+
expect(queryByText("& & foo&ampbar", { exact: false })).toBeNull();
28+
});
29+
it("should allow decoding non-strict encoded HTML entities", () => {
30+
const { queryByText } = render(
31+
<TextReducer decodeHtmlEntities decodeHtmlEntitiesOptions={{ strict: false }}>
32+
<Markdown>&</Markdown>&amp foo&ampbar
33+
</TextReducer>
34+
);
35+
expect(queryByText("& &amp foo&ampbar", { exact: false })).toBeNull();
36+
expect(queryByText("& & foo&ampbar", { exact: false })).not.toBeNull();
37+
});
38+
});

0 commit comments

Comments
 (0)