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,10 +8,9 @@ const extendCTBInitialDataMiddleware = () => {
) {
const i18n = { localized: false };
const pluginOptions =
action.data && action.data.pluginOptions
? { ...action.data.pluginOptions, i18n }
: { i18n };
const pluginOptions = action.data.pluginOptions
? { ...action.data.pluginOptions, i18n }
: { i18n };
const data = { ...action.data, pluginOptions };
@ -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,111 +1,92 @@
import middlewares from '../middlewares';
describe('extendCTBInitialDataMiddleware', () => {
it('forwards the action when the action, type does not match', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {};
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 = {};
extendCTBInitialDataMiddleware()(next)(action);
extendCTBInitialDataMiddleware()(next)(action);
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', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: undefined,
actionType: undefined,
};
extendCTBInitialDataMiddleware()(next)(action);
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', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: undefined,
};
extendCTBInitialDataMiddleware()(next)(action);
expect(next).toBeCalledWith(action);
});
it('adds a pluginOptions to the action when action.data is undefined', () => {
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 } },
},
expect(next).toBeCalledWith(action);
});
});
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',
data: {},
};
it('forwards the action when the modalType and actionType are undefined', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: undefined,
actionType: undefined,
};
extendCTBInitialDataMiddleware()(next)(action);
extendCTBInitialDataMiddleware()(next)(action);
expect(next).toBeCalledWith({
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'create',
data: {
pluginOptions: { i18n: { localized: true } },
},
expect(next).toBeCalledWith(action);
});
});
it('modifies the data.pluginOptions in the action when it already exists', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'create',
data: {
pluginOptions: {
somePluginThings: true,
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: undefined,
actionType: undefined,
data: { pluginOptions: { i18n: { localized: false } } },
};
extendCTBInitialDataMiddleware()(next)(action);
expect(next).toBeCalledWith(action);
});
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: 'edit',
data: {},
};
extendCTBInitialDataMiddleware()(next)(action);
expect(next).toBeCalledWith({
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'edit',
data: {
pluginOptions: { i18n: { localized: false } },
},
},
};
});
});
extendCTBInitialDataMiddleware()(next)(action);
it('modifies the data.pluginOptions in the action when it already exists', () => {
const extendCTBInitialDataMiddleware = middlewares[0]();
const next = jest.fn();
const action = {
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'edit',
data: {
pluginOptions: {
somePluginThings: true,
},
},
};
expect(next).toBeCalledWith({
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'create',
data: {
pluginOptions: { i18n: { localized: true }, somePluginThings: true },
},
extendCTBInitialDataMiddleware()(next)(action);
expect(next).toBeCalledWith({
type: 'ContentTypeBuilder/FormModal/SET_DATA_TO_EDIT',
modalType: 'contentType',
actionType: 'edit',
data: {
pluginOptions: { i18n: { localized: false }, somePluginThings: true },
},
});
});
});
});