From ead6805489e735bc72c3631b51ab0d9ead7bef63 Mon Sep 17 00:00:00 2001 From: Bogdan Doroschenko Date: Wed, 26 Aug 2020 21:48:40 +0300 Subject: [PATCH] make unit test is determinate --- .../blog/no date.md | 0 .../__fixtures__/website/blog/simple-slug.md | 2 +- .../src/__tests__/index.test.ts | 67 ++++++++++--------- 3 files changed, 38 insertions(+), 31 deletions(-) rename packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/{website => website-blog-without-date}/blog/no date.md (100%) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/no date.md b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website-blog-without-date/blog/no date.md similarity index 100% rename from packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/no date.md rename to packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website-blog-without-date/blog/no date.md diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/simple-slug.md b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/simple-slug.md index 470ad3a684bf..9bee9390a934 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/simple-slug.md +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/simple-slug.md @@ -1,7 +1,7 @@ --- slug: /simple/slug title: Simple Slug -date: 2020-08-16 +date: 2020-08-15 --- simple url slug diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts index 7c06d2b812eb..76dc827e3e80 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts @@ -21,9 +21,8 @@ function validateAndNormalize(schema, options) { } describe('loadBlog', () => { - const siteDir = path.join(__dirname, '__fixtures__', 'website'); const pluginPath = 'blog'; - const getBlogPosts = async () => { + const getBlogPosts = async (siteDir) => { const generatedFilesDir: string = path.resolve(siteDir, '.docusaurus'); const siteConfig = { title: 'Hello', @@ -48,11 +47,8 @@ describe('loadBlog', () => { }; test('simple website', async () => { - const blogPosts = await getBlogPosts(); - const noDateSource = path.join('@site', pluginPath, 'no date.md'); - const noDateSourceBirthTime = ( - await fs.stat(noDateSource.replace('@site', siteDir)) - ).birthtime; + const siteDir = path.join(__dirname, '__fixtures__', 'website'); + const blogPosts = await getBlogPosts(siteDir); expect({ ...blogPosts.find((v) => v.metadata.title === 'date-matter').metadata, @@ -99,27 +95,6 @@ describe('loadBlog', () => { truncated: false, }); - expect({ - ...blogPosts.find((v) => v.metadata.title === 'no date').metadata, - ...{prevItem: undefined}, - }).toEqual({ - editUrl: - 'https://github.com/facebook/docusaurus/edit/master/website-1x/blog/no date.md', - permalink: '/blog/no date', - readingTime: 0.01, - source: noDateSource, - title: 'no date', - description: `no date`, - date: noDateSourceBirthTime, - tags: [], - prevItem: undefined, - nextItem: { - permalink: '/blog/hey/my super path/héllô', - title: 'Complex Slug', - }, - truncated: false, - }); - expect({ ...blogPosts.find((v) => v.metadata.title === 'Complex Slug').metadata, ...{prevItem: undefined}, @@ -157,7 +132,7 @@ describe('loadBlog', () => { permalink: '/blog/draft', title: 'draft', }, - date: new Date('2020-08-16'), + date: new Date('2020-08-15'), tags: [], truncated: false, }); @@ -165,8 +140,40 @@ describe('loadBlog', () => { test('draft blog post not exists in production build', async () => { process.env.NODE_ENV = 'production'; - const blogPosts = await getBlogPosts(); + const siteDir = path.join(__dirname, '__fixtures__', 'website'); + const blogPosts = await getBlogPosts(siteDir); expect(blogPosts.find((v) => v.metadata.title === 'draft')).toBeUndefined(); }); + + test('create blog post without date', async () => { + const siteDir = path.join( + __dirname, + '__fixtures__', + 'website-blog-without-date', + ); + const blogPosts = await getBlogPosts(siteDir); + const noDateSource = path.join('@site', pluginPath, 'no date.md'); + const noDateSourceBirthTime = ( + await fs.stat(noDateSource.replace('@site', siteDir)) + ).birthtime; + + expect({ + ...blogPosts.find((v) => v.metadata.title === 'no date').metadata, + ...{prevItem: undefined}, + }).toEqual({ + editUrl: + 'https://github.com/facebook/docusaurus/edit/master/website-1x/blog/no date.md', + permalink: '/blog/no date', + readingTime: 0.01, + source: noDateSource, + title: 'no date', + description: `no date`, + date: noDateSourceBirthTime, + tags: [], + prevItem: undefined, + nextItem: undefined, + truncated: false, + }); + }); });