docusaurus/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts

133 lines
3.9 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {truncate, parseBlogFileName} from '../blogUtils';
describe('truncate', () => {
test('truncates texts', () => {
expect(
truncate('aaa\n<!-- truncate -->\nbbb\nccc', /<!-- truncate -->/),
).toEqual('aaa\n');
expect(
truncate('\n<!-- truncate -->\nbbb\nccc', /<!-- truncate -->/),
).toEqual('\n');
});
test('leaves texts without markers', () => {
expect(truncate('aaa\nbbb\nccc', /<!-- truncate -->/)).toEqual(
'aaa\nbbb\nccc',
);
expect(truncate('', /<!-- truncate -->/)).toEqual('');
});
});
describe('parseBlogFileName', () => {
test('parse file', () => {
expect(parseBlogFileName('some-post.md')).toEqual({
date: undefined,
text: 'some-post',
slug: '/some-post',
});
});
test('parse folder', () => {
expect(parseBlogFileName('some-post/index.md')).toEqual({
date: undefined,
text: 'some-post',
slug: '/some-post',
});
});
test('parse nested file', () => {
expect(parseBlogFileName('some-post/some-file.md')).toEqual({
date: undefined,
text: 'some-post/some-file',
slug: '/some-post/some-file',
});
});
test('parse nested folder', () => {
expect(parseBlogFileName('some-post/some-subfolder/index.md')).toEqual({
date: undefined,
text: 'some-post/some-subfolder',
slug: '/some-post/some-subfolder',
});
});
test('parse file respecting date convention', () => {
expect(
parseBlogFileName('2021-05-12-announcing-docusaurus-two-beta.md'),
).toEqual({
date: new Date('2021-05-12Z'),
text: 'announcing-docusaurus-two-beta',
slug: '/2021/05/12/announcing-docusaurus-two-beta',
});
});
test('parse folder name respecting date convention', () => {
expect(
parseBlogFileName('2021-05-12-announcing-docusaurus-two-beta/index.md'),
).toEqual({
date: new Date('2021-05-12Z'),
text: 'announcing-docusaurus-two-beta',
slug: '/2021/05/12/announcing-docusaurus-two-beta',
});
});
test('parse folder tree respecting date convention', () => {
expect(
parseBlogFileName('2021/05/12/announcing-docusaurus-two-beta/index.md'),
).toEqual({
date: new Date('2021-05-12Z'),
text: 'announcing-docusaurus-two-beta',
slug: '/2021/05/12/announcing-docusaurus-two-beta',
});
});
test('parse folder name/tree (mixed) respecting date convention', () => {
expect(
parseBlogFileName('2021/05-12-announcing-docusaurus-two-beta/index.md'),
).toEqual({
date: new Date('2021-05-12Z'),
text: 'announcing-docusaurus-two-beta',
slug: '/2021/05/12/announcing-docusaurus-two-beta',
});
});
test('parse nested folder tree respecting date convention', () => {
expect(
parseBlogFileName(
'2021/05/12/announcing-docusaurus-two-beta/subfolder/subfile.md',
),
).toEqual({
date: new Date('2021-05-12Z'),
text: 'announcing-docusaurus-two-beta/subfolder/subfile',
slug: '/2021/05/12/announcing-docusaurus-two-beta/subfolder/subfile',
});
});
test('parse date in the middle of path', () => {
expect(
parseBlogFileName('team-a/2021/05/12/announcing-docusaurus-two-beta.md'),
).toEqual({
date: new Date('2021-05-12Z'),
text: 'announcing-docusaurus-two-beta',
slug: '/2021/05/12/team-a/announcing-docusaurus-two-beta',
});
});
test('parse date in the middle of a folder name', () => {
expect(
parseBlogFileName(
'team-a-2021-05-12-hey/announcing-docusaurus-two-beta.md',
),
).toEqual({
date: new Date('2021-05-12Z'),
text: 'hey/announcing-docusaurus-two-beta',
slug: '/2021/05/12/team-a-hey/announcing-docusaurus-two-beta',
});
});
});