Extract blog post truncation logic and add tests (#640)

This commit is contained in:
Yangshun Tay 2018-05-06 09:15:18 -07:00 committed by GitHub
parent 5b3f54741e
commit 8d676e6a5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 192 additions and 30 deletions

View 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.

View file

@ -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.

View 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.
"
`;

View 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();
});
});