diff --git a/packages/plugin-nested-docs/src/hooks/resaveChildren.ts b/packages/plugin-nested-docs/src/hooks/resaveChildren.ts index 783cde5bab..bac3467a9e 100644 --- a/packages/plugin-nested-docs/src/hooks/resaveChildren.ts +++ b/packages/plugin-nested-docs/src/hooks/resaveChildren.ts @@ -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, diff --git a/test/plugin-nested-docs/int.spec.ts b/test/plugin-nested-docs/int.spec.ts index 6c7359cd9b..90d9262c6e 100644 --- a/test/plugin-nested-docs/int.spec.ts +++ b/test/plugin-nested-docs/int.spec.ts @@ -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', () => {