|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
1 | 4 | // eslint-disable-next-line import/no-extraneous-dependencies |
2 | 5 | import { describe, expect } from "@jest/globals"; |
3 | | -import { restructure } from "./content-tree-enhancers.mjs"; |
| 6 | +import { enhance, restructure } from "./content-tree-enhancers.mjs"; |
4 | 7 |
|
5 | 8 | describe("restructure", () => { |
6 | 9 | it("applies filter result back to children array", () => { |
@@ -55,3 +58,41 @@ describe("restructure", () => { |
55 | 58 | expect(root.children.map((item) => item.title)).toEqual(["API", "Guides"]); |
56 | 59 | }); |
57 | 60 | }); |
| 61 | + |
| 62 | +describe("enhance", () => { |
| 63 | + const createBlogTree = (body) => { |
| 64 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), "webpack-blog-")); |
| 65 | + const blogDir = path.join(root, "blog"); |
| 66 | + fs.mkdirSync(blogDir); |
| 67 | + const filePath = path.join(blogDir, "example.mdx"); |
| 68 | + fs.writeFileSync(filePath, `---\ntitle: Example\n---\n\n${body}`); |
| 69 | + |
| 70 | + return { |
| 71 | + root, |
| 72 | + tree: { |
| 73 | + type: "file", |
| 74 | + path: filePath, |
| 75 | + extension: ".mdx", |
| 76 | + name: "example.mdx", |
| 77 | + }, |
| 78 | + }; |
| 79 | + }; |
| 80 | + |
| 81 | + it("does not append an ellipsis to an untruncated blog teaser", () => { |
| 82 | + const { root, tree } = createBlogTree("Short body."); |
| 83 | + |
| 84 | + enhance(tree, { dir: root }); |
| 85 | + |
| 86 | + expect(tree.teaser).toBe("Short body."); |
| 87 | + }); |
| 88 | + |
| 89 | + it("appends an ellipsis when the blog teaser is truncated", () => { |
| 90 | + const { root, tree } = createBlogTree( |
| 91 | + ["First line.", "Second line.", "Third line.", "Fourth line."].join("\n"), |
| 92 | + ); |
| 93 | + |
| 94 | + enhance(tree, { dir: root }); |
| 95 | + |
| 96 | + expect(tree.teaser).toBe("First line. Second line. Third line...."); |
| 97 | + }); |
| 98 | +}); |
0 commit comments