feat: docs plugin options sidebarCollapsible + sidebarCollapsed (#5203)

* Add prop

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Add `collapsible` option to sidebar item

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Add eslint-ignore

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Move new page

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Allow in autogenerated

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Fix tests

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Move config options to plugin-docs

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Make non-collapsible items always expanded

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* docs versioning cli should receive a single options object

* Update cli.test.ts

* revert validateCategoryMetadataFile change

* remove theme usage of themeConfig.sidebarCollapsible

* better handling of sidebar item category inconsistencies + add warning message

* Update snapshot

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Handle plugin option inconsistencies

* improve doc for new sidebarCollapsible doc options

* remove warning in fixSidebarItemInconsistencies as it will be annoyed for versioned sites, as "collapsed" is already persisted in sidebar json files

Co-authored-by: slorber <lorber.sebastien@gmail.com>
This commit is contained in:
Joshua Chen 2021-07-23 20:24:36 +08:00 committed by GitHub
parent b38c35a36d
commit 24156efcfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 487 additions and 109 deletions

View file

@ -5,9 +5,11 @@ Object {
"version-1.0.0/docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "version-1.0.0/foo/bar",
@ -36,6 +38,7 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "version-1.0.0/hello",
@ -54,6 +57,7 @@ Object {
"version-2.0.0/docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "version-2.0.0/foo/bar",
@ -65,6 +69,7 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "version-2.0.0/hello",

View file

@ -27,9 +27,11 @@ Object {
"docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "foo/bar",
@ -58,6 +60,7 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "hello",
@ -410,12 +413,12 @@ Object {
\\"docsSidebars\\": {
\\"docs\\": [
{
\\"collapsed\\": true,
\\"type\\": \\"category\\",
\\"collapsed\\": true,
\\"collapsible\\": true,
\\"label\\": \\"Test\\",
\\"items\\": [
{
\\"collapsed\\": true,
\\"type\\": \\"category\\",
\\"label\\": \\"foo\\",
\\"items\\": [
@ -429,7 +432,9 @@ Object {
\\"label\\": \\"baz\\",
\\"href\\": \\"/docs/foo/bazSlug.html\\"
}
]
],
\\"collapsible\\": true,
\\"collapsed\\": true
},
{
\\"type\\": \\"link\\",
@ -444,8 +449,9 @@ Object {
]
},
{
\\"collapsed\\": true,
\\"type\\": \\"category\\",
\\"collapsed\\": true,
\\"collapsible\\": true,
\\"label\\": \\"Guides\\",
\\"items\\": [
{
@ -806,6 +812,10 @@ Object {
"type": "autogenerated",
},
"numberPrefixParser": [Function],
"options": Object {
"sidebarCollapsed": true,
"sidebarCollapsible": true,
},
"version": Object {
"contentPath": "docs",
"versionName": "current",
@ -992,6 +1002,7 @@ Object {
"version-1.0.0/docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "version-1.0.0/foo/bar",
@ -1007,6 +1018,7 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "version-1.0.0/hello",
@ -1025,6 +1037,7 @@ Object {
"version-1.0.1/docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "version-1.0.1/foo/bar",
@ -1036,6 +1049,7 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "version-1.0.1/hello",
@ -1054,6 +1068,7 @@ Object {
"docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "foo/bar",
@ -1065,6 +1080,7 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "hello",
@ -1404,8 +1420,9 @@ Object {
\\"docsSidebars\\": {
\\"version-1.0.0/docs\\": [
{
\\"collapsed\\": true,
\\"type\\": \\"category\\",
\\"collapsed\\": true,
\\"collapsible\\": true,
\\"label\\": \\"Test\\",
\\"items\\": [
{
@ -1421,8 +1438,9 @@ Object {
]
},
{
\\"collapsed\\": true,
\\"type\\": \\"category\\",
\\"collapsed\\": true,
\\"collapsible\\": true,
\\"label\\": \\"Guides\\",
\\"items\\": [
{
@ -1444,8 +1462,9 @@ Object {
\\"docsSidebars\\": {
\\"version-1.0.1/docs\\": [
{
\\"collapsed\\": true,
\\"type\\": \\"category\\",
\\"collapsed\\": true,
\\"collapsible\\": true,
\\"label\\": \\"Test\\",
\\"items\\": [
{
@ -1456,8 +1475,9 @@ Object {
]
},
{
\\"collapsed\\": true,
\\"type\\": \\"category\\",
\\"collapsed\\": true,
\\"collapsible\\": true,
\\"label\\": \\"Guides\\",
\\"items\\": [
{
@ -1479,8 +1499,9 @@ Object {
\\"docsSidebars\\": {
\\"docs\\": [
{
\\"collapsed\\": true,
\\"type\\": \\"category\\",
\\"collapsed\\": true,
\\"collapsible\\": true,
\\"label\\": \\"Test\\",
\\"items\\": [
{
@ -1491,8 +1512,9 @@ Object {
]
},
{
\\"collapsed\\": true,
\\"type\\": \\"category\\",
\\"collapsed\\": true,
\\"collapsible\\": true,
\\"label\\": \\"Guides\\",
\\"items\\": [
{
@ -1514,8 +1536,9 @@ Object {
\\"docsSidebars\\": {
\\"version-1.0.1/docs\\": [
{
\\"collapsed\\": true,
\\"type\\": \\"category\\",
\\"collapsed\\": true,
\\"collapsible\\": true,
\\"label\\": \\"Test\\",
\\"items\\": [
{
@ -1888,6 +1911,7 @@ Object {
"version-1.0.1/docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "version-withSlugs/rootAbsoluteSlug",

View file

@ -5,6 +5,7 @@ Object {
"docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"href": "https://github.com",
@ -24,9 +25,11 @@ Object {
"docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"collapsed": false,
"collapsible": true,
"items": Array [
Object {
"id": "doc1",
@ -42,9 +45,11 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"collapsed": false,
"collapsible": true,
"items": Array [
Object {
"id": "doc2",
@ -67,6 +72,7 @@ Object {
"docs": Array [
Object {
"collapsed": false,
"collapsible": true,
"items": Array [
Object {
"id": "doc1",
@ -78,6 +84,7 @@ Object {
},
Object {
"collapsed": false,
"collapsible": true,
"items": Array [
Object {
"id": "doc2",
@ -96,6 +103,7 @@ Object {
"docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "a",
@ -103,9 +111,11 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "c",
@ -113,6 +123,7 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "d",
@ -120,6 +131,7 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "e",
@ -158,6 +170,7 @@ Object {
"docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "greeting",
@ -180,6 +193,7 @@ Object {
"docs": Array [
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "foo/bar",
@ -204,6 +218,7 @@ Object {
},
Object {
"collapsed": true,
"collapsible": true,
"items": Array [
Object {
"id": "hello",

View file

@ -7,7 +7,7 @@
import path from 'path';
import {cliDocsVersionCommand} from '../cli';
import {PathOptions} from '../types';
import {PathOptions, SidebarOptions} from '../types';
import fs from 'fs-extra';
import {
getVersionedDocsDirPath,
@ -21,9 +21,12 @@ const fixtureDir = path.join(__dirname, '__fixtures__');
describe('docsVersion', () => {
const simpleSiteDir = path.join(fixtureDir, 'simple-site');
const versionedSiteDir = path.join(fixtureDir, 'versioned-site');
const DEFAULT_OPTIONS: PathOptions = {
const DEFAULT_OPTIONS: PathOptions & SidebarOptions = {
path: 'docs',
sidebarPath: '',
sidebarCollapsed: true,
sidebarCollapsible: true,
};
test('no version tag provided', () => {
@ -186,17 +189,17 @@ describe('docsVersion', () => {
let versionedSidebarPath;
writeMock.mockImplementationOnce((filepath, content) => {
versionedSidebarPath = filepath;
versionedSidebar = JSON.parse(content);
versionedSidebar = JSON.parse(content as string);
});
let versionsPath;
let versions;
writeMock.mockImplementationOnce((filepath, content) => {
versionsPath = filepath;
versions = JSON.parse(content);
versions = JSON.parse(content as string);
});
const consoleMock = jest.spyOn(console, 'log').mockImplementation();
const options = {
path: 'docs',
...DEFAULT_OPTIONS,
sidebarPath: path.join(simpleSiteDir, 'sidebars.json'),
};
cliDocsVersionCommand('1.0.0', simpleSiteDir, DEFAULT_PLUGIN_ID, options);
@ -234,17 +237,17 @@ describe('docsVersion', () => {
let versionedSidebarPath;
writeMock.mockImplementationOnce((filepath, content) => {
versionedSidebarPath = filepath;
versionedSidebar = JSON.parse(content);
versionedSidebar = JSON.parse(content as string);
});
let versionsPath;
let versions;
writeMock.mockImplementationOnce((filepath, content) => {
versionsPath = filepath;
versions = JSON.parse(content);
versions = JSON.parse(content as string);
});
const consoleMock = jest.spyOn(console, 'log').mockImplementation();
const options = {
path: 'docs',
...DEFAULT_OPTIONS,
sidebarPath: path.join(versionedSiteDir, 'sidebars.json'),
};
cliDocsVersionCommand(
@ -289,16 +292,17 @@ describe('docsVersion', () => {
let versionedSidebarPath;
writeMock.mockImplementationOnce((filepath, content) => {
versionedSidebarPath = filepath;
versionedSidebar = JSON.parse(content);
versionedSidebar = JSON.parse(content as string);
});
let versionsPath;
let versions;
writeMock.mockImplementationOnce((filepath, content) => {
versionsPath = filepath;
versions = JSON.parse(content);
versions = JSON.parse(content as string);
});
const consoleMock = jest.spyOn(console, 'log').mockImplementation();
const options = {
...DEFAULT_OPTIONS,
path: 'community',
sidebarPath: path.join(versionedSiteDir, 'community_sidebars.json'),
};

View file

@ -270,6 +270,8 @@ describe('simple website', () => {
expect(mock).toHaveBeenCalledWith('1.0.0', siteDir, DEFAULT_PLUGIN_ID, {
path: 'docs',
sidebarPath,
sidebarCollapsed: true,
sidebarCollapsible: true,
});
mock.mockRestore();
});
@ -478,6 +480,8 @@ describe('versioned website', () => {
expect(mock).toHaveBeenCalledWith('2.0.0', siteDir, DEFAULT_PLUGIN_ID, {
path: routeBasePath,
sidebarPath,
sidebarCollapsed: true,
sidebarCollapsible: true,
});
mock.mockRestore();
});
@ -729,6 +733,8 @@ describe('versioned website (community)', () => {
expect(mock).toHaveBeenCalledWith('2.0.0', siteDir, pluginId, {
path: routeBasePath,
sidebarPath,
sidebarCollapsed: true,
sidebarCollapsible: true,
});
mock.mockRestore();
});
@ -907,6 +913,7 @@ describe('site with full autogenerated sidebar', () => {
type: 'category',
label: 'Guides',
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',
@ -938,6 +945,7 @@ describe('site with full autogenerated sidebar', () => {
type: 'category',
label: 'API (label from _category_.json)',
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',
@ -947,6 +955,7 @@ describe('site with full autogenerated sidebar', () => {
type: 'category',
label: 'Core APIs',
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',
@ -963,6 +972,7 @@ describe('site with full autogenerated sidebar', () => {
type: 'category',
label: 'Extension APIs (label from _category_.yml)',
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',
@ -1461,6 +1471,7 @@ describe('site with partial autogenerated sidebars', () => {
type: 'category',
label: 'Some category',
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',
@ -1650,6 +1661,7 @@ describe('site with partial autogenerated sidebars 2 (fix #4638)', () => {
type: 'category',
label: 'Core APIs',
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',
@ -1666,6 +1678,7 @@ describe('site with partial autogenerated sidebars 2 (fix #4638)', () => {
type: 'category',
label: 'Extension APIs (label from _category_.yml)', // Fix #4638
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',
@ -1782,6 +1795,7 @@ describe('site with custom sidebar items generator', () => {
type: 'category',
label: 'API (label from _category_.json)',
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',
@ -1791,6 +1805,7 @@ describe('site with custom sidebar items generator', () => {
type: 'category',
label: 'Extension APIs (label from _category_.yml)',
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',
@ -1806,6 +1821,7 @@ describe('site with custom sidebar items generator', () => {
type: 'category',
label: 'Core APIs',
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',
@ -1827,6 +1843,7 @@ describe('site with custom sidebar items generator', () => {
type: 'category',
label: 'Guides',
collapsed: true,
collapsible: true,
items: [
{
type: 'doc',

View file

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import {OptionsSchema, DEFAULT_OPTIONS} from '../options';
import {OptionsSchema, DEFAULT_OPTIONS, validateOptions} from '../options';
import {normalizePluginOptions} from '@docusaurus/utils-validation';
import {DefaultSidebarItemsGenerator} from '../sidebarItemsGenerator';
import {
@ -13,11 +13,22 @@ import {
DisabledNumberPrefixParser,
} from '../numberPrefix';
import {GlobExcludeDefault} from '@docusaurus/utils';
import {PluginOptions} from '../types';
// the type of remark/rehype plugins is function
const markdownPluginsFunctionStub = () => {};
const markdownPluginsObjectStub = {};
function testValidateOptions(options: Partial<PluginOptions>) {
return validateOptions({
options: {
...DEFAULT_OPTIONS,
...options,
},
validate: normalizePluginOptions,
});
}
describe('normalizeDocsPluginOptions', () => {
test('should return default options for undefined user options', async () => {
const {value, error} = await OptionsSchema.validate({});
@ -58,6 +69,8 @@ describe('normalizeDocsPluginOptions', () => {
label: 'world',
},
},
sidebarCollapsible: false,
sidebarCollapsed: false,
};
const {value, error} = await OptionsSchema.validate(userOptions);
expect(value).toEqual(userOptions);
@ -230,4 +243,30 @@ describe('normalizeDocsPluginOptions', () => {
`"\\"versions.current.hey\\" is not allowed"`,
);
});
test('should handle sidebarCollapsed option inconsistencies', () => {
expect(
testValidateOptions({
...DEFAULT_OPTIONS,
sidebarCollapsible: true,
sidebarCollapsed: undefined,
}).sidebarCollapsed,
).toEqual(true);
expect(
testValidateOptions({
...DEFAULT_OPTIONS,
sidebarCollapsible: false,
sidebarCollapsed: undefined,
}).sidebarCollapsed,
).toEqual(false);
expect(
testValidateOptions({
...DEFAULT_OPTIONS,
sidebarCollapsible: false,
sidebarCollapsed: true,
}).sidebarCollapsed,
).toEqual(false);
});
});

View file

@ -9,14 +9,13 @@ import {
CategoryMetadatasFile,
DefaultSidebarItemsGenerator,
} from '../sidebarItemsGenerator';
import {DefaultCategoryCollapsedValue} from '../sidebars';
import {Sidebar, SidebarItemsGenerator} from '../types';
import fs from 'fs-extra';
import {DefaultNumberPrefixParser} from '../numberPrefix';
describe('DefaultSidebarItemsGenerator', () => {
function testDefaultSidebarItemsGenerator(
options: Partial<Parameters<SidebarItemsGenerator>[0]>,
params: Partial<Parameters<SidebarItemsGenerator>[0]>,
) {
return DefaultSidebarItemsGenerator({
numberPrefixParser: DefaultNumberPrefixParser,
@ -29,7 +28,11 @@ describe('DefaultSidebarItemsGenerator', () => {
contentPath: 'docs',
},
docs: [],
...options,
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
...params,
});
}
@ -110,6 +113,10 @@ describe('DefaultSidebarItemsGenerator', () => {
frontMatter: {},
},
],
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
});
expect(sidebarSlice).toEqual([
@ -190,6 +197,10 @@ describe('DefaultSidebarItemsGenerator', () => {
frontMatter: {},
},
],
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
});
expect(sidebarSlice).toEqual([
@ -197,7 +208,8 @@ describe('DefaultSidebarItemsGenerator', () => {
{
type: 'category',
label: 'Tutorials',
collapsed: DefaultCategoryCollapsedValue,
collapsed: true,
collapsible: true,
items: [
{type: 'doc', id: 'tutorial1'},
{type: 'doc', id: 'tutorial2'},
@ -207,12 +219,14 @@ describe('DefaultSidebarItemsGenerator', () => {
type: 'category',
label: 'Guides',
collapsed: false,
collapsible: true,
items: [
{type: 'doc', id: 'guide1'},
{
type: 'category',
label: 'SubGuides (metadata file label)',
collapsed: DefaultCategoryCollapsedValue,
collapsed: true,
collapsible: true,
items: [{type: 'doc', id: 'nested-guide'}],
},
{type: 'doc', id: 'guide2'},
@ -233,6 +247,7 @@ describe('DefaultSidebarItemsGenerator', () => {
'subfolder/subsubfolder/subsubsubfolder3/_category_.json': {
position: 1,
label: 'subsubsubfolder3 (_category_.json label)',
collapsible: false,
collapsed: false,
},
});
@ -305,6 +320,10 @@ describe('DefaultSidebarItemsGenerator', () => {
frontMatter: {},
},
],
options: {
sidebarCollapsed: true,
sidebarCollapsible: true,
},
});
expect(sidebarSlice).toEqual([
@ -312,6 +331,7 @@ describe('DefaultSidebarItemsGenerator', () => {
type: 'category',
label: 'subsubsubfolder3 (_category_.json label)',
collapsed: false,
collapsible: false,
items: [
{type: 'doc', id: 'doc8'},
{type: 'doc', id: 'doc7'},
@ -321,6 +341,7 @@ describe('DefaultSidebarItemsGenerator', () => {
type: 'category',
label: 'subsubsubfolder2 (_category_.yml label)',
collapsed: true,
collapsible: true,
items: [{type: 'doc', id: 'doc6'}],
},
{type: 'doc', id: 'doc1'},
@ -329,6 +350,7 @@ describe('DefaultSidebarItemsGenerator', () => {
type: 'category',
label: 'subsubsubfolder',
collapsed: true,
collapsible: true,
items: [{type: 'doc', id: 'doc5'}],
},
] as Sidebar);

View file

@ -17,6 +17,7 @@ import {
processSidebars,
DefaultSidebars,
DisabledSidebars,
fixSidebarItemInconsistencies,
} from '../sidebars';
import {
Sidebar,
@ -24,6 +25,8 @@ import {
SidebarItemsGenerator,
Sidebars,
UnprocessedSidebars,
SidebarOptions,
SidebarItemCategory,
} from '../types';
import {DefaultSidebarItemsGenerator} from '../sidebarItemsGenerator';
@ -31,15 +34,19 @@ import {DefaultSidebarItemsGenerator} from '../sidebarItemsGenerator';
describe('loadSidebars', () => {
const fixtureDir = path.join(__dirname, '__fixtures__', 'sidebars');
const options: SidebarOptions = {
sidebarCollapsed: true,
sidebarCollapsible: true,
};
test('sidebars with known sidebar item type', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars.json');
const result = loadSidebars(sidebarPath);
const result = loadSidebars(sidebarPath, options);
expect(result).toMatchSnapshot();
});
test('sidebars with deep level of category', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-category.js');
const result = loadSidebars(sidebarPath);
const result = loadSidebars(sidebarPath, options);
expect(result).toMatchSnapshot();
});
@ -49,8 +56,8 @@ describe('loadSidebars', () => {
fixtureDir,
'sidebars-category-shorthand.js',
);
const sidebar1 = loadSidebars(sidebarPath1);
const sidebar2 = loadSidebars(sidebarPath2);
const sidebar1 = loadSidebars(sidebarPath1, options);
const sidebar2 = loadSidebars(sidebarPath2, options);
expect(sidebar1).toEqual(sidebar2);
});
@ -59,7 +66,9 @@ describe('loadSidebars', () => {
fixtureDir,
'sidebars-category-wrong-items.json',
);
expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
expect(() =>
loadSidebars(sidebarPath, options),
).toThrowErrorMatchingInlineSnapshot(
`"Error loading {\\"type\\":\\"category\\",\\"label\\":\\"Category Label\\",\\"items\\":\\"doc1\\"}: \\"items\\" must be an array."`,
);
});
@ -69,7 +78,9 @@ describe('loadSidebars', () => {
fixtureDir,
'sidebars-category-wrong-label.json',
);
expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
expect(() =>
loadSidebars(sidebarPath, options),
).toThrowErrorMatchingInlineSnapshot(
`"Error loading {\\"type\\":\\"category\\",\\"label\\":true,\\"items\\":[\\"doc1\\"]}: \\"label\\" must be a string."`,
);
});
@ -79,7 +90,9 @@ describe('loadSidebars', () => {
fixtureDir,
'sidebars-doc-id-not-string.json',
);
expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
expect(() =>
loadSidebars(sidebarPath, options),
).toThrowErrorMatchingInlineSnapshot(
`"Error loading {\\"type\\":\\"doc\\",\\"id\\":[\\"doc1\\"]}: \\"id\\" must be a string."`,
);
});
@ -89,33 +102,38 @@ describe('loadSidebars', () => {
fixtureDir,
'sidebars-first-level-not-category.js',
);
const result = loadSidebars(sidebarPath);
const result = loadSidebars(sidebarPath, options);
expect(result).toMatchSnapshot();
});
test('sidebars link', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-link.json');
const result = loadSidebars(sidebarPath);
const result = loadSidebars(sidebarPath, options);
expect(result).toMatchSnapshot();
});
test('sidebars link wrong label', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-link-wrong-label.json');
expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
expect(() =>
loadSidebars(sidebarPath, options),
).toThrowErrorMatchingInlineSnapshot(
`"Error loading {\\"type\\":\\"link\\",\\"label\\":false,\\"href\\":\\"https://github.com\\"}: \\"label\\" must be a string."`,
);
});
test('sidebars link wrong href', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-link-wrong-href.json');
expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
expect(() =>
loadSidebars(sidebarPath, options),
).toThrowErrorMatchingInlineSnapshot(
`"Error loading {\\"type\\":\\"link\\",\\"label\\":\\"GitHub\\",\\"href\\":[\\"example.com\\"]}: \\"href\\" must be a string."`,
);
});
test('sidebars with unknown sidebar item type', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-unknown-type.json');
expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(`
expect(() => loadSidebars(sidebarPath, options))
.toThrowErrorMatchingInlineSnapshot(`
"Unknown sidebar item type \\"superman\\". Sidebar item is {\\"type\\":\\"superman\\"}.
"
`);
@ -123,26 +141,28 @@ describe('loadSidebars', () => {
test('sidebars with known sidebar item type but wrong field', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-wrong-field.json');
expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
expect(() =>
loadSidebars(sidebarPath, options),
).toThrowErrorMatchingInlineSnapshot(
`"Unknown sidebar item keys: href. Item: {\\"type\\":\\"category\\",\\"label\\":\\"category\\",\\"href\\":\\"https://github.com\\"}"`,
);
});
test('unexisting path', () => {
expect(loadSidebars('badpath')).toEqual(DisabledSidebars);
expect(loadSidebars('badpath', options)).toEqual(DisabledSidebars);
});
test('undefined path', () => {
expect(loadSidebars(undefined)).toEqual(DefaultSidebars);
expect(loadSidebars(undefined, options)).toEqual(DefaultSidebars);
});
test('literal false path', () => {
expect(loadSidebars(false)).toEqual(DisabledSidebars);
expect(loadSidebars(false, options)).toEqual(DisabledSidebars);
});
test('sidebars with category.collapsed property', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-collapsed.json');
const result = loadSidebars(sidebarPath);
const result = loadSidebars(sidebarPath, options);
expect(result).toMatchSnapshot();
});
@ -151,7 +171,7 @@ describe('loadSidebars', () => {
fixtureDir,
'sidebars-collapsed-first-level.json',
);
const result = loadSidebars(sidebarPath);
const result = loadSidebars(sidebarPath, options);
expect(result).toMatchSnapshot();
});
});
@ -162,23 +182,27 @@ describe('collectSidebarDocItems', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category1',
items: [
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Subcategory 1',
items: [{type: 'doc', id: 'doc1'}],
},
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Subcategory 2',
items: [
{type: 'doc', id: 'doc2'},
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Sub sub category 1',
items: [{type: 'doc', id: 'doc3'}],
},
@ -189,6 +213,7 @@ describe('collectSidebarDocItems', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category2',
items: [
{type: 'doc', id: 'doc4'},
@ -213,23 +238,27 @@ describe('collectSidebarCategories', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category1',
items: [
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Subcategory 1',
items: [{type: 'doc', id: 'doc1'}],
},
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Subcategory 2',
items: [
{type: 'doc', id: 'doc2'},
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Sub sub category 1',
items: [{type: 'doc', id: 'doc3'}],
},
@ -240,6 +269,7 @@ describe('collectSidebarCategories', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category2',
items: [
{type: 'doc', id: 'doc4'},
@ -266,6 +296,7 @@ describe('collectSidebarLinks', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category1',
items: [
{
@ -276,6 +307,7 @@ describe('collectSidebarLinks', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Subcategory 2',
items: [
{
@ -302,11 +334,13 @@ describe('collectSidebarsDocIds', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category1',
items: [
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Subcategory 1',
items: [{type: 'doc', id: 'doc1'}],
},
@ -319,6 +353,7 @@ describe('collectSidebarsDocIds', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category2',
items: [
{type: 'doc', id: 'doc3'},
@ -345,11 +380,13 @@ describe('transformSidebarItems', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category1',
items: [
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Subcategory 1',
items: [{type: 'doc', id: 'doc1'}],
customProps: {fakeProp: false},
@ -357,12 +394,14 @@ describe('transformSidebarItems', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Subcategory 2',
items: [
{type: 'doc', id: 'doc2'},
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Sub sub category 1',
items: [
{type: 'doc', id: 'doc3', customProps: {lorem: 'ipsum'}},
@ -375,6 +414,7 @@ describe('transformSidebarItems', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category2',
items: [
{type: 'doc', id: 'doc4'},
@ -394,11 +434,13 @@ describe('transformSidebarItems', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'MODIFIED LABEL: Category1',
items: [
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'MODIFIED LABEL: Subcategory 1',
items: [{type: 'doc', id: 'doc1'}],
customProps: {fakeProp: false},
@ -406,12 +448,14 @@ describe('transformSidebarItems', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'MODIFIED LABEL: Subcategory 2',
items: [
{type: 'doc', id: 'doc2'},
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'MODIFIED LABEL: Sub sub category 1',
items: [
{type: 'doc', id: 'doc3', customProps: {lorem: 'ipsum'}},
@ -424,6 +468,7 @@ describe('transformSidebarItems', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'MODIFIED LABEL: Category2',
items: [
{type: 'doc', id: 'doc4'},
@ -463,6 +508,7 @@ describe('processSidebars', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
items: [{type: 'doc', id: 'doc2'}],
label: 'Category',
},
@ -474,6 +520,7 @@ describe('processSidebars', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
items: [{type: 'doc', id: 'doc4'}],
label: 'Category',
},
@ -491,6 +538,7 @@ describe('processSidebars', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
items: [
{type: 'doc', id: 'doc2'},
{type: 'autogenerated', dirName: 'dir1'},
@ -507,6 +555,7 @@ describe('processSidebars', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
items: [{type: 'doc', id: 'doc4'}],
label: 'Category',
},
@ -541,6 +590,7 @@ describe('processSidebars', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
items: [{type: 'doc', id: 'doc2'}, ...StaticGeneratedSidebarSlice],
label: 'Category',
},
@ -554,6 +604,7 @@ describe('processSidebars', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
items: [{type: 'doc', id: 'doc4'}],
label: 'Category',
},
@ -567,11 +618,13 @@ describe('createSidebarsUtils', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category1',
items: [
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Subcategory 1',
items: [{type: 'doc', id: 'doc1'}],
},
@ -584,6 +637,7 @@ describe('createSidebarsUtils', () => {
{
type: 'category',
collapsed: false,
collapsible: true,
label: 'Category2',
items: [
{type: 'doc', id: 'doc3'},
@ -637,3 +691,56 @@ describe('createSidebarsUtils', () => {
});
});
});
describe('fixSidebarItemInconsistencies', () => {
test('should not fix good category', () => {
const category: SidebarItemCategory = {
type: 'category',
label: 'Cat',
items: [],
collapsible: true,
collapsed: true,
};
expect(fixSidebarItemInconsistencies(category)).toEqual(category);
});
test('should fix bad category', () => {
const category: SidebarItemCategory = {
type: 'category',
label: 'Cat',
items: [],
collapsible: false,
collapsed: true, // Bad because collapsible=false
};
expect(fixSidebarItemInconsistencies(category)).toEqual({
...category,
collapsed: false,
});
});
test('should fix bad subcategory', () => {
const subCategory: SidebarItemCategory = {
type: 'category',
label: 'SubCat',
items: [],
collapsible: false,
collapsed: true, // Bad because collapsible=false
};
const category: SidebarItemCategory = {
type: 'category',
label: 'Cat',
items: [subCategory],
collapsible: true,
collapsed: true,
};
expect(fixSidebarItemInconsistencies(category)).toEqual({
...category,
items: [
{
...subCategory,
collapsed: false,
},
],
});
});
});