mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-29 10:17:55 +02:00
test: fix some type errors in test files (#7486)
This commit is contained in:
parent
624735bd92
commit
e2e40b8f5f
50 changed files with 319 additions and 182 deletions
17
jest/deps.d.ts
vendored
Normal file
17
jest/deps.d.ts
vendored
Normal file
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
// modules only used in tests
|
||||
|
||||
declare module 'to-vfile';
|
||||
|
||||
declare module 'remark-mdx';
|
||||
|
||||
declare module '@testing-utils/git' {
|
||||
const createTempRepo: typeof import('./utils/git').createTempRepo;
|
||||
export {createTempRepo};
|
||||
}
|
|
@ -21,7 +21,7 @@ function process(doc: string, plugins: Plugin[] = []) {
|
|||
return removePosition(processor.runSync(processor.parse(doc)), true);
|
||||
}
|
||||
|
||||
function heading(label: string, id: string) {
|
||||
function heading(label: string | null, id: string) {
|
||||
return u(
|
||||
'heading',
|
||||
{depth: 2, data: {id, hProperties: {id}}},
|
||||
|
|
|
@ -13,15 +13,15 @@ import plugin from '..';
|
|||
import transformImage, {type PluginOptions} from '../../transformImage';
|
||||
|
||||
const processFixture = async (name: string, options?: PluginOptions) => {
|
||||
const filePath = path.join(__dirname, `__fixtures__/${name}.md`);
|
||||
const siteDir = path.join(__dirname, `__fixtures__`);
|
||||
const staticDirs = [
|
||||
path.join(__dirname, '__fixtures__/static'),
|
||||
path.join(__dirname, '__fixtures__/static2'),
|
||||
path.join(siteDir, 'static'),
|
||||
path.join(siteDir, 'static2'),
|
||||
];
|
||||
const file = await vfile.read(filePath);
|
||||
const file = await vfile.read(path.join(siteDir, `${name}.md`));
|
||||
const result = await remark()
|
||||
.use(mdx)
|
||||
.use(transformImage, {...options, filePath, staticDirs})
|
||||
.use(transformImage, {...options, siteDir, staticDirs})
|
||||
.use(plugin, {
|
||||
...options,
|
||||
staticDirs,
|
||||
|
|
|
@ -9,7 +9,7 @@ import {normalizePluginOptions} from '@docusaurus/utils-validation';
|
|||
import {validateOptions, DEFAULT_OPTIONS} from '../options';
|
||||
import type {Options} from '../options';
|
||||
|
||||
function testValidate(options: Options) {
|
||||
function testValidate(options?: Options) {
|
||||
return validateOptions({validate: normalizePluginOptions, options});
|
||||
}
|
||||
|
||||
|
|
|
@ -176,11 +176,11 @@ describe('writeRedirectFiles', () => {
|
|||
await writeRedirectFiles(filesMetadata);
|
||||
|
||||
await expect(
|
||||
fs.readFile(filesMetadata[0].fileAbsolutePath, 'utf8'),
|
||||
fs.readFile(filesMetadata[0]!.fileAbsolutePath, 'utf8'),
|
||||
).resolves.toBe('content 1');
|
||||
|
||||
await expect(
|
||||
fs.readFile(filesMetadata[1].fileAbsolutePath, 'utf8'),
|
||||
fs.readFile(filesMetadata[1]!.fileAbsolutePath, 'utf8'),
|
||||
).resolves.toBe('content 2');
|
||||
});
|
||||
|
||||
|
@ -195,7 +195,7 @@ describe('writeRedirectFiles', () => {
|
|||
];
|
||||
|
||||
await fs.outputFile(
|
||||
filesMetadata[0].fileAbsolutePath,
|
||||
filesMetadata[0]!.fileAbsolutePath,
|
||||
'file already exists!',
|
||||
);
|
||||
|
||||
|
|
|
@ -66,6 +66,6 @@ const UserOptionsSchema = Joi.object<PluginOptions>({
|
|||
export function validateOptions({
|
||||
validate,
|
||||
options: userOptions,
|
||||
}: OptionValidationContext<Options, PluginOptions>): PluginOptions {
|
||||
}: OptionValidationContext<Options | undefined, PluginOptions>): PluginOptions {
|
||||
return validate(UserOptionsSchema, userOptions);
|
||||
}
|
||||
|
|
|
@ -44,11 +44,14 @@ exports[`translateContent falls back when translation is incomplete 1`] = `
|
|||
],
|
||||
"blogPosts": [
|
||||
{
|
||||
"content": "",
|
||||
"id": "hello",
|
||||
"metadata": {
|
||||
"authors": [],
|
||||
"date": 2021-07-19T00:00:00.000Z,
|
||||
"description": "/blog/2021/06/19/hello",
|
||||
"formattedDate": "June 19, 2021",
|
||||
"frontMatter": {},
|
||||
"permalink": "/blog/2021/06/19/hello",
|
||||
"source": "/blog/2021/06/19/hello",
|
||||
"tags": [],
|
||||
|
@ -85,11 +88,14 @@ exports[`translateContent returns translated loaded 1`] = `
|
|||
],
|
||||
"blogPosts": [
|
||||
{
|
||||
"content": "",
|
||||
"id": "hello",
|
||||
"metadata": {
|
||||
"authors": [],
|
||||
"date": 2021-07-19T00:00:00.000Z,
|
||||
"description": "/blog/2021/06/19/hello",
|
||||
"formattedDate": "June 19, 2021",
|
||||
"frontMatter": {},
|
||||
"permalink": "/blog/2021/06/19/hello",
|
||||
"source": "/blog/2021/06/19/hello",
|
||||
"tags": [],
|
||||
|
|
|
@ -211,7 +211,11 @@ describe('linkify', () => {
|
|||
title: 'date-matter',
|
||||
},
|
||||
truncated: false,
|
||||
frontMatter: {},
|
||||
authors: [],
|
||||
formattedDate: '',
|
||||
},
|
||||
content: '',
|
||||
},
|
||||
];
|
||||
|
||||
|
|
|
@ -21,6 +21,9 @@ const DefaultI18N: I18n = {
|
|||
defaultLocale: 'en',
|
||||
localeConfigs: {
|
||||
en: {
|
||||
label: 'English',
|
||||
direction: 'ltr',
|
||||
htmlLang: 'en',
|
||||
calendar: 'gregory',
|
||||
},
|
||||
},
|
||||
|
|
|
@ -56,7 +56,9 @@ function testField(params: {
|
|||
);
|
||||
} catch (err) {
|
||||
// eslint-disable-next-line jest/no-conditional-expect
|
||||
expect(err.message).toMatch(new RegExp(escapeStringRegexp(message)));
|
||||
expect((err as Error).message).toMatch(
|
||||
new RegExp(escapeStringRegexp(message)),
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -41,7 +41,14 @@ function getI18n(locale: string): I18n {
|
|||
currentLocale: locale,
|
||||
locales: [locale],
|
||||
defaultLocale: locale,
|
||||
localeConfigs: {[locale]: {calendar: 'gregory'}},
|
||||
localeConfigs: {
|
||||
[locale]: {
|
||||
calendar: 'gregory',
|
||||
label: locale,
|
||||
htmlLang: locale,
|
||||
direction: 'ltr',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -297,28 +304,28 @@ describe('blog plugin', () => {
|
|||
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||
const blogPostsFrench = await getBlogPosts(siteDir, {}, getI18n('fr'));
|
||||
expect(blogPostsFrench).toHaveLength(8);
|
||||
expect(blogPostsFrench[0].metadata.formattedDate).toMatchInlineSnapshot(
|
||||
expect(blogPostsFrench[0]!.metadata.formattedDate).toMatchInlineSnapshot(
|
||||
`"6 mars 2021"`,
|
||||
);
|
||||
expect(blogPostsFrench[1].metadata.formattedDate).toMatchInlineSnapshot(
|
||||
expect(blogPostsFrench[1]!.metadata.formattedDate).toMatchInlineSnapshot(
|
||||
`"5 mars 2021"`,
|
||||
);
|
||||
expect(blogPostsFrench[2].metadata.formattedDate).toMatchInlineSnapshot(
|
||||
expect(blogPostsFrench[2]!.metadata.formattedDate).toMatchInlineSnapshot(
|
||||
`"16 août 2020"`,
|
||||
);
|
||||
expect(blogPostsFrench[3].metadata.formattedDate).toMatchInlineSnapshot(
|
||||
expect(blogPostsFrench[3]!.metadata.formattedDate).toMatchInlineSnapshot(
|
||||
`"15 août 2020"`,
|
||||
);
|
||||
expect(blogPostsFrench[4].metadata.formattedDate).toMatchInlineSnapshot(
|
||||
expect(blogPostsFrench[4]!.metadata.formattedDate).toMatchInlineSnapshot(
|
||||
`"27 février 2020"`,
|
||||
);
|
||||
expect(blogPostsFrench[5].metadata.formattedDate).toMatchInlineSnapshot(
|
||||
expect(blogPostsFrench[5]!.metadata.formattedDate).toMatchInlineSnapshot(
|
||||
`"2 janvier 2019"`,
|
||||
);
|
||||
expect(blogPostsFrench[6].metadata.formattedDate).toMatchInlineSnapshot(
|
||||
expect(blogPostsFrench[6]!.metadata.formattedDate).toMatchInlineSnapshot(
|
||||
`"1 janvier 2019"`,
|
||||
);
|
||||
expect(blogPostsFrench[7].metadata.formattedDate).toMatchInlineSnapshot(
|
||||
expect(blogPostsFrench[7]!.metadata.formattedDate).toMatchInlineSnapshot(
|
||||
`"14 décembre 2018"`,
|
||||
);
|
||||
});
|
||||
|
|
|
@ -9,7 +9,7 @@ import {normalizePluginOptions} from '@docusaurus/utils-validation';
|
|||
import {validateOptions, DEFAULT_OPTIONS} from '../options';
|
||||
import type {Options} from '@docusaurus/plugin-content-blog';
|
||||
|
||||
function testValidate(options: Options) {
|
||||
function testValidate(options?: Options) {
|
||||
return validateOptions({validate: normalizePluginOptions, options});
|
||||
}
|
||||
|
||||
|
@ -44,13 +44,14 @@ describe('validateOptions', () => {
|
|||
});
|
||||
|
||||
it('accepts valid user options', () => {
|
||||
const userOptions = {
|
||||
const userOptions: Options = {
|
||||
...defaultOptions,
|
||||
routeBasePath: 'myBlog',
|
||||
beforeDefaultRemarkPlugins: [],
|
||||
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
|
||||
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
|
||||
rehypePlugins: [
|
||||
// @ts-expect-error: it seems to work in practice
|
||||
markdownPluginsObjectStub,
|
||||
[markdownPluginsFunctionStub, {option1: '42'}],
|
||||
],
|
||||
|
@ -73,6 +74,7 @@ describe('validateOptions', () => {
|
|||
expect(() =>
|
||||
testValidate({
|
||||
feedOptions: {
|
||||
// @ts-expect-error: test
|
||||
type: 'none',
|
||||
},
|
||||
}),
|
||||
|
@ -138,6 +140,7 @@ describe('validateOptions', () => {
|
|||
|
||||
it('rejects "abcdef" sidebar count', () => {
|
||||
const userOptions = {blogSidebarCount: 'abcdef'};
|
||||
// @ts-expect-error: test
|
||||
expect(() => testValidate(userOptions)).toThrowErrorMatchingInlineSnapshot(
|
||||
`""blogSidebarCount" must be one of [ALL, number]"`,
|
||||
);
|
||||
|
@ -153,6 +156,7 @@ describe('validateOptions', () => {
|
|||
|
||||
it('rejects 42 sidebar title', () => {
|
||||
const userOptions = {blogSidebarTitle: 42};
|
||||
// @ts-expect-error: test
|
||||
expect(() => testValidate(userOptions)).toThrowErrorMatchingInlineSnapshot(
|
||||
`""blogSidebarTitle" must be a string"`,
|
||||
);
|
||||
|
|
|
@ -33,7 +33,10 @@ const sampleBlogPosts: BlogPost[] = [
|
|||
tags: [],
|
||||
title: 'Hello',
|
||||
truncated: true,
|
||||
authors: [],
|
||||
frontMatter: {},
|
||||
},
|
||||
content: '',
|
||||
},
|
||||
];
|
||||
|
||||
|
|
|
@ -135,7 +135,7 @@ const PluginOptionSchema = Joi.object<PluginOptions>({
|
|||
export function validateOptions({
|
||||
validate,
|
||||
options,
|
||||
}: OptionValidationContext<Options, PluginOptions>): PluginOptions {
|
||||
}: OptionValidationContext<Options | undefined, PluginOptions>): PluginOptions {
|
||||
const validatedOptions = validate(PluginOptionSchema, options);
|
||||
return validatedOptions;
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import footnoteIDFixer from '../footnoteIDFixer';
|
|||
|
||||
const processFixture = async (name: string) => {
|
||||
const filepath = path.join(__dirname, `__fixtures__/${name}.md`);
|
||||
const result = await mdx(await fs.readFile(filepath), {
|
||||
const result = await mdx(await fs.readFile(filepath, 'utf8'), {
|
||||
filepath,
|
||||
remarkPlugins: [footnoteIDFixer],
|
||||
});
|
||||
|
|
Binary file not shown.
|
@ -56,7 +56,9 @@ function testField(params: {
|
|||
);
|
||||
} catch (err) {
|
||||
// eslint-disable-next-line jest/no-conditional-expect
|
||||
expect(err.message).toMatch(new RegExp(escapeStringRegexp(message)));
|
||||
expect((err as Error).message).toMatch(
|
||||
new RegExp(escapeStringRegexp(message)),
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -9,6 +9,7 @@ import {toGlobalDataVersion} from '../globalData';
|
|||
import {createSidebarsUtils} from '../sidebars/utils';
|
||||
import {getCategoryGeneratedIndexMetadataList} from '../categoryGeneratedIndex';
|
||||
import type {Sidebars} from '../sidebars/types';
|
||||
import type {DocMetadata} from '@docusaurus/plugin-content-docs';
|
||||
|
||||
describe('toGlobalDataVersion', () => {
|
||||
it('generates the right docs, sidebars, and metadata', () => {
|
||||
|
@ -25,7 +26,7 @@ describe('toGlobalDataVersion', () => {
|
|||
sidebar: 'tutorial',
|
||||
frontMatter: {},
|
||||
},
|
||||
];
|
||||
] as DocMetadata[];
|
||||
const sidebars: Sidebars = {
|
||||
tutorial: [
|
||||
{
|
||||
|
@ -46,6 +47,8 @@ describe('toGlobalDataVersion', () => {
|
|||
id: 'doc',
|
||||
},
|
||||
],
|
||||
collapsed: false,
|
||||
collapsible: true,
|
||||
},
|
||||
],
|
||||
links: [
|
||||
|
@ -75,6 +78,8 @@ describe('toGlobalDataVersion', () => {
|
|||
id: 'doc',
|
||||
},
|
||||
],
|
||||
collapsed: false,
|
||||
collapsible: true,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
@ -85,7 +90,6 @@ describe('toGlobalDataVersion', () => {
|
|||
label: 'Label',
|
||||
isLast: true,
|
||||
path: '/current',
|
||||
mainDocId: 'main',
|
||||
docs,
|
||||
drafts: [
|
||||
{
|
||||
|
@ -93,7 +97,7 @@ describe('toGlobalDataVersion', () => {
|
|||
permalink: '/current/draft',
|
||||
sidebar: undefined,
|
||||
},
|
||||
],
|
||||
] as DocMetadata[],
|
||||
sidebars,
|
||||
categoryGeneratedIndices: getCategoryGeneratedIndexMetadataList({
|
||||
docs,
|
||||
|
|
|
@ -26,13 +26,28 @@ import * as cliDocs from '../cli';
|
|||
import {validateOptions} from '../options';
|
||||
|
||||
import type {RouteConfig} from '@docusaurus/types';
|
||||
import type {LoadedVersion} from '@docusaurus/plugin-content-docs';
|
||||
import type {SidebarItem, SidebarItemsGeneratorOption} from '../sidebars/types';
|
||||
import type {
|
||||
LoadedVersion,
|
||||
PropSidebarItemLink,
|
||||
} from '@docusaurus/plugin-content-docs';
|
||||
import type {
|
||||
SidebarItemsGeneratorOption,
|
||||
NormalizedSidebar,
|
||||
} from '../sidebars/types';
|
||||
|
||||
function findDocById(version: LoadedVersion, unversionedId: string) {
|
||||
function findDocById(
|
||||
version: LoadedVersion | undefined,
|
||||
unversionedId: string,
|
||||
) {
|
||||
if (!version) {
|
||||
throw new Error('Version not found');
|
||||
}
|
||||
return version.docs.find((item) => item.unversionedId === unversionedId);
|
||||
}
|
||||
function getDocById(version: LoadedVersion, unversionedId: string) {
|
||||
function getDocById(version: LoadedVersion | undefined, unversionedId: string) {
|
||||
if (!version) {
|
||||
throw new Error('Version not found');
|
||||
}
|
||||
const doc = findDocById(version, unversionedId);
|
||||
if (!doc) {
|
||||
throw new Error(
|
||||
|
@ -83,7 +98,10 @@ Entries created:
|
|||
getGlobalData: () => globalDataContainer,
|
||||
getRouteConfigs: () => routeConfigs,
|
||||
|
||||
checkVersionMetadataPropCreated: (version: LoadedVersion) => {
|
||||
checkVersionMetadataPropCreated: (version: LoadedVersion | undefined) => {
|
||||
if (!version) {
|
||||
throw new Error('Version not found');
|
||||
}
|
||||
const versionMetadataProp = getCreatedDataByPrefix(
|
||||
`version-${_.kebabCase(version.versionName)}-metadata-prop`,
|
||||
);
|
||||
|
@ -164,7 +182,7 @@ describe('sidebar', () => {
|
|||
const result = await plugin.loadContent!();
|
||||
|
||||
expect(result.loadedVersions).toHaveLength(1);
|
||||
expect(result.loadedVersions[0].sidebars).toMatchSnapshot();
|
||||
expect(result.loadedVersions[0]!.sidebars).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('site with disabled sidebar', async () => {
|
||||
|
@ -182,7 +200,7 @@ describe('sidebar', () => {
|
|||
const result = await plugin.loadContent!();
|
||||
|
||||
expect(result.loadedVersions).toHaveLength(1);
|
||||
expect(result.loadedVersions[0].sidebars).toEqual(DisabledSidebars);
|
||||
expect(result.loadedVersions[0]!.sidebars).toEqual(DisabledSidebars);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -309,7 +327,7 @@ describe('simple website', () => {
|
|||
|
||||
expect(getDocById(currentVersion, 'foo/bar')).toMatchSnapshot();
|
||||
|
||||
expect(currentVersion.sidebars).toMatchSnapshot();
|
||||
expect(currentVersion!.sidebars).toMatchSnapshot();
|
||||
|
||||
const {actions, utils} = createFakeActions(pluginContentDir);
|
||||
|
||||
|
@ -427,10 +445,12 @@ describe('versioned website', () => {
|
|||
expect(getDocById(version101, 'hello')).toMatchSnapshot();
|
||||
expect(getDocById(version100, 'foo/baz')).toMatchSnapshot();
|
||||
|
||||
expect(currentVersion.sidebars).toMatchSnapshot('current version sidebars');
|
||||
expect(version101.sidebars).toMatchSnapshot('101 version sidebars');
|
||||
expect(version100.sidebars).toMatchSnapshot('100 version sidebars');
|
||||
expect(versionWithSlugs.sidebars).toMatchSnapshot(
|
||||
expect(currentVersion!.sidebars).toMatchSnapshot(
|
||||
'current version sidebars',
|
||||
);
|
||||
expect(version101!.sidebars).toMatchSnapshot('101 version sidebars');
|
||||
expect(version100!.sidebars).toMatchSnapshot('100 version sidebars');
|
||||
expect(versionWithSlugs!.sidebars).toMatchSnapshot(
|
||||
'withSlugs version sidebars',
|
||||
);
|
||||
|
||||
|
@ -534,8 +554,10 @@ describe('versioned website (community)', () => {
|
|||
expect(getDocById(currentVersion, 'team')).toMatchSnapshot();
|
||||
expect(getDocById(version100, 'team')).toMatchSnapshot();
|
||||
|
||||
expect(currentVersion.sidebars).toMatchSnapshot('current version sidebars');
|
||||
expect(version100.sidebars).toMatchSnapshot('100 version sidebars');
|
||||
expect(currentVersion!.sidebars).toMatchSnapshot(
|
||||
'current version sidebars',
|
||||
);
|
||||
expect(version100!.sidebars).toMatchSnapshot('100 version sidebars');
|
||||
|
||||
const {actions, utils} = createFakeActions(pluginContentDir);
|
||||
await plugin.contentLoaded!({
|
||||
|
@ -544,8 +566,8 @@ describe('versioned website (community)', () => {
|
|||
allContent: {},
|
||||
});
|
||||
|
||||
utils.checkVersionMetadataPropCreated(currentVersion);
|
||||
utils.checkVersionMetadataPropCreated(version100);
|
||||
utils.checkVersionMetadataPropCreated(currentVersion!);
|
||||
utils.checkVersionMetadataPropCreated(version100!);
|
||||
|
||||
utils.expectSnapshot();
|
||||
});
|
||||
|
@ -574,18 +596,22 @@ describe('site with doc label', () => {
|
|||
|
||||
it('label in sidebar.json is used', async () => {
|
||||
const {content} = await loadSite();
|
||||
const loadedVersion = content.loadedVersions[0];
|
||||
const loadedVersion = content.loadedVersions[0]!;
|
||||
const sidebarProps = toSidebarsProp(loadedVersion);
|
||||
|
||||
expect(sidebarProps.docs[0].label).toBe('Hello One');
|
||||
expect((sidebarProps.docs![0] as PropSidebarItemLink).label).toBe(
|
||||
'Hello One',
|
||||
);
|
||||
});
|
||||
|
||||
it('sidebar_label in doc has higher precedence over label in sidebar.json', async () => {
|
||||
const {content} = await loadSite();
|
||||
const loadedVersion = content.loadedVersions[0];
|
||||
const loadedVersion = content.loadedVersions[0]!;
|
||||
const sidebarProps = toSidebarsProp(loadedVersion);
|
||||
|
||||
expect(sidebarProps.docs[1].label).toBe('Hello 2 From Doc');
|
||||
expect((sidebarProps.docs![1] as PropSidebarItemLink).label).toBe(
|
||||
'Hello 2 From Doc',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -614,14 +640,14 @@ describe('site with full autogenerated sidebar', () => {
|
|||
|
||||
it('sidebar is fully autogenerated', async () => {
|
||||
const {content} = await loadSite();
|
||||
const version = content.loadedVersions[0];
|
||||
const version = content.loadedVersions[0]!;
|
||||
|
||||
expect(version.sidebars).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('docs in fully generated sidebar have correct metadata', async () => {
|
||||
const {content} = await loadSite();
|
||||
const version = content.loadedVersions[0];
|
||||
const version = content.loadedVersions[0]!;
|
||||
|
||||
expect(getDocById(version, 'getting-started')).toMatchSnapshot();
|
||||
expect(getDocById(version, 'installation')).toMatchSnapshot();
|
||||
|
@ -675,14 +701,14 @@ describe('site with partial autogenerated sidebars', () => {
|
|||
|
||||
it('sidebar is partially autogenerated', async () => {
|
||||
const {content} = await loadSite();
|
||||
const version = content.loadedVersions[0];
|
||||
const version = content.loadedVersions[0]!;
|
||||
|
||||
expect(version.sidebars).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('docs in partially generated sidebar have correct metadata', async () => {
|
||||
const {content} = await loadSite();
|
||||
const version = content.loadedVersions[0];
|
||||
const version = content.loadedVersions[0]!;
|
||||
|
||||
// Only looking at the docs of the autogen sidebar, others metadata should
|
||||
// not be affected
|
||||
|
@ -731,7 +757,7 @@ describe('site with partial autogenerated sidebars 2 (fix #4638)', () => {
|
|||
|
||||
it('sidebar is partially autogenerated', async () => {
|
||||
const {content} = await loadSite();
|
||||
const version = content.loadedVersions[0];
|
||||
const version = content.loadedVersions[0]!;
|
||||
|
||||
expect(version.sidebars).toMatchSnapshot();
|
||||
});
|
||||
|
@ -763,8 +789,10 @@ describe('site with custom sidebar items generator', () => {
|
|||
const customSidebarItemsGeneratorMock = jest.fn(async () => []);
|
||||
const {siteDir} = await loadSite(customSidebarItemsGeneratorMock);
|
||||
|
||||
const generatorArg: Parameters<SidebarItemsGeneratorOption>[0] =
|
||||
customSidebarItemsGeneratorMock.mock.calls[0][0];
|
||||
const generatorArg = (
|
||||
customSidebarItemsGeneratorMock.mock
|
||||
.calls[0] as unknown as Parameters<SidebarItemsGeneratorOption>
|
||||
)[0];
|
||||
|
||||
// Make test pass even if docs are in different order and paths are
|
||||
// absolutes
|
||||
|
@ -797,12 +825,12 @@ describe('site with custom sidebar items generator', () => {
|
|||
const {content} = await loadSite(customSidebarItemsGenerator);
|
||||
const version = content.loadedVersions[0];
|
||||
|
||||
expect(version.sidebars).toMatchSnapshot();
|
||||
expect(version!.sidebars).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('sidebarItemsGenerator can wrap/enhance/sort/reverse the default sidebar generator', async () => {
|
||||
function reverseSidebarItems(items: SidebarItem[]): SidebarItem[] {
|
||||
const result: SidebarItem[] = items.map((item) => {
|
||||
function reverseSidebarItems(items: NormalizedSidebar): NormalizedSidebar {
|
||||
const result: NormalizedSidebar = items.map((item) => {
|
||||
if (item.type === 'category') {
|
||||
return {...item, items: reverseSidebarItems(item.items)};
|
||||
}
|
||||
|
@ -821,7 +849,7 @@ describe('site with custom sidebar items generator', () => {
|
|||
};
|
||||
|
||||
const {content} = await loadSite(reversedSidebarItemsGenerator);
|
||||
const version = content.loadedVersions[0];
|
||||
const version = content.loadedVersions[0]!;
|
||||
|
||||
expect(version.sidebars).toMatchSnapshot();
|
||||
});
|
||||
|
|
|
@ -36,7 +36,7 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
});
|
||||
|
||||
it('accepts correctly defined user options', () => {
|
||||
const userOptions = {
|
||||
const userOptions: Options = {
|
||||
path: 'my-docs', // Path to data on filesystem, relative to site dir.
|
||||
routeBasePath: 'my-docs', // URL Route.
|
||||
tagsBasePath: 'tags', // URL Tags Route.
|
||||
|
@ -51,6 +51,7 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
docTagsListComponent: '@theme/DocTagsListPage',
|
||||
docCategoryGeneratedIndexComponent:
|
||||
'@theme/DocCategoryGeneratedIndexPage',
|
||||
// @ts-expect-error: it seems to work in practice?
|
||||
remarkPlugins: [markdownPluginsObjectStub],
|
||||
rehypePlugins: [markdownPluginsFunctionStub],
|
||||
beforeDefaultRehypePlugins: [],
|
||||
|
@ -79,16 +80,17 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
expect(testValidate(userOptions)).toEqual({
|
||||
...defaultOptions,
|
||||
...userOptions,
|
||||
remarkPlugins: [...userOptions.remarkPlugins, expect.any(Array)],
|
||||
remarkPlugins: [...userOptions.remarkPlugins!, expect.any(Array)],
|
||||
});
|
||||
});
|
||||
|
||||
it('accepts correctly defined remark and rehype plugin options', () => {
|
||||
const userOptions = {
|
||||
const userOptions: Options = {
|
||||
beforeDefaultRemarkPlugins: [],
|
||||
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
|
||||
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
|
||||
rehypePlugins: [
|
||||
// @ts-expect-error: it seems to work in practice
|
||||
markdownPluginsObjectStub,
|
||||
[markdownPluginsFunctionStub, {option1: '42'}],
|
||||
],
|
||||
|
@ -96,12 +98,12 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
expect(testValidate(userOptions)).toEqual({
|
||||
...defaultOptions,
|
||||
...userOptions,
|
||||
remarkPlugins: [...userOptions.remarkPlugins, expect.any(Array)],
|
||||
remarkPlugins: [...userOptions.remarkPlugins!, expect.any(Array)],
|
||||
});
|
||||
});
|
||||
|
||||
it('accepts admonitions false', () => {
|
||||
const admonitionsFalse = {
|
||||
const admonitionsFalse: Options = {
|
||||
admonitions: false,
|
||||
};
|
||||
expect(testValidate(admonitionsFalse)).toEqual({
|
||||
|
@ -111,7 +113,7 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
});
|
||||
|
||||
it('rejects admonitions true', () => {
|
||||
const admonitionsTrue = {
|
||||
const admonitionsTrue: Options = {
|
||||
admonitions: true,
|
||||
};
|
||||
expect(() =>
|
||||
|
@ -124,7 +126,10 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
it('accepts numberPrefixParser function', () => {
|
||||
function customNumberPrefixParser() {}
|
||||
expect(
|
||||
testValidate({numberPrefixParser: customNumberPrefixParser}),
|
||||
testValidate({
|
||||
numberPrefixParser:
|
||||
customNumberPrefixParser as unknown as Options['numberPrefixParser'],
|
||||
}),
|
||||
).toEqual({
|
||||
...defaultOptions,
|
||||
numberPrefixParser: customNumberPrefixParser,
|
||||
|
@ -148,6 +153,7 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
it('rejects invalid remark plugin options', () => {
|
||||
expect(() =>
|
||||
testValidate({
|
||||
// @ts-expect-error: test
|
||||
remarkPlugins: [[{option1: '42'}, markdownPluginsFunctionStub]],
|
||||
}),
|
||||
).toThrowErrorMatchingInlineSnapshot(`
|
||||
|
@ -161,6 +167,7 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
expect(() =>
|
||||
testValidate({
|
||||
rehypePlugins: [
|
||||
// @ts-expect-error: test
|
||||
[
|
||||
markdownPluginsFunctionStub,
|
||||
{option1: '42'},
|
||||
|
@ -176,6 +183,7 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
});
|
||||
|
||||
it('rejects bad path inputs', () => {
|
||||
// @ts-expect-error: test
|
||||
expect(() => testValidate({path: 2})).toThrowErrorMatchingInlineSnapshot(
|
||||
`""path" must be a string"`,
|
||||
);
|
||||
|
@ -183,12 +191,14 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
|
||||
it('rejects bad include inputs', () => {
|
||||
expect(() =>
|
||||
// @ts-expect-error: test
|
||||
testValidate({include: '**/*.{md,mdx}'}),
|
||||
).toThrowErrorMatchingInlineSnapshot(`""include" must be an array"`);
|
||||
});
|
||||
|
||||
it('rejects bad showLastUpdateTime inputs', () => {
|
||||
expect(() =>
|
||||
// @ts-expect-error: test
|
||||
testValidate({showLastUpdateTime: 'true'}),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`""showLastUpdateTime" must be a boolean"`,
|
||||
|
@ -197,12 +207,14 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
|
||||
it('rejects bad remarkPlugins input', () => {
|
||||
expect(() =>
|
||||
// @ts-expect-error: test
|
||||
testValidate({remarkPlugins: 'remark-math'}),
|
||||
).toThrowErrorMatchingInlineSnapshot(`""remarkPlugins" must be an array"`);
|
||||
});
|
||||
|
||||
it('rejects bad lastVersion', () => {
|
||||
expect(() =>
|
||||
// @ts-expect-error: test
|
||||
testValidate({lastVersion: false}),
|
||||
).toThrowErrorMatchingInlineSnapshot(`""lastVersion" must be a string"`);
|
||||
});
|
||||
|
@ -212,6 +224,7 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
testValidate({
|
||||
versions: {
|
||||
current: {
|
||||
// @ts-expect-error: test
|
||||
hey: 3,
|
||||
},
|
||||
|
||||
|
|
|
@ -21,30 +21,30 @@ describe('toTagDocListProp', () => {
|
|||
docIds: ['id1', 'id3'],
|
||||
};
|
||||
|
||||
const doc1: Doc = {
|
||||
const doc1 = {
|
||||
id: 'id1',
|
||||
title: 'ZZZ 1',
|
||||
description: 'Description 1',
|
||||
permalink: '/doc1',
|
||||
};
|
||||
const doc2: Doc = {
|
||||
} as Doc;
|
||||
const doc2 = {
|
||||
id: 'id2',
|
||||
title: 'XXX 2',
|
||||
description: 'Description 2',
|
||||
permalink: '/doc2',
|
||||
};
|
||||
const doc3: Doc = {
|
||||
} as Doc;
|
||||
const doc3 = {
|
||||
id: 'id3',
|
||||
title: 'AAA 3',
|
||||
description: 'Description 3',
|
||||
permalink: '/doc3',
|
||||
};
|
||||
const doc4: Doc = {
|
||||
} as Doc;
|
||||
const doc4 = {
|
||||
id: 'id4',
|
||||
title: 'UUU 4',
|
||||
description: 'Description 4',
|
||||
permalink: '/doc4',
|
||||
};
|
||||
} as Doc;
|
||||
|
||||
const result = toTagDocListProp({
|
||||
allTagsPath,
|
||||
|
|
|
@ -17,7 +17,7 @@ declare module '@docusaurus/plugin-content-docs' {
|
|||
Tag,
|
||||
} from '@docusaurus/utils';
|
||||
import type {Plugin, LoadContext} from '@docusaurus/types';
|
||||
import type {Required} from 'utility-types';
|
||||
import type {Overwrite, Required} from 'utility-types';
|
||||
|
||||
export type Assets = {
|
||||
image?: string;
|
||||
|
@ -206,7 +206,22 @@ declare module '@docusaurus/plugin-content-docs' {
|
|||
*/
|
||||
tagsBasePath: string;
|
||||
};
|
||||
export type Options = Partial<PluginOptions>;
|
||||
export type Options = Partial<
|
||||
Overwrite<
|
||||
PluginOptions,
|
||||
{
|
||||
/**
|
||||
* Custom parsing logic to extract number prefixes from file names. Use
|
||||
* `false` to disable this behavior and leave the docs untouched, and
|
||||
* `true` to use the default parser.
|
||||
*
|
||||
* @param filename One segment of the path, without any slashes.
|
||||
* @see https://docusaurus.io/docs/sidebar#using-number-prefixes
|
||||
*/
|
||||
numberPrefixParser: PluginOptions['numberPrefixParser'] | boolean;
|
||||
}
|
||||
>
|
||||
>;
|
||||
export type SidebarsConfig = import('./sidebars/types').SidebarsConfig;
|
||||
|
||||
export type VersionMetadata = ContentPaths & {
|
||||
|
|
|
@ -100,10 +100,13 @@ describe('createSidebarsUtils', () => {
|
|||
const sidebar4: Sidebar = [
|
||||
{
|
||||
type: 'category',
|
||||
collapsed: false,
|
||||
collapsible: true,
|
||||
label: 'Related',
|
||||
items: [
|
||||
{type: 'link', href: 'https://facebook.com'},
|
||||
{type: 'link', href: 'https://reactjs.org'},
|
||||
{type: 'link', href: 'https://docusaurus.io'},
|
||||
{type: 'link', href: 'https://facebook.com', label: 'Facebook'},
|
||||
{type: 'link', href: 'https://reactjs.org', label: 'React'},
|
||||
{type: 'link', href: 'https://docusaurus.io', label: 'Docusaurus'},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
@ -696,10 +699,10 @@ describe('toNavigationLink', () => {
|
|||
|
||||
it('with doc items', () => {
|
||||
expect(toNavigationLink({type: 'doc', id: 'doc1'}, docsById)).toEqual(
|
||||
toDocNavigationLink(docsById.doc1),
|
||||
toDocNavigationLink(docsById.doc1!),
|
||||
);
|
||||
expect(toNavigationLink({type: 'doc', id: 'doc2'}, docsById)).toEqual(
|
||||
toDocNavigationLink(docsById.doc2),
|
||||
toDocNavigationLink(docsById.doc2!),
|
||||
);
|
||||
expect(() =>
|
||||
toNavigationLink({type: 'doc', id: 'doc3'}, docsById),
|
||||
|
@ -724,7 +727,7 @@ describe('toNavigationLink', () => {
|
|||
},
|
||||
docsById,
|
||||
),
|
||||
).toEqual(toDocNavigationLink(docsById.doc1));
|
||||
).toEqual(toDocNavigationLink(docsById.doc1!));
|
||||
expect(() =>
|
||||
toNavigationLink(
|
||||
{
|
||||
|
|
|
@ -10,7 +10,7 @@ import path from 'path';
|
|||
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
|
||||
import {readVersionsMetadata} from '../index';
|
||||
import {DEFAULT_OPTIONS} from '../../options';
|
||||
import type {I18n} from '@docusaurus/types';
|
||||
import type {I18n, LoadContext} from '@docusaurus/types';
|
||||
import type {
|
||||
PluginOptions,
|
||||
VersionMetadata,
|
||||
|
@ -37,7 +37,7 @@ describe('readVersionsMetadata', () => {
|
|||
siteDir: simpleSiteDir,
|
||||
baseUrl: '/',
|
||||
i18n: DefaultI18N,
|
||||
};
|
||||
} as LoadContext;
|
||||
|
||||
const vCurrent: VersionMetadata = {
|
||||
contentPath: path.join(simpleSiteDir, 'docs'),
|
||||
|
@ -198,7 +198,7 @@ describe('readVersionsMetadata', () => {
|
|||
siteDir: versionedSiteDir,
|
||||
baseUrl: '/',
|
||||
i18n: DefaultI18N,
|
||||
};
|
||||
} as LoadContext;
|
||||
|
||||
const vCurrent: VersionMetadata = {
|
||||
contentPath: path.join(versionedSiteDir, 'docs'),
|
||||
|
@ -636,7 +636,7 @@ describe('readVersionsMetadata', () => {
|
|||
siteDir: versionedSiteDir,
|
||||
baseUrl: '/',
|
||||
i18n: DefaultI18N,
|
||||
};
|
||||
} as LoadContext;
|
||||
|
||||
const vCurrent: VersionMetadata = {
|
||||
contentPath: path.join(versionedSiteDir, 'community'),
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
import React from 'react';
|
||||
import {EnumChangefreq} from 'sitemap';
|
||||
import createSitemap from '../createSitemap';
|
||||
import type {PluginOptions} from '../options';
|
||||
import type {DocusaurusConfig} from '@docusaurus/types';
|
||||
|
||||
describe('createSitemap', () => {
|
||||
|
@ -31,7 +32,8 @@ describe('createSitemap', () => {
|
|||
|
||||
it('empty site', () =>
|
||||
expect(async () => {
|
||||
await createSitemap({} as DocusaurusConfig, [], {}, {});
|
||||
// @ts-expect-error: test
|
||||
await createSitemap({}, [], {}, {} as PluginOptions);
|
||||
}).rejects.toThrow(
|
||||
'URL in docusaurus.config.js cannot be empty/undefined.',
|
||||
));
|
||||
|
@ -148,6 +150,7 @@ describe('createSitemap', () => {
|
|||
{
|
||||
'/noindex': {
|
||||
meta: {
|
||||
// @ts-expect-error: bad lib def
|
||||
toComponent: () => [
|
||||
React.createElement('meta', {name: 'robots', content: 'noindex'}),
|
||||
],
|
||||
|
|
|
@ -23,7 +23,7 @@ describe('validateOptions', () => {
|
|||
});
|
||||
|
||||
it('accepts correctly defined user options', () => {
|
||||
const userOptions = {
|
||||
const userOptions: Options = {
|
||||
changefreq: 'yearly',
|
||||
priority: 0.9,
|
||||
ignorePatterns: ['/search/**'],
|
||||
|
@ -52,9 +52,11 @@ describe('validateOptions', () => {
|
|||
|
||||
it('rejects bad ignorePatterns inputs', () => {
|
||||
expect(() =>
|
||||
// @ts-expect-error: test
|
||||
testValidate({ignorePatterns: '/search'}),
|
||||
).toThrowErrorMatchingInlineSnapshot(`""ignorePatterns" must be an array"`);
|
||||
expect(() =>
|
||||
// @ts-expect-error: test
|
||||
testValidate({ignorePatterns: [/^\/search/]}),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`""ignorePatterns[0]" must be a string"`,
|
||||
|
|
|
@ -9,7 +9,7 @@ import {updateTranslationFileMessages} from '@docusaurus/utils';
|
|||
import {getTranslationFiles, translateThemeConfig} from '../translations';
|
||||
import type {ThemeConfig} from '@docusaurus/theme-common';
|
||||
|
||||
const ThemeConfigSample: ThemeConfig = {
|
||||
const ThemeConfigSample = {
|
||||
colorMode: {},
|
||||
announcementBar: {},
|
||||
prism: {},
|
||||
|
@ -47,7 +47,7 @@ const ThemeConfigSample: ThemeConfig = {
|
|||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
} as unknown as ThemeConfig;
|
||||
|
||||
const ThemeConfigSampleSimpleFooter: ThemeConfig = {
|
||||
...ThemeConfigSample,
|
||||
|
|
|
@ -674,7 +674,7 @@ describe('themeConfig', () => {
|
|||
});
|
||||
|
||||
it('max config', () => {
|
||||
const colorMode = {
|
||||
const colorMode: ThemeConfig['colorMode'] = {
|
||||
defaultMode: 'dark',
|
||||
disableSwitch: false,
|
||||
respectPrefersColorScheme: true,
|
||||
|
|
|
@ -1032,10 +1032,10 @@ declare module '@theme/Tabs' {
|
|||
readonly lazy?: boolean;
|
||||
readonly block?: boolean;
|
||||
readonly children: readonly ReactElement<TabItemProps>[];
|
||||
readonly defaultValue?: string | null;
|
||||
readonly defaultValue?: string | number | null;
|
||||
readonly values?: readonly {
|
||||
value: string;
|
||||
label?: string;
|
||||
value: string | number;
|
||||
label?: string | number;
|
||||
attributes?: {[key: string]: unknown};
|
||||
}[];
|
||||
readonly groupId?: string;
|
||||
|
|
|
@ -115,7 +115,7 @@ function TabsComponent(props: Props): JSX.Element {
|
|||
setSelectedValue(newTabValue);
|
||||
|
||||
if (groupId != null) {
|
||||
setTabGroupChoices(groupId, newTabValue);
|
||||
setTabGroupChoices(groupId, String(newTabValue));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -26,6 +26,7 @@ import type {
|
|||
PropSidebarItemLink,
|
||||
PropVersionMetadata,
|
||||
} from '@docusaurus/plugin-content-docs';
|
||||
import type {DocusaurusContext} from '@docusaurus/types';
|
||||
|
||||
// Make tests more readable with some useful category item defaults
|
||||
function testCategory(
|
||||
|
@ -294,19 +295,22 @@ describe('isActiveSidebarItem', () => {
|
|||
|
||||
describe('useSidebarBreadcrumbs', () => {
|
||||
const createUseSidebarBreadcrumbsMock =
|
||||
(sidebar: PropSidebar, breadcrumbsOption?: boolean) => (location: string) =>
|
||||
(sidebar: PropSidebar | undefined, breadcrumbsOption?: boolean) =>
|
||||
(location: string) =>
|
||||
renderHook(() => useSidebarBreadcrumbs(), {
|
||||
wrapper: ({children}) => (
|
||||
<StaticRouter location={location}>
|
||||
<Context.Provider
|
||||
// eslint-disable-next-line react/jsx-no-constructed-context-values
|
||||
value={{
|
||||
globalData: {
|
||||
'docusaurus-plugin-content-docs': {
|
||||
default: {path: '/', breadcrumbs: breadcrumbsOption},
|
||||
value={
|
||||
// eslint-disable-next-line react/jsx-no-constructed-context-values
|
||||
{
|
||||
globalData: {
|
||||
'docusaurus-plugin-content-docs': {
|
||||
default: {path: '/', breadcrumbs: breadcrumbsOption},
|
||||
},
|
||||
},
|
||||
},
|
||||
}}>
|
||||
} as unknown as DocusaurusContext
|
||||
}>
|
||||
<DocsSidebarProvider name="sidebarName" items={sidebar}>
|
||||
{children}
|
||||
</DocsSidebarProvider>
|
||||
|
@ -421,7 +425,9 @@ describe('useSidebarBreadcrumbs', () => {
|
|||
});
|
||||
|
||||
it('returns null when there is no sidebar', () => {
|
||||
expect(createUseSidebarBreadcrumbsMock(null, false)('/foo')).toBeNull();
|
||||
expect(
|
||||
createUseSidebarBreadcrumbsMock(undefined, false)('/foo'),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -436,9 +442,12 @@ describe('useCurrentSidebarCategory', () => {
|
|||
),
|
||||
}).result.current;
|
||||
it('works', () => {
|
||||
const category = {
|
||||
const category: PropSidebarItemCategory = {
|
||||
type: 'category',
|
||||
label: 'Category',
|
||||
href: '/cat',
|
||||
collapsible: true,
|
||||
collapsed: false,
|
||||
items: [
|
||||
{type: 'link', href: '/cat/foo', label: 'Foo'},
|
||||
{type: 'link', href: '/cat/bar', label: 'Bar'},
|
||||
|
@ -453,8 +462,11 @@ describe('useCurrentSidebarCategory', () => {
|
|||
});
|
||||
|
||||
it('throws for non-category index page', () => {
|
||||
const category = {
|
||||
const category: PropSidebarItemCategory = {
|
||||
type: 'category',
|
||||
label: 'Category',
|
||||
collapsible: true,
|
||||
collapsed: false,
|
||||
items: [
|
||||
{type: 'link', href: '/cat/foo', label: 'Foo'},
|
||||
{type: 'link', href: '/cat/bar', label: 'Bar'},
|
||||
|
|
|
@ -24,7 +24,7 @@ describe('usePluralForm', () => {
|
|||
i18n: {
|
||||
currentLocale: 'en',
|
||||
},
|
||||
});
|
||||
} as DocusaurusContext);
|
||||
expect(mockUsePluralForm().selectMessage(1, 'one|many')).toBe('one');
|
||||
expect(mockUsePluralForm().selectMessage(10, 'one|many')).toBe('many');
|
||||
});
|
||||
|
@ -34,7 +34,7 @@ describe('usePluralForm', () => {
|
|||
i18n: {
|
||||
currentLocale: 'zh-Hans',
|
||||
},
|
||||
});
|
||||
} as DocusaurusContext);
|
||||
const consoleMock = jest
|
||||
.spyOn(console, 'error')
|
||||
.mockImplementation(() => {});
|
||||
|
@ -50,7 +50,7 @@ describe('usePluralForm', () => {
|
|||
i18n: {
|
||||
currentLocale: 'en',
|
||||
},
|
||||
});
|
||||
} as DocusaurusContext);
|
||||
expect(mockUsePluralForm().selectMessage(10, 'many')).toBe('many');
|
||||
});
|
||||
|
||||
|
@ -59,7 +59,7 @@ describe('usePluralForm', () => {
|
|||
i18n: {
|
||||
currentLocale: 'zh-Hans',
|
||||
},
|
||||
});
|
||||
} as DocusaurusContext);
|
||||
const consoleMock = jest
|
||||
.spyOn(console, 'error')
|
||||
.mockImplementation(() => {});
|
||||
|
|
|
@ -78,6 +78,7 @@ describe('readDefaultCodeTranslationMessages', () => {
|
|||
it('for empty locale', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
name: 'default',
|
||||
locale: '',
|
||||
dirPath,
|
||||
}),
|
||||
|
|
|
@ -58,7 +58,7 @@ describe('mapAsyncSequential', () => {
|
|||
const timeBefore = Date.now();
|
||||
await expect(
|
||||
mapAsyncSequential(items, async (item) => {
|
||||
const itemTimeout = itemToTimeout[item];
|
||||
const itemTimeout = itemToTimeout[item]!;
|
||||
itemMapStartsAt[item] = Date.now();
|
||||
await sleep(itemTimeout);
|
||||
itemMapEndsAt[item] = Date.now();
|
||||
|
@ -72,12 +72,10 @@ describe('mapAsyncSequential', () => {
|
|||
const totalTimeouts = _.sum(Object.values(itemToTimeout));
|
||||
expect(timeTotal).toBeGreaterThanOrEqual(totalTimeouts - 100);
|
||||
|
||||
expect(itemMapStartsAt['1']).toBeGreaterThanOrEqual(0);
|
||||
expect(itemMapStartsAt['2']).toBeGreaterThanOrEqual(
|
||||
itemMapEndsAt['1'] - 100,
|
||||
);
|
||||
expect(itemMapStartsAt[1]).toBeGreaterThanOrEqual(0);
|
||||
expect(itemMapStartsAt[2]).toBeGreaterThanOrEqual(itemMapEndsAt[1]! - 100);
|
||||
expect(itemMapStartsAt['3']).toBeGreaterThanOrEqual(
|
||||
itemMapEndsAt['2'] - 100,
|
||||
itemMapEndsAt[2]! - 100,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -10,7 +10,7 @@ import {getFileLoaderUtils} from '../webpackUtils';
|
|||
describe('getFileLoaderUtils()', () => {
|
||||
it('plugin svgo/removeViewBox and removeTitle should be disabled', () => {
|
||||
const {oneOf} = getFileLoaderUtils().rules.svg();
|
||||
expect(oneOf[0].use).toContainEqual(
|
||||
expect(oneOf![0]!.use).toContainEqual(
|
||||
expect.objectContaining({
|
||||
loader: require.resolve('@svgr/webpack'),
|
||||
options: expect.objectContaining({
|
||||
|
|
|
@ -46,7 +46,7 @@ describe('flat', () => {
|
|||
expect(
|
||||
flat({
|
||||
foo: {
|
||||
bar: value,
|
||||
bar: value as string,
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
|
|
|
@ -7,13 +7,14 @@
|
|||
|
||||
import {jest} from '@jest/globals';
|
||||
import normalizeLocation from '../normalizeLocation';
|
||||
import type {Location} from 'history';
|
||||
|
||||
describe('normalizeLocation', () => {
|
||||
it('rewrites locations with index.html', () => {
|
||||
expect(
|
||||
normalizeLocation({
|
||||
pathname: '/index.html',
|
||||
}),
|
||||
} as Location),
|
||||
).toEqual({
|
||||
pathname: '/',
|
||||
});
|
||||
|
@ -23,7 +24,7 @@ describe('normalizeLocation', () => {
|
|||
pathname: '/docs/introduction/index.html',
|
||||
search: '?search=foo',
|
||||
hash: '#features',
|
||||
}),
|
||||
} as Location),
|
||||
).toEqual({
|
||||
pathname: '/docs/introduction',
|
||||
search: '?search=foo',
|
||||
|
@ -35,7 +36,7 @@ describe('normalizeLocation', () => {
|
|||
pathname: '/index.html',
|
||||
search: '',
|
||||
hash: '#features',
|
||||
}),
|
||||
} as Location),
|
||||
).toEqual({
|
||||
pathname: '/',
|
||||
search: '',
|
||||
|
@ -47,7 +48,7 @@ describe('normalizeLocation', () => {
|
|||
expect(
|
||||
normalizeLocation({
|
||||
pathname: '/docs/installation.html',
|
||||
}),
|
||||
} as Location),
|
||||
).toEqual({
|
||||
pathname: '/docs/installation',
|
||||
});
|
||||
|
@ -56,7 +57,7 @@ describe('normalizeLocation', () => {
|
|||
pathname: '/docs/introduction/foo.html',
|
||||
search: '',
|
||||
hash: '#bar',
|
||||
}),
|
||||
} as Location),
|
||||
).toEqual({
|
||||
pathname: '/docs/introduction/foo',
|
||||
search: '',
|
||||
|
@ -65,7 +66,7 @@ describe('normalizeLocation', () => {
|
|||
});
|
||||
|
||||
it('does not strip extension if the route location has one', () => {
|
||||
expect(normalizeLocation({pathname: '/page.html'})).toEqual({
|
||||
expect(normalizeLocation({pathname: '/page.html'} as Location)).toEqual({
|
||||
pathname: '/page.html',
|
||||
});
|
||||
});
|
||||
|
@ -78,7 +79,7 @@ describe('normalizeLocation', () => {
|
|||
pathname: '/docs/introduction',
|
||||
search: '',
|
||||
hash: '#features',
|
||||
}),
|
||||
} as Location),
|
||||
).toEqual({
|
||||
pathname: '/docs/introduction',
|
||||
search: '',
|
||||
|
@ -91,7 +92,7 @@ describe('normalizeLocation', () => {
|
|||
pathname: '/docs/introduction',
|
||||
search: '',
|
||||
hash: '#features',
|
||||
}),
|
||||
} as Location),
|
||||
).toEqual({
|
||||
pathname: '/docs/introduction',
|
||||
search: '',
|
||||
|
@ -102,7 +103,7 @@ describe('normalizeLocation', () => {
|
|||
expect(
|
||||
normalizeLocation({
|
||||
pathname: '/',
|
||||
}),
|
||||
} as Location),
|
||||
).toEqual({
|
||||
pathname: '/',
|
||||
});
|
||||
|
|
|
@ -31,7 +31,7 @@ describe('interpolate', () => {
|
|||
object: {hello: 'world'},
|
||||
array: ['Hello'],
|
||||
};
|
||||
// Do we need to improve the JS type -> String conversion logic here?
|
||||
// @ts-expect-error: test
|
||||
expect(interpolate(text, values)).toMatchInlineSnapshot(
|
||||
`"42 Hello [object Object] Hello"`,
|
||||
);
|
||||
|
@ -52,6 +52,7 @@ describe('interpolate', () => {
|
|||
// Should we emit warnings in such case?
|
||||
const text = 'Hello {name} how are you {unprovidedValue}?';
|
||||
const values = {name: 'Sébastien', extraValue: 'today'};
|
||||
// @ts-expect-error: test
|
||||
expect(interpolate(text, values)).toMatchInlineSnapshot(
|
||||
`"Hello Sébastien how are you {unprovidedValue}?"`,
|
||||
);
|
||||
|
@ -61,6 +62,7 @@ describe('interpolate', () => {
|
|||
// Should we emit warnings in such case?
|
||||
const text = 'Hello {name} how are you {day}?';
|
||||
expect(interpolate(text)).toEqual(text);
|
||||
// @ts-expect-error: test
|
||||
expect(interpolate(text, {})).toEqual(text);
|
||||
});
|
||||
|
||||
|
@ -84,6 +86,7 @@ describe('interpolate', () => {
|
|||
extraUselessValue1: <div>test</div>,
|
||||
extraUselessValue2: 'hi',
|
||||
};
|
||||
// @ts-expect-error: test
|
||||
expect(interpolate(text, values)).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
@ -133,6 +136,7 @@ describe('<Interpolate>', () => {
|
|||
`"The Docusaurus <Interpolate> component only accept simple string values. Received: React element"`,
|
||||
);
|
||||
expect(() =>
|
||||
// @ts-expect-error: test
|
||||
renderer.create(<Interpolate>{null}</Interpolate>),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"The Docusaurus <Interpolate> component only accept simple string values. Received: object"`,
|
||||
|
|
|
@ -28,7 +28,7 @@ describe('useBaseUrl', () => {
|
|||
baseUrl: '/',
|
||||
url: 'https://docusaurus.io',
|
||||
},
|
||||
});
|
||||
} as DocusaurusContext);
|
||||
|
||||
expect(mockUseBaseUrl('hello')).toBe('/hello');
|
||||
expect(mockUseBaseUrl('/hello')).toBe('/hello');
|
||||
|
@ -56,7 +56,7 @@ describe('useBaseUrl', () => {
|
|||
baseUrl: '/docusaurus/',
|
||||
url: 'https://docusaurus.io',
|
||||
},
|
||||
});
|
||||
} as DocusaurusContext);
|
||||
|
||||
expect(mockUseBaseUrl('')).toBe('');
|
||||
expect(mockUseBaseUrl('hello')).toBe('/docusaurus/hello');
|
||||
|
@ -97,7 +97,7 @@ describe('useBaseUrlUtils().withBaseUrl()', () => {
|
|||
baseUrl: '/',
|
||||
url: 'https://docusaurus.io',
|
||||
},
|
||||
});
|
||||
} as DocusaurusContext);
|
||||
|
||||
expect(withBaseUrl('hello')).toBe('/hello');
|
||||
expect(withBaseUrl('/hello')).toBe('/hello');
|
||||
|
@ -125,7 +125,7 @@ describe('useBaseUrlUtils().withBaseUrl()', () => {
|
|||
baseUrl: '/docusaurus/',
|
||||
url: 'https://docusaurus.io',
|
||||
},
|
||||
});
|
||||
} as DocusaurusContext);
|
||||
|
||||
expect(withBaseUrl('hello')).toBe('/docusaurus/hello');
|
||||
expect(withBaseUrl('/hello')).toBe('/docusaurus/hello');
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
import path from 'path';
|
||||
import {loadPlugins} from '..';
|
||||
import type {Props} from '@docusaurus/types';
|
||||
|
||||
describe('loadPlugins', () => {
|
||||
it('loads plugins', async () => {
|
||||
|
@ -16,7 +17,6 @@ describe('loadPlugins', () => {
|
|||
siteDir,
|
||||
generatedFilesDir: path.join(siteDir, '.docusaurus'),
|
||||
outDir: path.join(siteDir, 'build'),
|
||||
// @ts-expect-error: good enough
|
||||
siteConfig: {
|
||||
baseUrl: '/',
|
||||
trailingSlash: true,
|
||||
|
@ -51,7 +51,7 @@ describe('loadPlugins', () => {
|
|||
],
|
||||
},
|
||||
siteConfigPath: path.join(siteDir, 'docusaurus.config.js'),
|
||||
}),
|
||||
} as unknown as Props),
|
||||
).resolves.toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -24,14 +24,14 @@ describe('initPlugins', () => {
|
|||
expect(context.siteConfig.plugins).toHaveLength(4);
|
||||
expect(plugins).toHaveLength(8);
|
||||
|
||||
expect(plugins[0].name).toBe('preset-plugin1');
|
||||
expect(plugins[1].name).toBe('preset-plugin2');
|
||||
expect(plugins[2].name).toBe('preset-theme1');
|
||||
expect(plugins[3].name).toBe('preset-theme2');
|
||||
expect(plugins[4].name).toBe('first-plugin');
|
||||
expect(plugins[5].name).toBe('second-plugin');
|
||||
expect(plugins[6].name).toBe('third-plugin');
|
||||
expect(plugins[7].name).toBe('fourth-plugin');
|
||||
expect(plugins[0]!.name).toBe('preset-plugin1');
|
||||
expect(plugins[1]!.name).toBe('preset-plugin2');
|
||||
expect(plugins[2]!.name).toBe('preset-theme1');
|
||||
expect(plugins[3]!.name).toBe('preset-theme2');
|
||||
expect(plugins[4]!.name).toBe('first-plugin');
|
||||
expect(plugins[5]!.name).toBe('second-plugin');
|
||||
expect(plugins[6]!.name).toBe('third-plugin');
|
||||
expect(plugins[7]!.name).toBe('fourth-plugin');
|
||||
expect(context.siteConfig.themeConfig).toEqual({a: 1});
|
||||
});
|
||||
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
import {ensureUniquePluginInstanceIds} from '../pluginIds';
|
||||
import type {InitializedPlugin} from '@docusaurus/types';
|
||||
|
||||
function createTestPlugin(name: string, id?: string): InitializedPlugin {
|
||||
// @ts-expect-error: good enough for tests
|
||||
function createTestPlugin(name: string, id?: string) {
|
||||
return {
|
||||
name,
|
||||
options: {id},
|
||||
};
|
||||
options: {id: id ?? 'default'},
|
||||
} as InitializedPlugin;
|
||||
}
|
||||
|
||||
describe('ensureUniquePluginInstanceIds', () => {
|
||||
|
|
|
@ -62,7 +62,7 @@ describe('loadPresets', () => {
|
|||
[path.join(__dirname, '__fixtures__/presets/preset-plugins.js')],
|
||||
],
|
||||
},
|
||||
} as Partial<LoadContext>;
|
||||
} as unknown as LoadContext;
|
||||
const presets = await loadPresets(context);
|
||||
expect(presets).toMatchSnapshot();
|
||||
});
|
||||
|
@ -78,7 +78,7 @@ describe('loadPresets', () => {
|
|||
],
|
||||
],
|
||||
},
|
||||
} as Partial<LoadContext>;
|
||||
} as unknown as LoadContext;
|
||||
const presets = await loadPresets(context);
|
||||
expect(presets).toMatchSnapshot();
|
||||
});
|
||||
|
@ -98,7 +98,7 @@ describe('loadPresets', () => {
|
|||
],
|
||||
],
|
||||
},
|
||||
} as Partial<LoadContext>;
|
||||
} as unknown as LoadContext;
|
||||
const presets = await loadPresets(context);
|
||||
expect(presets).toMatchSnapshot();
|
||||
});
|
||||
|
|
|
@ -20,6 +20,7 @@ import {
|
|||
} from '../translations';
|
||||
import type {
|
||||
InitializedPlugin,
|
||||
LoadedPlugin,
|
||||
TranslationFile,
|
||||
TranslationFileContent,
|
||||
} from '@docusaurus/types';
|
||||
|
@ -399,7 +400,7 @@ describe('writePluginTranslations', () => {
|
|||
options: {
|
||||
id: 'my-plugin-id',
|
||||
},
|
||||
},
|
||||
} as LoadedPlugin,
|
||||
|
||||
options: {},
|
||||
}),
|
||||
|
@ -426,11 +427,10 @@ describe('localizePluginTranslationFile', () => {
|
|||
siteDir,
|
||||
locale: 'fr',
|
||||
translationFile,
|
||||
// @ts-expect-error: enough for this test
|
||||
plugin: {
|
||||
name: 'my-plugin-name',
|
||||
options: {},
|
||||
},
|
||||
} as LoadedPlugin,
|
||||
});
|
||||
|
||||
expect(localizedTranslationFile).toEqual(translationFile);
|
||||
|
@ -466,11 +466,10 @@ describe('localizePluginTranslationFile', () => {
|
|||
siteDir,
|
||||
locale: 'fr',
|
||||
translationFile,
|
||||
// @ts-expect-error: enough for this test
|
||||
plugin: {
|
||||
name: 'my-plugin-name',
|
||||
options: {},
|
||||
},
|
||||
} as LoadedPlugin,
|
||||
});
|
||||
|
||||
expect(localizedTranslationFile).toEqual({
|
||||
|
@ -521,25 +520,30 @@ describe('readCodeTranslationFileContent', () => {
|
|||
|
||||
it('fails for invalid translation file content', async () => {
|
||||
await expect(() =>
|
||||
// @ts-expect-error: test
|
||||
testReadTranslation('HEY'),
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`""value" must be of type object"`,
|
||||
);
|
||||
await expect(() =>
|
||||
// @ts-expect-error: test
|
||||
testReadTranslation(42),
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`""value" must be of type object"`,
|
||||
);
|
||||
await expect(() =>
|
||||
// @ts-expect-error: test
|
||||
testReadTranslation({key: {description: 'no message'}}),
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(`""key.message" is required"`);
|
||||
await expect(() =>
|
||||
// @ts-expect-error: test
|
||||
testReadTranslation({key: {message: 42}}),
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`""key.message" must be a string"`,
|
||||
);
|
||||
await expect(() =>
|
||||
testReadTranslation({
|
||||
// @ts-expect-error: test
|
||||
key: {message: 'Message', description: 42},
|
||||
}),
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
|
|
|
@ -15,7 +15,7 @@ import {
|
|||
extractSiteSourceCodeTranslations,
|
||||
} from '../translationsExtractor';
|
||||
import {getBabelOptions} from '../../../webpack/utils';
|
||||
import type {InitializedPlugin} from '@docusaurus/types';
|
||||
import type {InitializedPlugin, LoadedPlugin} from '@docusaurus/types';
|
||||
|
||||
const TestBabelOptions = getBabelOptions({
|
||||
isServer: true,
|
||||
|
@ -693,7 +693,7 @@ export default function MyComponent(props: Props) {
|
|||
plugin1,
|
||||
plugin2,
|
||||
{name: 'dummy', options: {}, version: {type: 'synthetic'}} as const,
|
||||
];
|
||||
] as LoadedPlugin[];
|
||||
const translations = await extractSiteSourceCodeTranslations(
|
||||
siteDir,
|
||||
plugins,
|
||||
|
|
|
@ -63,7 +63,7 @@ describe('babel transpilation exclude logic', () => {
|
|||
});
|
||||
|
||||
describe('base webpack config', () => {
|
||||
const props: Props = {
|
||||
const props = {
|
||||
outDir: '',
|
||||
siteDir: path.resolve(__dirname, '__fixtures__', 'base_test_site'),
|
||||
siteConfig: {staticDirectories: ['static']},
|
||||
|
@ -98,7 +98,7 @@ describe('base webpack config', () => {
|
|||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
} as Props;
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
|
|
|
@ -15,7 +15,11 @@ describe('webpack production config', () => {
|
|||
it('simple', async () => {
|
||||
jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||
const props = await loadSetup('simple');
|
||||
const config = await createServerConfig({props});
|
||||
const config = await createServerConfig({
|
||||
props,
|
||||
onHeadTagsCollected: () => {},
|
||||
onLinksCollected: () => {},
|
||||
});
|
||||
const errors = webpack.validate(config);
|
||||
expect(errors).toBeUndefined();
|
||||
});
|
||||
|
@ -23,7 +27,11 @@ describe('webpack production config', () => {
|
|||
it('custom', async () => {
|
||||
jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||
const props = await loadSetup('custom');
|
||||
const config = await createServerConfig({props});
|
||||
const config = await createServerConfig({
|
||||
props,
|
||||
onHeadTagsCollected: () => {},
|
||||
onLinksCollected: () => {},
|
||||
});
|
||||
const errors = webpack.validate(config);
|
||||
expect(errors).toBeUndefined();
|
||||
});
|
||||
|
|
|
@ -60,6 +60,7 @@ describe('extending generated webpack config', () => {
|
|||
},
|
||||
};
|
||||
|
||||
// @ts-expect-error: Testing an edge-case that we did not write types for
|
||||
const configureWebpack: Plugin['configureWebpack'] = (
|
||||
generatedConfig,
|
||||
isServer,
|
||||
|
@ -125,14 +126,16 @@ describe('extending generated webpack config', () => {
|
|||
},
|
||||
};
|
||||
|
||||
const createConfigureWebpack: (mergeStrategy?: {
|
||||
[key: string]: 'prepend' | 'append';
|
||||
}) => Plugin['configureWebpack'] = (mergeStrategy) => () => ({
|
||||
module: {
|
||||
rules: [{use: 'zzz'}],
|
||||
},
|
||||
mergeStrategy,
|
||||
});
|
||||
const createConfigureWebpack =
|
||||
(mergeStrategy?: {
|
||||
[key: string]: 'prepend' | 'append';
|
||||
}): Plugin['configureWebpack'] =>
|
||||
() => ({
|
||||
module: {
|
||||
rules: [{use: 'zzz'}],
|
||||
},
|
||||
mergeStrategy,
|
||||
});
|
||||
|
||||
const defaultStrategyMergeConfig = applyConfigureWebpack(
|
||||
createConfigureWebpack(),
|
||||
|
|
|
@ -8,12 +8,7 @@
|
|||
import rule from '../no-untranslated-text';
|
||||
import {getCommonValidTests, RuleTester} from './testUtils';
|
||||
|
||||
const errorsJSX = [
|
||||
{messageId: 'translateChildren', type: 'JSXElement'},
|
||||
] as const;
|
||||
const errorsJSXFragment = [
|
||||
{messageId: 'translateChildren', type: 'JSXFragment'},
|
||||
];
|
||||
const errorsJSX = [{messageId: 'translateChildren'}] as const;
|
||||
|
||||
const ruleTester = new RuleTester({
|
||||
parser: '@typescript-eslint/parser',
|
||||
|
@ -110,7 +105,7 @@ ruleTester.run('no-untranslated-text', rule, {
|
|||
},
|
||||
{
|
||||
code: '<>text</>',
|
||||
errors: errorsJSXFragment,
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: '<Component>· — ×</Component>',
|
||||
|
|
|
@ -8,12 +8,8 @@
|
|||
import rule from '../string-literal-i18n-messages';
|
||||
import {getCommonValidTests, RuleTester} from './testUtils';
|
||||
|
||||
const errorsJSX = [
|
||||
{messageId: 'translateChildren', type: 'JSXElement'},
|
||||
] as const;
|
||||
const errorsFunc = [
|
||||
{messageId: 'translateArg', type: 'CallExpression'},
|
||||
] as const;
|
||||
const errorsJSX = [{messageId: 'translateChildren'}] as const;
|
||||
const errorsFunc = [{messageId: 'translateArg'}] as const;
|
||||
|
||||
const ruleTester = new RuleTester({
|
||||
parser: '@typescript-eslint/parser',
|
||||
|
|
Loading…
Add table
Reference in a new issue