From 7a9c3c0edc0741072a9de25e9c15dbdaa765e28d Mon Sep 17 00:00:00 2001 From: soupette Date: Fri, 5 Feb 2021 16:01:14 +0100 Subject: [PATCH] Add missing tests Signed-off-by: soupette --- ...extendCTBAttributeInitialDataMiddleware.js | 2 + ...CTBAttrributeInitialDataMiddleware.test.js | 124 ++++++++++++++++++ .../extendCTBInitialDataMiddleware.test.js | 2 +- 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 packages/strapi-plugin-i18n/admin/src/middlewares/tests/extendCTBAttrributeInitialDataMiddleware.test.js diff --git a/packages/strapi-plugin-i18n/admin/src/middlewares/extendCTBAttributeInitialDataMiddleware.js b/packages/strapi-plugin-i18n/admin/src/middlewares/extendCTBAttributeInitialDataMiddleware.js index d07a66ee27..0937e5de83 100644 --- a/packages/strapi-plugin-i18n/admin/src/middlewares/extendCTBAttributeInitialDataMiddleware.js +++ b/packages/strapi-plugin-i18n/admin/src/middlewares/extendCTBAttributeInitialDataMiddleware.js @@ -1,6 +1,8 @@ const extendCTBAttributeInitialDataMiddleware = () => { return ({ getState }) => next => action => { const enhanceAction = () => { + // the block here is to catch the error when trying to access the state + // of the ctb when the plugin is not mounted try { const hasi18nEnabled = getState().getIn([ 'content-type-builder_dataManagerProvider', diff --git a/packages/strapi-plugin-i18n/admin/src/middlewares/tests/extendCTBAttrributeInitialDataMiddleware.test.js b/packages/strapi-plugin-i18n/admin/src/middlewares/tests/extendCTBAttrributeInitialDataMiddleware.test.js new file mode 100644 index 0000000000..5e532d8c93 --- /dev/null +++ b/packages/strapi-plugin-i18n/admin/src/middlewares/tests/extendCTBAttrributeInitialDataMiddleware.test.js @@ -0,0 +1,124 @@ +import extendCTBAttributeInitialDataMiddleware from '../extendCTBAttributeInitialDataMiddleware'; + +describe('i18n | middlewares | extendCTBAttributeInitialDataMiddleware', () => { + it('should forward the action if the type is undefined', () => { + const middleware = extendCTBAttributeInitialDataMiddleware(); + const action = { test: true, type: undefined }; + const getState = jest.fn(); + + const next = jest.fn(); + + middleware({ getState })(next)(action); + + expect(next).toBeCalledWith(action); + }); + + it('should forward if the type is not correct', () => { + const middleware = extendCTBAttributeInitialDataMiddleware(); + const action = { test: true, type: 'TEST' }; + const getState = jest.fn(); + + const next = jest.fn(); + + middleware({ getState })(next)(action); + + expect(next).toBeCalledWith(action); + }); + + describe('should forward when the type is ContentTypeBuilder/FormModal/SET_ATTRIBUTE_DATA_SCHEMA', () => { + it('should forward if the forTarget is not contentType', () => { + const middleware = extendCTBAttributeInitialDataMiddleware(); + const action = { + forTarget: 'contentType', + type: 'ContentTypeBuilder/FormModal/SET_ATTRIBUTE_DATA_SCHEMA', + }; + const getState = jest.fn(); + + const next = jest.fn(); + + middleware({ getState })(next)(action); + + expect(next).toBeCalledWith(action); + }); + + it('should forward if the i18n is not activated is not contentType', () => { + const middleware = extendCTBAttributeInitialDataMiddleware(); + const action = { + forTarget: 'contentType', + attributeType: 'text', + type: 'ContentTypeBuilder/FormModal/SET_ATTRIBUTE_DATA_SCHEMA', + }; + const getState = jest.fn(() => ({ + getIn: jest.fn(() => false), + })); + + const next = jest.fn(); + + middleware({ getState })(next)(action); + + expect(next).toBeCalledWith(action); + }); + + it('should forward if the ctb is not mounted', () => { + const middleware = extendCTBAttributeInitialDataMiddleware(); + const action = { + forTarget: 'contentType', + attributeType: 'text', + type: 'ContentTypeBuilder/FormModal/SET_ATTRIBUTE_DATA_SCHEMA', + }; + const getState = jest.fn(() => ({ + getIn: undefined, + })); + + const next = jest.fn(); + + middleware({ getState })(next)(action); + + expect(next).toBeCalledWith(action); + }); + }); + + it('should add the action.pluginOptions if the type is correct and i18n is activated', () => { + const middleware = extendCTBAttributeInitialDataMiddleware(); + const action = { + forTarget: 'contentType', + attributeType: 'text', + type: 'ContentTypeBuilder/FormModal/SET_ATTRIBUTE_DATA_SCHEMA', + }; + const getState = jest.fn(() => ({ + // i18n is activated + getIn: jest.fn(() => true), + })); + + const next = jest.fn(); + + middleware({ getState })(next)(action); + + expect(next).toBeCalledWith({ + ...action, + options: { pluginOptions: { i18n: { localized: true } } }, + }); + }); + + it('should modify the options.pluginOptions when it exists', () => { + const middleware = extendCTBAttributeInitialDataMiddleware(); + const action = { + forTarget: 'contentType', + type: 'ContentTypeBuilder/FormModal/RESET_PROPS_AND_SET_FORM_FOR_ADDING_AN_EXISTING_COMPO', + options: { pluginOptions: { pluginTest: { ok: true } } }, + }; + const getState = jest.fn(() => ({ + // i18n is activated + getIn: jest.fn(() => true), + })); + + const next = jest.fn(); + + middleware({ getState })(next)(action); + + expect(next).toBeCalledWith({ + ...action, + options: { pluginOptions: { pluginTest: { ok: true }, i18n: { localized: true } } }, + }); + }); +}); diff --git a/packages/strapi-plugin-i18n/admin/src/middlewares/tests/extendCTBInitialDataMiddleware.test.js b/packages/strapi-plugin-i18n/admin/src/middlewares/tests/extendCTBInitialDataMiddleware.test.js index 2a341c4c11..0ebacf57a0 100644 --- a/packages/strapi-plugin-i18n/admin/src/middlewares/tests/extendCTBInitialDataMiddleware.test.js +++ b/packages/strapi-plugin-i18n/admin/src/middlewares/tests/extendCTBInitialDataMiddleware.test.js @@ -1,6 +1,6 @@ import middleware from '../extendCTBInitialDataMiddleware'; -describe('extendCTBInitialDataMiddleware', () => { +describe('i18n | middlewares | extendCTBInitialDataMiddleware', () => { describe('the action type matches "ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT"', () => { it('forwards the action when the action type does not match', () => { const extendCTBInitialDataMiddleware = middleware();