Skip to content

Commit 08a3dfb

Browse files
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.
1 parent fc83823 commit 08a3dfb

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

test/live-preview/int.spec.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Payload } from 'payload'
1+
import type { FieldSchemaJSON, Payload } from 'payload'
22

33
import {
44
handleMessage,
@@ -7,21 +7,20 @@ import {
77
traverseRichText,
88
} from '@payloadcms/live-preview'
99
import path from 'path'
10-
import { getFileByPath } from 'payload'
10+
import { createClientConfig, getFileByPath, getLocalI18n } from 'payload'
1111
import { fieldSchemaToJSON } from 'payload/shared'
1212
import { fileURLToPath } from 'url'
1313

1414
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
1515
import type { Media, Page, Post, Tenant } from './payload-types.js'
1616

17-
import { Pages } from './collections/Pages.js'
1817
import config from './config.js'
1918
import { postsSlug, tenantsSlug } from './shared.js'
2019

2120
const filename = fileURLToPath(import.meta.url)
2221
const dirname = path.dirname(filename)
2322

24-
const schemaJSON = fieldSchemaToJSON(Pages.fields, config)
23+
let schemaJSON: FieldSchemaJSON
2524

2625
let payload: Payload
2726
let restClient: NextRESTClient
@@ -83,6 +82,23 @@ describe('Collections - Live Preview', () => {
8382
},
8483
file,
8584
})
85+
86+
// get schemaJSON from client config
87+
const resolvedConfig = await config
88+
const i18n = await getLocalI18n({
89+
config: resolvedConfig,
90+
language: 'en',
91+
})
92+
const clientConfig = createClientConfig({
93+
config: resolvedConfig,
94+
i18n,
95+
importMap: {},
96+
})
97+
const clientFields = clientConfig.collections.find((c) => c.slug === 'pages')?.fields
98+
if (!clientFields) {
99+
throw new Error("Couldn't find client fields for 'pages' collection")
100+
}
101+
schemaJSON = fieldSchemaToJSON(clientFields, clientConfig)
86102
})
87103

88104
afterAll(async () => {
@@ -134,27 +150,6 @@ describe('Collections - Live Preview', () => {
134150
expect(handledMessage.title).toEqual('Test Page (Changed)')
135151
})
136152

137-
it('merges data', async () => {
138-
const initialData: Partial<Page> = {
139-
id: '123',
140-
title: 'Test Page',
141-
}
142-
143-
const mergedData = await mergeData({
144-
depth: 1,
145-
fieldSchema: schemaJSON,
146-
incomingData: {
147-
title: 'Test Page (Merged)',
148-
},
149-
initialData,
150-
serverURL,
151-
returnNumberOfRequests: true,
152-
})
153-
154-
expect(mergedData.id).toEqual(initialData.id)
155-
expect(mergedData._numberOfRequests).toEqual(0)
156-
})
157-
158153
it('— strings - merges data', async () => {
159154
const initialData: Partial<Page> = {
160155
title: 'Test Page',

0 commit comments

Comments
 (0)