Skip to content

Commit

Permalink
test: add HTML tests as well as test for different HTML structures
Browse files Browse the repository at this point in the history
  • Loading branch information
nik-rev committed Sep 30, 2024
1 parent 90f71bb commit ad9b277
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 9 deletions.
66 changes: 57 additions & 9 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import rehypeStringify from "rehype-stringify";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeParse from "rehype-parse";
import { unified } from "unified";
import rehypeSemanticBlockquotes from "./index.ts";

import test from "node:test";
import assert from "node:assert";

const tests = [
const markdownTests = [
[
"Will do nothing if there is no linebreak between the credit and the content",
"Do nothing if there is no linebreak between the credit and the content",
`> Better to admit you walked through the wrong door than spend your life in the wrong room.
> @ Josh Davis
`,
Expand All @@ -19,7 +20,7 @@ const tests = [
</blockquote>`,
],
[
"Will not transform markdown link",
"Does not transform markdown link",
`> Better to admit you walked through the wrong door than spend your life in the wrong room.
>
> @ [Josh Davis](https://somewhere.com)
Expand All @@ -29,7 +30,7 @@ const tests = [
</blockquote><figcaption data-blockquote-credit=""><p><a href="https://somewhere.com">Josh Davis</a></p></figcaption></figure>`,
],
[
"Will not transform complicated markdown syntax with reveral nodes",
"Does not transform complicated markdown syntax with reveral nodes",
`
> Better to admit you walked through the wrong door than spend your life in the wrong room.
>
Expand All @@ -40,7 +41,7 @@ const tests = [
</blockquote><figcaption data-blockquote-credit=""><p>Credit: <a href="https://www.somewhere.com">Josh Davis</a>, we obtained the quote at <strong>some website</strong></p></figcaption></figure>`,
],
[
"Will transform a multi-line blockquote with a caption",
"Transforms a multi-line blockquote with a caption",
`
> Better to admit you walked through the wrong door
> than spend your life in the wrong room.
Expand All @@ -53,7 +54,7 @@ than spend your life in the wrong room.</p>
</blockquote><figcaption data-blockquote-credit=""><p>Josh Davis</p></figcaption></figure>`,
],
[
"Will transform a blockquote with a nested list in the content",
"Transforms a blockquote with a nested list in the content",
`
> Here are some steps:
>
Expand All @@ -73,7 +74,7 @@ than spend your life in the wrong room.</p>
</blockquote><figcaption data-blockquote-credit=""><p>Jane Doe</p></figcaption></figure>`,
],
[
"Will transform a blockquote with inline code in the content",
"Transforms a blockquote with inline code in the content",
`
> Use the \`console.log()\` function to debug your code.
>
Expand All @@ -85,7 +86,40 @@ than spend your life in the wrong room.</p>
],
];

tests.forEach(async ([message, input, expected]) => {
const htmlTests = [
[
"Transforms a simple HTML blockquote with credit",
`<blockquote><p>Simple quote.</p><p>@ Author Name</p></blockquote>`,
`<html><head></head><body><figure data-blockquote-container=""><blockquote data-blockquote-content=""><p>Simple quote.</p></blockquote><figcaption data-blockquote-credit=""><p>Author Name</p></figcaption></figure></body></html>`
],
[
"Handles minified HTML",
`<blockquote><p>Minified quote.</p><p>@ Author</p></blockquote>`,
`<html><head></head><body><figure data-blockquote-container=""><blockquote data-blockquote-content=""><p>Minified quote.</p></blockquote><figcaption data-blockquote-credit=""><p>Author</p></figcaption></figure></body></html>`
],
[
"Handles nested elements in blockquote",
`<blockquote><p>Quote with <em>emphasis</em> and <strong>strong</strong>.</p><p>@ Author</p></blockquote>`,
`<html><head></head><body><figure data-blockquote-container=""><blockquote data-blockquote-content=""><p>Quote with <em>emphasis</em> and <strong>strong</strong>.</p></blockquote><figcaption data-blockquote-credit=""><p>Author</p></figcaption></figure></body></html>`
],
[
"Does not transform when credit syntax is not at the end",
`<blockquote><p>@ Not at the end.</p><p>This should not transform.</p></blockquote>`,
`<html><head></head><body><blockquote><p>@ Not at the end.</p><p>This should not transform.</p></blockquote></body></html>`
],
[
"Handles multiple paragraphs in blockquote",
`<blockquote><p>First paragraph.</p><p>Second paragraph.</p><p>@ Author</p></blockquote>`,
`<html><head></head><body><figure data-blockquote-container=""><blockquote data-blockquote-content=""><p>First paragraph.</p><p>Second paragraph.</p></blockquote><figcaption data-blockquote-credit=""><p>Author</p></figcaption></figure></body></html>`
],
[
"Preserves whitespace in credit",
`<blockquote><p>Quote.</p><p>@ Author with spaces </p></blockquote>`,
`<html><head></head><body><figure data-blockquote-container=""><blockquote data-blockquote-content=""><p>Quote.</p></blockquote><figcaption data-blockquote-credit=""><p> Author with spaces </p></figcaption></figure></body></html>`
]
];

markdownTests.forEach(async ([message, input, expected]) => {
const actual = String(
await unified()
.use(remarkParse)
Expand All @@ -95,7 +129,21 @@ tests.forEach(async ([message, input, expected]) => {
.process(input),
);

test(message, () => {
test(`MD -> HTML: ${message}`, () => {
assert.strictEqual(actual, expected);
});
});

htmlTests.forEach(async ([message, input, expected]) => {
const actual = String(
await unified()
.use(rehypeParse)
.use(rehypeSemanticBlockquotes)
.use(rehypeStringify)
.process(input)
);

test(`HTML: ${message}`, () => {
assert.strictEqual(actual, expected);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"rehype-parse": "^9.0.1",
"rehype-stringify": "^10.0.0",
"remark-parse": "^11.0.0",
"remark-rehype": "^11.1.1",
Expand Down
82 changes: 82 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ad9b277

Please sign in to comment.