Merge pull request #10591 from strapi/v4/db-fix-ctb-tests

Fix FormModal reducer tests
This commit is contained in:
cyril lopez 2021-07-08 09:46:45 +02:00 committed by GitHub
commit f831c72dd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 235 additions and 224 deletions

View File

@ -1,6 +1,7 @@
import { fromJS, List } from 'immutable';
import pluralize from 'pluralize';
import { snakeCase } from 'lodash';
import getRelationType from '../../utils/getRelationType';
import makeUnique from '../../utils/makeUnique';
import { createComponentUid } from './utils/createUid';
import { shouldPluralizeName, shouldPluralizeTargetAttribute } from './utils/relations';
@ -61,30 +62,33 @@ const reducer = (state = initialState, action) => {
},
} = action;
// Special case for the admin user...
let didChangeNatureBecauseOfRestrictedRelation = false;
let didChangeRelationTypeBecauseOfRestrictedRelation = false;
let changedRelationType = null;
return obj
.update('target', () => value)
.update('relation', currentRelation => {
const currentRelationType = getRelationType(
currentRelation,
obj.get('targetAttribute')
);
// Don't change the relation type if the allowed relations are not restricted
// TODO: replace with an obj { relation: 'x', bidirctional: true|false } when BE ready
if (targetContentTypeAllowedRelations === null) {
return currentRelation;
}
if (!targetContentTypeAllowedRelations.includes(currentRelation)) {
if (!targetContentTypeAllowedRelations.includes(currentRelationType)) {
const relationToSet = targetContentTypeAllowedRelations[0];
didChangeNatureBecauseOfRestrictedRelation = true;
didChangeRelationTypeBecauseOfRestrictedRelation = true;
changedRelationType = relationToSet;
if (relationToSet === 'oneWay') {
// TODO change targetAttribute
return 'oneToOne';
}
if (relationToSet === 'manyWay') {
// TODO change targetAttribute
return 'oneToMany';
}
@ -94,7 +98,7 @@ const reducer = (state = initialState, action) => {
return currentRelation;
})
.update('name', () => {
if (didChangeNatureBecauseOfRestrictedRelation) {
if (didChangeRelationTypeBecauseOfRestrictedRelation) {
return pluralize(
snakeCase(selectedContentTypeFriendlyName),
shouldPluralizeName(changedRelationType)
@ -116,7 +120,7 @@ const reducer = (state = initialState, action) => {
// Case when we need to change the relation to oneWay (ex: admin user)
if (
didChangeNatureBecauseOfRestrictedRelation &&
didChangeRelationTypeBecauseOfRestrictedRelation &&
['oneWay', 'manyWay'].includes(changedRelationType)
) {
return null;
@ -124,7 +128,7 @@ const reducer = (state = initialState, action) => {
return pluralize(
snakeCase(oneThatIsCreatingARelationWithAnother),
shouldPluralizeTargetAttribute(obj.get('nature'))
shouldPluralizeTargetAttribute(obj.get('relation'))
);
});
});

View File

@ -62,7 +62,7 @@ describe('CTB | components | FormModal | reducer | actions', () => {
});
describe(actions.ON_CHANGE, () => {
it('Should update the modifiedData object correctly if it is not relation', () => {
it('Should update the modifiedData object correctly', () => {
const action = {
type: actions.ON_CHANGE,
keys: ['name'],
@ -74,214 +74,6 @@ describe('CTB | components | FormModal | reducer | actions', () => {
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the nature change correctly from oneWay to manyToMany', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'category test',
nature: 'oneWay',
targetAttribute: '-',
target: 'application::category.category',
unique: false,
dominant: null,
columnName: null,
targetColumnName: null,
})
);
const action = {
type: actions.ON_CHANGE,
keys: ['nature'],
value: 'manyToMany',
targetContentType: 'application::category.category',
oneThatIsCreatingARelationWithAnother: 'address',
};
const expected = state
.setIn(['modifiedData', 'nature'], 'manyToMany')
.setIn(['modifiedData', 'dominant'], true)
.setIn(['modifiedData', 'name'], 'category_tests')
.setIn(['modifiedData', 'targetAttribute'], 'addresses');
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the nature change correctly from manyToMany to oneWay', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'category_tests',
nature: 'manyToMany',
targetAttribute: 'addresses',
target: 'application::category.category',
unique: false,
dominant: true,
columnName: null,
targetColumnName: 'test',
})
);
const action = {
type: actions.ON_CHANGE,
keys: ['nature'],
value: 'oneWay',
targetContentType: 'application::category.category',
oneThatIsCreatingARelationWithAnother: 'address',
};
const expected = state
.setIn(['modifiedData', 'nature'], 'oneWay')
.setIn(['modifiedData', 'dominant'], null)
.setIn(['modifiedData', 'name'], 'category_test')
.setIn(['modifiedData', 'targetAttribute'], '-')
.setIn(['modifiedData', 'targetColumnName'], null);
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the nature change correctly from oneToOne to oneToMany', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'category_test',
nature: 'oneToOne',
targetAttribute: 'address',
target: 'application::category.category',
unique: false,
dominant: null,
columnName: null,
targetColumnName: 'test',
})
);
const action = {
type: actions.ON_CHANGE,
keys: ['nature'],
value: 'oneToMany',
targetContentType: 'application::category.category',
oneThatIsCreatingARelationWithAnother: 'address',
};
const expected = state
.setIn(['modifiedData', 'nature'], 'oneToMany')
.setIn(['modifiedData', 'name'], 'category_tests');
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the target change correctly for a one side relation (oneWay, manyWay)', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'category test',
nature: 'oneWay',
targetAttribute: '-',
target: 'application::category.category',
unique: false,
dominant: null,
columnName: null,
targetColumnName: null,
})
);
const action = {
type: actions.ON_CHANGE,
keys: ['target'],
value: 'application::address.address',
oneThatIsCreatingARelationWithAnother: 'address',
selectedContentTypeFriendlyName: 'address',
targetContentTypeAllowedRelations: null,
};
const expected = state
.setIn(['modifiedData', 'target'], 'application::address.address')
.setIn(['modifiedData', 'name'], 'address');
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the target change correctly for the manyToMany relation', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'categories',
nature: 'manyToMany',
targetAttribute: 'addresses',
target: 'application::category.category',
unique: false,
dominant: true,
columnName: null,
targetColumnName: null,
})
);
const action = {
type: actions.ON_CHANGE,
keys: ['target'],
value: 'application::country.country',
oneThatIsCreatingARelationWithAnother: 'address',
selectedContentTypeFriendlyName: 'country',
targetContentTypeAllowedRelations: null,
};
const expected = state
.setIn(['modifiedData', 'target'], 'application::country.country')
.setIn(['modifiedData', 'name'], 'countries');
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the target change correctly if the target has restricted relations and the nature is not correct', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'categories',
nature: 'manyToMany',
targetAttribute: 'addresses',
target: 'application::category.category',
unique: false,
dominant: true,
columnName: null,
targetColumnName: null,
})
);
const action = {
type: actions.ON_CHANGE,
keys: ['target'],
value: 'application::country.country',
oneThatIsCreatingARelationWithAnother: 'address',
selectedContentTypeFriendlyName: 'country',
targetContentTypeAllowedRelations: ['oneWay'],
};
const expected = state
.setIn(['modifiedData', 'target'], 'application::country.country')
.setIn(['modifiedData', 'name'], 'country')
.setIn(['modifiedData', 'targetAttribute'], '-')
.setIn(['modifiedData', 'nature'], 'oneWay');
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the target change correctly if the target has restricted relations and the nature is correct', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'categories',
nature: 'manyWay',
targetAttribute: 'addresses',
target: 'application::category.category',
unique: false,
dominant: true,
columnName: null,
targetColumnName: null,
})
);
const action = {
type: actions.ON_CHANGE,
keys: ['target'],
value: 'application::country.country',
oneThatIsCreatingARelationWithAnother: 'address',
selectedContentTypeFriendlyName: 'country',
targetContentTypeAllowedRelations: ['oneWay', 'manyWay'],
};
const expected = state
.setIn(['modifiedData', 'target'], 'application::country.country')
.setIn(['modifiedData', 'name'], 'countries')
.setIn(['modifiedData', 'targetAttribute'], '-');
expect(reducer(state, action)).toEqual(expected);
});
it('should remove the default value if the type of date input type has been changed', () => {
const state = initialState.setIn(
['modifiedData'],
@ -406,6 +198,200 @@ describe('CTB | components | FormModal | reducer | actions', () => {
});
});
describe('ON_CHANGE_RELATION_TARGET', () => {
it('Should handle the target change correctly for a one side relation (oneWay, manyWay)', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'category test',
relation: 'oneToOne',
targetAttribute: null,
target: 'application::category.category',
type: 'relation',
})
);
const action = {
type: actions.ON_CHANGE_RELATION_TARGET,
target: {
value: 'application::address.address',
oneThatIsCreatingARelationWithAnother: 'address',
selectedContentTypeFriendlyName: 'address',
targetContentTypeAllowedRelations: null,
},
};
const expected = state
.setIn(['modifiedData', 'target'], 'application::address.address')
.setIn(['modifiedData', 'name'], 'address');
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the target change correctly for the manyToMany relation', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'categories',
relation: 'manyToMany',
targetAttribute: 'addresses',
target: 'application::category.category',
type: 'relation',
})
);
const action = {
type: actions.ON_CHANGE_RELATION_TARGET,
target: {
value: 'application::country.country',
oneThatIsCreatingARelationWithAnother: 'address',
selectedContentTypeFriendlyName: 'country',
targetContentTypeAllowedRelations: null,
},
};
const expected = state
.setIn(['modifiedData', 'target'], 'application::country.country')
.setIn(['modifiedData', 'name'], 'countries');
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the target change correctly if the target has restricted relations and the relation type is not correct', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'categories',
relation: 'manyToMany',
targetAttribute: 'addresses',
target: 'application::category.category',
type: 'relation',
})
);
const action = {
type: actions.ON_CHANGE_RELATION_TARGET,
target: {
value: 'application::country.country',
oneThatIsCreatingARelationWithAnother: 'address',
selectedContentTypeFriendlyName: 'country',
targetContentTypeAllowedRelations: ['oneWay'],
},
};
const expected = state
.setIn(['modifiedData', 'target'], 'application::country.country')
.setIn(['modifiedData', 'name'], 'country')
.setIn(['modifiedData', 'targetAttribute'], null)
.setIn(['modifiedData', 'relation'], 'oneToOne');
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the target change correctly if the target has restricted relations and the relation type is correct', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'categories',
relation: 'oneToMany',
targetAttribute: null,
target: 'application::category.category',
type: 'relation',
})
);
const action = {
type: actions.ON_CHANGE_RELATION_TARGET,
target: {
value: 'application::country.country',
oneThatIsCreatingARelationWithAnother: 'address',
selectedContentTypeFriendlyName: 'country',
targetContentTypeAllowedRelations: ['oneWay', 'manyWay'],
},
};
const expected = state
.setIn(['modifiedData', 'target'], 'application::country.country')
.setIn(['modifiedData', 'name'], 'countries')
.setIn(['modifiedData', 'targetAttribute'], null);
expect(reducer(state, action)).toEqual(expected);
});
});
describe('ON_CHANGE_RELATION_TYPE', () => {
it('Should handle the relation type change correctly from oneWay to manyToMany', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'category test',
relation: 'oneToOne',
targetAttribute: null,
target: 'application::category.category',
type: 'relation',
})
);
const action = {
type: actions.ON_CHANGE_RELATION_TYPE,
target: {
value: 'manyToMany',
oneThatIsCreatingARelationWithAnother: 'address',
},
};
const expected = state
.setIn(['modifiedData', 'relation'], 'manyToMany')
.setIn(['modifiedData', 'name'], 'category_tests')
.setIn(['modifiedData', 'targetAttribute'], 'addresses');
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the relation type change correctly from manyToMany to oneWay', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'category_tests',
relation: 'manyToMany',
targetAttribute: 'addresses',
target: 'application::category.category',
type: 'relation',
})
);
const action = {
type: actions.ON_CHANGE_RELATION_TYPE,
target: {
value: 'oneWay',
oneThatIsCreatingARelationWithAnother: 'address',
},
};
const expected = state
.setIn(['modifiedData', 'relation'], 'oneToOne')
.setIn(['modifiedData', 'name'], 'category_test')
.setIn(['modifiedData', 'targetAttribute'], null);
expect(reducer(state, action)).toEqual(expected);
});
it('Should handle the relation type change correctly from oneToOne to oneToMany', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'category_test',
relation: 'oneToOne',
targetAttribute: 'address',
target: 'application::category.category',
type: 'relation',
})
);
const action = {
type: actions.ON_CHANGE_RELATION_TYPE,
target: {
value: 'oneToMany',
oneThatIsCreatingARelationWithAnother: 'address',
},
};
const expected = state
.setIn(['modifiedData', 'relation'], 'oneToMany')
.setIn(['modifiedData', 'name'], 'category_tests');
expect(reducer(state, action)).toEqual(expected);
});
});
describe('RESET_PROPS', () => {
it('Should return the initialState', () => {
const state = initialState.setIn(['modifiedData'], 'test');
@ -691,20 +677,17 @@ describe('CTB | components | FormModal | reducer | actions', () => {
nameToSetForRelation: 'address test',
targetUid: 'application::address.address',
isEditing: false,
modifiedDataToSetForEditing: { name: null, targetAttribute: '-' },
modifiedDataToSetForEditing: { name: null },
step: null,
};
const expected = initialState.set(
'modifiedData',
fromJS({
name: 'address_test',
nature: 'oneWay',
targetAttribute: '-',
relation: 'oneToOne',
targetAttribute: null,
target: 'application::address.address',
unique: false,
dominant: null,
columnName: null,
targetColumnName: null,
type: 'relation',
})
);

View File

@ -0,0 +1,24 @@
import getRelationType from '../getRelationType';
describe('CTB | utils | getRelationType', () => {
it('Should return oneWay', () => {
const relation = 'oneToOne';
expect(getRelationType(relation, null)).toEqual('oneWay');
expect(getRelationType(relation, undefined)).toEqual('oneWay');
});
it('Should return manyWay', () => {
const relation = 'oneToMany';
expect(getRelationType(relation, null)).toEqual('manyWay');
expect(getRelationType(relation, undefined)).toEqual('manyWay');
});
it('Should return the relation when the target attribute is defined', () => {
const relation = 'test';
expect(getRelationType(relation, 'test')).toEqual('test');
expect(getRelationType(relation, 'test')).toEqual('test');
});
});