mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-03 08:49:51 +02:00
Extract blog post truncation logic and add tests (#640)
This commit is contained in:
parent
5b3f54741e
commit
8d676e6a5a
13 changed files with 192 additions and 30 deletions
15
lib/core/__tests__/__fixtures__/blog-post-with-truncate.md
Normal file
15
lib/core/__tests__/__fixtures__/blog-post-with-truncate.md
Normal file
|
@ -0,0 +1,15 @@
|
|||
---
|
||||
title: Truncation Example
|
||||
---
|
||||
|
||||
All this will be part of the blog post summary.
|
||||
|
||||
Even this.
|
||||
|
||||
<!--truncate-->
|
||||
|
||||
But anything from here on down will not be.
|
||||
|
||||
Not this.
|
||||
|
||||
Or this.
|
|
@ -0,0 +1,13 @@
|
|||
---
|
||||
title: Non-truncation Example
|
||||
---
|
||||
|
||||
All this will be part of the blog post summary.
|
||||
|
||||
Even this.
|
||||
|
||||
And anything from here on down will still be.
|
||||
|
||||
And this.
|
||||
|
||||
And this too.
|
66
lib/core/__tests__/__snapshots__/utils.test.js.snap
Normal file
66
lib/core/__tests__/__snapshots__/utils.test.js.snap
Normal file
|
@ -0,0 +1,66 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`utils extractBlogPostBeforeTruncate 1`] = `
|
||||
"---
|
||||
title: Truncation Example
|
||||
---
|
||||
|
||||
All this will be part of the blog post summary.
|
||||
|
||||
Even this.
|
||||
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`utils extractBlogPostBeforeTruncate 2`] = `
|
||||
"---
|
||||
title: Non-truncation Example
|
||||
---
|
||||
|
||||
All this will be part of the blog post summary.
|
||||
|
||||
Even this.
|
||||
|
||||
And anything from here on down will still be.
|
||||
|
||||
And this.
|
||||
|
||||
And this too.
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`utils extractBlogPostSummary 1`] = `
|
||||
"---
|
||||
title: Truncation Example
|
||||
---
|
||||
|
||||
All this will be part of the blog post summary.
|
||||
|
||||
Even this.
|
||||
|
||||
<!--truncate-->
|
||||
|
||||
But anything from here on down will not be.
|
||||
|
||||
Not this.
|
||||
|
||||
Or this.
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`utils extractBlogPostSummary 2`] = `
|
||||
"---
|
||||
title: Non-truncation Example
|
||||
---
|
||||
|
||||
All this will be part of the blog post summary.
|
||||
|
||||
Even this.
|
||||
|
||||
And anything from here on down will still be.
|
||||
|
||||
And this.
|
||||
|
||||
And this too.
|
||||
"
|
||||
`;
|
49
lib/core/__tests__/utils.test.js
Normal file
49
lib/core/__tests__/utils.test.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
const utils = require('../utils');
|
||||
const readFileSync = require('fs').readFileSync;
|
||||
|
||||
const blogPostWithTruncateContents = readFileSync(
|
||||
path.join(__dirname, '__fixtures__', 'blog-post-with-truncate.md'),
|
||||
'utf-8'
|
||||
);
|
||||
|
||||
const blogPostWithoutTruncateContents = readFileSync(
|
||||
path.join(__dirname, '__fixtures__', 'blog-post-without-truncate.md'),
|
||||
'utf-8'
|
||||
);
|
||||
|
||||
describe('utils', () => {
|
||||
test('blogPostHasTruncateMarker', () => {
|
||||
expect(utils.blogPostHasTruncateMarker(blogPostWithTruncateContents)).toBe(
|
||||
true
|
||||
);
|
||||
expect(
|
||||
utils.blogPostHasTruncateMarker(blogPostWithoutTruncateContents)
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test('extractBlogPostBeforeTruncate', () => {
|
||||
expect(
|
||||
utils.extractBlogPostBeforeTruncate(blogPostWithTruncateContents)
|
||||
).toMatchSnapshot();
|
||||
expect(
|
||||
utils.extractBlogPostBeforeTruncate(blogPostWithoutTruncateContents)
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('extractBlogPostSummary', () => {
|
||||
expect(
|
||||
utils.extractBlogPostSummary(blogPostWithTruncateContents)
|
||||
).toMatchSnapshot();
|
||||
expect(
|
||||
utils.extractBlogPostSummary(blogPostWithoutTruncateContents)
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue