From 28750a4a1cc0fb62f4f6764a88a1851833f054cf Mon Sep 17 00:00:00 2001 From: Yo Iwamoto Date: Mon, 8 Apr 2024 20:45:36 +0900 Subject: [PATCH] update transform-article-html test --- .../transform-article-html.test.tsx.snap | 4 +- src/lib/transform-article-html.test.tsx | 62 ++++++++++++------- src/lib/transform-article-html.ts | 2 +- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/lib/__snapshots__/transform-article-html.test.tsx.snap b/src/lib/__snapshots__/transform-article-html.test.tsx.snap index fb89fd8..9899ca1 100644 --- a/src/lib/__snapshots__/transform-article-html.test.tsx.snap +++ b/src/lib/__snapshots__/transform-article-html.test.tsx.snap @@ -12,5 +12,7 @@ exports[`transformArticleHtml > transform 結果の snapshot が一致するこ
  1. Item 1
  2. Item 2
  3. -
" + +
src/hello-world.tsconsole.log("Hello, world!");
+
" `; diff --git a/src/lib/transform-article-html.test.tsx b/src/lib/transform-article-html.test.tsx index f5d4375..b75ae92 100644 --- a/src/lib/transform-article-html.test.tsx +++ b/src/lib/transform-article-html.test.tsx @@ -12,33 +12,49 @@ const html = `

Heading 2

  1. Item 1
  2. Item 2
  3. -
`; + +
+console.log("Hello, world!");
+
`; describe("transformArticleHtml", () => { - it("見出しがアンカーリンクで囲われていること", () => { - const transformed = transformArticleHtml(html); - render( - // biome-ignore lint/security/noDangerouslySetInnerHtml: body は HTML 形式で取得されるため -
, - ); - expect(screen.getByRole("link", { name: "Heading 2" })).toHaveAttribute( - "href", - "#heading-2", - ); - expect(screen.getByRole("link", { name: "Heading 3" })).toHaveAttribute( - "href", - "#heading-3", - ); + describe("assignIdToHeadings", () => { + it("見出しがアンカーリンクで囲われていること", () => { + const transformed = transformArticleHtml(html); + render( + // biome-ignore lint/security/noDangerouslySetInnerHtml: body は HTML 形式で取得されるため +
, + ); + expect(screen.getByRole("link", { name: "Heading 2" })).toHaveAttribute( + "href", + "#heading-2", + ); + expect(screen.getByRole("link", { name: "Heading 3" })).toHaveAttribute( + "href", + "#heading-3", + ); + }); + + it("見出し要素が存在すること", () => { + const transformed = transformArticleHtml(html); + render( + // biome-ignore lint/security/noDangerouslySetInnerHtml: body は HTML 形式で取得されるため +
, + ); + expect(screen.getByRole("heading", { name: "Heading 2" })).toBeDefined(); + expect(screen.getByRole("heading", { name: "Heading 3" })).toBeDefined(); + }); }); - it("見出し要素が存在すること", () => { - const transformed = transformArticleHtml(html); - render( - // biome-ignore lint/security/noDangerouslySetInnerHtml: body は HTML 形式で取得されるため -
, - ); - expect(screen.getByRole("heading", { name: "Heading 2" })).toBeDefined(); - expect(screen.getByRole("heading", { name: "Heading 3" })).toBeDefined(); + describe("addFileNameElement", () => { + it("ファイル名が表示されていること", () => { + const transformed = transformArticleHtml(html); + render( + // biome-ignore lint/security/noDangerouslySetInnerHtml: body は HTML 形式で取得されるため +
, + ); + expect(screen.getByText("src/hello-world.ts")).toBeDefined(); + }); }); it("transform 結果の snapshot が一致すること", () => { diff --git a/src/lib/transform-article-html.ts b/src/lib/transform-article-html.ts index af85731..a3529bb 100644 --- a/src/lib/transform-article-html.ts +++ b/src/lib/transform-article-html.ts @@ -36,7 +36,7 @@ function assignIdToHeadings(document: DOMWindow["document"]) { } } -const FILE_NAME_CLASS_PATTERN = /^language-[a-z]+:([a-z0-9.-\/]+)$/i; +const FILE_NAME_CLASS_PATTERN = /^language-[a-z]+:([a-z0-9.\-\/]+)$/i; function addFileNameElement(document: Document) { const codeElements = document.querySelectorAll("pre code");