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