mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 07:33:17 +00:00
fix: nullish values should only be replaced when they're top level attributes
This commit is contained in:
parent
e19c0f92cb
commit
687a421933
@ -50,7 +50,7 @@ const findAllAndReplaceSetup = (components, predicate = () => false, replacement
|
||||
if (value.type === 'component') {
|
||||
const componentAttributes = components[value.component].attributes;
|
||||
|
||||
if (!value.repeatable && typeof acc[key] === 'object') {
|
||||
if (!value.repeatable && acc[key] && typeof acc[key] === 'object') {
|
||||
acc[key] = findAllAndReplace(acc[key], componentAttributes, {
|
||||
ignoreFalseyValues,
|
||||
path: [...path, key],
|
||||
|
||||
@ -130,6 +130,106 @@ describe('findAllAndReplace', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('null values', () => {
|
||||
const nullishData = {
|
||||
categories: null,
|
||||
repeatable_repeatable_relations: null,
|
||||
repeatable_relations: null,
|
||||
dynamic_relations: null,
|
||||
comp_relation: null,
|
||||
};
|
||||
|
||||
it('should replace the first level of relations', () => {
|
||||
const data = findAllAndReplace(
|
||||
components,
|
||||
(value) => value.type === 'relation',
|
||||
'replaced'
|
||||
)(nullishData, schema);
|
||||
|
||||
expect(data).toMatchInlineSnapshot(`
|
||||
{
|
||||
"categories": "replaced",
|
||||
"comp_relation": null,
|
||||
"dynamic_relations": null,
|
||||
"repeatable_relations": null,
|
||||
"repeatable_repeatable_relations": null,
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should not replace relations in single components', () => {
|
||||
const data = findAllAndReplace(
|
||||
components,
|
||||
(value) => value.type === 'relation',
|
||||
'replaced'
|
||||
)(nullishData, schema);
|
||||
|
||||
expect(data).toMatchInlineSnapshot(`
|
||||
{
|
||||
"categories": "replaced",
|
||||
"comp_relation": null,
|
||||
"dynamic_relations": null,
|
||||
"repeatable_relations": null,
|
||||
"repeatable_repeatable_relations": null,
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should not replace relation instances in a repeatable component', () => {
|
||||
const data = findAllAndReplace(
|
||||
components,
|
||||
(value) => value.type === 'relation',
|
||||
'replaced'
|
||||
)(nullishData, schema);
|
||||
|
||||
expect(data).toMatchInlineSnapshot(`
|
||||
{
|
||||
"categories": "replaced",
|
||||
"comp_relation": null,
|
||||
"dynamic_relations": null,
|
||||
"repeatable_relations": null,
|
||||
"repeatable_repeatable_relations": null,
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should not replace all relation instances in nested repeatable components', () => {
|
||||
const data = findAllAndReplace(
|
||||
components,
|
||||
(value) => value.type === 'relation',
|
||||
'replaced'
|
||||
)(nullishData, schema);
|
||||
|
||||
expect(data).toMatchInlineSnapshot(`
|
||||
{
|
||||
"categories": "replaced",
|
||||
"comp_relation": null,
|
||||
"dynamic_relations": null,
|
||||
"repeatable_relations": null,
|
||||
"repeatable_repeatable_relations": null,
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should not replace relation instances in dynamic zones correctly', () => {
|
||||
const data = findAllAndReplace(
|
||||
components,
|
||||
(value) => value.type === 'relation',
|
||||
'replaced'
|
||||
)(nullishData, schema);
|
||||
|
||||
expect(data).toMatchInlineSnapshot(`
|
||||
{
|
||||
"categories": "replaced",
|
||||
"comp_relation": null,
|
||||
"dynamic_relations": null,
|
||||
"repeatable_relations": null,
|
||||
"repeatable_repeatable_relations": null,
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('replacement as a function', () => {
|
||||
it('should pass the data object (not the schema) and use the returned value', () => {
|
||||
const data = findAllAndReplace(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user