Skip to content

Commit

Permalink
refactor/fix validateThemeConfig tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Sep 4, 2020
1 parent 1d99014 commit 0b11a4d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,15 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import merge from 'lodash/merge';

const {
validateThemeConfig,
DEFAULT_COLOR_MODE_CONFIG,
} = require('../validateThemeConfig');
const {ThemeConfigSchema, DEFAULT_CONFIG} = require('../validateThemeConfig');

const mergeDefault = (config) => merge({}, DEFAULT_COLOR_MODE_CONFIG, config);
const {normalizeThemeConfig} = require('@docusaurus/utils-validation');

function testValidateThemeConfig(themeConfig) {
function validate(schema, cfg) {
const {value, error} = schema.validate(cfg, {
convert: false,
});
if (error) {
throw error;
}
return value;
}

return validateThemeConfig({themeConfig, validate});
return normalizeThemeConfig(ThemeConfigSchema, themeConfig);
}

describe('themeConfig', () => {
Expand Down Expand Up @@ -88,7 +76,7 @@ describe('themeConfig', () => {
},
};
expect(testValidateThemeConfig(userConfig)).toEqual({
colorMode: DEFAULT_COLOR_MODE_CONFIG,
...DEFAULT_CONFIG,
...userConfig,
});
});
Expand All @@ -104,7 +92,7 @@ describe('themeConfig', () => {
},
};
expect(testValidateThemeConfig(altTagConfig)).toEqual({
colorMode: DEFAULT_COLOR_MODE_CONFIG,
...DEFAULT_CONFIG,
...altTagConfig,
});
});
Expand All @@ -116,20 +104,24 @@ describe('themeConfig', () => {
},
};
expect(testValidateThemeConfig(prismConfig)).toEqual({
colorMode: DEFAULT_COLOR_MODE_CONFIG,
...DEFAULT_CONFIG,
...prismConfig,
});
});

describe('color mode config', () => {
const withDefaultValues = (colorMode) =>
merge({}, DEFAULT_CONFIG.colorMode, colorMode);

test('minimal config', () => {
const colorMode = {
switchConfig: {
darkIcon: '🌙',
},
};
expect(testValidateThemeConfig({colorMode})).toEqual({
colorMode: mergeDefault(colorMode),
...DEFAULT_CONFIG,
colorMode: withDefaultValues(colorMode),
});
});

Expand All @@ -151,21 +143,27 @@ describe('themeConfig', () => {
},
};
expect(testValidateThemeConfig({colorMode})).toEqual({
colorMode: mergeDefault(colorMode),
...DEFAULT_CONFIG,
colorMode: withDefaultValues(colorMode),
});
});

test('undefined config', () => {
const colorMode = undefined;
expect(testValidateThemeConfig({colorMode})).toEqual({
colorMode: mergeDefault(colorMode),
...DEFAULT_CONFIG,
colorMode: withDefaultValues(colorMode),
});
});

test('empty config', () => {
const colorMode = {};
expect(testValidateThemeConfig({colorMode})).toEqual({
colorMode: mergeDefault(colorMode),
...DEFAULT_CONFIG,
colorMode: {
...DEFAULT_CONFIG.colorMode,
...colorMode,
},
});
});

Expand All @@ -174,7 +172,8 @@ describe('themeConfig', () => {
switchConfig: {},
};
expect(testValidateThemeConfig({colorMode})).toEqual({
colorMode: mergeDefault(colorMode),
...DEFAULT_CONFIG,
colorMode: withDefaultValues(colorMode),
});
});
});
Expand Down
12 changes: 10 additions & 2 deletions packages/docusaurus-theme-classic/src/validateThemeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ const DEFAULT_COLOR_MODE_CONFIG = {
lightIconStyle: {},
},
};
exports.DEFAULT_COLOR_MODE_CONFIG = DEFAULT_COLOR_MODE_CONFIG;

const DEFAULT_CONFIG = {
colorMode: DEFAULT_COLOR_MODE_CONFIG,
metadatas: [],
};
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;

const NavbarItemPosition = Joi.string().equal('left', 'right').default('left');

Expand Down Expand Up @@ -173,7 +178,9 @@ const ThemeConfigSchema = Joi.object({
}),
colorMode: ColorModeSchema,
image: Joi.string(),
metadatas: Joi.array().items(HtmlMetadataSchema).default([]),
metadatas: Joi.array()
.items(HtmlMetadataSchema)
.default(DEFAULT_CONFIG.metadatas),
announcementBar: Joi.object({
id: Joi.string().default('announcement-bar'),
content: Joi.string(),
Expand Down Expand Up @@ -226,6 +233,7 @@ const ThemeConfigSchema = Joi.object({
additionalLanguages: Joi.array().items(Joi.string()),
}).unknown(),
});
exports.ThemeConfigSchema = ThemeConfigSchema;

exports.validateThemeConfig = ({validate, themeConfig}) => {
return validate(ThemeConfigSchema, themeConfig);
Expand Down

0 comments on commit 0b11a4d

Please sign in to comment.