From f8a631d84e71e70ce12a6cff4fd6ce3b5d8e33db Mon Sep 17 00:00:00 2001 From: Mark Kaylor Date: Tue, 21 Mar 2023 11:41:06 +0100 Subject: [PATCH] Test excludeFromGeneration and customizer --- .../services/__tests__/documentation.test.js | 45 +++++++++++++++++++ .../server/services/documentation.js | 3 +- .../documentation/server/services/override.js | 12 +++-- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/packages/plugins/documentation/server/services/__tests__/documentation.test.js b/packages/plugins/documentation/server/services/__tests__/documentation.test.js index 9701ba8980..aef4d0842b 100644 --- a/packages/plugins/documentation/server/services/__tests__/documentation.test.js +++ b/packages/plugins/documentation/server/services/__tests__/documentation.test.js @@ -319,5 +319,50 @@ describe('Documentation service', () => { 'test-new-component' ); }); + it('excludes apis and plugins from generation', async () => { + const overrideService = override({ strapi: global.strapi }); + + overrideService.excludeFromGeneration('kitchensink'); + + global.strapi.plugins.documentation = { + service: jest.fn((name) => { + const mockServices = { + override: overrideService, + }; + + return mockServices[name]; + }), + }; + + const docService = documentation({ strapi: global.strapi }); + await docService.generateFullDoc(); + const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1]; + const mockFinalDoc = lastMockCall[1]; + + expect( + Object.keys(mockFinalDoc.paths).find((path) => path.includes('kitchensink')) + ).toBeUndefined(); + expect( + Object.keys(mockFinalDoc.components.schemas).find((compo) => compo.includes('Kitchensink')) + ).toBeUndefined(); + }); + it("applies a user's customizer function", async () => { + global.strapi.config.get = () => ({ + ...defaultConfig, + 'x-strapi-config': { + ...defaultConfig['x-strapi-config'], + customizer(draft) { + draft.paths['/kitchensinks'] = { get: { responses: { 200: { description: 'test' } } } }; + }, + }, + }); + const docService = documentation({ strapi: global.strapi }); + await docService.generateFullDoc(); + const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1]; + const mockFinalDoc = lastMockCall[1]; + expect(mockFinalDoc.paths['/kitchensinks']).toEqual({ + get: { responses: { 200: { description: 'test' } } }, + }); + }); }); }); diff --git a/packages/plugins/documentation/server/services/documentation.js b/packages/plugins/documentation/server/services/documentation.js index 08f6453ca0..784d116b16 100755 --- a/packages/plugins/documentation/server/services/documentation.js +++ b/packages/plugins/documentation/server/services/documentation.js @@ -138,8 +138,9 @@ module.exports = ({ strapi }) => { let paths = {}; let schemas = {}; const apis = this.getPluginAndApiInfo(); + const apisThatNeedGeneratedDocumentation = apis.filter( - ({ name }) => !overrideService.excludedApisAndPlugins.includes(name) + ({ name }) => !overrideService.excludedFromGeneration.includes(name) ); for (const api of apisThatNeedGeneratedDocumentation) { const apiName = api.name; diff --git a/packages/plugins/documentation/server/services/override.js b/packages/plugins/documentation/server/services/override.js index facc3572dd..79405b1c8f 100644 --- a/packages/plugins/documentation/server/services/override.js +++ b/packages/plugins/documentation/server/services/override.js @@ -4,13 +4,19 @@ const { getPluginsThatNeedDocumentation } = require('./utils/get-plugins-that-ne module.exports = ({ strapi }) => { const registeredOverrides = []; - const excludedApisAndPlugins = []; + const excludedFromGeneration = []; /** * * @param {string | string[]} api - The name of the api or and array of apis to exclude from generation */ const excludeFromGeneration = (api) => { - excludedApisAndPlugins.concat(api); + if (Array.isArray(api)) { + excludedFromGeneration.push(...api); + + return; + } + + excludedFromGeneration.push(api); }; /** * @TODO pluginOrigin should be required in next major release @@ -46,6 +52,6 @@ module.exports = ({ strapi }) => { registeredOverrides, registerOverride, excludeFromGeneration, - excludedApisAndPlugins, + excludedFromGeneration, }; };