Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve test coverage; properly test core client APIs #6905

Merged
merged 2 commits into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ export default {
// Jest can't resolve CSS or asset imports
'^.+\\.(css|jpe?g|png|svg)$': '<rootDir>/jest/emptyModule.js',

// TODO we need to allow Jest to resolve core Webpack aliases automatically
// Using src instead of lib, so we always get fresh source
'@docusaurus/(browserContext|BrowserOnly|ComponentCreator|constants|docusaurusContext|ExecutionEnvironment|Head|Interpolate|isInternalUrl|Link|Noop|renderRoutes|router|Translate|use.*)':
'@docusaurus/core/lib/client/exports/$1',
'@docusaurus/core/src/client/exports/$1',
// Maybe point to a fixture?
'@generated/.*': '<rootDir>/jest/emptyModule.js',
// TODO use "projects" + multiple configs if we work on another theme?
'@theme/(.*)': '@docusaurus/theme-classic/src/theme/$1',
'@site/(.*)': 'website/$1',

// TODO why Jest can't figure node package entry points?
// Using src instead of lib, so we always get fresh source
'@docusaurus/plugin-content-docs/client':
'@docusaurus/plugin-content-docs/lib/client/index.js',
'@docusaurus/plugin-content-docs/src/client/index.ts',
},
globals: {
window: {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ Lorem ipsum
Some content here

## I ♥ unicode.

export const c = 1;
"
`;

Expand Down Expand Up @@ -241,5 +243,7 @@ Lorem ipsum
Some content here

## I ♥ unicode.

export const c = 1;
"
`;
3 changes: 1 addition & 2 deletions packages/docusaurus-mdx-loader/src/remark/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function toValue(node: PhrasingContent | Heading): string {
case 'link':
return stringifyContent(node);
default:
return toString(node);
}

return toString(node);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const RedirectPluginOptionValidation = Joi.object<RedirectOption>({
const isString = Joi.string().required().not(null);

const UserOptionsSchema = Joi.object<UserPluginOptions>({
id: Joi.string().optional(), // TODO remove once validation migrated to new system
id: Joi.string().optional(), // TODO remove once validation migrated to new system
fromExtensions: Joi.array().items(isString),
toExtensions: Joi.array().items(isString),
redirects: Joi.array().items(RedirectPluginOptionValidation),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`toGlobalDataVersion generates the right docs, sidebars, and metadata 1`] = `
Object {
"docs": Array [
Object {
"id": "main",
"path": "/current/main",
"sidebar": "tutorial",
},
Object {
"id": "doc",
"path": "/current/doc",
"sidebar": "tutorial",
},
Object {
"id": "/current/generated",
"path": "/current/generated",
"sidebar": "tutorial",
},
],
"isLast": true,
"label": "Label",
"mainDocId": "main",
"name": "current",
"path": "/current",
"sidebars": Object {
"another": Object {
"link": Object {
"label": "Generated",
"path": "/current/generated",
},
},
"links": Object {},
"tutorial": Object {
"link": Object {
"label": "main",
"path": "/current/main",
},
},
},
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* 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 {toGlobalDataVersion} from '../globalData';

describe('toGlobalDataVersion', () => {
it('generates the right docs, sidebars, and metadata', () => {
expect(
toGlobalDataVersion({
versionName: 'current',
versionLabel: 'Label',
isLast: true,
versionPath: '/current',
mainDocId: 'main',
docs: [
{
unversionedId: 'main',
permalink: '/current/main',
sidebar: 'tutorial',
},
{
unversionedId: 'doc',
permalink: '/current/doc',
sidebar: 'tutorial',
},
],
sidebars: {
another: [
{
type: 'category',
label: 'Generated',
link: {
type: 'generated-index',
permalink: '/current/generated',
},
items: [
{
type: 'doc',
id: 'doc',
},
],
},
],
tutorial: [
{
type: 'doc',
id: 'main',
},
{
type: 'category',
label: 'Generated',
link: {
type: 'generated-index',
permalink: '/current/generated',
},
items: [
{
type: 'doc',
id: 'doc',
},
],
},
],
links: [
{
type: 'link',
href: 'foo',
label: 'Foo',
},
{
type: 'link',
href: 'bar',
label: 'Bar',
},
],
},
categoryGeneratedIndices: [
{
title: 'Generated',
slug: '/current/generated',
permalink: '/current/generated',
sidebar: 'tutorial',
},
],
versionBanner: 'unreleased',
versionBadge: true,
versionClassName: 'current-cls',
tagsPath: '/current/tags',
contentPath: '',
contentPathLocalized: '',
sidebarFilePath: '',
routePriority: 0.5,
}),
).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ describe('getSlug', () => {
).toBe('/dir with spâce/hey $hello/my dôc');
});

it('throws for invalid routes', () => {
expect(() =>
getSlug({
baseID: 'my dôc',
source: '@site/docs/dir with spâce/hey $hello/doc.md',
sourceDirName: '/dir with spâce/hey $hello',
frontMatterSlug: '//',
}),
).toThrowErrorMatchingInlineSnapshot(`
"We couldn't compute a valid slug for document with ID \\"my dôc\\" in \\"/dir with spâce/hey $hello\\" directory.
The slug we computed looks invalid: //.
Maybe your slug front matter is incorrect or there are special characters in the file path?
By using front matter to set a custom slug, you should be able to fix this error:

---
slug: /my/customDocPath
---
"
`);
});

it('handles current dir', () => {
expect(
getSlug({baseID: 'doc', source: '@site/docs/doc.md', sourceDirName: '.'}),
Expand Down
Loading