From d92c0009edc8ceb8ed6e253e2b590b11fd5ba301 Mon Sep 17 00:00:00 2001 From: Jessica Chowdhury <67977755+JessChowdhury@users.noreply.github.com> Date: Mon, 24 Feb 2025 16:10:21 +0000 Subject: [PATCH] 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 --- .../src/hooks/resaveChildren.ts | 5 +- test/plugin-nested-docs/int.spec.ts | 86 ++++++++++++++++++- 2 files changed, 86 insertions(+), 5 deletions(-) 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', () => {