chore(live-preview): load schemaJSON from proper client config in integration tests (#12167)

### What?

The integration tests in live-preview has been using the
`fieldSchemaToJSON` method with wrong params/types.

It's defined as
```
export const fieldSchemaToJSON = (fields: ClientField[], config: ClientConfig): FieldSchemaJSON
```

In the test setup
`fields` was set to `Pages.fields` which was `Field[]`, not the expected
`ClientField[]`
`config` was set to `config` which was `Promise<SanitizedConfig>` not
the expected `ClientConfig`

### Why?

I'm working on some other changes to live-preview where I need the
proper values wired up correctly to properly add integration tests.

The test has worked up until now because Field is very similar to
ClientField. But it should test with the correct type.

### How?

By creating the clientConfig and using the correct types/params when
calling fieldSchemaToJSON in the test setup.

**Note:** Removed test "Collections - Live Preview › merges data", the
test worked before because **id** field is not part of Field, but part
of ClientField. So test code does not behave like this in real scenario
when real ClientField is used. There are lots of real tests for correct
data, removed this one which seems very simple and not correct.
This commit is contained in:
Anders Semb Hermansen
2025-05-22 12:44:03 +02:00
committed by GitHub
parent fc83823e5d
commit 08a3dfbbcb

View File

@@ -1,4 +1,4 @@
import type { Payload } from 'payload'
import type { FieldSchemaJSON, Payload } from 'payload'
import {
handleMessage,
@@ -7,21 +7,20 @@ import {
traverseRichText,
} from '@payloadcms/live-preview'
import path from 'path'
import { getFileByPath } from 'payload'
import { createClientConfig, getFileByPath, getLocalI18n } from 'payload'
import { fieldSchemaToJSON } from 'payload/shared'
import { fileURLToPath } from 'url'
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import type { Media, Page, Post, Tenant } from './payload-types.js'
import { Pages } from './collections/Pages.js'
import config from './config.js'
import { postsSlug, tenantsSlug } from './shared.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const schemaJSON = fieldSchemaToJSON(Pages.fields, config)
let schemaJSON: FieldSchemaJSON
let payload: Payload
let restClient: NextRESTClient
@@ -83,6 +82,23 @@ describe('Collections - Live Preview', () => {
},
file,
})
// get schemaJSON from client config
const resolvedConfig = await config
const i18n = await getLocalI18n({
config: resolvedConfig,
language: 'en',
})
const clientConfig = createClientConfig({
config: resolvedConfig,
i18n,
importMap: {},
})
const clientFields = clientConfig.collections.find((c) => c.slug === 'pages')?.fields
if (!clientFields) {
throw new Error("Couldn't find client fields for 'pages' collection")
}
schemaJSON = fieldSchemaToJSON(clientFields, clientConfig)
})
afterAll(async () => {
@@ -134,27 +150,6 @@ describe('Collections - Live Preview', () => {
expect(handledMessage.title).toEqual('Test Page (Changed)')
})
it('merges data', async () => {
const initialData: Partial<Page> = {
id: '123',
title: 'Test Page',
}
const mergedData = await mergeData({
depth: 1,
fieldSchema: schemaJSON,
incomingData: {
title: 'Test Page (Merged)',
},
initialData,
serverURL,
returnNumberOfRequests: true,
})
expect(mergedData.id).toEqual(initialData.id)
expect(mergedData._numberOfRequests).toEqual(0)
})
it('— strings - merges data', async () => {
const initialData: Partial<Page> = {
title: 'Test Page',