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
This commit is contained in:
Patrik
2025-02-17 15:55:34 -05:00
committed by GitHub
parent e6fea1d132
commit ee0ac7f9c0
2 changed files with 66 additions and 66 deletions

View File

@@ -1,5 +1,4 @@
import type { Page } from '@playwright/test' import type { Page } from '@playwright/test'
import type { TypeWithID } from 'payload'
import { expect, test } from '@playwright/test' import { expect, test } from '@playwright/test'
import * as path from 'path' import * as path from 'path'
@@ -8,7 +7,14 @@ import { wait } from 'payload/shared'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import type { PayloadTestSDK } from '../helpers/sdk/index.js' import type { PayloadTestSDK } from '../helpers/sdk/index.js'
import type { Config } from './payload-types.js' import type {
Config,
Page as PageType,
PayloadLockedDocument,
Post,
Test,
User,
} from './payload-types.js'
import { import {
ensureCompilationIsDone, ensureCompilationIsDone,
@@ -19,7 +25,6 @@ import {
import { AdminUrlUtil } from '../helpers/adminUrlUtil.js' import { AdminUrlUtil } from '../helpers/adminUrlUtil.js'
import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js' import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js'
import { POLL_TOPASS_TIMEOUT, TEST_TIMEOUT_LONG } from '../playwright.config.js' import { POLL_TOPASS_TIMEOUT, TEST_TIMEOUT_LONG } from '../playwright.config.js'
import { postsSlug } from './collections/Posts/index.js'
const filename = fileURLToPath(import.meta.url) const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename) const dirname = path.dirname(filename)
@@ -80,12 +85,12 @@ describe('Locked Documents', () => {
}) })
describe('list view - collections', () => { describe('list view - collections', () => {
let postDoc let postDoc: Post
let anotherPostDoc let anotherPostDoc: Post
let user2 let user2: User
let lockedDoc let lockedDoc: PayloadLockedDocument
let testDoc let testDoc: Test
let testLockedDoc let testLockedDoc: PayloadLockedDocument
beforeAll(async () => { beforeAll(async () => {
postDoc = await createPostDoc({ postDoc = await createPostDoc({
@@ -326,14 +331,14 @@ describe('Locked Documents', () => {
}) })
describe('document locking / unlocking - one user', () => { describe('document locking / unlocking - one user', () => {
let postDoc let postDoc: Post
let postDocTwo let postDocTwo: Post
let expiredDocOne let expiredDocOne: Test
let expiredLockedDocOne let expiredLockedDocOne: PayloadLockedDocument
let expiredDocTwo let expiredDocTwo: Test
let expiredLockedDocTwo let expiredLockedDocTwo: PayloadLockedDocument
let testDoc let testDoc: Test
let user2 let user2: User
beforeAll(async () => { beforeAll(async () => {
postDoc = await createPostDoc({ postDoc = await createPostDoc({
@@ -636,11 +641,11 @@ describe('Locked Documents', () => {
}) })
describe('document locking - incoming user', () => { describe('document locking - incoming user', () => {
let postDoc let postDoc: Post
let user2 let user2: User
let lockedDoc let lockedDoc: PayloadLockedDocument
let expiredTestDoc let expiredTestDoc: Test
let expiredTestLockedDoc let expiredTestLockedDoc: PayloadLockedDocument
beforeAll(async () => { beforeAll(async () => {
postDoc = await createPostDoc({ postDoc = await createPostDoc({
@@ -775,9 +780,9 @@ describe('Locked Documents', () => {
}) })
describe('document take over - modal - incoming user', () => { describe('document take over - modal - incoming user', () => {
let postDoc let postDoc: Post
let user2 let user2: User
let lockedDoc let lockedDoc: PayloadLockedDocument
beforeAll(async () => { beforeAll(async () => {
postDoc = await createPostDoc({ postDoc = await createPostDoc({
@@ -855,7 +860,7 @@ describe('Locked Documents', () => {
const userEmail = const userEmail =
// eslint-disable-next-line playwright/no-conditional-in-test // eslint-disable-next-line playwright/no-conditional-in-test
lockedDoc.docs[0].user.value && lockedDoc.docs[0]?.user.value &&
typeof lockedDoc.docs[0].user.value === 'object' && typeof lockedDoc.docs[0].user.value === 'object' &&
'email' in lockedDoc.docs[0].user.value && 'email' in lockedDoc.docs[0].user.value &&
lockedDoc.docs[0].user.value.email lockedDoc.docs[0].user.value.email
@@ -865,9 +870,9 @@ describe('Locked Documents', () => {
}) })
describe('document take over - doc - incoming user', () => { describe('document take over - doc - incoming user', () => {
let postDoc let postDoc: Post
let user2 let user2: User
let lockedDoc let lockedDoc: PayloadLockedDocument
beforeAll(async () => { beforeAll(async () => {
postDoc = await createPostDoc({ postDoc = await createPostDoc({
@@ -947,7 +952,7 @@ describe('Locked Documents', () => {
const userEmail = const userEmail =
// eslint-disable-next-line playwright/no-conditional-in-test // eslint-disable-next-line playwright/no-conditional-in-test
lockedDoc.docs[0].user.value && lockedDoc.docs[0]?.user.value &&
typeof lockedDoc.docs[0].user.value === 'object' && typeof lockedDoc.docs[0].user.value === 'object' &&
'email' in lockedDoc.docs[0].user.value && 'email' in lockedDoc.docs[0].user.value &&
lockedDoc.docs[0].user.value.email lockedDoc.docs[0].user.value.email
@@ -957,8 +962,8 @@ describe('Locked Documents', () => {
}) })
describe('document locking - previous user', () => { describe('document locking - previous user', () => {
let postDoc let postDoc: Post
let user2 let user2: User
beforeAll(async () => { beforeAll(async () => {
postDoc = await createPostDoc({ postDoc = await createPostDoc({
@@ -1011,7 +1016,7 @@ describe('Locked Documents', () => {
// Update payload-locks collection document with different user // Update payload-locks collection document with different user
await payload.update({ await payload.update({
id: lockedDoc.docs[0].id, id: lockedDoc.docs[0]?.id as number | string,
collection: lockedDocumentCollection, collection: lockedDocumentCollection,
data: { data: {
user: { user: {
@@ -1033,7 +1038,7 @@ describe('Locked Documents', () => {
await payload.delete({ await payload.delete({
collection: lockedDocumentCollection, collection: lockedDocumentCollection,
id: lockedDoc.docs[0].id, id: lockedDoc.docs[0]?.id,
}) })
}) })
@@ -1062,7 +1067,7 @@ describe('Locked Documents', () => {
// Update payload-locks collection document with different user // Update payload-locks collection document with different user
await payload.update({ await payload.update({
id: lockedDoc.docs[0].id, id: lockedDoc.docs[0]?.id as number | string,
collection: lockedDocumentCollection, collection: lockedDocumentCollection,
data: { data: {
user: { user: {
@@ -1089,7 +1094,7 @@ describe('Locked Documents', () => {
await payload.delete({ await payload.delete({
collection: lockedDocumentCollection, collection: lockedDocumentCollection,
id: lockedDoc.docs[0].id, id: lockedDoc.docs[0]?.id,
}) })
}) })
@@ -1118,7 +1123,7 @@ describe('Locked Documents', () => {
// Update payload-locks collection document with different user // Update payload-locks collection document with different user
await payload.update({ await payload.update({
id: lockedDoc.docs[0].id, id: lockedDoc.docs[0]?.id as number | string,
collection: lockedDocumentCollection, collection: lockedDocumentCollection,
data: { data: {
user: { user: {
@@ -1151,9 +1156,9 @@ describe('Locked Documents', () => {
}) })
describe('dashboard - globals', () => { describe('dashboard - globals', () => {
let user2 let user2: User
let lockedMenuGlobal let lockedMenuGlobal: PayloadLockedDocument
let lockedAdminGlobal let lockedAdminGlobal: PayloadLockedDocument
beforeAll(async () => { beforeAll(async () => {
user2 = await payload.create({ user2 = await payload.create({
@@ -1298,27 +1303,23 @@ describe('Locked Documents', () => {
}) })
}) })
async function createPageDoc(data: any): Promise<Record<string, unknown> & TypeWithID> { async function createPageDoc(data: Partial<PageType>): Promise<PageType> {
return payload.create({ return payload.create({
collection: 'pages', collection: 'pages',
data, data,
}) as unknown as Promise<Record<string, unknown> & TypeWithID> }) as unknown as Promise<PageType>
} }
async function createPostDoc(data: any): Promise<Record<string, unknown> & TypeWithID> { async function createPostDoc(data: Partial<Post>): Promise<Post> {
return payload.create({ return payload.create({
collection: 'posts', collection: 'posts',
data, data,
}) as unknown as Promise<Record<string, unknown> & TypeWithID> }) as unknown as Promise<Post>
} }
async function createTestDoc(data: any): Promise<Record<string, unknown> & TypeWithID> { async function createTestDoc(data: Partial<Test>): Promise<Test> {
return payload.create({ return payload.create({
collection: 'tests', collection: 'tests',
data, data,
}) as unknown as Promise<Record<string, unknown> & TypeWithID> }) as unknown as Promise<Test>
}
async function deleteAllPosts() {
await payload.delete({ collection: postsSlug, where: { id: { exists: true } } })
} }

View File

@@ -1,4 +1,4 @@
import type { Payload, SanitizedCollectionConfig } from 'payload' import type { Payload, SanitizedCollectionConfig, SanitizedGlobalConfig } from 'payload'
import path from 'path' import path from 'path'
import { Locked, NotFound } from 'payload' import { Locked, NotFound } from 'payload'
@@ -6,7 +6,7 @@ import { wait } from 'payload/shared'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import type { NextRESTClient } from '../helpers/NextRESTClient.js' import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import type { Menu, Page, Post } from './payload-types.js' import type { Menu, Page, Post, User } from './payload-types.js'
import { devUser } from '../credentials.js' import { devUser } from '../credentials.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js' import { initPayloadInt } from '../helpers/initPayloadInt.js'
@@ -32,9 +32,12 @@ describe('Locked documents', () => {
let postConfig: SanitizedCollectionConfig let postConfig: SanitizedCollectionConfig
beforeAll(async () => { beforeAll(async () => {
// @ts-expect-error: initPayloadInt does not have a proper type definition
;({ payload, restClient } = await initPayloadInt(dirname)) ;({ payload, restClient } = await initPayloadInt(dirname))
postConfig = payload.config.collections.find(({ slug }) => slug === postsSlug) postConfig = payload.config.collections.find(
({ slug }) => slug === postsSlug,
) as SanitizedCollectionConfig
const loginResult = await payload.login({ const loginResult = await payload.login({
collection: 'users', collection: 'users',
@@ -45,7 +48,8 @@ describe('Locked documents', () => {
}) })
user = loginResult.user user = loginResult.user
token = loginResult.token
token = loginResult.token as string
user2 = await payload.create({ user2 = await payload.create({
collection: 'users', collection: 'users',
@@ -198,7 +202,9 @@ describe('Locked documents', () => {
it('should allow update of stale locked document - global', async () => { it('should allow update of stale locked document - global', async () => {
// Set lock duration to 1 second for testing purposes // Set lock duration to 1 second for testing purposes
const globalConfig = payload.config.globals.find(({ slug }) => slug === menuSlug) const globalConfig = payload.config.globals.find(
({ slug }) => slug === menuSlug,
) as SanitizedGlobalConfig
globalConfig.lockDocuments = { duration: 1 } globalConfig.lockDocuments = { duration: 1 }
// Give locking ownership to another user // Give locking ownership to another user
const lockedGlobalInstance = await payload.create({ const lockedGlobalInstance = await payload.create({
@@ -266,7 +272,6 @@ describe('Locked documents', () => {
relationTo: 'posts', relationTo: 'posts',
value: newPost.id, value: newPost.id,
}, },
editedAt: new Date().toISOString(),
globalSlug: undefined, globalSlug: undefined,
user: { user: {
relationTo: 'users', relationTo: 'users',
@@ -284,7 +289,7 @@ describe('Locked documents', () => {
overrideLock: false, // necessary to trigger the lock check overrideLock: false, // necessary to trigger the lock check
id: newPost.id, id: newPost.id,
}) })
} catch (error) { } catch (error: any) {
expect(error).toBeInstanceOf(Locked) expect(error).toBeInstanceOf(Locked)
expect(error.message).toMatch(/currently locked by another user and cannot be updated/) expect(error.message).toMatch(/currently locked by another user and cannot be updated/)
} }
@@ -304,7 +309,6 @@ describe('Locked documents', () => {
collection: lockedDocumentCollection, collection: lockedDocumentCollection,
data: { data: {
document: undefined, document: undefined,
editedAt: new Date().toISOString(),
globalSlug: menuSlug, globalSlug: menuSlug,
user: { user: {
relationTo: 'users', relationTo: 'users',
@@ -321,7 +325,7 @@ describe('Locked documents', () => {
overrideLock: false, // necessary to trigger the lock check overrideLock: false, // necessary to trigger the lock check
slug: menuSlug, slug: menuSlug,
}) })
} catch (error) { } catch (error: any) {
expect(error).toBeInstanceOf(Locked) expect(error).toBeInstanceOf(Locked)
expect(error.message).toMatch(/currently locked by another user and cannot be updated/) expect(error.message).toMatch(/currently locked by another user and cannot be updated/)
} }
@@ -351,7 +355,6 @@ describe('Locked documents', () => {
relationTo: 'posts', relationTo: 'posts',
value: newPost3.id, value: newPost3.id,
}, },
editedAt: new Date().toISOString(),
globalSlug: undefined, globalSlug: undefined,
user: { user: {
relationTo: 'users', relationTo: 'users',
@@ -366,7 +369,7 @@ describe('Locked documents', () => {
id: newPost3.id, id: newPost3.id,
overrideLock: false, // necessary to trigger the lock check overrideLock: false, // necessary to trigger the lock check
}) })
} catch (error) { } catch (error: any) {
expect(error).toBeInstanceOf(Locked) expect(error).toBeInstanceOf(Locked)
expect(error.message).toMatch(/currently locked and cannot be deleted/) expect(error.message).toMatch(/currently locked and cannot be deleted/)
} }
@@ -457,7 +460,6 @@ describe('Locked documents', () => {
const lockedDocInstance = await payload.create({ const lockedDocInstance = await payload.create({
collection: lockedDocumentCollection, collection: lockedDocumentCollection,
data: { data: {
editedAt: new Date().toISOString(),
user: { user: {
relationTo: 'users', relationTo: 'users',
value: user2.id, value: user2.id,
@@ -509,7 +511,6 @@ describe('Locked documents', () => {
const lockedGlobalInstance = await payload.create({ const lockedGlobalInstance = await payload.create({
collection: lockedDocumentCollection, collection: lockedDocumentCollection,
data: { data: {
editedAt: new Date().toISOString(),
globalSlug: menuSlug, globalSlug: menuSlug,
user: { user: {
relationTo: 'users', relationTo: 'users',
@@ -564,7 +565,6 @@ describe('Locked documents', () => {
const lockedDocInstance = await payload.create({ const lockedDocInstance = await payload.create({
collection: lockedDocumentCollection, collection: lockedDocumentCollection,
data: { data: {
editedAt: new Date().toISOString(),
user: { user: {
relationTo: 'users', relationTo: 'users',
value: user2.id, value: user2.id,
@@ -623,7 +623,6 @@ describe('Locked documents', () => {
const lockedDocInstance = await payload.create({ const lockedDocInstance = await payload.create({
collection: lockedDocumentCollection, collection: lockedDocumentCollection,
data: { data: {
editedAt: new Date().toISOString(),
user: { user: {
relationTo: 'users', relationTo: 'users',
value: user2.id, value: user2.id,
@@ -653,6 +652,6 @@ describe('Locked documents', () => {
}) })
expect(docsFromLocksCollection.docs).toHaveLength(1) expect(docsFromLocksCollection.docs).toHaveLength(1)
expect(docsFromLocksCollection.docs[0].user.value?.id).toEqual(user.id) expect((docsFromLocksCollection.docs[0]?.user.value as User)?.id).toEqual(user.id)
}) })
}) })