Skip to content

Commit ee0ac7f

Browse files
authored
test: resolves locked-documents type errors (#11223)
This update addresses all TS errors in the e2e & int tests for locked documents. - Corrects type mismatches - Adds type assertions
1 parent e6fea1d commit ee0ac7f

File tree

2 files changed

+66
-66
lines changed

2 files changed

+66
-66
lines changed

test/locked-documents/e2e.spec.ts

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Page } from '@playwright/test'
2-
import type { TypeWithID } from 'payload'
32

43
import { expect, test } from '@playwright/test'
54
import * as path from 'path'
@@ -8,7 +7,14 @@ import { wait } from 'payload/shared'
87
import { fileURLToPath } from 'url'
98

109
import type { PayloadTestSDK } from '../helpers/sdk/index.js'
11-
import type { Config } from './payload-types.js'
10+
import type {
11+
Config,
12+
Page as PageType,
13+
PayloadLockedDocument,
14+
Post,
15+
Test,
16+
User,
17+
} from './payload-types.js'
1218

1319
import {
1420
ensureCompilationIsDone,
@@ -19,7 +25,6 @@ import {
1925
import { AdminUrlUtil } from '../helpers/adminUrlUtil.js'
2026
import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js'
2127
import { POLL_TOPASS_TIMEOUT, TEST_TIMEOUT_LONG } from '../playwright.config.js'
22-
import { postsSlug } from './collections/Posts/index.js'
2328

2429
const filename = fileURLToPath(import.meta.url)
2530
const dirname = path.dirname(filename)
@@ -80,12 +85,12 @@ describe('Locked Documents', () => {
8085
})
8186

8287
describe('list view - collections', () => {
83-
let postDoc
84-
let anotherPostDoc
85-
let user2
86-
let lockedDoc
87-
let testDoc
88-
let testLockedDoc
88+
let postDoc: Post
89+
let anotherPostDoc: Post
90+
let user2: User
91+
let lockedDoc: PayloadLockedDocument
92+
let testDoc: Test
93+
let testLockedDoc: PayloadLockedDocument
8994

9095
beforeAll(async () => {
9196
postDoc = await createPostDoc({
@@ -326,14 +331,14 @@ describe('Locked Documents', () => {
326331
})
327332

328333
describe('document locking / unlocking - one user', () => {
329-
let postDoc
330-
let postDocTwo
331-
let expiredDocOne
332-
let expiredLockedDocOne
333-
let expiredDocTwo
334-
let expiredLockedDocTwo
335-
let testDoc
336-
let user2
334+
let postDoc: Post
335+
let postDocTwo: Post
336+
let expiredDocOne: Test
337+
let expiredLockedDocOne: PayloadLockedDocument
338+
let expiredDocTwo: Test
339+
let expiredLockedDocTwo: PayloadLockedDocument
340+
let testDoc: Test
341+
let user2: User
337342

338343
beforeAll(async () => {
339344
postDoc = await createPostDoc({
@@ -636,11 +641,11 @@ describe('Locked Documents', () => {
636641
})
637642

638643
describe('document locking - incoming user', () => {
639-
let postDoc
640-
let user2
641-
let lockedDoc
642-
let expiredTestDoc
643-
let expiredTestLockedDoc
644+
let postDoc: Post
645+
let user2: User
646+
let lockedDoc: PayloadLockedDocument
647+
let expiredTestDoc: Test
648+
let expiredTestLockedDoc: PayloadLockedDocument
644649

645650
beforeAll(async () => {
646651
postDoc = await createPostDoc({
@@ -775,9 +780,9 @@ describe('Locked Documents', () => {
775780
})
776781

777782
describe('document take over - modal - incoming user', () => {
778-
let postDoc
779-
let user2
780-
let lockedDoc
783+
let postDoc: Post
784+
let user2: User
785+
let lockedDoc: PayloadLockedDocument
781786

782787
beforeAll(async () => {
783788
postDoc = await createPostDoc({
@@ -855,7 +860,7 @@ describe('Locked Documents', () => {
855860

856861
const userEmail =
857862
// eslint-disable-next-line playwright/no-conditional-in-test
858-
lockedDoc.docs[0].user.value &&
863+
lockedDoc.docs[0]?.user.value &&
859864
typeof lockedDoc.docs[0].user.value === 'object' &&
860865
'email' in lockedDoc.docs[0].user.value &&
861866
lockedDoc.docs[0].user.value.email
@@ -865,9 +870,9 @@ describe('Locked Documents', () => {
865870
})
866871

867872
describe('document take over - doc - incoming user', () => {
868-
let postDoc
869-
let user2
870-
let lockedDoc
873+
let postDoc: Post
874+
let user2: User
875+
let lockedDoc: PayloadLockedDocument
871876

872877
beforeAll(async () => {
873878
postDoc = await createPostDoc({
@@ -947,7 +952,7 @@ describe('Locked Documents', () => {
947952

948953
const userEmail =
949954
// eslint-disable-next-line playwright/no-conditional-in-test
950-
lockedDoc.docs[0].user.value &&
955+
lockedDoc.docs[0]?.user.value &&
951956
typeof lockedDoc.docs[0].user.value === 'object' &&
952957
'email' in lockedDoc.docs[0].user.value &&
953958
lockedDoc.docs[0].user.value.email
@@ -957,8 +962,8 @@ describe('Locked Documents', () => {
957962
})
958963

959964
describe('document locking - previous user', () => {
960-
let postDoc
961-
let user2
965+
let postDoc: Post
966+
let user2: User
962967

963968
beforeAll(async () => {
964969
postDoc = await createPostDoc({
@@ -1011,7 +1016,7 @@ describe('Locked Documents', () => {
10111016

10121017
// Update payload-locks collection document with different user
10131018
await payload.update({
1014-
id: lockedDoc.docs[0].id,
1019+
id: lockedDoc.docs[0]?.id as number | string,
10151020
collection: lockedDocumentCollection,
10161021
data: {
10171022
user: {
@@ -1033,7 +1038,7 @@ describe('Locked Documents', () => {
10331038

10341039
await payload.delete({
10351040
collection: lockedDocumentCollection,
1036-
id: lockedDoc.docs[0].id,
1041+
id: lockedDoc.docs[0]?.id,
10371042
})
10381043
})
10391044

@@ -1062,7 +1067,7 @@ describe('Locked Documents', () => {
10621067

10631068
// Update payload-locks collection document with different user
10641069
await payload.update({
1065-
id: lockedDoc.docs[0].id,
1070+
id: lockedDoc.docs[0]?.id as number | string,
10661071
collection: lockedDocumentCollection,
10671072
data: {
10681073
user: {
@@ -1089,7 +1094,7 @@ describe('Locked Documents', () => {
10891094

10901095
await payload.delete({
10911096
collection: lockedDocumentCollection,
1092-
id: lockedDoc.docs[0].id,
1097+
id: lockedDoc.docs[0]?.id,
10931098
})
10941099
})
10951100

@@ -1118,7 +1123,7 @@ describe('Locked Documents', () => {
11181123

11191124
// Update payload-locks collection document with different user
11201125
await payload.update({
1121-
id: lockedDoc.docs[0].id,
1126+
id: lockedDoc.docs[0]?.id as number | string,
11221127
collection: lockedDocumentCollection,
11231128
data: {
11241129
user: {
@@ -1151,9 +1156,9 @@ describe('Locked Documents', () => {
11511156
})
11521157

11531158
describe('dashboard - globals', () => {
1154-
let user2
1155-
let lockedMenuGlobal
1156-
let lockedAdminGlobal
1159+
let user2: User
1160+
let lockedMenuGlobal: PayloadLockedDocument
1161+
let lockedAdminGlobal: PayloadLockedDocument
11571162

11581163
beforeAll(async () => {
11591164
user2 = await payload.create({
@@ -1298,27 +1303,23 @@ describe('Locked Documents', () => {
12981303
})
12991304
})
13001305

1301-
async function createPageDoc(data: any): Promise<Record<string, unknown> & TypeWithID> {
1306+
async function createPageDoc(data: Partial<PageType>): Promise<PageType> {
13021307
return payload.create({
13031308
collection: 'pages',
13041309
data,
1305-
}) as unknown as Promise<Record<string, unknown> & TypeWithID>
1310+
}) as unknown as Promise<PageType>
13061311
}
13071312

1308-
async function createPostDoc(data: any): Promise<Record<string, unknown> & TypeWithID> {
1313+
async function createPostDoc(data: Partial<Post>): Promise<Post> {
13091314
return payload.create({
13101315
collection: 'posts',
13111316
data,
1312-
}) as unknown as Promise<Record<string, unknown> & TypeWithID>
1317+
}) as unknown as Promise<Post>
13131318
}
13141319

1315-
async function createTestDoc(data: any): Promise<Record<string, unknown> & TypeWithID> {
1320+
async function createTestDoc(data: Partial<Test>): Promise<Test> {
13161321
return payload.create({
13171322
collection: 'tests',
13181323
data,
1319-
}) as unknown as Promise<Record<string, unknown> & TypeWithID>
1320-
}
1321-
1322-
async function deleteAllPosts() {
1323-
await payload.delete({ collection: postsSlug, where: { id: { exists: true } } })
1324+
}) as unknown as Promise<Test>
13241325
}

test/locked-documents/int.spec.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { Payload, SanitizedCollectionConfig } from 'payload'
1+
import type { Payload, SanitizedCollectionConfig, SanitizedGlobalConfig } from 'payload'
22

33
import path from 'path'
44
import { Locked, NotFound } from 'payload'
55
import { wait } from 'payload/shared'
66
import { fileURLToPath } from 'url'
77

88
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
9-
import type { Menu, Page, Post } from './payload-types.js'
9+
import type { Menu, Page, Post, User } from './payload-types.js'
1010

1111
import { devUser } from '../credentials.js'
1212
import { initPayloadInt } from '../helpers/initPayloadInt.js'
@@ -32,9 +32,12 @@ describe('Locked documents', () => {
3232
let postConfig: SanitizedCollectionConfig
3333

3434
beforeAll(async () => {
35+
// @ts-expect-error: initPayloadInt does not have a proper type definition
3536
;({ payload, restClient } = await initPayloadInt(dirname))
3637

37-
postConfig = payload.config.collections.find(({ slug }) => slug === postsSlug)
38+
postConfig = payload.config.collections.find(
39+
({ slug }) => slug === postsSlug,
40+
) as SanitizedCollectionConfig
3841

3942
const loginResult = await payload.login({
4043
collection: 'users',
@@ -45,7 +48,8 @@ describe('Locked documents', () => {
4548
})
4649

4750
user = loginResult.user
48-
token = loginResult.token
51+
52+
token = loginResult.token as string
4953

5054
user2 = await payload.create({
5155
collection: 'users',
@@ -198,7 +202,9 @@ describe('Locked documents', () => {
198202

199203
it('should allow update of stale locked document - global', async () => {
200204
// Set lock duration to 1 second for testing purposes
201-
const globalConfig = payload.config.globals.find(({ slug }) => slug === menuSlug)
205+
const globalConfig = payload.config.globals.find(
206+
({ slug }) => slug === menuSlug,
207+
) as SanitizedGlobalConfig
202208
globalConfig.lockDocuments = { duration: 1 }
203209
// Give locking ownership to another user
204210
const lockedGlobalInstance = await payload.create({
@@ -266,7 +272,6 @@ describe('Locked documents', () => {
266272
relationTo: 'posts',
267273
value: newPost.id,
268274
},
269-
editedAt: new Date().toISOString(),
270275
globalSlug: undefined,
271276
user: {
272277
relationTo: 'users',
@@ -284,7 +289,7 @@ describe('Locked documents', () => {
284289
overrideLock: false, // necessary to trigger the lock check
285290
id: newPost.id,
286291
})
287-
} catch (error) {
292+
} catch (error: any) {
288293
expect(error).toBeInstanceOf(Locked)
289294
expect(error.message).toMatch(/currently locked by another user and cannot be updated/)
290295
}
@@ -304,7 +309,6 @@ describe('Locked documents', () => {
304309
collection: lockedDocumentCollection,
305310
data: {
306311
document: undefined,
307-
editedAt: new Date().toISOString(),
308312
globalSlug: menuSlug,
309313
user: {
310314
relationTo: 'users',
@@ -321,7 +325,7 @@ describe('Locked documents', () => {
321325
overrideLock: false, // necessary to trigger the lock check
322326
slug: menuSlug,
323327
})
324-
} catch (error) {
328+
} catch (error: any) {
325329
expect(error).toBeInstanceOf(Locked)
326330
expect(error.message).toMatch(/currently locked by another user and cannot be updated/)
327331
}
@@ -351,7 +355,6 @@ describe('Locked documents', () => {
351355
relationTo: 'posts',
352356
value: newPost3.id,
353357
},
354-
editedAt: new Date().toISOString(),
355358
globalSlug: undefined,
356359
user: {
357360
relationTo: 'users',
@@ -366,7 +369,7 @@ describe('Locked documents', () => {
366369
id: newPost3.id,
367370
overrideLock: false, // necessary to trigger the lock check
368371
})
369-
} catch (error) {
372+
} catch (error: any) {
370373
expect(error).toBeInstanceOf(Locked)
371374
expect(error.message).toMatch(/currently locked and cannot be deleted/)
372375
}
@@ -457,7 +460,6 @@ describe('Locked documents', () => {
457460
const lockedDocInstance = await payload.create({
458461
collection: lockedDocumentCollection,
459462
data: {
460-
editedAt: new Date().toISOString(),
461463
user: {
462464
relationTo: 'users',
463465
value: user2.id,
@@ -509,7 +511,6 @@ describe('Locked documents', () => {
509511
const lockedGlobalInstance = await payload.create({
510512
collection: lockedDocumentCollection,
511513
data: {
512-
editedAt: new Date().toISOString(),
513514
globalSlug: menuSlug,
514515
user: {
515516
relationTo: 'users',
@@ -564,7 +565,6 @@ describe('Locked documents', () => {
564565
const lockedDocInstance = await payload.create({
565566
collection: lockedDocumentCollection,
566567
data: {
567-
editedAt: new Date().toISOString(),
568568
user: {
569569
relationTo: 'users',
570570
value: user2.id,
@@ -623,7 +623,6 @@ describe('Locked documents', () => {
623623
const lockedDocInstance = await payload.create({
624624
collection: lockedDocumentCollection,
625625
data: {
626-
editedAt: new Date().toISOString(),
627626
user: {
628627
relationTo: 'users',
629628
value: user2.id,
@@ -653,6 +652,6 @@ describe('Locked documents', () => {
653652
})
654653

655654
expect(docsFromLocksCollection.docs).toHaveLength(1)
656-
expect(docsFromLocksCollection.docs[0].user.value?.id).toEqual(user.id)
655+
expect((docsFromLocksCollection.docs[0]?.user.value as User)?.id).toEqual(user.id)
657656
})
658657
})

0 commit comments

Comments
 (0)