Skip to content

Commit eacdf5a

Browse files
add unit tests for generateBlogData
1 parent 7004629 commit eacdf5a

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import { Readable } from 'node:stream';
2+
3+
import generateBlogData from '@/next-data/generators/blogData.mjs';
4+
5+
let files = [];
6+
7+
jest.mock('node:path', () => ({
8+
...jest.requireActual('node:path'),
9+
join: jest.fn((_base, filePath) => filePath),
10+
}));
11+
12+
jest.mock('node:fs', () => {
13+
const originalFs = jest.requireActual('node:fs');
14+
return {
15+
...originalFs,
16+
createReadStream: jest.fn(filename => {
17+
const readable = new Readable();
18+
const file = files.find(f => f.path === filename);
19+
readable.push(`---\n`);
20+
file.frontMatterContent.forEach(line => readable.push(`${line}\n`));
21+
readable.push(`---\n`);
22+
readable.push(null);
23+
readable.close = () => {};
24+
return readable;
25+
}),
26+
};
27+
});
28+
29+
jest.mock('../../../next.helpers.mjs', () => {
30+
const originalHelpers = jest.requireActual('../../../next.helpers.mjs');
31+
return {
32+
...originalHelpers,
33+
getMarkdownFiles: () => Promise.resolve(files.map(file => file.path)),
34+
};
35+
});
36+
37+
describe('generateBlogData', () => {
38+
it('should return zero posts and only the default "all" category is no md file is found', async () => {
39+
files = [];
40+
41+
const blogData = await generateBlogData();
42+
43+
expect(blogData.categories).toStrictEqual(['all']);
44+
expect(blogData.posts).toStrictEqual([]);
45+
});
46+
47+
it('should collect the data from a single md file if only one is found', async () => {
48+
files = [
49+
{
50+
path: 'pages/en/blog/post1.md',
51+
frontMatterContent: [
52+
`date: '2020-01-01T00:00:00.000Z'`,
53+
`title: POST 1`,
54+
`author: author`,
55+
],
56+
},
57+
];
58+
59+
const blogData = await generateBlogData();
60+
61+
expect(blogData.posts.length).toBe(1);
62+
const post = blogData.posts[0];
63+
expect(post.title).toEqual('POST 1');
64+
expect(post.date).toEqual(new Date('2020-01-01T00:00:00.000Z'));
65+
expect(post.author).toEqual('author');
66+
});
67+
68+
it('should collect the data from multiple md files', async () => {
69+
const currentDate = new Date();
70+
71+
files = [
72+
{
73+
path: 'pages/en/blog/post1.md',
74+
frontMatterContent: [
75+
`date: '2020-01-01T00:00:00.000Z'`,
76+
`title: POST 1`,
77+
`author: author-a`,
78+
],
79+
},
80+
{
81+
path: 'pages/en/blog/post2.md',
82+
frontMatterContent: [
83+
`date: '2020-01-02T00:00:00.000Z'`,
84+
`title: POST 2`,
85+
`author: author-b`,
86+
],
87+
},
88+
{
89+
path: 'pages/en/blog/post3.md',
90+
frontMatterContent: [
91+
// no date specified (the date defaults to the current date)
92+
`title: POST 3`,
93+
`author: author-c`,
94+
],
95+
},
96+
];
97+
98+
const blogData = await generateBlogData();
99+
100+
expect(blogData.posts.length).toBe(3);
101+
expect(blogData.posts[0].title).toEqual('POST 1');
102+
expect(blogData.posts[0].date).toEqual(
103+
new Date('2020-01-01T00:00:00.000Z')
104+
);
105+
expect(blogData.posts[0].author).toEqual('author-a');
106+
expect(blogData.posts[1].title).toEqual('POST 2');
107+
expect(blogData.posts[1].date).toEqual(
108+
new Date('2020-01-02T00:00:00.000Z')
109+
);
110+
expect(blogData.posts[1].author).toEqual('author-b');
111+
expect(blogData.posts[2].title).toEqual('POST 3');
112+
expect(blogData.posts[2].date.setMilliseconds(0)).toEqual(
113+
currentDate.setMilliseconds(0)
114+
);
115+
expect(blogData.posts[2].author).toEqual('author-c');
116+
});
117+
118+
it('should generate categories based on the categories of md files and their years', async () => {
119+
files = [
120+
{
121+
path: 'pages/en/blog/post1.md',
122+
frontMatterContent: [
123+
"date: '2020-01-01T00:00:00.000Z'",
124+
'category: category-a',
125+
],
126+
},
127+
{
128+
path: 'pages/en/blog/sub-dir/post2.md',
129+
frontMatterContent: [
130+
"date: '2020-01-02T00:00:00.000Z'",
131+
'category: category-b',
132+
],
133+
},
134+
{
135+
path: 'pages/en/blog/post3.md',
136+
frontMatterContent: [
137+
"date: '2021-03-13T00:00:00.000Z'",
138+
// no category specified (it should be "uncategorized")
139+
],
140+
},
141+
{
142+
path: 'pages/en/blog/post4.md',
143+
frontMatterContent: [
144+
// no date specified (the date defaults to the current date)
145+
'category: category-b',
146+
],
147+
},
148+
];
149+
150+
const blogData = await generateBlogData();
151+
152+
expect(blogData.categories.sort()).toStrictEqual([
153+
'all',
154+
'category-a',
155+
'category-b',
156+
'uncategorized',
157+
'year-2020',
158+
'year-2021',
159+
`year-${new Date().getUTCFullYear()}`,
160+
]);
161+
});
162+
163+
it('should generate slugs based on the md filenames and categories', async () => {
164+
files = [
165+
{
166+
path: 'pages/en/blog/post1.md',
167+
frontMatterContent: ['category: category-a'],
168+
},
169+
{
170+
path: 'pages/en/blog/post2.md',
171+
frontMatterContent: ['category: category-b'],
172+
},
173+
{
174+
path: 'pages/en/blog/post3.md',
175+
frontMatterContent: [
176+
// no category specified
177+
],
178+
},
179+
];
180+
181+
const blogData = await generateBlogData();
182+
183+
expect(blogData.posts.map(p => p.slug).sort()).toStrictEqual([
184+
'/blog/category-a/post1',
185+
'/blog/category-b/post2',
186+
'/blog/uncategorized/post3',
187+
]);
188+
});
189+
});

0 commit comments

Comments
 (0)