Files
ipal-kit/test/hooksReq.test.ts
Radosław SmolińskiandClaude Opus 5.5 14fd2c497f Server entries no longer load 'use client' modules
The main entry and next/middleware re-exported module barrels that also
export React client components (LanguageSwitcher, AlternateLinks*,
AccessibilityWidget), so payload.config, the CLI and the proxy bundle
evaluated React UI. They now import concrete files; i18n client
components moved to modules/i18n/client.ts. test/entryGraph guards it.
Also lint fixes in tests added on this branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 17:17:42 +02:00

47 lines
2.2 KiB
TypeScript

/** Hooks run their Local API calls inside the save's request (transaction) without corrupting it. */
import { describe, expect, test, vi } from 'vitest'
import { buildCrossLocaleFieldSync } from '../src/modules/hooks/buildCrossLocaleFieldSync.js'
import { buildPreventDeleteSystemPage } from '../src/modules/hooks/preventDeleteSystemPage.js'
const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { code: 'en', label: 'EN' }] } as const
/** Mimics the Local API: a call with `req` sets req.locale / req.fallbackLocale to its own. */
function fakeReq() {
const req: Record<string, unknown> = { fallbackLocale: 'pl', locale: 'pl' }
const adopt = (args: { locale?: string; req?: Record<string, unknown> }) => {
if (args.req && args.locale) {
args.req.locale = args.locale
args.req.fallbackLocale = 'null'
}
}
const findByID = vi.fn(async (args: { locale?: string; req?: Record<string, unknown> }) => {
adopt(args)
return { id: 1, layout: [{ id: 'r1', blockType: 'hero', image: 7 }] }
})
const update = vi.fn(async (args: { locale?: string; req?: Record<string, unknown> }) => {
adopt(args)
return {}
})
req.payload = { findByID, findGlobal: vi.fn(async () => ({})), logger: { error: vi.fn() }, update }
return { findByID, req, update }
}
describe('hooks pass req', () => {
test('cross-locale sync reads and writes in the request, then restores its locale', async () => {
const { findByID, req, update } = fakeReq()
const hook = buildCrossLocaleFieldSync({ collection: 'pages', config, fields: ['image'] })
await hook({ doc: { id: 1, layout: [{ id: 'r1', blockType: 'hero', image: 9 }] }, req } as never)
expect(findByID).toHaveBeenCalledWith(expect.objectContaining({ locale: 'en', req }))
expect(update).toHaveBeenCalledWith(expect.objectContaining({ locale: 'en', req }))
expect(req.locale).toBe('pl')
expect(req.fallbackLocale).toBe('pl')
})
test('preventDeleteSystemPage reads Site Settings in the request', async () => {
const { req } = fakeReq()
await buildPreventDeleteSystemPage()({ id: 3, req } as never)
expect((req.payload as { findGlobal: ReturnType<typeof vi.fn> }).findGlobal).toHaveBeenCalledWith(expect.objectContaining({ req }))
})
})