Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Jun 17, 2022
1 parent 398ee50 commit 5c02b12
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 18 deletions.
Empty file.
Empty file.
Empty file.
Empty file.

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 @@ -66,3 +66,12 @@ exports[`docsVersion second docs instance versioning 1`] = `
],
}
`;

exports[`docsVersion works with custom i18n paths 1`] = `
[
{
"dirName": ".",
"type": "autogenerated",
},
]
`;
Binary file not shown.

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
@@ -0,0 +1,118 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`load loads props for site with custom i18n path 1`] = `
{
"baseUrl": "/",
"codeTranslations": {},
"generatedFilesDir": "<PROJECT_ROOT>/packages/docusaurus/src/server/__tests__/__fixtures__/custom-i18n-site/.docusaurus",
"headTags": "",
"i18n": {
"currentLocale": "en",
"defaultLocale": "en",
"localeConfigs": {
"en": {
"calendar": "gregory",
"direction": "ltr",
"htmlLang": "en",
"label": "English",
"path": "en-custom",
},
"zh-Hans": {
"calendar": "gregory",
"direction": "ltr",
"htmlLang": "zh-Hans",
"label": "简体中文",
"path": "zh-Hans-custom",
},
},
"locales": [
"en",
"zh-Hans",
],
"path": "i18n",
},
"localizationDir": "<PROJECT_ROOT>/packages/docusaurus/src/server/__tests__/__fixtures__/custom-i18n-site/i18n/en-custom",
"outDir": "<PROJECT_ROOT>/packages/docusaurus/src/server/__tests__/__fixtures__/custom-i18n-site/build",
"plugins": [
{
"content": undefined,
"getClientModules": [Function],
"injectHtmlTags": [Function],
"name": "docusaurus-bootstrap-plugin",
"options": {
"id": "default",
},
"path": "<PROJECT_ROOT>/packages/docusaurus/src/server/__tests__/__fixtures__/custom-i18n-site",
"version": {
"type": "synthetic",
},
},
{
"configureWebpack": [Function],
"content": undefined,
"name": "docusaurus-mdx-fallback-plugin",
"options": {
"id": "default",
},
"path": ".",
"version": {
"type": "synthetic",
},
},
],
"postBodyTags": "",
"preBodyTags": "",
"routes": [],
"routesPaths": [
"/404.html",
],
"siteConfig": {
"baseUrl": "/",
"baseUrlIssueBanner": true,
"clientModules": [],
"customFields": {},
"i18n": {
"defaultLocale": "en",
"localeConfigs": {
"en": {
"direction": "ltr",
"path": "en-custom",
},
"zh-Hans": {
"direction": "ltr",
"path": "zh-Hans-custom",
},
},
"locales": [
"en",
"zh-Hans",
],
"path": "i18n",
},
"noIndex": false,
"onBrokenLinks": "throw",
"onBrokenMarkdownLinks": "warn",
"onDuplicateRoutes": "warn",
"plugins": [],
"presets": [],
"scripts": [],
"staticDirectories": [
"static",
],
"stylesheets": [],
"tagline": "",
"themeConfig": {},
"themes": [],
"title": "Site",
"titleDelimiter": "|",
"url": "https://example.com",
},
"siteConfigPath": "<PROJECT_ROOT>/packages/docusaurus/src/server/__tests__/__fixtures__/custom-i18n-site/docusaurus.config.js",
"siteDir": "<PROJECT_ROOT>/packages/docusaurus/src/server/__tests__/__fixtures__/custom-i18n-site",
"siteMetadata": {
"docusaurusVersion": "<CURRENT_VERSION>",
"pluginVersions": {},
"siteVersion": undefined,
},
}
`;
45 changes: 45 additions & 0 deletions packages/docusaurus/src/server/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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 path from 'path';
import {mergeWithCustomize} from 'webpack-merge';
import {loadSetup} from './testUtils';
import type {Props} from '@docusaurus/types';
import type {DeepPartial} from 'utility-types';

describe('load', () => {
it('loads props for site with custom i18n path', async () => {
const props = await loadSetup('custom-i18n-site');
expect(props).toMatchSnapshot();
const props2 = await loadSetup('custom-i18n-site', {locale: 'zh-Hans'});
expect(props2).toEqual(
mergeWithCustomize<DeepPartial<Props>>({
customizeArray(a, b, key) {
return ['routesPaths', 'plugins'].includes(key) ? b : undefined;
},
})(props, {
baseUrl: '/zh-Hans/',
i18n: {
currentLocale: 'zh-Hans',
},
localizationDir: path.join(
__dirname,
'__fixtures__/custom-i18n-site/i18n/zh-Hans-custom',
),
outDir: path.join(
__dirname,
'__fixtures__/custom-i18n-site/build/zh-Hans',
),
routesPaths: ['/zh-Hans/404.html'],
siteConfig: {
baseUrl: '/zh-Hans/',
},
plugins: props2.plugins,
}),
);
});
});
18 changes: 6 additions & 12 deletions packages/docusaurus/src/server/__tests__/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@
*/

import path from 'path';
import {load} from '../index';
import {load, type LoadContextOptions} from '../index';
import type {Props} from '@docusaurus/types';

// Helper methods to setup dummy/fake projects.
export default async function loadSetup(name: string): Promise<Props> {
export async function loadSetup(
name: string,
options?: Partial<LoadContextOptions>,
): Promise<Props> {
const fixtures = path.join(__dirname, '__fixtures__');
const simpleSite = path.join(fixtures, 'simple-site');
const customSite = path.join(fixtures, 'custom-site');

switch (name) {
case 'custom':
return load({siteDir: customSite});
case 'simple':
default:
return load({siteDir: simpleSite});
}
return load({siteDir: path.join(fixtures, name), ...options});
}
1 change: 1 addition & 0 deletions packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const LocaleConfigSchema = Joi.object({
htmlLang: Joi.string(),
direction: Joi.string().equal('ltr', 'rtl').default('ltr'),
calendar: Joi.string(),
path: Joi.string(),
});

const I18N_CONFIG_SCHEMA = Joi.object<I18nConfig>({
Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus/src/webpack/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
import webpack from 'webpack';

import createClientConfig from '../client';
import loadSetup from '../../server/__tests__/testUtils';
import {loadSetup} from '../../server/__tests__/testUtils';

describe('webpack dev config', () => {
it('simple', async () => {
const props = await loadSetup('simple');
const props = await loadSetup('simple-site');
const config = await createClientConfig(props);
const errors = webpack.validate(config);
expect(errors).toBeUndefined();
});

it('custom', async () => {
const props = await loadSetup('custom');
const props = await loadSetup('custom-site');
const config = await createClientConfig(props);
const errors = webpack.validate(config);
expect(errors).toBeUndefined();
Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus/src/webpack/__tests__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {jest} from '@jest/globals';
import webpack from 'webpack';

import createServerConfig from '../server';
import loadSetup from '../../server/__tests__/testUtils';
import {loadSetup} from '../../server/__tests__/testUtils';

describe('webpack production config', () => {
it('simple', async () => {
jest.spyOn(console, 'log').mockImplementation(() => {});
const props = await loadSetup('simple');
const props = await loadSetup('simple-site');
const config = await createServerConfig({
props,
onHeadTagsCollected: () => {},
Expand All @@ -26,7 +26,7 @@ describe('webpack production config', () => {

it('custom', async () => {
jest.spyOn(console, 'log').mockImplementation(() => {});
const props = await loadSetup('custom');
const props = await loadSetup('custom-site');
const config = await createServerConfig({
props,
onHeadTagsCollected: () => {},
Expand Down

0 comments on commit 5c02b12

Please sign in to comment.