From 08a3dfbbcb17b81dd08b50c695bfcf3a703250fb Mon Sep 17 00:00:00 2001 From: Anders Semb Hermansen Date: Thu, 22 May 2025 12:44:03 +0200 Subject: [PATCH] chore(live-preview): load schemaJSON from proper client config in integration tests (#12167) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 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` 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. --- test/live-preview/int.spec.ts | 45 ++++++++++++++++------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/test/live-preview/int.spec.ts b/test/live-preview/int.spec.ts index 0e93bc2844..ef392a36da 100644 --- a/test/live-preview/int.spec.ts +++ b/test/live-preview/int.spec.ts @@ -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 = { - 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 = { title: 'Test Page',