-
Notifications
You must be signed in to change notification settings - Fork 1
Enhance duplicate page action #985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b2629a6
Add clearLayoutRelationships utility to remove relationships on page …
rchlfryn 59dc6bc
Update provisionTenant endpoint to use clearLayoutRelationships
rchlfryn 4672a52
Add typing for clearLayoutRelationships
rchlfryn 3acf6b8
add test for duplicatePageToTenant
rchlfryn 5628a7e
Delete removeIdKey
rchlfryn db4ca30
Add note about pages and nav copied in draft state
rchlfryn 0998f18
Hide extra text about draft before copy
rchlfryn 914b16a
Update test from tenant slug update
rchlfryn b9a64fb
Change provisioning tenant to use DVAC as structure for pages and nav…
rchlfryn c367dc4
only append copy to page title and slug if page exists
rchlfryn fd8ea06
Update failing test - change to created
rchlfryn 4228276
Add `ButtonBlock` & `CalloutBlock` to `clearLayoutRelationships` tha…
rchlfryn fa4606c
Update provisioning script to use DVAC nav as structure for page crea…
rchlfryn c1499bf
Update e2e test
rchlfryn 11c1c6c
e2e test failure - i guess use exact
rchlfryn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| jest.mock('../../src/constants/defaults', () => ({ | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| DEFAULT_BLOCKS: require('./fixtures/mockBlocks').DEFAULT_BLOCKS, | ||
| })) | ||
|
|
||
| jest.mock('../../src/blocks/NACMedia/config', () => ({ | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| NACMediaBlock: require('./fixtures/mockBlocks').NACMediaBlock, | ||
| })) | ||
|
|
||
| jest.mock('../../src/blocks/Button/config', () => ({ | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| ButtonBlock: require('./fixtures/mockBlocks').ButtonBlock, | ||
| })) | ||
|
|
||
| jest.mock('../../src/blocks/Callout/config', () => ({ | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| CalloutBlock: require('./fixtures/mockBlocks').CalloutBlock, | ||
| })) | ||
|
|
||
| jest.mock('../../src/payload.config', () => ({})) | ||
|
|
||
| jest.mock('payload', () => ({ | ||
| getPayload: jest.fn(), | ||
| })) | ||
|
|
||
| import { duplicatePageToTenant } from '@/collections/Pages/endpoints/duplicatePageToTenant' | ||
| import type { Payload, PayloadRequest } from 'payload' | ||
| import { getPayload } from 'payload' | ||
|
|
||
| const mockFind = jest.fn() | ||
| const mockCreate = jest.fn() | ||
|
|
||
| const mockTenant = { id: 42, name: 'Test AC', slug: 'tac' } | ||
|
|
||
| beforeEach(() => { | ||
| jest | ||
| .mocked(getPayload) | ||
| // eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
| .mockResolvedValue({ find: mockFind, create: mockCreate } as unknown as Payload) | ||
| // First find call: tenant lookup. Second find call: slug existence check (no conflict by default). | ||
| mockFind | ||
| .mockReset() | ||
| .mockResolvedValueOnce({ docs: [mockTenant] }) | ||
| .mockResolvedValueOnce({ docs: [] }) | ||
| mockCreate.mockReset().mockResolvedValue({ id: 99 }) | ||
| }) | ||
|
|
||
| function buildRequest( | ||
| tenantSlug: string | undefined, | ||
| body: Record<string, unknown>, | ||
| ): PayloadRequest { | ||
| // eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
| return { | ||
| routeParams: tenantSlug !== undefined ? { tenantSlug } : undefined, | ||
| json: async () => body, | ||
| } as unknown as PayloadRequest | ||
| } | ||
|
|
||
| describe('duplicatePageToTenant', () => { | ||
| it('creates the page as a draft', async () => { | ||
| const req = buildRequest('42', { | ||
| newPage: { title: 'About', slug: 'about', layout: [] }, | ||
| }) | ||
| await duplicatePageToTenant(req) | ||
| expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ draft: true })) | ||
| }) | ||
|
|
||
| it('appends " - Copy" when a page with the same slug exists', async () => { | ||
| // Slug existence check returns a match | ||
| mockFind | ||
| .mockReset() | ||
| .mockResolvedValueOnce({ docs: [mockTenant] }) | ||
| .mockResolvedValueOnce({ docs: [{ id: 1, slug: 'about-us' }] }) | ||
| const req = buildRequest('42', { | ||
| newPage: { title: 'About Us', slug: 'about-us', layout: [] }, | ||
| }) | ||
| await duplicatePageToTenant(req) | ||
| expect(mockCreate).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| data: expect.objectContaining({ title: 'About Us - Copy', slug: 'about-us-copy' }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('keeps original title and slug when no conflict exists', async () => { | ||
| const req = buildRequest('42', { | ||
| newPage: { title: 'About Us', slug: 'about-us', layout: [] }, | ||
| }) | ||
| await duplicatePageToTenant(req) | ||
| expect(mockCreate).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| data: expect.objectContaining({ title: 'About Us', slug: 'about-us' }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('sets the tenant from the lookup result', async () => { | ||
| const req = buildRequest('42', { | ||
| newPage: { title: 'About', slug: 'about', layout: [] }, | ||
| }) | ||
| await duplicatePageToTenant(req) | ||
| expect(mockCreate).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| data: expect.objectContaining({ tenant: mockTenant }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('passes layout through clearLayoutRelationships', async () => { | ||
| const req = buildRequest('42', { | ||
| newPage: { | ||
| title: 'About', | ||
| slug: 'about', | ||
| layout: [{ blockType: 'singleBlogPost', post: 123, backgroundColor: 'red' }], | ||
| }, | ||
| }) | ||
| await duplicatePageToTenant(req) | ||
| const layout = mockCreate.mock.calls[0][0].data.layout | ||
| expect(layout[0]).not.toHaveProperty('post') | ||
| expect(layout[0]).toHaveProperty('backgroundColor', 'red') | ||
| }) | ||
|
|
||
| it('falls back to empty layout when newPage.layout is absent', async () => { | ||
| const req = buildRequest('42', { newPage: { title: 'About', slug: 'about' } }) | ||
| await duplicatePageToTenant(req) | ||
| expect(mockCreate).toHaveBeenCalledWith( | ||
| expect.objectContaining({ data: expect.objectContaining({ layout: [] }) }), | ||
| ) | ||
| }) | ||
|
|
||
| it('looks up the tenant using tenantSlug from route param', async () => { | ||
| const req = buildRequest('tac', { | ||
| newPage: { title: 'About', slug: 'about', layout: [] }, | ||
| }) | ||
| await duplicatePageToTenant(req) | ||
| expect(mockFind).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| collection: 'tenants', | ||
| where: { slug: { equals: 'tac' } }, | ||
| }), | ||
| ) | ||
| }) | ||
| }) | ||
|
rchlfryn marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Minimal block configs for testing clearLayoutRelationships. | ||
| // These mirror the shape of Payload Block configs without importing from payload. | ||
| export const DEFAULT_BLOCKS = [ | ||
| { | ||
| slug: 'singleBlogPost', | ||
| fields: [ | ||
| { type: 'relationship', name: 'post', relationTo: 'posts' }, | ||
| { type: 'text', name: 'backgroundColor' }, | ||
| ], | ||
| }, | ||
| { | ||
| slug: 'mediaBlock', | ||
| fields: [{ type: 'upload', name: 'media', relationTo: 'media' }], | ||
| }, | ||
| { | ||
| slug: 'sponsorsBlock', | ||
| fields: [ | ||
| { type: 'relationship', name: 'sponsors', relationTo: 'sponsors', hasMany: true }, | ||
| { type: 'text', name: 'sponsorsLayout' }, | ||
| ], | ||
| }, | ||
| { | ||
| slug: 'singleEvent', | ||
| fields: [ | ||
| { type: 'relationship', name: 'event', relationTo: 'events' }, | ||
| { type: 'text', name: 'backgroundColor' }, | ||
| ], | ||
| }, | ||
| { | ||
| slug: 'blogList', | ||
| fields: [ | ||
| { type: 'text', name: 'postOptions' }, | ||
| { | ||
| type: 'group', | ||
| name: 'staticOptions', | ||
| fields: [{ type: 'relationship', name: 'staticPosts', relationTo: 'posts', hasMany: true }], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| slug: 'imageLinkGrid', | ||
| fields: [ | ||
| { | ||
| type: 'array', | ||
| name: 'columns', | ||
| fields: [ | ||
| { type: 'upload', name: 'image', relationTo: 'media' }, | ||
| { type: 'text', name: 'caption' }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ] | ||
|
|
||
| export const NACMediaBlock = { slug: 'nacMedia', fields: [] } | ||
|
|
||
| // Lexical-embedded blocks (used inside richText BlocksFeature, not in DEFAULT_BLOCKS) | ||
| export const ButtonBlock = { | ||
| slug: 'buttonBlock', | ||
| fields: [ | ||
| { | ||
| type: 'group', | ||
| name: 'button', | ||
| fields: [ | ||
| { | ||
| type: 'row', | ||
| fields: [{ type: 'radio', name: 'type' }], | ||
| }, | ||
| { | ||
| type: 'row', | ||
| fields: [ | ||
| { | ||
| type: 'relationship', | ||
| name: 'reference', | ||
| relationTo: ['pages', 'builtInPages', 'posts'], | ||
| }, | ||
| { type: 'text', name: 'url' }, | ||
| { type: 'text', name: 'label' }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| } | ||
|
|
||
| export const CalloutBlock = { | ||
| slug: 'calloutBlock', | ||
| fields: [ | ||
| { type: 'richText', name: 'richText' }, | ||
| { type: 'text', name: 'style' }, | ||
| ], | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.