test: resolves select type errors (#11235)

This PR fixes all typescript errors inside the `select` test suite.
This commit is contained in:
Sasha
2025-02-18 00:33:44 +02:00
committed by GitHub
parent d49de7bdf8
commit e78500feef

View File

@@ -1,5 +1,6 @@
import path from 'path' import path from 'path'
import { deepCopyObject, type Payload } from 'payload' import { deepCopyObject, type Payload } from 'payload'
import { assert } from 'ts-essentials'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import type { NextRESTClient } from '../helpers/NextRESTClient.js' import type { NextRESTClient } from '../helpers/NextRESTClient.js'
@@ -28,6 +29,8 @@ describe('Select', () => {
// --__--__--__--__--__--__--__--__--__ // --__--__--__--__--__--__--__--__--__
beforeAll(async () => { beforeAll(async () => {
const initialized = await initPayloadInt(dirname) const initialized = await initPayloadInt(dirname)
assert(initialized.payload)
assert(initialized.restClient)
;({ payload, restClient } = initialized) ;({ payload, restClient } = initialized)
}) })
@@ -213,7 +216,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
group: { group: {
text: post.group.text, text: post.group?.text,
}, },
}) })
}) })
@@ -249,7 +252,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
tab: { tab: {
text: post.tab.text, text: post.tab?.text,
}, },
}) })
}) })
@@ -282,7 +285,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
array: post.array.map((item) => ({ id: item.id })), array: post.array?.map((item) => ({ id: item.id })),
}) })
}) })
@@ -316,7 +319,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
array: post.array.map((item) => ({ array: post.array?.map((item) => ({
id: item.id, id: item.id,
text: item.text, text: item.text,
})), })),
@@ -335,7 +338,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
blocks: post.blocks.map((block) => ({ blockType: block.blockType, id: block.id })), blocks: post.blocks?.map((block) => ({ blockType: block.blockType, id: block.id })),
}) })
}) })
@@ -369,7 +372,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
blocks: post.blocks.map((block) => blocks: post.blocks?.map((block) =>
// eslint-disable-next-line jest/no-conditional-in-test // eslint-disable-next-line jest/no-conditional-in-test
block.blockType === 'cta' block.blockType === 'cta'
? block ? block
@@ -395,7 +398,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
blocks: post.blocks.map((block) => blocks: post.blocks?.map((block) =>
// eslint-disable-next-line jest/no-conditional-in-test // eslint-disable-next-line jest/no-conditional-in-test
block.blockType === 'cta' block.blockType === 'cta'
? { id: block.id, blockType: block.blockType, ctaText: block.ctaText } ? { id: block.id, blockType: block.blockType, ctaText: block.ctaText }
@@ -567,7 +570,7 @@ describe('Select', () => {
const expected = deepCopyObject(post) const expected = deepCopyObject(post)
delete expected.group.text delete expected.group?.text
expect(res).toStrictEqual(expected) expect(res).toStrictEqual(expected)
}) })
@@ -603,7 +606,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
...post, ...post,
array: post.array.map((item) => ({ array: post.array?.map((item) => ({
id: item.id, id: item.id,
number: item.number, number: item.number,
})), })),
@@ -641,7 +644,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
...post, ...post,
blocks: post.blocks.map((block) => blocks: post.blocks?.map((block) =>
// eslint-disable-next-line jest/no-conditional-in-test // eslint-disable-next-line jest/no-conditional-in-test
block.blockType === 'cta' ? { id: block.id, blockType: block.blockType } : block, block.blockType === 'cta' ? { id: block.id, blockType: block.blockType } : block,
), ),
@@ -662,8 +665,11 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
...post, ...post,
blocks: post.blocks.map((block) => { blocks: post.blocks?.map((block) => {
delete block['ctaText'] // eslint-disable-next-line jest/no-conditional-in-test
if ('ctaText' in block) {
delete block['ctaText']
}
return block return block
}), }),
@@ -671,6 +677,7 @@ describe('Select', () => {
}) })
it('should exclude a point field', async () => { it('should exclude a point field', async () => {
// eslint-disable-next-line jest/no-conditional-in-test
if (payload.db.name === 'sqlite') { if (payload.db.name === 'sqlite') {
return return
} }
@@ -807,7 +814,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
group: { group: {
text: post.group.text, text: post.group?.text,
}, },
}) })
}) })
@@ -826,7 +833,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
groupSecond: { groupSecond: {
text: post.groupSecond.text, text: post.groupSecond?.text,
}, },
}) })
}) })
@@ -842,7 +849,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
array: post.array.map((item) => ({ id: item.id })), array: post.array?.map((item) => ({ id: item.id })),
}) })
}) })
@@ -874,7 +881,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
array: post.array.map((item) => ({ array: post.array?.map((item) => ({
id: item.id, id: item.id,
text: item.text, text: item.text,
})), })),
@@ -894,7 +901,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
arraySecond: post.arraySecond.map((item) => ({ arraySecond: post.arraySecond?.map((item) => ({
id: item.id, id: item.id,
text: item.text, text: item.text,
})), })),
@@ -912,7 +919,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
blocks: post.blocks.map((block) => ({ blockType: block.blockType, id: block.id })), blocks: post.blocks?.map((block) => ({ blockType: block.blockType, id: block.id })),
}) })
}) })
@@ -944,7 +951,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
blocks: post.blocks.map((block) => blocks: post.blocks?.map((block) =>
// eslint-disable-next-line jest/no-conditional-in-test // eslint-disable-next-line jest/no-conditional-in-test
block.blockType === 'cta' block.blockType === 'cta'
? block ? block
@@ -969,7 +976,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
blocks: post.blocks.map((block) => blocks: post.blocks?.map((block) =>
// eslint-disable-next-line jest/no-conditional-in-test // eslint-disable-next-line jest/no-conditional-in-test
block.blockType === 'cta' block.blockType === 'cta'
? { id: block.id, blockType: block.blockType, ctaText: block.ctaText } ? { id: block.id, blockType: block.blockType, ctaText: block.ctaText }
@@ -994,7 +1001,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
blocksSecond: post.blocksSecond.map((block) => blocksSecond: post.blocksSecond?.map((block) =>
// eslint-disable-next-line jest/no-conditional-in-test // eslint-disable-next-line jest/no-conditional-in-test
block.blockType === 'second' block.blockType === 'second'
? { id: block.id, blockType: block.blockType, text: block.text } ? { id: block.id, blockType: block.blockType, text: block.text }
@@ -1019,7 +1026,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
blocksSecond: post.blocksSecond.map((block) => blocksSecond: post.blocksSecond?.map((block) =>
// eslint-disable-next-line jest/no-conditional-in-test // eslint-disable-next-line jest/no-conditional-in-test
block.blockType === 'first' block.blockType === 'first'
? { id: block.id, blockType: block.blockType, firstText: block.firstText } ? { id: block.id, blockType: block.blockType, firstText: block.firstText }
@@ -1144,7 +1151,7 @@ describe('Select', () => {
const expected = deepCopyObject(post) const expected = deepCopyObject(post)
delete expected.group.text delete expected.group?.text
expect(res).toStrictEqual(expected) expect(res).toStrictEqual(expected)
}) })
@@ -1162,7 +1169,7 @@ describe('Select', () => {
const expected = deepCopyObject(post) const expected = deepCopyObject(post)
delete expected.groupSecond.text delete expected.groupSecond?.text
expect(res).toStrictEqual(expected) expect(res).toStrictEqual(expected)
}) })
@@ -1196,7 +1203,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
...post, ...post,
array: post.array.map((item) => ({ array: post.array?.map((item) => ({
id: item.id, id: item.id,
number: item.number, number: item.number,
})), })),
@@ -1216,7 +1223,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
...post, ...post,
arraySecond: post.arraySecond.map((item) => ({ arraySecond: post.arraySecond?.map((item) => ({
id: item.id, id: item.id,
number: item.number, number: item.number,
})), })),
@@ -1252,7 +1259,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
...post, ...post,
blocks: post.blocks.map((block) => blocks: post.blocks?.map((block) =>
// eslint-disable-next-line jest/no-conditional-in-test // eslint-disable-next-line jest/no-conditional-in-test
block.blockType === 'cta' ? { id: block.id, blockType: block.blockType } : block, block.blockType === 'cta' ? { id: block.id, blockType: block.blockType } : block,
), ),
@@ -1272,8 +1279,11 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
...post, ...post,
blocks: post.blocks.map((block) => { blocks: post.blocks?.map((block) => {
delete block['ctaText'] // eslint-disable-next-line jest/no-conditional-in-test
if ('ctaText' in block) {
delete block['ctaText']
}
return block return block
}), }),
@@ -1293,7 +1303,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
...post, ...post,
blocksSecond: post.blocksSecond.map((block) => { blocksSecond: post.blocksSecond?.map((block) => {
// eslint-disable-next-line jest/no-conditional-in-test // eslint-disable-next-line jest/no-conditional-in-test
if (block.blockType === 'second') { if (block.blockType === 'second') {
delete block['text'] delete block['text']
@@ -1317,8 +1327,11 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
...post, ...post,
blocksSecond: post.blocksSecond.map((block) => { blocksSecond: post.blocksSecond?.map((block) => {
delete block['firstText'] // eslint-disable-next-line jest/no-conditional-in-test
if ('firstText' in block) {
delete block['firstText']
}
return block return block
}), }),
@@ -1346,10 +1359,10 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
group: { group: {
array: post.group.array.map((item) => ({ array: post.group?.array?.map((item) => ({
id: item.id, id: item.id,
group: { group: {
text: item.group.text, text: item.group?.text,
}, },
})), })),
}, },
@@ -1366,7 +1379,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
group: { group: {
array: post.group.array.map((item) => ({ array: post.group?.array?.map((item) => ({
id: item.id, id: item.id,
group: item.group, group: item.group,
})), })),
@@ -1384,7 +1397,7 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
group: { group: {
blocks: post.group.blocks.map((item) => ({ blocks: post.group?.blocks?.map((item) => ({
id: item.id, id: item.id,
blockType: item.blockType, blockType: item.blockType,
text: item.text, text: item.text,
@@ -1402,9 +1415,9 @@ describe('Select', () => {
expect(res).toStrictEqual({ expect(res).toStrictEqual({
id: postId, id: postId,
arrayTop: post.arrayTop.map((item) => ({ arrayTop: post.arrayTop?.map((item) => ({
id: item.id, id: item.id,
arrayNested: item.arrayNested.map((item) => ({ arrayNested: item.arrayNested?.map((item) => ({
id: item.id, id: item.id,
text: item.text, text: item.text,
})), })),
@@ -1528,7 +1541,7 @@ describe('Select', () => {
expect(res.docs[0]).toStrictEqual({ expect(res.docs[0]).toStrictEqual({
id: postId, id: postId,
array: post.array.map((each) => ({ id: each.id })), array: post.array?.map((each) => ({ id: each.id })),
}) })
}) })
@@ -1548,7 +1561,7 @@ describe('Select', () => {
expect(res.docs[0]).toStrictEqual({ expect(res.docs[0]).toStrictEqual({
id: postId, id: postId,
blocks: post.blocks.map((each) => ({ blockType: each.blockType, id: each.id })), blocks: post.blocks?.map((each) => ({ blockType: each.blockType, id: each.id })),
}) })
}) })
@@ -1570,6 +1583,8 @@ describe('Select', () => {
const doc = res.docs[0] const doc = res.docs[0]
assert(doc)
expect(doc.createdAt).toBeUndefined() expect(doc.createdAt).toBeUndefined()
expect(doc.updatedAt).toBeUndefined() expect(doc.updatedAt).toBeUndefined()
expect(doc.version.number).toBeUndefined() expect(doc.version.number).toBeUndefined()
@@ -1663,6 +1678,8 @@ describe('Select', () => {
select: { text: true }, select: { text: true },
}) })
assert(res.docs[0])
expect(Object.keys(res.docs[0])).toStrictEqual(['id', 'text']) expect(Object.keys(res.docs[0])).toStrictEqual(['id', 'text'])
}) })
@@ -1691,6 +1708,8 @@ describe('Select', () => {
select: { text: true }, select: { text: true },
}) })
assert(res.docs[0])
expect(Object.keys(res.docs[0])).toStrictEqual(['id', 'text']) expect(Object.keys(res.docs[0])).toStrictEqual(['id', 'text'])
}) })
@@ -1793,7 +1812,7 @@ describe('Select', () => {
expect(res).toMatchObject({ expect(res).toMatchObject({
id: postId, id: postId,
group: { group: {
text: post.group.text, text: post.group?.text,
}, },
}) })
}) })
@@ -1875,7 +1894,7 @@ describe('Select', () => {
const expected = deepCopyObject(post) const expected = deepCopyObject(post)
delete expected.group.text delete expected.group?.text
expect(res).toMatchObject(expected) expect(res).toMatchObject(expected)
}) })
@@ -1926,24 +1945,25 @@ describe('Select', () => {
additional: 'additional-data', additional: 'additional-data',
}, },
}) })
expectedHomePage = { expectedHomePage = {
id: homePage.id, id: homePage.id,
slug: homePage.slug, slug: homePage.slug,
array: [ array: [
{ {
id: homePage.array[0].id, id: homePage.array![0]!.id!,
title: homePage.array[0].title, title: homePage.array![0]!.title!,
}, },
], ],
blocks: [ blocks: [
{ {
blockType: homePage.blocks[0].blockType, blockType: homePage.blocks![0]!.blockType,
id: homePage.blocks[0].id, id: homePage.blocks![0]!.id!,
title: homePage.blocks[0].title, title: homePage.blocks![0]!.title!,
}, },
], ],
} }
expectedHomePageOverride = { id: homePage.id, additional: homePage.additional } expectedHomePageOverride = { id: homePage.id, additional: homePage.additional! }
aboutPage = await payload.create({ aboutPage = await payload.create({
depth: 0, depth: 0,
collection: 'pages', collection: 'pages',
@@ -2002,19 +2022,11 @@ describe('Select', () => {
it('local API - should populate with the defaultPopulate select shape', async () => { it('local API - should populate with the defaultPopulate select shape', async () => {
const result = await payload.findByID({ collection: 'pages', depth: 1, id: aboutPage.id }) const result = await payload.findByID({ collection: 'pages', depth: 1, id: aboutPage.id })
const { const block = result.content![0]!
content: [
{ const { doc, docHasManyPoly, docMany, docPoly } = block.link
link: { doc, docHasManyPoly, docMany, docPoly }, const richTextSlateRel = block.richTextSlate![0]!
richTextSlate: [richTextSlateRel], const richTextLexicalRel = block.richTextLexical!.root.children[0]!
richTextLexical: {
root: {
children: [richTextLexicalRel],
},
},
},
],
} = result
expect(doc).toStrictEqual(expectedHomePage) expect(doc).toStrictEqual(expectedHomePage)
expect(docMany).toStrictEqual([expectedHomePage]) expect(docMany).toStrictEqual([expectedHomePage])
@@ -2155,19 +2167,11 @@ describe('Select', () => {
expect(resultFind).toStrictEqual(result) expect(resultFind).toStrictEqual(result)
const { const block = result.content![0]!
content: [
{ const { doc, docHasManyPoly, docMany, docPoly } = block.link
link: { doc, docHasManyPoly, docMany, docPoly }, const richTextSlateRel = block.richTextSlate![0]!
richTextSlate: [richTextSlateRel], const richTextLexicalRel = block.richTextLexical!.root.children[0]!
richTextLexical: {
root: {
children: [richTextLexicalRel],
},
},
},
],
} = result
expect(doc).toStrictEqual(expectedHomePageOverride) expect(doc).toStrictEqual(expectedHomePageOverride)
expect(docMany).toStrictEqual([expectedHomePageOverride]) expect(docMany).toStrictEqual([expectedHomePageOverride])
@@ -2250,8 +2254,11 @@ describe('Select', () => {
depth: 3, depth: 3,
populate: { pages: { slug: true, relatedPage: true } }, populate: { pages: { slug: true, relatedPage: true } },
}) })
expect(result.relatedPage.id).toBe(page_2.id)
expect(result.relatedPage.relatedPage as Page).toStrictEqual({ const relatedPage = result.relatedPage as Page
expect(relatedPage.id).toBe(page_2.id)
expect(relatedPage.relatedPage).toStrictEqual({
id: page_1.id, id: page_1.id,
slug: page_1.slug, slug: page_1.slug,
relatedPage: null, relatedPage: null,