From 8321df56547618586de9a985cf9a9866aaf4ad3c Mon Sep 17 00:00:00 2001 From: sebastien Date: Fri, 4 Jul 2025 13:37:58 +0200 Subject: [PATCH] add unit tests for blog translate wiring --- .../src/__tests__/index.test.ts | 120 +++++++++++++++--- 1 file changed, 103 insertions(+), 17 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts index 7565c603c4..f465459d9c 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts @@ -6,7 +6,7 @@ */ import {jest} from '@jest/globals'; -import path from 'path'; +import * as path from 'path'; import {normalizePluginOptions} from '@docusaurus/utils-validation'; import { posixPath, @@ -23,6 +23,7 @@ import type { I18n, Validate, MarkdownConfig, + I18nLocaleConfig, } from '@docusaurus/types'; import type { BlogPost, @@ -68,7 +69,10 @@ Available blog post titles are:\n- ${blogPosts return post; } -function getI18n(locale: string): I18n { +function getI18n( + locale: string, + localeConfigOptions?: Partial, +): I18n { return { currentLocale: locale, locales: [locale], @@ -81,6 +85,8 @@ function getI18n(locale: string): I18n { htmlLang: locale, direction: 'ltr', path: locale, + translate: true, + ...localeConfigOptions, }, }, }; @@ -95,8 +101,9 @@ const BaseEditUrl = 'https://baseEditUrl.com/edit'; const getPlugin = async ( siteDir: string, pluginOptions: Partial = {}, - i18n: I18n = DefaultI18N, + i18nOptions: Partial = {}, ) => { + const i18n = {...DefaultI18N, ...i18nOptions}; const generatedFilesDir: string = path.resolve(siteDir, '.docusaurus'); const localizationDir = path.join( siteDir, @@ -154,20 +161,34 @@ const getBlogTags = async ( }; describe('blog plugin', () => { - it('getPathsToWatch returns right files', async () => { - const siteDir = path.join(__dirname, '__fixtures__', 'website'); - const plugin = await getPlugin(siteDir); - const pathsToWatch = plugin.getPathsToWatch!(); - const relativePathsToWatch = pathsToWatch.map((p) => - posixPath(path.relative(siteDir, p)), - ); - expect(relativePathsToWatch).toEqual([ - 'i18n/en/docusaurus-plugin-content-blog/authors.yml', - 'i18n/en/docusaurus-plugin-content-blog/tags.yml', - 'blog/tags.yml', - 'i18n/en/docusaurus-plugin-content-blog/**/*.{md,mdx}', - 'blog/**/*.{md,mdx}', - ]); + describe('getPathsToWatch', () => { + async function runTest({translate}: {translate: boolean}) { + const siteDir = path.join(__dirname, '__fixtures__', 'website'); + const plugin = await getPlugin(siteDir, {}, getI18n('en', {translate})); + const pathsToWatch = plugin.getPathsToWatch!(); + return pathsToWatch.map((p) => posixPath(path.relative(siteDir, p))); + } + + it('getPathsToWatch returns right files', async () => { + const relativePathsToWatch = await runTest({translate: true}); + expect(relativePathsToWatch).toEqual([ + 'i18n/en/docusaurus-plugin-content-blog/authors.yml', + 'i18n/en/docusaurus-plugin-content-blog/tags.yml', + // 'blog/authors.yml', // TODO weird that it's not here but tags is? + 'blog/tags.yml', + 'i18n/en/docusaurus-plugin-content-blog/**/*.{md,mdx}', + 'blog/**/*.{md,mdx}', + ]); + }); + + it('getPathsToWatch returns right files (translate: false)', async () => { + const relativePathsToWatch = await runTest({translate: false}); + expect(relativePathsToWatch).toEqual([ + 'blog/authors.yml', + 'blog/tags.yml', + 'blog/**/*.{md,mdx}', + ]); + }); }); it('builds a simple website', async () => { @@ -378,6 +399,54 @@ describe('blog plugin', () => { }); }); + describe('i18n config translate is wired properly', () => { + async function runTest({translate}: {translate: boolean}) { + const siteDir = path.join(__dirname, '__fixtures__', 'website'); + const blogPosts = await getBlogPosts( + siteDir, + {}, + getI18n('en', {translate}), + ); + + // Simpler to snapshot + return blogPosts.map((post) => post.metadata.title); + } + + it('works with translate: false', async () => { + await expect(runTest({translate: false})).resolves.toMatchInlineSnapshot(` + [ + "test links", + "MDX Blog Sample with require calls", + "Full Blog Sample", + "Complex Slug", + "Simple Slug", + "draft", + "unlisted", + "some heading", + "date-matter", + "Happy 1st Birthday Slash!", + ] + `); + }); + + it('works with translate: true', async () => { + await expect(runTest({translate: true})).resolves.toMatchInlineSnapshot(` + [ + "test links", + "MDX Blog Sample with require calls", + "Full Blog Sample", + "Complex Slug", + "Simple Slug", + "draft", + "unlisted", + "some heading", + "date-matter", + "Happy 1st Birthday Slash! (translated)", + ] + `); + }); + }); + it('handles edit URL with editLocalizedBlogs: true', async () => { const siteDir = path.join(__dirname, '__fixtures__', 'website'); const blogPosts = await getBlogPosts(siteDir, {editLocalizedFiles: true}); @@ -391,6 +460,23 @@ describe('blog plugin', () => { ); }); + it('handles edit URL with editLocalizedBlogs: true and translate: false', async () => { + const siteDir = path.join(__dirname, '__fixtures__', 'website'); + const blogPosts = await getBlogPosts( + siteDir, + {editLocalizedFiles: true}, + getI18n('en', {translate: false}), + ); + + const localizedBlogPost = blogPosts.find( + (v) => v.metadata.title === 'Happy 1st Birthday Slash!', + )!; + + expect(localizedBlogPost.metadata.editUrl).toBe( + `${BaseEditUrl}/blog/2018-12-14-Happy-First-Birthday-Slash.md`, + ); + }); + it('handles edit URL with editUrl function', async () => { const siteDir = path.join(__dirname, '__fixtures__', 'website');