|
| 1 | +jest.mock('../../src/constants/defaults', () => ({ |
| 2 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 3 | + DEFAULT_BLOCKS: require('./fixtures/mockBlocks').DEFAULT_BLOCKS, |
| 4 | +})) |
| 5 | + |
| 6 | +jest.mock('../../src/blocks/NACMedia/config', () => ({ |
| 7 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 8 | + NACMediaBlock: require('./fixtures/mockBlocks').NACMediaBlock, |
| 9 | +})) |
| 10 | + |
| 11 | +jest.mock('../../src/blocks/Button/config', () => ({ |
| 12 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 13 | + ButtonBlock: require('./fixtures/mockBlocks').ButtonBlock, |
| 14 | +})) |
| 15 | + |
| 16 | +jest.mock('../../src/blocks/Callout/config', () => ({ |
| 17 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 18 | + CalloutBlock: require('./fixtures/mockBlocks').CalloutBlock, |
| 19 | +})) |
| 20 | + |
| 21 | +jest.mock('../../src/payload.config', () => ({})) |
| 22 | + |
| 23 | +jest.mock('payload', () => ({ |
| 24 | + getPayload: jest.fn(), |
| 25 | +})) |
| 26 | + |
| 27 | +import { duplicatePageToTenant } from '@/collections/Pages/endpoints/duplicatePageToTenant' |
| 28 | +import type { Payload, PayloadRequest } from 'payload' |
| 29 | +import { getPayload } from 'payload' |
| 30 | + |
| 31 | +const mockFind = jest.fn() |
| 32 | +const mockCreate = jest.fn() |
| 33 | + |
| 34 | +const mockTenant = { id: 42, name: 'Test AC', slug: 'tac' } |
| 35 | + |
| 36 | +beforeEach(() => { |
| 37 | + jest |
| 38 | + .mocked(getPayload) |
| 39 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 40 | + .mockResolvedValue({ find: mockFind, create: mockCreate } as unknown as Payload) |
| 41 | + // First find call: tenant lookup. Second find call: slug existence check (no conflict by default). |
| 42 | + mockFind |
| 43 | + .mockReset() |
| 44 | + .mockResolvedValueOnce({ docs: [mockTenant] }) |
| 45 | + .mockResolvedValueOnce({ docs: [] }) |
| 46 | + mockCreate.mockReset().mockResolvedValue({ id: 99 }) |
| 47 | +}) |
| 48 | + |
| 49 | +function buildRequest( |
| 50 | + tenantSlug: string | undefined, |
| 51 | + body: Record<string, unknown>, |
| 52 | +): PayloadRequest { |
| 53 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 54 | + return { |
| 55 | + routeParams: tenantSlug !== undefined ? { tenantSlug } : undefined, |
| 56 | + json: async () => body, |
| 57 | + } as unknown as PayloadRequest |
| 58 | +} |
| 59 | + |
| 60 | +describe('duplicatePageToTenant', () => { |
| 61 | + it('creates the page as a draft', async () => { |
| 62 | + const req = buildRequest('42', { |
| 63 | + newPage: { title: 'About', slug: 'about', layout: [] }, |
| 64 | + }) |
| 65 | + await duplicatePageToTenant(req) |
| 66 | + expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ draft: true })) |
| 67 | + }) |
| 68 | + |
| 69 | + it('appends " - Copy" when a page with the same slug exists', async () => { |
| 70 | + // Slug existence check returns a match |
| 71 | + mockFind |
| 72 | + .mockReset() |
| 73 | + .mockResolvedValueOnce({ docs: [mockTenant] }) |
| 74 | + .mockResolvedValueOnce({ docs: [{ id: 1, slug: 'about-us' }] }) |
| 75 | + const req = buildRequest('42', { |
| 76 | + newPage: { title: 'About Us', slug: 'about-us', layout: [] }, |
| 77 | + }) |
| 78 | + await duplicatePageToTenant(req) |
| 79 | + expect(mockCreate).toHaveBeenCalledWith( |
| 80 | + expect.objectContaining({ |
| 81 | + data: expect.objectContaining({ title: 'About Us - Copy', slug: 'about-us-copy' }), |
| 82 | + }), |
| 83 | + ) |
| 84 | + }) |
| 85 | + |
| 86 | + it('keeps original title and slug when no conflict exists', async () => { |
| 87 | + const req = buildRequest('42', { |
| 88 | + newPage: { title: 'About Us', slug: 'about-us', layout: [] }, |
| 89 | + }) |
| 90 | + await duplicatePageToTenant(req) |
| 91 | + expect(mockCreate).toHaveBeenCalledWith( |
| 92 | + expect.objectContaining({ |
| 93 | + data: expect.objectContaining({ title: 'About Us', slug: 'about-us' }), |
| 94 | + }), |
| 95 | + ) |
| 96 | + }) |
| 97 | + |
| 98 | + it('sets the tenant from the lookup result', async () => { |
| 99 | + const req = buildRequest('42', { |
| 100 | + newPage: { title: 'About', slug: 'about', layout: [] }, |
| 101 | + }) |
| 102 | + await duplicatePageToTenant(req) |
| 103 | + expect(mockCreate).toHaveBeenCalledWith( |
| 104 | + expect.objectContaining({ |
| 105 | + data: expect.objectContaining({ tenant: mockTenant }), |
| 106 | + }), |
| 107 | + ) |
| 108 | + }) |
| 109 | + |
| 110 | + it('passes layout through clearLayoutRelationships', async () => { |
| 111 | + const req = buildRequest('42', { |
| 112 | + newPage: { |
| 113 | + title: 'About', |
| 114 | + slug: 'about', |
| 115 | + layout: [{ blockType: 'singleBlogPost', post: 123, backgroundColor: 'red' }], |
| 116 | + }, |
| 117 | + }) |
| 118 | + await duplicatePageToTenant(req) |
| 119 | + const layout = mockCreate.mock.calls[0][0].data.layout |
| 120 | + expect(layout[0]).not.toHaveProperty('post') |
| 121 | + expect(layout[0]).toHaveProperty('backgroundColor', 'red') |
| 122 | + }) |
| 123 | + |
| 124 | + it('falls back to empty layout when newPage.layout is absent', async () => { |
| 125 | + const req = buildRequest('42', { newPage: { title: 'About', slug: 'about' } }) |
| 126 | + await duplicatePageToTenant(req) |
| 127 | + expect(mockCreate).toHaveBeenCalledWith( |
| 128 | + expect.objectContaining({ data: expect.objectContaining({ layout: [] }) }), |
| 129 | + ) |
| 130 | + }) |
| 131 | + |
| 132 | + it('looks up the tenant using tenantSlug from route param', async () => { |
| 133 | + const req = buildRequest('tac', { |
| 134 | + newPage: { title: 'About', slug: 'about', layout: [] }, |
| 135 | + }) |
| 136 | + await duplicatePageToTenant(req) |
| 137 | + expect(mockFind).toHaveBeenCalledWith( |
| 138 | + expect.objectContaining({ |
| 139 | + collection: 'tenants', |
| 140 | + where: { slug: { equals: 'tac' } }, |
| 141 | + }), |
| 142 | + ) |
| 143 | + }) |
| 144 | +}) |
0 commit comments