fix(plugin-nested-docs): corrects data shape of breadcrumbs returned in hooks (#10866)
### What? The `plugin-nested-docs` returns an array of breadcrumbs - the `resaveChildren` file accidentally processed the breadcrumbs twice, once where the data is updated and once within the `populateBreadcrumbs` function which was causing the objects to be double nested. ### How? Removes the extra nesting from `resaveChildren` file and allows the `populateBreadcrumbs` to return the final data. Fixes #10855
This commit is contained in:
committed by
GitHub
parent
d32608649c
commit
d92c0009ed
@@ -81,10 +81,7 @@ const resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs)
|
||||
await req.payload.update({
|
||||
id: child.id,
|
||||
collection: collection.slug,
|
||||
data: {
|
||||
...child,
|
||||
[breadcrumbSlug]: await populateBreadcrumbs(req, pluginConfig, collection, child),
|
||||
},
|
||||
data: populateBreadcrumbs(req, pluginConfig, collection, child),
|
||||
depth: 0,
|
||||
draft: isDraft,
|
||||
locale: req.locale,
|
||||
|
||||
@@ -76,7 +76,6 @@ describe('@payloadcms/plugin-nested-docs', () => {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// update parent doc
|
||||
await payload.update({
|
||||
collection: 'pages',
|
||||
@@ -110,6 +109,91 @@ describe('@payloadcms/plugin-nested-docs', () => {
|
||||
// @ts-ignore
|
||||
expect(lastUpdatedChildBreadcrumbs[0].url).toStrictEqual('/11-children-updated')
|
||||
})
|
||||
|
||||
it('should return breadcrumbs as an array of objects', async () => {
|
||||
const parentDoc = await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
title: 'parent doc',
|
||||
slug: 'parent-doc',
|
||||
_status: 'published',
|
||||
},
|
||||
})
|
||||
|
||||
const childDoc = await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
title: 'child doc',
|
||||
slug: 'child-doc',
|
||||
parent: parentDoc.id,
|
||||
_status: 'published',
|
||||
},
|
||||
})
|
||||
|
||||
// expect breadcrumbs to be an array
|
||||
expect(childDoc.breadcrumbs).toBeInstanceOf(Array)
|
||||
expect(childDoc.breadcrumbs).toBeDefined()
|
||||
|
||||
// expect each to be objects
|
||||
childDoc.breadcrumbs?.map((breadcrumb) => {
|
||||
expect(breadcrumb).toBeInstanceOf(Object)
|
||||
})
|
||||
})
|
||||
|
||||
it('should update child doc breadcrumb without affecting any other data', async () => {
|
||||
const parentDoc = await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
title: 'parent doc',
|
||||
slug: 'parent',
|
||||
},
|
||||
})
|
||||
|
||||
const childDoc = await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
title: 'child doc',
|
||||
slug: 'child',
|
||||
parent: parentDoc.id,
|
||||
_status: 'published',
|
||||
},
|
||||
})
|
||||
|
||||
await payload.update({
|
||||
collection: 'pages',
|
||||
id: parentDoc.id,
|
||||
data: {
|
||||
title: 'parent updated',
|
||||
slug: 'parent-updated',
|
||||
_status: 'published',
|
||||
},
|
||||
})
|
||||
|
||||
const updatedChild = await payload
|
||||
.find({
|
||||
collection: 'pages',
|
||||
where: {
|
||||
id: {
|
||||
equals: childDoc.id,
|
||||
},
|
||||
},
|
||||
})
|
||||
.then(({ docs }) => docs[0])
|
||||
|
||||
if (!updatedChild) {
|
||||
return
|
||||
}
|
||||
|
||||
// breadcrumbs should be updated
|
||||
expect(updatedChild.breadcrumbs).toHaveLength(2)
|
||||
|
||||
expect(updatedChild.breadcrumbs?.[0]?.url).toStrictEqual('/parent-updated')
|
||||
expect(updatedChild.breadcrumbs?.[1]?.url).toStrictEqual('/parent-updated/child')
|
||||
|
||||
// no other data should be affected
|
||||
expect(updatedChild.title).toEqual('child doc')
|
||||
expect(updatedChild.slug).toEqual('child')
|
||||
})
|
||||
})
|
||||
|
||||
describe('overrides', () => {
|
||||
|
||||
Reference in New Issue
Block a user