mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 07:33:17 +00:00
refactor: return the same data format for search and relations
This commit is contained in:
parent
cf23e39892
commit
5e3490104b
@ -1,12 +1,15 @@
|
||||
import { normalizeRelation } from './normalizeRelations';
|
||||
|
||||
export const normalizeSearchResults = (relations, { mainFieldName }) => {
|
||||
const { data } = relations;
|
||||
const { pages = [] } = data ?? {};
|
||||
|
||||
return {
|
||||
...relations,
|
||||
data: {
|
||||
pages: [...(relations?.data?.pages ?? [])]?.map((page) =>
|
||||
page?.results.map((relation) => normalizeRelation(relation, { mainFieldName }))
|
||||
),
|
||||
},
|
||||
data: pages
|
||||
.map((page) =>
|
||||
(page ?? []).results.map((relation) => normalizeRelation(relation, { mainFieldName }))
|
||||
)
|
||||
.flat(),
|
||||
};
|
||||
};
|
||||
|
||||
@ -30,15 +30,11 @@ const FIXTURE_RELATIONS = {
|
||||
describe('RelationInputDataManager || normalizeSearchResults', () => {
|
||||
test('add publicationState attribute to each relation', () => {
|
||||
expect(normalizeSearchResults(FIXTURE_RELATIONS, {})).toStrictEqual({
|
||||
data: {
|
||||
pages: [
|
||||
[
|
||||
expect.objectContaining({ publicationState: 'published' }),
|
||||
expect.objectContaining({ publicationState: 'draft' }),
|
||||
expect.objectContaining({ publicationState: false }),
|
||||
],
|
||||
],
|
||||
},
|
||||
data: [
|
||||
expect.objectContaining({ publicationState: 'published' }),
|
||||
expect.objectContaining({ publicationState: 'draft' }),
|
||||
expect.objectContaining({ publicationState: false }),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
@ -48,21 +44,17 @@ describe('RelationInputDataManager || normalizeSearchResults', () => {
|
||||
mainFieldName: 'name',
|
||||
})
|
||||
).toStrictEqual({
|
||||
data: {
|
||||
pages: [
|
||||
[
|
||||
expect.objectContaining({
|
||||
mainField: FIXTURE_RELATIONS.data.pages[0].results[0].name,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
mainField: FIXTURE_RELATIONS.data.pages[0].results[1].name,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
mainField: FIXTURE_RELATIONS.data.pages[0].results[2].name,
|
||||
}),
|
||||
],
|
||||
],
|
||||
},
|
||||
data: [
|
||||
expect.objectContaining({
|
||||
mainField: FIXTURE_RELATIONS.data.pages[0].results[0].name,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
mainField: FIXTURE_RELATIONS.data.pages[0].results[1].name,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
mainField: FIXTURE_RELATIONS.data.pages[0].results[2].name,
|
||||
}),
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user