test: fix ALL type errors in tests (#7487)

This commit is contained in:
Joshua Chen 2022-05-25 14:01:10 +08:00 committed by GitHub
parent e2e40b8f5f
commit d50fe3b670
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 407 additions and 184 deletions

View file

@ -27,10 +27,6 @@ describe('DefaultSidebarItemsGenerator', () => {
contentPath: 'docs',
},
docs: [],
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
categoriesMetadata: {},
...params,
});
@ -69,6 +65,8 @@ describe('DefaultSidebarItemsGenerator', () => {
frontMatter: {
sidebar_label: 'doc1 sidebar label',
},
title: '',
unversionedId: 'doc1',
},
{
id: 'doc2',
@ -76,6 +74,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '.',
sidebarPosition: 3,
frontMatter: {},
title: '',
unversionedId: 'doc2',
},
{
id: 'doc3',
@ -83,6 +83,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '.',
sidebarPosition: 1,
frontMatter: {},
title: '',
unversionedId: 'doc3',
},
{
id: 'doc4',
@ -90,6 +92,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '.',
sidebarPosition: 1.5,
frontMatter: {},
title: '',
unversionedId: 'doc4',
},
{
id: 'doc5',
@ -97,12 +101,12 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '.',
sidebarPosition: undefined,
frontMatter: {},
title: '',
unversionedId: 'doc5',
},
],
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
isCategoryIndex: () => false,
categoriesMetadata: {},
});
expect(sidebarSlice).toMatchSnapshot();
@ -144,6 +148,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '.',
sidebarPosition: 0,
frontMatter: {},
title: '',
unversionedId: 'intro',
},
{
id: 'tutorials-index',
@ -151,6 +157,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '01-Tutorials',
sidebarPosition: 2,
frontMatter: {},
title: 'Tutorials',
unversionedId: 'tutorials-index',
},
{
id: 'tutorial2',
@ -158,6 +166,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '01-Tutorials',
sidebarPosition: 2,
frontMatter: {},
title: '',
unversionedId: 'tutorial2',
},
{
id: 'tutorial1',
@ -165,12 +175,16 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '01-Tutorials',
sidebarPosition: 1,
frontMatter: {},
title: '',
unversionedId: 'tutorial1',
},
{
id: 'guides-index',
source: '@site/docs/02-Guides/02-Guides.md', // TODO should we allow to just use "Guides.md" to have an index?
sourceDirName: '02-Guides',
frontMatter: {},
title: 'Guides',
unversionedId: 'guides-index',
},
{
id: 'guide2',
@ -178,6 +192,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '02-Guides',
sidebarPosition: 2,
frontMatter: {},
title: '',
unversionedId: 'guide2',
},
{
id: 'guide1',
@ -187,6 +203,8 @@ describe('DefaultSidebarItemsGenerator', () => {
frontMatter: {
sidebar_class_name: 'foo',
},
title: '',
unversionedId: 'guide1',
},
{
id: 'nested-guide',
@ -194,6 +212,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '02-Guides/01-SubGuides',
sidebarPosition: undefined,
frontMatter: {},
title: '',
unversionedId: 'nested-guide',
},
{
id: 'end',
@ -201,12 +221,10 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '.',
sidebarPosition: 3,
frontMatter: {},
title: '',
unversionedId: 'end',
},
],
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
});
expect(sidebarSlice).toMatchSnapshot();
@ -249,6 +267,7 @@ describe('DefaultSidebarItemsGenerator', () => {
title: 'Subsubsubfolder category label',
sidebarPosition: undefined,
frontMatter: {},
unversionedId: 'doc1',
},
{
id: 'doc2',
@ -256,6 +275,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: 'subfolder',
sidebarPosition: undefined,
frontMatter: {},
title: '',
unversionedId: 'doc2',
},
{
id: 'doc3',
@ -263,6 +284,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '.',
sidebarPosition: undefined,
frontMatter: {},
title: '',
unversionedId: 'doc2',
},
{
id: 'doc4',
@ -270,6 +293,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: 'subfolder/subsubfolder',
sidebarPosition: undefined,
frontMatter: {},
title: '',
unversionedId: 'doc4',
},
{
id: 'doc5',
@ -277,6 +302,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: 'subfolder/subsubfolder/subsubsubfolder',
sidebarPosition: undefined,
frontMatter: {},
title: '',
unversionedId: 'doc5',
},
{
id: 'doc6',
@ -284,6 +311,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: 'subfolder/subsubfolder/subsubsubfolder2',
sidebarPosition: undefined,
frontMatter: {},
title: '',
unversionedId: 'doc6',
},
{
id: 'doc7',
@ -291,6 +320,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: 'subfolder/subsubfolder/subsubsubfolder3',
sidebarPosition: 2,
frontMatter: {},
title: '',
unversionedId: 'doc7',
},
{
id: 'doc8',
@ -298,12 +329,10 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: 'subfolder/subsubfolder/subsubsubfolder3',
sidebarPosition: 1,
frontMatter: {},
title: '',
unversionedId: 'doc8',
},
],
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
});
expect(sidebarSlice).toMatchSnapshot();
@ -339,42 +368,51 @@ describe('DefaultSidebarItemsGenerator', () => {
source: '@site/docs/Category/index.md',
sourceDirName: 'Category',
frontMatter: {},
title: '',
unversionedId: 'parent/doc1',
},
{
id: 'parent/doc2',
source: '@site/docs/Category/doc2.md',
sourceDirName: 'Category',
frontMatter: {},
title: '',
unversionedId: 'parent/doc2',
},
{
id: 'parent/doc3',
source: '@site/docs/Category/doc3.md',
sourceDirName: 'Category',
frontMatter: {},
title: '',
unversionedId: 'parent/doc3',
},
{
id: 'parent/doc4',
source: '@site/docs/Category2/doc1.md',
sourceDirName: 'Category2',
frontMatter: {},
title: '',
unversionedId: 'parent/doc4',
},
{
id: 'parent/doc5',
source: '@site/docs/Category2/index.md',
sourceDirName: 'Category2',
frontMatter: {},
title: '',
unversionedId: 'parent/doc5',
},
{
id: 'parent/doc6',
source: '@site/docs/Category2/doc3.md',
sourceDirName: 'Category2',
frontMatter: {},
title: '',
unversionedId: 'parent/doc6',
},
],
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
isCategoryIndex: () => false,
});
expect(sidebarSlice).toMatchSnapshot();
@ -387,7 +425,7 @@ describe('DefaultSidebarItemsGenerator', () => {
return (
fileName.replace(
`${DefaultNumberPrefixParser(
directories[0],
directories[0]!,
).filename.toLowerCase()}-`,
'',
) === 'index'
@ -409,6 +447,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '.',
sidebarPosition: 0,
frontMatter: {},
title: '',
unversionedId: 'intro',
},
{
id: 'tutorials-index',
@ -416,6 +456,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '01-Tutorials',
sidebarPosition: 2,
frontMatter: {},
title: 'Tutorials',
unversionedId: 'tutorials-index',
},
{
id: 'tutorial2',
@ -423,6 +465,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '01-Tutorials',
sidebarPosition: 2,
frontMatter: {},
title: '',
unversionedId: 'tutorial2',
},
{
id: 'tutorial1',
@ -430,12 +474,16 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '01-Tutorials',
sidebarPosition: 1,
frontMatter: {},
title: '',
unversionedId: 'tutorial1',
},
{
id: 'not-guides-index',
source: '@site/docs/02-Guides/README.md',
sourceDirName: '02-Guides',
frontMatter: {},
title: 'Guides',
unversionedId: 'not-guides-index',
},
{
id: 'guide2',
@ -443,6 +491,8 @@ describe('DefaultSidebarItemsGenerator', () => {
sourceDirName: '02-Guides',
sidebarPosition: 2,
frontMatter: {},
title: 'guide2',
unversionedId: 'guide2',
},
{
id: 'guide1',
@ -452,12 +502,10 @@ describe('DefaultSidebarItemsGenerator', () => {
frontMatter: {
sidebar_class_name: 'foo',
},
title: '',
unversionedId: 'guide1',
},
],
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
});
expect(sidebarSlice).toMatchSnapshot();
@ -491,12 +539,9 @@ describe('DefaultSidebarItemsGenerator', () => {
source: '@site/docs/category/intro.md',
sourceDirName: 'category',
frontMatter: {},
title: '',
},
],
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
});
expect(() => generateSidebar()).toThrowErrorMatchingInlineSnapshot(`

View file

@ -11,7 +11,10 @@ import {createSlugger} from '@docusaurus/utils';
import {loadSidebars, DisabledSidebars} from '../index';
import {DefaultSidebarItemsGenerator} from '../generator';
import type {SidebarProcessorParams} from '../types';
import type {DocMetadata} from '@docusaurus/plugin-content-docs';
import type {
DocMetadata,
VersionMetadata,
} from '@docusaurus/plugin-content-docs';
describe('loadSidebars', () => {
const fixtureDir = path.join(__dirname, '__fixtures__', 'sidebars');
@ -25,14 +28,14 @@ describe('loadSidebars', () => {
id: 'bar',
frontMatter: {},
},
],
] as DocMetadata[],
drafts: [],
version: {
path: 'version',
contentPath: path.join(fixtureDir, 'docs'),
contentPathLocalized: path.join(fixtureDir, 'docs'),
},
categoryLabelSlugger: null,
} as VersionMetadata,
categoryLabelSlugger: {slug: (v) => v},
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: true},
};
it('sidebars with known sidebar item type', async () => {
@ -137,7 +140,7 @@ describe('loadSidebars', () => {
sourceDirName: 'tutorials/tutorial-basics',
frontMatter: {},
},
],
] as DocMetadata[],
});
expect(result).toMatchSnapshot();
});
@ -146,7 +149,7 @@ describe('loadSidebars', () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-drafts.json');
const result = await loadSidebars(sidebarPath, {
...params,
drafts: [{id: 'draft1'}, {id: 'draft2'}, {id: 'draft3'}],
drafts: [{id: 'draft1'}, {id: 'draft2'}, {id: 'draft3'}] as DocMetadata[],
categoryLabelSlugger: createSlugger(),
});
expect(result).toMatchSnapshot();
@ -169,7 +172,7 @@ describe('loadSidebars', () => {
version: {
contentPath: path.join(fixtureDir, 'invalid-docs'),
contentPathLocalized: path.join(fixtureDir, 'invalid-docs'),
},
} as VersionMetadata,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`""foo" is not allowed"`);
expect(consoleWarnMock).toBeCalledWith(

View file

@ -41,7 +41,9 @@ describe('normalization', () => {
expect(() =>
normalizeSidebars({
sidebar: {
// @ts-expect-error: test
Category: {type: 'autogenerated', dirName: 'foo'},
// @ts-expect-error: test
Category2: {type: 'autogenerated', dirName: 'bar'},
},
}),
@ -55,6 +57,7 @@ describe('normalization', () => {
'foo',
{
Category: {
// @ts-expect-error: test
type: 'category',
items: ['bar', 'baz'],
},
@ -70,6 +73,7 @@ describe('normalization', () => {
sidebar: [
'foo',
{
// @ts-expect-error: test
Category: 'bar',
},
],
@ -80,6 +84,7 @@ describe('normalization', () => {
expect(() =>
normalizeSidebars({
// @ts-expect-error: test
sidebar: 'item',
}),
).toThrowErrorMatchingInlineSnapshot(

View file

@ -5,7 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/
import {postProcessSidebars} from '../postProcessor';
import {
postProcessSidebars,
type SidebarPostProcessorParams,
} from '../postProcessor';
describe('postProcess', () => {
it('transforms category without subitems', () => {
@ -36,7 +39,7 @@ describe('postProcess', () => {
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: true},
version: {path: 'version'},
drafts: [],
},
} as unknown as SidebarPostProcessorParams,
);
expect(processedSidebar).toMatchSnapshot();
@ -56,7 +59,7 @@ describe('postProcess', () => {
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: true},
version: {path: 'version'},
drafts: [],
},
} as unknown as SidebarPostProcessorParams,
);
}).toThrowErrorMatchingInlineSnapshot(
`"Sidebar category Bad category has neither any subitem nor a link. This makes this item not able to link to anything."`,
@ -82,7 +85,7 @@ describe('postProcess', () => {
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: true},
version: {path: 'version'},
drafts: [],
},
} as unknown as SidebarPostProcessorParams,
),
).toMatchSnapshot();
@ -103,7 +106,7 @@ describe('postProcess', () => {
sidebarOptions: {sidebarCollapsed: false, sidebarCollapsible: false},
version: {path: 'version'},
drafts: [],
},
} as unknown as SidebarPostProcessorParams,
),
).toMatchSnapshot();
@ -123,7 +126,7 @@ describe('postProcess', () => {
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: false},
version: {path: 'version'},
drafts: [],
},
} as unknown as SidebarPostProcessorParams,
),
).toMatchSnapshot();
});
@ -153,7 +156,7 @@ describe('postProcess', () => {
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: true},
version: {path: 'version'},
drafts: [{id: 'foo', unversionedId: 'foo'}],
},
} as unknown as SidebarPostProcessorParams,
),
).toMatchSnapshot();
});

View file

@ -12,7 +12,7 @@ import {DefaultSidebarItemsGenerator} from '../generator';
import {DefaultNumberPrefixParser} from '../../numberPrefix';
import {isCategoryIndex} from '../../docs';
import type {
SidebarItem,
Sidebar,
SidebarItemsGenerator,
NormalizedSidebar,
NormalizedSidebars,
@ -24,12 +24,12 @@ import type {VersionMetadata} from '@docusaurus/plugin-content-docs';
describe('processSidebars', () => {
function createStaticSidebarItemGenerator(
sidebarSlice: SidebarItem[],
sidebarSlice: NormalizedSidebar,
): SidebarItemsGenerator {
return jest.fn(() => sidebarSlice);
}
const StaticGeneratedSidebarSlice: NormalizedSidebar = [
const StaticGeneratedSidebarSlice: Sidebar = [
{type: 'doc', id: 'doc-generated-id-1'},
{type: 'doc', id: 'doc-generated-id-2'},
];
@ -215,7 +215,7 @@ describe('processSidebars', () => {
},
];
const unprocessedSidebars = {
const unprocessedSidebars: NormalizedSidebars = {
someSidebar: [{type: 'autogenerated', dirName: 'dir2'}],
};

View file

@ -18,7 +18,7 @@ import type {
SidebarItemCategoryLink,
} from './types';
type SidebarPostProcessorParams = SidebarProcessorParams & {
export type SidebarPostProcessorParams = SidebarProcessorParams & {
draftIds: Set<string>;
};