Add missing tests

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2021-03-12 11:21:15 +01:00
parent 362a0675f5
commit 96968dd7e1
2 changed files with 300 additions and 32 deletions

View File

@ -119,13 +119,9 @@ const extendCMEditViewLayoutMiddleware = () => () => next => action => {
// This might break the cm, has the user might be redirected to the homepage
if (!currentLocale) {
console.error('The locale must be defined');
return next(action);
}
console.log(action.layout);
const editLayoutPath = getPathToContentType(['layouts', 'edit']);
const editRelationsPath = getPathToContentType(['layouts', 'editRelations']);
const editLayout = get(action, editLayoutPath);
@ -158,4 +154,9 @@ const extendCMEditViewLayoutMiddleware = () => () => next => action => {
const getPathToContentType = pathArray => ['layout', 'contentType', ...pathArray];
export default extendCMEditViewLayoutMiddleware;
export { enhanceEditLayout, enhanceRelationLayout };
export {
enhanceComponentLayoutForRelations,
enhanceComponentsLayout,
enhanceEditLayout,
enhanceRelationLayout,
};

View File

@ -2,6 +2,7 @@ import React from 'react';
import { Globe, GlobeCrossed } from '@buffetjs/icons';
import { getTrad } from '../../utils';
import extendCMEditViewLayoutMiddleware, {
enhanceComponentsLayout,
enhanceEditLayout,
enhanceRelationLayout,
} from '../extendCMEditViewLayoutMiddleware';
@ -58,7 +59,7 @@ describe('i18n | Middlewares | extendCMEditViewLayoutMiddleware', () => {
expect(next).toBeCalledWith(action);
});
it('should modify the editRelations layout when i18n is enabled', () => {
it('should forward the action when i18n is enabled and the query.locale is not defined', () => {
const layout = {
contentType: {
uid: 'test',
@ -88,6 +89,44 @@ describe('i18n | Middlewares | extendCMEditViewLayoutMiddleware', () => {
const next = jest.fn();
middleware()(next)(action);
expect(next).toBeCalledWith(action);
});
it('should modify the editRelations layout when i18n is enabled and the query.locale is defined', () => {
const layout = {
contentType: {
uid: 'test',
pluginOptions: { i18n: { localized: true } },
layouts: {
edit: [],
editRelations: [
{
fieldSchema: {},
metadatas: {},
name: 'addresses',
queryInfos: {
test: true,
defaultParams: {},
},
size: 6,
targetModelPluginOptions: {},
},
],
},
},
components: {},
};
const action = {
type: 'ContentManager/EditViewLayoutManager/SET_LAYOUT',
layout,
query: { locale: 'en' },
};
const middleware = extendCMEditViewLayoutMiddleware();
const next = jest.fn();
middleware()(next)(action);
expect(next).toBeCalledWith({
...action,
layout: {
@ -101,7 +140,10 @@ describe('i18n | Middlewares | extendCMEditViewLayoutMiddleware', () => {
fieldSchema: {},
metadatas: {},
name: 'addresses',
queryInfos: {},
queryInfos: {
test: true,
defaultParams: {},
},
size: 6,
targetModelPluginOptions: {},
labelIcon: {
@ -117,34 +159,186 @@ describe('i18n | Middlewares | extendCMEditViewLayoutMiddleware', () => {
});
});
describe('enhanceRelationLayout', () => {
it('should add the labelIcon key to all relations fields', () => {
const editRelations = [
{
fieldSchema: {},
metadatas: {},
name: 'addresses',
queryInfos: {},
size: 6,
targetModelPluginOptions: {},
},
];
const expected = [
{
fieldSchema: {},
metadatas: {},
name: 'addresses',
queryInfos: {},
size: 6,
targetModelPluginOptions: {},
labelIcon: {
title: { id: localizedTrad, defaultMessage: localizedTradDefaultMessage },
icon: <Globe />,
describe('enhanceComponentsLayout', () => {
it('should not enhance the field when the type is not relation', () => {
const components = {
test: {
test: true,
layouts: {
edit: [
[
{
name: 'title',
fieldSchema: { type: 'string' },
},
{
name: 'content',
fieldSchema: { type: 'string' },
},
],
],
},
},
];
};
const expected = {
test: {
test: true,
layouts: {
edit: [
[
{
name: 'title',
fieldSchema: { type: 'string' },
},
{
name: 'content',
fieldSchema: { type: 'string' },
},
],
],
},
},
};
expect(enhanceRelationLayout(editRelations)).toEqual(expected);
expect(enhanceComponentsLayout(components)).toEqual(expected);
});
it('should not enhance the field when the type is relation and the targetModel.pluginOptions.i18.localized is disabled', () => {
const components = {
test: {
test: true,
layouts: {
edit: [
[
{
name: 'title',
fieldSchema: { type: 'relation' },
targetModelPluginOptions: { i18n: { localized: false } },
},
{
name: 'content',
fieldSchema: { type: 'string' },
},
],
],
},
},
};
const expected = {
test: {
test: true,
layouts: {
edit: [
[
{
name: 'title',
fieldSchema: { type: 'relation' },
targetModelPluginOptions: { i18n: { localized: false } },
},
{
name: 'content',
fieldSchema: { type: 'string' },
},
],
],
},
},
};
expect(enhanceComponentsLayout(components)).toEqual(expected);
});
it('should modify the relation field when the targetModelPluginOptions.i18n.localized is enabled', () => {
const components = {
foo: {
test: true,
layouts: {
edit: [
[
{
name: 'title',
fieldSchema: { type: 'relation' },
targetModelPluginOptions: { i18n: { localized: true } },
queryInfos: {
defaultParams: { test: true },
},
},
{
name: 'content',
fieldSchema: { type: 'string' },
},
],
],
},
},
bar: {
test: true,
layouts: {
edit: [
[
{
name: 'title',
fieldSchema: { type: 'relation' },
targetModelPluginOptions: { i18n: { localized: true } },
queryInfos: {
defaultParams: { test: true },
},
},
{
name: 'content',
fieldSchema: { type: 'string' },
},
],
],
},
},
};
const expected = {
foo: {
test: true,
layouts: {
edit: [
[
{
name: 'title',
fieldSchema: { type: 'relation' },
targetModelPluginOptions: { i18n: { localized: true } },
queryInfos: {
defaultParams: { test: true, _locale: 'en' },
},
},
{
name: 'content',
fieldSchema: { type: 'string' },
},
],
],
},
},
bar: {
test: true,
layouts: {
edit: [
[
{
name: 'title',
fieldSchema: { type: 'relation' },
targetModelPluginOptions: { i18n: { localized: true } },
queryInfos: {
defaultParams: { test: true, _locale: 'en' },
},
},
{
name: 'content',
fieldSchema: { type: 'string' },
},
],
],
},
},
};
expect(enhanceComponentsLayout(components, 'en')).toEqual(expected);
});
});
@ -263,4 +457,77 @@ describe('i18n | Middlewares | extendCMEditViewLayoutMiddleware', () => {
expect(enhanceEditLayout(edit)).toEqual(expected);
});
});
describe('enhanceRelationLayout', () => {
it('should add the labelIcon key to all relations fields', () => {
const editRelations = [
{
fieldSchema: {},
metadatas: {},
name: 'addresses',
queryInfos: {},
size: 6,
targetModelPluginOptions: {},
},
];
const expected = [
{
fieldSchema: {},
metadatas: {},
name: 'addresses',
queryInfos: {},
size: 6,
targetModelPluginOptions: {},
labelIcon: {
title: { id: localizedTrad, defaultMessage: localizedTradDefaultMessage },
icon: <Globe />,
},
},
];
expect(enhanceRelationLayout(editRelations, 'en')).toEqual(expected);
});
it('should add the locale to the queryInfos.defaultParams when the targetModelPluginOptions.i18n.localized is enabled', () => {
const editRelations = [
{
fieldSchema: {},
metadatas: {},
name: 'addresses',
queryInfos: {
defaultParams: {
test: true,
},
},
size: 6,
targetModelPluginOptions: {
i18n: { localized: true },
},
},
];
const expected = [
{
fieldSchema: {},
metadatas: {},
name: 'addresses',
queryInfos: {
defaultParams: {
test: true,
_locale: 'en',
},
},
size: 6,
targetModelPluginOptions: {
i18n: { localized: true },
},
labelIcon: {
title: { id: localizedTrad, defaultMessage: localizedTradDefaultMessage },
icon: <Globe />,
},
},
];
expect(enhanceRelationLayout(editRelations, 'en')).toEqual(expected);
});
});
});