Test only specified plugins are documented

This commit is contained in:
Mark Kaylor 2023-03-14 17:52:14 +01:00
parent 23acbabc95
commit 273116941e
2 changed files with 95 additions and 5 deletions

View File

@ -20,6 +20,10 @@ const mockStrapiInstance = {
config: {
get: jest.fn(() => defaultConfig),
},
log: {
info: jest.fn(),
warn: jest.fn(),
},
};
jest.mock('fs-extra', () => ({
@ -55,4 +59,84 @@ describe('Documentation service', () => {
await expect(validatePromise).resolves.not.toThrow();
});
describe('Determines the plugins that need documentation', () => {
it('generates documentation for the default plugins if the user provided nothing in the config', async () => {
const docService = documentation({ strapi: global.strapi });
const pluginsToDocument = docService.getPluginsThatNeedDocumentation();
const expectededPlugins = ['email', 'upload', 'users-permissions'];
expect(pluginsToDocument).toEqual(expectededPlugins);
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc['x-strapi-config'].plugins).toEqual(expectededPlugins);
});
it("generates documentation only for plugins in the user's config", async () => {
global.strapi.config.get.mockReturnValueOnce({
...defaultConfig,
'x-strapi-config': { ...defaultConfig['x-strapi-config'], plugins: ['email'] },
});
const docService = documentation({ strapi: global.strapi });
const pluginsToDocument = docService.getPluginsThatNeedDocumentation();
expect(pluginsToDocument).toEqual(['email']);
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc['x-strapi-config'].plugins).toEqual(['email']);
});
it('does not generate documentation for any plugins', async () => {
global.strapi.config.get.mockReturnValueOnce({
...defaultConfig,
'x-strapi-config': { ...defaultConfig['x-strapi-config'], plugins: [] },
});
const docService = documentation({ strapi: global.strapi });
const pluginsToDocument = docService.getPluginsThatNeedDocumentation();
expect(pluginsToDocument).toEqual([]);
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc['x-strapi-config'].plugins).toEqual([]);
});
});
describe('Handles overrides', () => {
it("does not apply an override if the plugin providing the override isn't specified in the x-strapi-config.plugins", async () => {
global.strapi.config.get.mockReturnValueOnce({
...defaultConfig,
'x-strapi-config': { ...defaultConfig['x-strapi-config'], plugins: [] },
});
const docService = documentation({ strapi: global.strapi });
docService.registerDoc(
{
'/test': {
get: {
tags: ['Users-Permissions - Users & Roles'],
summary: 'Get list of users',
responses: {},
},
},
},
'users-permissions'
);
expect(global.strapi.log.info).toHaveBeenCalledWith(
`@strapi/documentation will not use the override provided by users-permissions since the plugin was not specified in the x-strapi-config.plugins array`
);
await docService.generateFullDoc();
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1];
expect(mockFinalDoc.paths['/test']).toBeUndefined();
});
});
});

View File

@ -16,6 +16,8 @@ module.exports = ({ strapi }) => {
return {
registerDoc(doc, pluginOrigin) {
const plugins = this.getPluginsThatNeedDocumentation();
let registeredDoc = doc;
if (pluginOrigin) {
if (!plugins.includes(pluginOrigin)) {
return strapi.log.info(
@ -28,7 +30,6 @@ module.exports = ({ strapi }) => {
);
}
let registeredDoc = doc;
// parseYaml
if (typeof doc === 'string') {
registeredDoc = require('yaml').parse(registeredDoc);
@ -110,7 +111,7 @@ module.exports = ({ strapi }) => {
// Default plugins that need documentation generated
const defaultPlugins = ['email', 'upload', 'users-permissions'];
// User specified plugins that need documentation generated
const userPluginsConfig = _.get(config, 'x-strapi-config.plugins');
const userPluginsConfig = config['x-strapi-config'].plugins;
if (userPluginsConfig === null) {
// The user hasn't specified any plugins to document, use the defaults
@ -198,6 +199,7 @@ module.exports = ({ strapi }) => {
]);
_.set(config, ['info', 'x-generation-date'], new Date().toISOString());
_.set(config, ['info', 'version'], version);
_.set(config, ['x-strapi-config', 'plugins'], this.getPluginsThatNeedDocumentation());
// Prepare final doc with default config and generated paths
const finalDoc = { ...config, paths };
// Add the default components to the final doc
@ -206,15 +208,19 @@ module.exports = ({ strapi }) => {
_.merge(finalDoc.components, { schemas });
// Apply the the registered overrides
registeredDocs.forEach((doc) => {
// Add tags
// Merge ovveride tags with the generated tags
finalDoc.tags = finalDoc.tags || [];
finalDoc.tags.push(...(doc.tags || []));
// Add Paths
// Merge override paths with the generated paths,
// If the override has common properties with the finalDoc,
// the properties specificed on the override will replace those found on the final doc
_.assign(finalDoc.paths, doc.paths);
// Add components
_.forEach(doc.components || {}, (val, key) => {
finalDoc.components[key] = finalDoc.components[key] || {};
// Merge override components with the generated components,
// If the override has common properties with the finalDoc,
// the properties specificed on the override will replace those found on the final doc
_.assign(finalDoc.components[key], val);
});
});