/
options.test.ts
267 lines (243 loc) · 7.92 KB
/
options.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* 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.
*/
import {GlobExcludeDefault} from '@docusaurus/utils';
import {normalizePluginOptions} from '@docusaurus/utils-validation';
import {validateOptions, DEFAULT_OPTIONS} from '../options';
import {DefaultSidebarItemsGenerator} from '../sidebars/generator';
import {
DefaultNumberPrefixParser,
DisabledNumberPrefixParser,
} from '../numberPrefix';
import type {Options, PluginOptions} from '@docusaurus/plugin-content-docs';
import type {Validate} from '@docusaurus/types';
// The type of remark/rehype plugins can be function/object
const markdownPluginsFunctionStub = () => {};
const markdownPluginsObjectStub = {};
function testValidate(options: Options) {
return validateOptions({
validate: normalizePluginOptions as Validate<Options, PluginOptions>,
options,
});
}
const defaultOptions = {
...DEFAULT_OPTIONS,
id: 'default',
// The admonitions plugin is automatically added. Not really worth testing
remarkPlugins: expect.any(Array),
};
describe('normalizeDocsPluginOptions', () => {
it('returns default options for undefined user options', () => {
expect(testValidate({})).toEqual(defaultOptions);
});
it('accepts correctly defined user options', () => {
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.
include: ['**/*.{md,mdx}'], // Extensions to include.
exclude: GlobExcludeDefault,
sidebarPath: 'my-sidebar', // Path to sidebar configuration for showing a list of markdown pages.
sidebarItemsGenerator: DefaultSidebarItemsGenerator,
numberPrefixParser: DefaultNumberPrefixParser,
docLayoutComponent: '@theme/DocPage',
docItemComponent: '@theme/DocItem',
docTagDocListComponent: '@theme/DocTagDocListPage',
docTagsListComponent: '@theme/DocTagsListPage',
docCategoryGeneratedIndexComponent:
'@theme/DocCategoryGeneratedIndexPage',
// @ts-expect-error: it seems to work in practice?
remarkPlugins: [markdownPluginsObjectStub],
rehypePlugins: [markdownPluginsFunctionStub],
beforeDefaultRehypePlugins: [],
beforeDefaultRemarkPlugins: [],
breadcrumbs: true,
showLastUpdateTime: true,
showLastUpdateAuthor: true,
admonitions: false,
includeCurrentVersion: false,
disableVersioning: true,
editCurrentVersion: true,
editLocalizedFiles: true,
versions: {
current: {
path: 'next',
label: 'next',
},
version1: {
path: 'hello',
label: 'world',
noIndex: true,
},
},
sidebarCollapsible: false,
sidebarCollapsed: false,
};
expect(testValidate(userOptions)).toEqual({
...defaultOptions,
...userOptions,
});
});
it('accepts correctly defined remark and rehype plugin options', () => {
const userOptions: Options = {
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
rehypePlugins: [
// @ts-expect-error: it seems to work in practice
markdownPluginsObjectStub,
[markdownPluginsFunctionStub, {option1: '42'}],
],
};
expect(testValidate(userOptions)).toEqual({
...defaultOptions,
...userOptions,
});
});
it('accepts admonitions false', () => {
const admonitionsFalse: Options = {
admonitions: false,
};
expect(testValidate(admonitionsFalse)).toEqual({
...defaultOptions,
...admonitionsFalse,
});
});
it('rejects admonitions array', () => {
expect(() =>
testValidate({
// @ts-expect-error: rejected value
admonitions: [],
}),
).toThrowErrorMatchingInlineSnapshot(
`""admonitions" does not look like a valid admonitions config"`,
);
});
it('accepts numberPrefixParser function', () => {
function customNumberPrefixParser() {}
expect(
testValidate({
numberPrefixParser:
customNumberPrefixParser as unknown as Options['numberPrefixParser'],
}),
).toEqual({
...defaultOptions,
numberPrefixParser: customNumberPrefixParser,
});
});
it('accepts numberPrefixParser false', () => {
expect(testValidate({numberPrefixParser: false})).toEqual({
...defaultOptions,
numberPrefixParser: DisabledNumberPrefixParser,
});
});
it('accepts numberPrefixParser true', () => {
expect(testValidate({numberPrefixParser: true})).toEqual({
...defaultOptions,
numberPrefixParser: DefaultNumberPrefixParser,
});
});
it('rejects invalid remark plugin options', () => {
expect(() =>
testValidate({
// @ts-expect-error: test
remarkPlugins: [[{option1: '42'}, markdownPluginsFunctionStub]],
}),
).toThrowErrorMatchingInlineSnapshot(`
""remarkPlugins[0]" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require("rehype-katex"), { strict: false }]\`, or
- A simple module, like \`require("remark-math")\`"
`);
});
it('rejects invalid rehype plugin options', () => {
expect(() =>
testValidate({
rehypePlugins: [
// @ts-expect-error: test
[
markdownPluginsFunctionStub,
{option1: '42'},
markdownPluginsFunctionStub,
],
],
}),
).toThrowErrorMatchingInlineSnapshot(`
""rehypePlugins[0]" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require("rehype-katex"), { strict: false }]\`, or
- A simple module, like \`require("remark-math")\`"
`);
});
it('rejects bad path inputs', () => {
// @ts-expect-error: test
expect(() => testValidate({path: 2})).toThrowErrorMatchingInlineSnapshot(
`""path" must be a string"`,
);
});
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"`,
);
});
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"`);
});
it('rejects bad versions', () => {
expect(() =>
testValidate({
versions: {
current: {
// @ts-expect-error: test
hey: 3,
},
version1: {
path: 'hello',
label: 'world',
},
},
}),
).toThrowErrorMatchingInlineSnapshot(
`""versions.current.hey" is not allowed"`,
);
});
it('handles sidebarCollapsed option inconsistencies', () => {
expect(
testValidate({
sidebarCollapsible: true,
sidebarCollapsed: undefined,
}).sidebarCollapsed,
).toBe(true);
expect(
testValidate({
sidebarCollapsible: false,
sidebarCollapsed: undefined,
}).sidebarCollapsed,
).toBe(false);
expect(
testValidate({
sidebarCollapsible: false,
sidebarCollapsed: true,
}).sidebarCollapsed,
).toBe(false);
});
});