-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcontent-tree-enhancers.test.mjs
More file actions
98 lines (83 loc) · 2.48 KB
/
content-tree-enhancers.test.mjs
File metadata and controls
98 lines (83 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
// eslint-disable-next-line import/no-extraneous-dependencies
import { describe, expect } from "@jest/globals";
import { enhance, restructure } from "./content-tree-enhancers.mjs";
describe("restructure", () => {
it("applies filter result back to children array", () => {
const originalChildren = [
{
type: "directory",
path: "src/content/guides",
title: "Guides",
},
{
type: "directory",
path: "src/content/api",
title: "API",
},
];
const root = {
type: "directory",
path: "src/content",
children: originalChildren,
};
restructure(root, { dir: "src/content" });
// Filter creates a new array; restructure must assign that result back.
expect(root.children).not.toBe(originalChildren);
expect(root.children).toHaveLength(2);
});
it("sorts children after restructuring", () => {
const root = {
type: "directory",
path: "src/content",
children: [
{
type: "directory",
path: "src/content/guides",
title: "Guides",
sort: 20,
},
{
type: "directory",
path: "src/content/api",
title: "API",
sort: 10,
},
],
};
restructure(root, { dir: "src/content" });
expect(root.children.map((item) => item.title)).toEqual(["API", "Guides"]);
});
});
describe("enhance", () => {
const createBlogTree = (body) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "webpack-blog-"));
const blogDir = path.join(root, "blog");
fs.mkdirSync(blogDir);
const filePath = path.join(blogDir, "example.mdx");
fs.writeFileSync(filePath, `---\ntitle: Example\n---\n\n${body}`);
return {
root,
tree: {
type: "file",
path: filePath,
extension: ".mdx",
name: "example.mdx",
},
};
};
it("does not append an ellipsis to an untruncated blog teaser", () => {
const { root, tree } = createBlogTree("Short body.");
enhance(tree, { dir: root });
expect(tree.teaser).toBe("Short body.");
});
it("appends an ellipsis when the blog teaser is truncated", () => {
const { root, tree } = createBlogTree(
["First line.", "Second line.", "Third line.", "Fourth line."].join("\n"),
);
enhance(tree, { dir: root });
expect(tree.teaser).toBe("First line. Second line. Third line....");
});
});