Files
payload/test/hooks/e2e.spec.ts
Jarrod Flesch 4b302f22a0 fix: incorrect formState after doc save (#9573)
### What?
When the document is saved the formState was not being reset from the
server.

### Why?
getFormState was not being called onSuccess of the form submission

### How?
The `Form` onSuccess function now allows for an optional return type of
`FormState` if the functions returns formState then we check to see if
that differs from the current formState on the client. If it does then
we dispatch the `REPLACE_STATE` action with the newState.

Fixes https://github.com/payloadcms/payload/issues/9423
2024-11-27 16:45:10 -05:00

75 lines
2.3 KiB
TypeScript

import type { Page } from '@playwright/test'
import type { CollectionSlug } from 'payload'
import { expect, test } from '@playwright/test'
import path from 'path'
import { fileURLToPath } from 'url'
import type { PayloadTestSDK } from '../helpers/sdk/index.js'
import type { Config } from './payload-types.js'
import { ensureCompilationIsDone, initPageConsoleErrorCatch, saveDocAndAssert } from '../helpers.js'
import { AdminUrlUtil } from '../helpers/adminUrlUtil.js'
import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js'
import { TEST_TIMEOUT_LONG } from '../playwright.config.js'
import { beforeValidateSlug } from './collectionSlugs.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const { beforeAll, beforeEach, describe } = test
let payload: PayloadTestSDK<Config>
describe('hooks', () => {
let url: AdminUrlUtil
let page: Page
let serverURL: string
beforeAll(async ({ browser }, testInfo) => {
testInfo.setTimeout(TEST_TIMEOUT_LONG)
;({ payload, serverURL } = await initPayloadE2ENoConfig<Config>({ dirname }))
url = new AdminUrlUtil(serverURL, 'before-validate')
const context = await browser.newContext()
page = await context.newPage()
initPageConsoleErrorCatch(page)
await ensureCompilationIsDone({ page, serverURL })
})
beforeEach(async () => {
await ensureCompilationIsDone({ page, serverURL })
await clearAllDocs()
})
test('should replace value with before validate response', async () => {
await page.goto(url.create)
await page.locator('#field-title').fill('should replace value with before validate response')
await saveDocAndAssert(page)
await expect(page.locator('#field-title')).toHaveValue('reset in beforeValidate')
await page
.locator('#field-title')
.fill('should replace value with before validate response again')
await saveDocAndAssert(page)
await expect(page.locator('#field-title')).toHaveValue('reset in beforeValidate')
})
})
async function clearAllDocs(): Promise<void> {
await clearCollectionDocs(beforeValidateSlug)
}
async function clearCollectionDocs(collectionSlug: CollectionSlug): Promise<void> {
await payload.delete({
collection: collectionSlug,
where: {
id: { exists: true },
},
})
}