Skip to content

Commit d92c000

Browse files
authored
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
1 parent d326086 commit d92c000

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

packages/plugin-nested-docs/src/hooks/resaveChildren.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ const resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs)
8181
await req.payload.update({
8282
id: child.id,
8383
collection: collection.slug,
84-
data: {
85-
...child,
86-
[breadcrumbSlug]: await populateBreadcrumbs(req, pluginConfig, collection, child),
87-
},
84+
data: populateBreadcrumbs(req, pluginConfig, collection, child),
8885
depth: 0,
8986
draft: isDraft,
9087
locale: req.locale,

test/plugin-nested-docs/int.spec.ts

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ describe('@payloadcms/plugin-nested-docs', () => {
7676
},
7777
})
7878
}
79-
8079
// update parent doc
8180
await payload.update({
8281
collection: 'pages',
@@ -110,6 +109,91 @@ describe('@payloadcms/plugin-nested-docs', () => {
110109
// @ts-ignore
111110
expect(lastUpdatedChildBreadcrumbs[0].url).toStrictEqual('/11-children-updated')
112111
})
112+
113+
it('should return breadcrumbs as an array of objects', async () => {
114+
const parentDoc = await payload.create({
115+
collection: 'pages',
116+
data: {
117+
title: 'parent doc',
118+
slug: 'parent-doc',
119+
_status: 'published',
120+
},
121+
})
122+
123+
const childDoc = await payload.create({
124+
collection: 'pages',
125+
data: {
126+
title: 'child doc',
127+
slug: 'child-doc',
128+
parent: parentDoc.id,
129+
_status: 'published',
130+
},
131+
})
132+
133+
// expect breadcrumbs to be an array
134+
expect(childDoc.breadcrumbs).toBeInstanceOf(Array)
135+
expect(childDoc.breadcrumbs).toBeDefined()
136+
137+
// expect each to be objects
138+
childDoc.breadcrumbs?.map((breadcrumb) => {
139+
expect(breadcrumb).toBeInstanceOf(Object)
140+
})
141+
})
142+
143+
it('should update child doc breadcrumb without affecting any other data', async () => {
144+
const parentDoc = await payload.create({
145+
collection: 'pages',
146+
data: {
147+
title: 'parent doc',
148+
slug: 'parent',
149+
},
150+
})
151+
152+
const childDoc = await payload.create({
153+
collection: 'pages',
154+
data: {
155+
title: 'child doc',
156+
slug: 'child',
157+
parent: parentDoc.id,
158+
_status: 'published',
159+
},
160+
})
161+
162+
await payload.update({
163+
collection: 'pages',
164+
id: parentDoc.id,
165+
data: {
166+
title: 'parent updated',
167+
slug: 'parent-updated',
168+
_status: 'published',
169+
},
170+
})
171+
172+
const updatedChild = await payload
173+
.find({
174+
collection: 'pages',
175+
where: {
176+
id: {
177+
equals: childDoc.id,
178+
},
179+
},
180+
})
181+
.then(({ docs }) => docs[0])
182+
183+
if (!updatedChild) {
184+
return
185+
}
186+
187+
// breadcrumbs should be updated
188+
expect(updatedChild.breadcrumbs).toHaveLength(2)
189+
190+
expect(updatedChild.breadcrumbs?.[0]?.url).toStrictEqual('/parent-updated')
191+
expect(updatedChild.breadcrumbs?.[1]?.url).toStrictEqual('/parent-updated/child')
192+
193+
// no other data should be affected
194+
expect(updatedChild.title).toEqual('child doc')
195+
expect(updatedChild.slug).toEqual('child')
196+
})
113197
})
114198

115199
describe('overrides', () => {

0 commit comments

Comments
 (0)