Add tests

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2021-02-04 17:08:41 +01:00
parent c68dc0fc33
commit fa83bc1508
2 changed files with 79 additions and 99 deletions

View File

@ -8,8 +8,7 @@ const extendCTBInitialDataMiddleware = () => {
) {
const i18n = { localized: false };
const pluginOptions =
action.data && action.data.pluginOptions
const pluginOptions = action.data.pluginOptions
? { ...action.data.pluginOptions, i18n }
: { i18n };
@ -21,7 +20,7 @@ const extendCTBInitialDataMiddleware = () => {
// Override the action if the pluginOption config does not contain i18n
// In this case we need to set the proper initialData shape
if (!has(action.data.pluginOptions, 'i18n')) {
if (!has(action.data.pluginOptions, 'i18n.localized')) {
return next({ ...action, data });
}
}

View File

@ -1,7 +1,8 @@
import middlewares from '../middlewares';
describe('extendCTBInitialDataMiddleware', () => {
it('forwards the action when the action, type does not match', () => {
describe('the action type matches "ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT"', () => {
it('forwards the action when the action type does not match', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {};
@ -11,7 +12,7 @@ describe('extendCTBInitialDataMiddleware', () => {
expect(next).toBeCalledWith(action);
});
it('forwards the action when the action type is "ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT" but modalType and actionType are undefined', () => {
it('forwards the action when the modalType and actionType are undefined', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {
@ -25,13 +26,14 @@ describe('extendCTBInitialDataMiddleware', () => {
expect(next).toBeCalledWith(action);
});
it('forwards the action when the action type is "ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT" and modalType is "contentType" but actionType are undefined', () => {
it('forwards the action when the action.data.pluginOptions.i18n.localized path exists', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
modalType: undefined,
actionType: undefined,
data: { pluginOptions: { i18n: { localized: false } } },
};
extendCTBInitialDataMiddleware()(next)(action);
@ -39,35 +41,13 @@ describe('extendCTBInitialDataMiddleware', () => {
expect(next).toBeCalledWith(action);
});
it('adds a pluginOptions to the action when action.data is undefined', () => {
it('adds a pluginOptions to the action when data is defined and the action', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'create',
data: undefined,
};
extendCTBInitialDataMiddleware()(next)(action);
expect(next).toBeCalledWith({
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'create',
data: {
pluginOptions: { i18n: { localized: true } },
},
});
});
it('adds a pluginOptions to the action when data is defined', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'create',
actionType: 'edit',
data: {},
};
@ -76,9 +56,9 @@ describe('extendCTBInitialDataMiddleware', () => {
expect(next).toBeCalledWith({
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'create',
actionType: 'edit',
data: {
pluginOptions: { i18n: { localized: true } },
pluginOptions: { i18n: { localized: false } },
},
});
});
@ -89,7 +69,7 @@ describe('extendCTBInitialDataMiddleware', () => {
const action = {
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'create',
actionType: 'edit',
data: {
pluginOptions: {
somePluginThings: true,
@ -102,10 +82,11 @@ describe('extendCTBInitialDataMiddleware', () => {
expect(next).toBeCalledWith({
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'create',
actionType: 'edit',
data: {
pluginOptions: { i18n: { localized: true }, somePluginThings: true },
pluginOptions: { i18n: { localized: false }, somePluginThings: true },
},
});
});
});
});