|
| 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 | + const post = blogData.posts[0]; |
| 102 | + expect(post.title).toEqual('POST 1'); |
| 103 | + expect(post.date).toEqual(new Date('2020-01-01T00:00:00.000Z')); |
| 104 | + expect(post.author).toEqual('author-a'); |
| 105 | + const post1 = blogData.posts[1]; |
| 106 | + expect(post1.title).toEqual('POST 2'); |
| 107 | + expect(post1.date).toEqual(new Date('2020-01-02T00:00:00.000Z')); |
| 108 | + expect(post1.author).toEqual('author-b'); |
| 109 | + const post2 = blogData.posts[2]; |
| 110 | + expect(post2.title).toEqual('POST 3'); |
| 111 | + expect(post2.date.setMilliseconds(0)).toEqual( |
| 112 | + currentDate.setMilliseconds(0) |
| 113 | + ); |
| 114 | + expect(post2.author).toEqual('author-c'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should generate categories based on the categories of md files and their years', async () => { |
| 118 | + files = [ |
| 119 | + { |
| 120 | + path: 'pages/en/blog/post1.md', |
| 121 | + frontMatterContent: [ |
| 122 | + "date: '2020-01-01T00:00:00.000Z'", |
| 123 | + 'category: category-a', |
| 124 | + ], |
| 125 | + }, |
| 126 | + { |
| 127 | + path: 'pages/en/blog/sub-dir/post2.md', |
| 128 | + frontMatterContent: [ |
| 129 | + "date: '2020-01-02T00:00:00.000Z'", |
| 130 | + 'category: category-b', |
| 131 | + ], |
| 132 | + }, |
| 133 | + { |
| 134 | + path: 'pages/en/blog/post3.md', |
| 135 | + frontMatterContent: [ |
| 136 | + "date: '2021-03-13T00:00:00.000Z'", |
| 137 | + // no category specified (it should be "uncategorized") |
| 138 | + ], |
| 139 | + }, |
| 140 | + { |
| 141 | + path: 'pages/en/blog/post4.md', |
| 142 | + frontMatterContent: [ |
| 143 | + // no date specified (the date defaults to the current date) |
| 144 | + 'category: category-b', |
| 145 | + ], |
| 146 | + }, |
| 147 | + ]; |
| 148 | + |
| 149 | + const blogData = await generateBlogData(); |
| 150 | + |
| 151 | + expect(blogData.categories.sort()).toStrictEqual([ |
| 152 | + 'all', |
| 153 | + 'category-a', |
| 154 | + 'category-b', |
| 155 | + 'uncategorized', |
| 156 | + 'year-2020', |
| 157 | + 'year-2021', |
| 158 | + `year-${new Date().getUTCFullYear()}`, |
| 159 | + ]); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should generate slugs based on the md filenames and categories', async () => { |
| 163 | + files = [ |
| 164 | + { |
| 165 | + path: 'pages/en/blog/post1.md', |
| 166 | + frontMatterContent: ['category: category-a'], |
| 167 | + }, |
| 168 | + { |
| 169 | + path: 'pages/en/blog/post2.md', |
| 170 | + frontMatterContent: ['category: category-b'], |
| 171 | + }, |
| 172 | + { |
| 173 | + path: 'pages/en/blog/post3.md', |
| 174 | + frontMatterContent: [ |
| 175 | + // no category specified |
| 176 | + ], |
| 177 | + }, |
| 178 | + ]; |
| 179 | + |
| 180 | + const blogData = await generateBlogData(); |
| 181 | + |
| 182 | + expect(blogData.posts.map(p => p.slug).sort()).toStrictEqual([ |
| 183 | + '/blog/category-a/post1', |
| 184 | + '/blog/category-b/post2', |
| 185 | + '/blog/uncategorized/post3', |
| 186 | + ]); |
| 187 | + }); |
| 188 | +}); |
0 commit comments