add tests for plugins

This commit is contained in:
sebastien 2025-07-04 17:11:09 +02:00
parent 4fe8019cec
commit 1989342792

View file

@ -11,12 +11,16 @@ import {loadPlugins, reloadPlugin} from '../plugins';
import {DEFAULT_FUTURE_CONFIG} from '../../configValidation'; import {DEFAULT_FUTURE_CONFIG} from '../../configValidation';
import type {LoadContext, Plugin, PluginConfig} from '@docusaurus/types'; import type {LoadContext, Plugin, PluginConfig} from '@docusaurus/types';
type TestOptions = {translate?: boolean};
async function testLoad({ async function testLoad({
plugins, plugins,
themes, themes,
options = {},
}: { }: {
plugins: PluginConfig<any>[]; plugins: PluginConfig<any>[];
themes: PluginConfig<any>[]; themes: PluginConfig<any>[];
options?: TestOptions;
}) { }) {
const siteDir = path.join(__dirname, '__fixtures__/site-with-plugin'); const siteDir = path.join(__dirname, '__fixtures__/site-with-plugin');
@ -25,6 +29,13 @@ async function testLoad({
siteConfigPath: path.join(siteDir, 'docusaurus.config.js'), siteConfigPath: path.join(siteDir, 'docusaurus.config.js'),
generatedFilesDir: path.join(siteDir, '.docusaurus'), generatedFilesDir: path.join(siteDir, '.docusaurus'),
outDir: path.join(siteDir, 'build'), outDir: path.join(siteDir, 'build'),
i18n: {
path: 'i18n',
locales: ['en'],
currentLocale: 'en',
defaultLocale: 'en',
localeConfigs: {en: {translate: options.translate ?? true}},
},
siteConfig: { siteConfig: {
baseUrl: '/', baseUrl: '/',
trailingSlash: true, trailingSlash: true,
@ -49,10 +60,12 @@ const SyntheticPluginNames = [
async function testPlugin<Content = unknown>( async function testPlugin<Content = unknown>(
pluginConfig: PluginConfig<Content>, pluginConfig: PluginConfig<Content>,
options?: TestOptions,
) { ) {
const {context, plugins, routes, globalData} = await testLoad({ const {context, plugins, routes, globalData} = await testLoad({
plugins: [pluginConfig], plugins: [pluginConfig],
themes: [], themes: [],
options,
}); });
const nonSyntheticPlugins = plugins.filter( const nonSyntheticPlugins = plugins.filter(
@ -86,65 +99,120 @@ describe('loadPlugins', () => {
expect(globalData).toEqual({}); expect(globalData).toEqual({});
}); });
it('typical plugin', async () => { describe('typical plugin', () => {
const {plugin, routes, globalData} = await testPlugin(() => ({ function typicalPlugin(options: TestOptions) {
name: 'plugin-name', return testPlugin(
loadContent: () => ({name: 'Toto', age: 42}), () => ({
translateContent: ({content}) => ({ name: 'plugin-name',
...content, loadContent: () => ({name: 'Toto', age: 42}),
name: `${content.name} (translated)`, translateContent: ({content}) => ({
}), ...content,
contentLoaded({content, actions}) { name: `${content.name} (translated)`,
actions.addRoute({ }),
path: '/foo', contentLoaded({content, actions}) {
component: 'Comp', actions.addRoute({
modules: {someModule: 'someModulePath'}, path: '/foo',
context: {someContext: 'someContextPath'}, component: 'Comp',
}); modules: {someModule: 'someModulePath'},
actions.setGlobalData({ context: {someContext: 'someContextPath'},
globalName: content.name, });
globalAge: content.age, actions.setGlobalData({
}); globalName: content.name,
}, globalAge: content.age,
})); });
},
}),
options,
);
}
expect(plugin.content).toMatchInlineSnapshot(` it('translated: true', async () => {
{ const {plugin, routes, globalData} = await typicalPlugin({
"age": 42, translate: true,
"name": "Toto (translated)", });
}
`); expect(plugin.content).toMatchInlineSnapshot(`
expect(routes).toMatchInlineSnapshot(` {
[ "age": 42,
"name": "Toto (translated)",
}
`);
expect(routes).toMatchInlineSnapshot(`
[
{
"component": "Comp",
"context": {
"data": {
"someContext": "someContextPath",
},
"plugin": "@generated/plugin-name/default/__plugin.json",
},
"modules": {
"someModule": "someModulePath",
},
"path": "/foo/",
"plugin": {
"id": "default",
"name": "plugin-name",
},
},
]
`);
expect(globalData).toMatchInlineSnapshot(`
{
"plugin-name": {
"default": {
"globalAge": 42,
"globalName": "Toto (translated)",
},
},
}
`);
});
it('translated: false', async () => {
const {plugin, routes, globalData} = await typicalPlugin({
translate: false,
});
expect(plugin.content).toMatchInlineSnapshot(`
{ {
"component": "Comp", "age": 42,
"context": { "name": "Toto",
"data": { }
"someContext": "someContextPath", `);
expect(routes).toMatchInlineSnapshot(`
[
{
"component": "Comp",
"context": {
"data": {
"someContext": "someContextPath",
},
"plugin": "@generated/plugin-name/default/__plugin.json",
},
"modules": {
"someModule": "someModulePath",
},
"path": "/foo/",
"plugin": {
"id": "default",
"name": "plugin-name",
},
},
]
`);
expect(globalData).toMatchInlineSnapshot(`
{
"plugin-name": {
"default": {
"globalAge": 42,
"globalName": "Toto",
}, },
"plugin": "@generated/plugin-name/default/__plugin.json",
}, },
"modules": { }
"someModule": "someModulePath", `);
}, });
"path": "/foo/",
"plugin": {
"id": "default",
"name": "plugin-name",
},
},
]
`);
expect(globalData).toMatchInlineSnapshot(`
{
"plugin-name": {
"default": {
"globalAge": 42,
"globalName": "Toto (translated)",
},
},
}
`);
}); });
it('plugin with options', async () => { it('plugin with options', async () => {