Skip to content

Commit

Permalink
test: test warnings on all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Jul 18, 2024
1 parent f8ad9fe commit 5cff6b3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/richtext/htmlAsRichText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const htmlAsRichText = (
config?: AsRichTextConfig,
): AsRichTextReturnType => {
const { result, messages } = unified()
.use(rehypeParse, { emitParseErrors: true })
.use(rehypeParse, { emitParseErrors: true, missingDoctype: 0 })
.use(rehypeRichText, config)
.processSync(html);

Expand Down
9 changes: 9 additions & 0 deletions test/__testutils__/testAsRichTextHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ type TestAsRichTextHelperArgs = {

config?: AsRichTextConfig;

/**
* Warnings that are expected to be present in the output.
*/
expectWarnings?: string[];

/**
* The rich text format is a lossy representation of HTML. Namely it does not
* preserves indentation and applies some optimizations to the output such as
Expand Down Expand Up @@ -55,6 +60,10 @@ const testAsRichTextHelperFactory = (
},
});

expect(
output.warnings.map((warning) => warning.message).sort(),
).toStrictEqual(args.expectWarnings?.sort() ?? []);

if (!args.expectAsHTMLNotToMatchInput) {
expect(outputAsHTML).toBe(args.input);
} else {
Expand Down
15 changes: 15 additions & 0 deletions test/richtext/__snapshots__/htmlAsRichText.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ exports[`transforms HTML to rich text > embed > iframe 1`] = `
]
`;

exports[`transforms HTML to rich text > embed > missing src 1`] = `[]`;

exports[`transforms HTML to rich text > image > empty alt 1`] = `
[
{
Expand Down Expand Up @@ -688,6 +690,8 @@ exports[`transforms HTML to rich text > image > missing alt 1`] = `
]
`;

exports[`transforms HTML to rich text > image > missing src 1`] = `[]`;

exports[`transforms HTML to rich text > image > non-prismic 1`] = `
[
{
Expand Down Expand Up @@ -1002,6 +1006,17 @@ exports[`transforms HTML to rich text > spans > hyperlink 1`] = `
]
`;

exports[`transforms HTML to rich text > spans > hyperlink missing href 1`] = `
[
{
"direction": "ltr",
"spans": [],
"text": "lorem ipsum dolor sit amet",
"type": "paragraph",
},
]
`;

exports[`transforms HTML to rich text > spans > label 1`] = `
[
{
Expand Down
48 changes: 22 additions & 26 deletions test/richtext/htmlAsRichText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe("transforms HTML to rich text", () => {
input: /* html */ `<p>lorem <a href="https://prismic.io">ipsum</a> dolor <a href="https://prismic.io" target="_blank">sit</a> amet</p>`,
});

testHTMLAsRichTextHelper("hyperlink missing href", {
input: /* html */ `<p>lorem <a>ipsum</a> dolor sit amet</p>`,
expectWarnings: [
"Element of type `hyperlink` is missing an `href` attribute",
],
expectAsHTMLNotToMatchInput: true,
});

testHTMLAsRichTextHelper("nested spans", {
input: /* html */ `<p>lorem <strong>ips<em>um <a href="https://prismic.io">dolor</a></em></strong> sit amet</p>`,
});
Expand Down Expand Up @@ -119,6 +127,12 @@ describe("transforms HTML to rich text", () => {
expectAsHTMLNotToMatchInput: true,
});

testHTMLAsRichTextHelper("missing src", {
input: /* html */ `<img />`,
expectWarnings: ["Element of type `img` is missing an `src` attribute"],
expectAsHTMLNotToMatchInput: true,
});

describe("extracts image in text nodes and resume previous text node", () => {
testHTMLAsRichTextHelper("basic", {
input: /* html */ `<p>lorem ipsum <img src="https://example.com/foo.png" alt="bar" /> dolor sit amet</p>`,
Expand Down Expand Up @@ -153,6 +167,12 @@ describe("transforms HTML to rich text", () => {
input: /* html */ `<blockquote class="twitter-tweet" src="https://twitter.com/li_hbr/status/1803718142222282829?ref_src=twsrc%5Etfw"><p lang="en" dir="ltr">Slack bot that uses AI to tl;dr; threads for you, anyone?</p>— Lucie (@li_hbr) <a href="https://twitter.com/li_hbr/status/1803718142222282829?ref_src=twsrc%5Etfw">June 20, 2024</a></blockquote>`,
config: { serializer: { blockquote: "embed" } },
});

testHTMLAsRichTextHelper("missing src", {
input: /* html */ `<iframe></iframe>`,
expectWarnings: ["Element of type `embed` is missing an `src` attribute"],
expectAsHTMLNotToMatchInput: true,
});
});

describe("configuration", () => {
Expand Down Expand Up @@ -224,7 +244,7 @@ describe("transforms HTML to rich text", () => {
});

testHTMLAsRichTextHelper("image node", {
input: /* html */ `<foo src="https://example.com/foo.png" alt="foo" />`,
input: /* html */ `<foo src="https://example.com/foo.png" alt="foo"></foo>`,
config: {
serializer: {
foo: () => ({
Expand All @@ -242,7 +262,7 @@ describe("transforms HTML to rich text", () => {
});

testHTMLAsRichTextHelper("embed node", {
input: /* html */ `<foo src="https://example.com/foo.png" />`,
input: /* html */ `<foo src="https://example.com/foo.png"></foo>`,
config: {
serializer: {
foo: () => ({
Expand Down Expand Up @@ -465,27 +485,3 @@ describe("transforms HTML to rich text", () => {
});
});
});

type WarnCase = {
name: string;
input: string;
};

it.each<WarnCase>([
{
name: "element of type `img` is missing an `src` attribute",
input: /* html */ `<img>`,
},
{
name: "element of type `embed` is missing an `src` attribute",
input: /* html */ `<iframe>lorem ipsum dolor sit amet</iframe>`,
},
{
name: "element of type `hyperlink` is missing an `href` attribute",
input: /* html */ `<p><a>missing-hyperlink-href</a></p>`,
},
])("warns on unprocessable elements ($name)", ({ name, input }) => {
const output = htmlAsRichText(input);

expect(output.warnings.toString()).toMatch(new RegExp(name, "i"));
});

0 comments on commit 5cff6b3

Please sign in to comment.