Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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,5 +1,244 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`blog plugin process blog posts load content 1`] = `
{
"/blog/tags/tag-1": {
"items": [
"/simple/slug/another",
"/another/tags",
"/another/tags2",
],
"label": "tag1",
"pages": [
{
"items": [
"/simple/slug/another",
],
"metadata": {
"blogDescription": "Blog",
"blogTitle": "Blog",
"nextPage": "/blog/tags/tag-1/page/2",
"page": 1,
"permalink": "/blog/tags/tag-1",
"postsPerPage": 1,
"previousPage": undefined,
"totalCount": 3,
"totalPages": 3,
},
},
{
"items": [
"/another/tags",
],
"metadata": {
"blogDescription": "Blog",
"blogTitle": "Blog",
"nextPage": "/blog/tags/tag-1/page/3",
"page": 2,
"permalink": "/blog/tags/tag-1/page/2",
"postsPerPage": 1,
"previousPage": "/blog/tags/tag-1",
"totalCount": 3,
"totalPages": 3,
},
},
{
"items": [
"/another/tags2",
],
"metadata": {
"blogDescription": "Blog",
"blogTitle": "Blog",
"nextPage": undefined,
"page": 3,
"permalink": "/blog/tags/tag-1/page/3",
"postsPerPage": 1,
"previousPage": "/blog/tags/tag-1/page/2",
"totalCount": 3,
"totalPages": 3,
},
},
],
"permalink": "/blog/tags/tag-1",
"unlisted": false,
},
"/blog/tags/tag-2": {
"items": [
"/another/tags",
"/another/tags2",
],
"label": "tag2",
"pages": [
{
"items": [
"/another/tags",
],
"metadata": {
"blogDescription": "Blog",
"blogTitle": "Blog",
"nextPage": "/blog/tags/tag-2/page/2",
"page": 1,
"permalink": "/blog/tags/tag-2",
"postsPerPage": 1,
"previousPage": undefined,
"totalCount": 2,
"totalPages": 2,
},
},
{
"items": [
"/another/tags2",
],
"metadata": {
"blogDescription": "Blog",
"blogTitle": "Blog",
"nextPage": undefined,
"page": 2,
"permalink": "/blog/tags/tag-2/page/2",
"postsPerPage": 1,
"previousPage": "/blog/tags/tag-2",
"totalCount": 2,
"totalPages": 2,
},
},
],
"permalink": "/blog/tags/tag-2",
"unlisted": false,
},
}
`;

exports[`blog plugin process blog posts load content 2`] = `
[
{
"content": "simple url slug",
"id": "/simple/slug/another",
"metadata": {
"authors": [
{
"imageURL": undefined,
"name": "Sébastien Lorber",
"title": "Docusaurus maintainer",
"url": "https://sebastienlorber.com",
},
],
"date": 2020-08-15T00:00:00.000Z,
"description": "simple url slug",
"editUrl": "https://baseEditUrl.com/edit/blog/another-simple-slug-with-tags.md",
"frontMatter": {
"author": "Sébastien Lorber",
"author_title": "Docusaurus maintainer",
"author_url": "https://sebastienlorber.com",
"date": 2020-08-15T00:00:00.000Z,
"slug": "/simple/slug/another",
"tags": [
"tag1",
],
"title": "Another Simple Slug",
},
"hasTruncateMarker": false,
"nextItem": {
"permalink": "/blog/another/tags",
"title": "Another With Tag",
},
"permalink": "/blog/simple/slug/another",
"readingTime": 0.015,
"source": "@site/blog/another-simple-slug-with-tags.md",
"tags": [
{
"label": "tag1",
"permalink": "/blog/tags/tag-1",
},
],
"title": "Another Simple Slug",
"unlisted": false,
},
},
{
"content": "with tag",
"id": "/another/tags",
"metadata": {
"authors": [],
"date": 2020-08-15T00:00:00.000Z,
"description": "with tag",
"editUrl": "https://baseEditUrl.com/edit/blog/another-with-tags.md",
"frontMatter": {
"date": 2020-08-15T00:00:00.000Z,
"slug": "/another/tags",
"tags": [
"tag1",
"tag2",
],
"title": "Another With Tag",
},
"hasTruncateMarker": false,
"nextItem": {
"permalink": "/blog/another/tags2",
"title": "Another With Tag",
},
"permalink": "/blog/another/tags",
"prevItem": {
"permalink": "/blog/simple/slug/another",
"title": "Another Simple Slug",
},
"readingTime": 0.01,
"source": "@site/blog/another-with-tags.md",
"tags": [
{
"label": "tag1",
"permalink": "/blog/tags/tag-1",
},
{
"label": "tag2",
"permalink": "/blog/tags/tag-2",
},
],
"title": "Another With Tag",
"unlisted": false,
},
},
{
"content": "with tag",
"id": "/another/tags2",
"metadata": {
"authors": [],
"date": 2020-08-15T00:00:00.000Z,
"description": "with tag",
"editUrl": "https://baseEditUrl.com/edit/blog/another-with-tags2.md",
"frontMatter": {
"date": 2020-08-15T00:00:00.000Z,
"slug": "/another/tags2",
"tags": [
"tag1",
"tag2",
],
"title": "Another With Tag",
},
"hasTruncateMarker": false,
"permalink": "/blog/another/tags2",
"prevItem": {
"permalink": "/blog/another/tags",
"title": "Another With Tag",
},
"readingTime": 0.01,
"source": "@site/blog/another-with-tags2.md",
"tags": [
{
"label": "tag1",
"permalink": "/blog/tags/tag-1",
},
{
"label": "tag2",
"permalink": "/blog/tags/tag-2",
},
],
"title": "Another With Tag",
"unlisted": false,
},
},
]
`;

exports[`blog plugin works on blog tags without pagination 1`] = `
{
"/blog/tags/tag-1": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import {
linkify,
getSourceToPermalink,
paginateBlogPosts,
applyProcessBlogPosts,
type LinkifyParams,
} from '../blogUtils';
import type {BlogBrokenMarkdownLink, BlogContentPaths} from '../types';
import type {BlogPost} from '@docusaurus/plugin-content-blog';
import type {DeepPartial} from 'utility-types';

describe('truncate', () => {
it('truncates texts', () => {
Expand Down Expand Up @@ -237,6 +239,7 @@ describe('linkify', () => {
frontMatter: {},
authors: [],
formattedDate: '',
unlisted: false,
},
content: '',
},
Expand Down Expand Up @@ -295,3 +298,99 @@ describe('linkify', () => {
} as BlogBrokenMarkdownLink);
});
});

const createBlogPost = (args: DeepPartial<BlogPost>): BlogPost => ({
id: '',
metadata: {
source: '',
title: '',
formattedDate: '',
permalink: '',
description: '',
hasTruncateMarker: false,
authors: [],
frontMatter: {},
tags: [],
unlisted: false,
...args.metadata,
} as BlogPost['metadata'],
content: args.content || '',
});

describe('processBlogPosts', () => {
const blogPost2022 = createBlogPost({
metadata: {date: new Date('2022-01-01')},
});
const blogPost2023 = createBlogPost({
metadata: {date: new Date('2023-01-01')},
});
const blogPost2024 = createBlogPost({
metadata: {date: new Date('2024-01-01')},
});

it('filter blogs only from 2024', async () => {
const processedBlogPosts = await applyProcessBlogPosts({
blogPosts: [blogPost2022, blogPost2023, blogPost2024],
processBlogPosts: async ({blogPosts}: {blogPosts: BlogPost[]}) =>
blogPosts.filter(
(blogPost) => blogPost.metadata.date.getFullYear() === 2024,
),
});

expect(processedBlogPosts).toEqual([blogPost2024]);
});

it('sort blogs by date in ascending order', async () => {
const processedBlogPosts = await applyProcessBlogPosts({
blogPosts: [blogPost2023, blogPost2022, blogPost2024],
processBlogPosts: async ({blogPosts}: {blogPosts: BlogPost[]}) =>
blogPosts.sort(
(a, b) => a.metadata.date.getTime() - b.metadata.date.getTime(),
),
});

expect(processedBlogPosts).toEqual([
blogPost2022,
blogPost2023,
blogPost2024,
]);
});

it('sort blogs by date in descending order', async () => {
const processedBlogPosts = await applyProcessBlogPosts({
blogPosts: [blogPost2023, blogPost2022, blogPost2024],
processBlogPosts: async ({blogPosts}: {blogPosts: BlogPost[]}) =>
blogPosts.sort(
(a, b) => b.metadata.date.getTime() - a.metadata.date.getTime(),
),
});

expect(processedBlogPosts).toEqual([
blogPost2024,
blogPost2023,
blogPost2022,
]);
});

it('processBlogPosts return 2022 only', async () => {
const processedBlogPosts = await applyProcessBlogPosts({
blogPosts: [blogPost2023, blogPost2022, blogPost2024],
processBlogPosts: async () => [blogPost2022],
});

expect(processedBlogPosts).toEqual([blogPost2022]);
});

it('processBlogPosts return undefined', async () => {
const processedBlogPosts = await applyProcessBlogPosts({
blogPosts: [blogPost2023, blogPost2022, blogPost2024],
processBlogPosts: async () => {},
});

expect(processedBlogPosts).toEqual([
blogPost2023,
blogPost2022,
blogPost2024,
]);
});
});
Loading