Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(v2): make blog posts unit test ordering determinate #3339

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
slug: /simple/slug
title: Simple Slug
date: 2020-08-16
date: 2020-08-15
---

simple url slug
67 changes: 37 additions & 30 deletions packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -157,16 +132,48 @@ describe('loadBlog', () => {
permalink: '/blog/draft',
title: 'draft',
},
date: new Date('2020-08-16'),
date: new Date('2020-08-15'),
tags: [],
truncated: false,
});
});

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,
});
});
});