mirror of
https://github.com/strapi/strapi.git
synced 2025-09-19 21:38:05 +00:00
fix: normalizeRelation tests
This commit is contained in:
parent
5e3490104b
commit
b08d7739e6
@ -32,7 +32,7 @@ export const normalizeRelation = (relation, { shouldAddLink, mainFieldName, targ
|
|||||||
|
|
||||||
export const normalizeRelations = (
|
export const normalizeRelations = (
|
||||||
relations,
|
relations,
|
||||||
{ shouldAddLink = false, mainFieldName, targetModel }
|
{ shouldAddLink = false, mainFieldName, targetModel } = {}
|
||||||
) => {
|
) => {
|
||||||
return [...(relations ?? [])]?.map((relation) =>
|
return [...(relations ?? [])]?.map((relation) =>
|
||||||
normalizeRelation(relation, {
|
normalizeRelation(relation, {
|
||||||
|
@ -1,219 +1,60 @@
|
|||||||
import { normalizeRelations } from '../normalizeRelations';
|
import { normalizeRelations } from '../normalizeRelations';
|
||||||
|
|
||||||
const FIXTURE_RELATIONS = {
|
const FIXTURE_RELATIONS = [
|
||||||
data: {
|
{
|
||||||
pages: [
|
id: 3,
|
||||||
{
|
name: 'Relation 3',
|
||||||
results: [
|
publishedAt: '2022-08-24T09:29:11.38',
|
||||||
{
|
|
||||||
id: 3,
|
|
||||||
name: 'Relation 3',
|
|
||||||
publishedAt: '2022-08-24T09:29:11.38',
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
id: 2,
|
|
||||||
name: 'Relation 2',
|
|
||||||
publishedAt: '',
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
name: 'Relation 1',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
};
|
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
name: 'Relation 2',
|
||||||
|
publishedAt: '',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: 'Relation 1',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
describe('RelationInputDataManager || normalizeRelations', () => {
|
describe('RelationInputDataManager || normalizeRelations', () => {
|
||||||
test('filters out deleted relations', () => {
|
|
||||||
expect(
|
|
||||||
normalizeRelations(FIXTURE_RELATIONS, {
|
|
||||||
modifiedData: { disconnect: [{ id: 1 }] },
|
|
||||||
})
|
|
||||||
).toStrictEqual({
|
|
||||||
data: {
|
|
||||||
pages: [
|
|
||||||
[
|
|
||||||
expect.objectContaining(FIXTURE_RELATIONS.data.pages[0].results[0]),
|
|
||||||
expect.objectContaining(FIXTURE_RELATIONS.data.pages[0].results[1]),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('returns empty array if all relations are deleted', () => {
|
|
||||||
expect(
|
|
||||||
normalizeRelations(FIXTURE_RELATIONS, {
|
|
||||||
modifiedData: { disconnect: [{ id: 1 }, { id: 2 }, { id: 3 }] },
|
|
||||||
})
|
|
||||||
).toStrictEqual({
|
|
||||||
data: {
|
|
||||||
pages: [],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('filter disconnected relations', () => {
|
|
||||||
expect(
|
|
||||||
normalizeRelations(FIXTURE_RELATIONS, {
|
|
||||||
modifiedData: { disconnect: [{ id: 2 }] },
|
|
||||||
})
|
|
||||||
).toStrictEqual({
|
|
||||||
data: {
|
|
||||||
pages: [
|
|
||||||
[
|
|
||||||
expect.objectContaining(FIXTURE_RELATIONS.data.pages[0].results[0]),
|
|
||||||
expect.objectContaining(FIXTURE_RELATIONS.data.pages[0].results[2]),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('add link to each relation', () => {
|
test('add link to each relation', () => {
|
||||||
expect(
|
expect(
|
||||||
normalizeRelations(FIXTURE_RELATIONS, {
|
normalizeRelations(FIXTURE_RELATIONS, {
|
||||||
modifiedData: { disconnect: [] },
|
|
||||||
shouldAddLink: true,
|
shouldAddLink: true,
|
||||||
targetModel: 'something',
|
targetModel: 'something',
|
||||||
})
|
})
|
||||||
).toStrictEqual({
|
).toStrictEqual([
|
||||||
data: {
|
expect.objectContaining({ href: '/content-manager/collectionType/something/3' }),
|
||||||
pages: [
|
expect.objectContaining({ href: '/content-manager/collectionType/something/2' }),
|
||||||
[
|
expect.objectContaining({ href: '/content-manager/collectionType/something/1' }),
|
||||||
expect.objectContaining({ href: '/content-manager/collectionType/something/3' }),
|
]);
|
||||||
expect.objectContaining({ href: '/content-manager/collectionType/something/2' }),
|
|
||||||
expect.objectContaining({ href: '/content-manager/collectionType/something/1' }),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('add publicationState attribute to each relation', () => {
|
test('add publicationState attribute to each relation', () => {
|
||||||
expect(
|
expect(normalizeRelations(FIXTURE_RELATIONS)).toStrictEqual([
|
||||||
normalizeRelations(FIXTURE_RELATIONS, {
|
expect.objectContaining({ publicationState: 'published' }),
|
||||||
modifiedData: { disconnect: [] },
|
expect.objectContaining({ publicationState: 'draft' }),
|
||||||
})
|
expect.objectContaining({ publicationState: false }),
|
||||||
).toStrictEqual({
|
]);
|
||||||
data: {
|
|
||||||
pages: [
|
|
||||||
[
|
|
||||||
expect.objectContaining({ publicationState: 'published' }),
|
|
||||||
expect.objectContaining({ publicationState: 'draft' }),
|
|
||||||
expect.objectContaining({ publicationState: false }),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('add mainField attribute to each relation', () => {
|
test('add mainField attribute to each relation', () => {
|
||||||
expect(
|
expect(
|
||||||
normalizeRelations(FIXTURE_RELATIONS, {
|
normalizeRelations(FIXTURE_RELATIONS, {
|
||||||
modifiedData: { disconnect: [] },
|
|
||||||
mainFieldName: 'name',
|
mainFieldName: 'name',
|
||||||
})
|
})
|
||||||
).toStrictEqual({
|
).toStrictEqual([
|
||||||
data: {
|
expect.objectContaining({
|
||||||
pages: [
|
mainField: FIXTURE_RELATIONS[0].name,
|
||||||
[
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
mainField: FIXTURE_RELATIONS.data.pages[0].results[0].name,
|
mainField: FIXTURE_RELATIONS[1].name,
|
||||||
}),
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
mainField: FIXTURE_RELATIONS.data.pages[0].results[1].name,
|
mainField: FIXTURE_RELATIONS[2].name,
|
||||||
}),
|
}),
|
||||||
expect.objectContaining({
|
]);
|
||||||
mainField: FIXTURE_RELATIONS.data.pages[0].results[2].name,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('allows to connect new relations, even though pages is empty', () => {
|
|
||||||
expect(
|
|
||||||
normalizeRelations(
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
pages: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
modifiedData: { connect: [{ id: 1 }] },
|
|
||||||
}
|
|
||||||
)
|
|
||||||
).toStrictEqual({
|
|
||||||
data: {
|
|
||||||
pages: [
|
|
||||||
[
|
|
||||||
expect.objectContaining({
|
|
||||||
id: 1,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('reverse order of relations pages', () => {
|
|
||||||
const fixtureExtended = {
|
|
||||||
pages: [
|
|
||||||
...FIXTURE_RELATIONS.data.pages,
|
|
||||||
{
|
|
||||||
results: [
|
|
||||||
{
|
|
||||||
id: 6,
|
|
||||||
name: 'Relation 6',
|
|
||||||
publishedAt: '2022-08-24T09:29:11.38',
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
id: 5,
|
|
||||||
name: 'Relation 5',
|
|
||||||
publishedAt: '',
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
id: 4,
|
|
||||||
name: 'Relation 4',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(
|
|
||||||
normalizeRelations(
|
|
||||||
{
|
|
||||||
data: fixtureExtended,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
modifiedData: { connect: [{ id: 6 }] },
|
|
||||||
}
|
|
||||||
)
|
|
||||||
).toStrictEqual({
|
|
||||||
data: {
|
|
||||||
pages: [
|
|
||||||
[
|
|
||||||
expect.objectContaining({ id: 6 }),
|
|
||||||
expect.objectContaining({ id: 5 }),
|
|
||||||
expect.objectContaining({ id: 4 }),
|
|
||||||
],
|
|
||||||
[
|
|
||||||
expect.objectContaining({ id: 3 }),
|
|
||||||
expect.objectContaining({ id: 2 }),
|
|
||||||
expect.objectContaining({ id: 1 }),
|
|
||||||
],
|
|
||||||
[expect.objectContaining({ id: 6 })],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user