Files
payloadcms/test/sdk/int.spec.ts
T. R. Bernstein 4a5f01a78f
Some checks failed
ci / changes (push) Has been cancelled
ci / lint (push) Has been cancelled
ci / build (push) Has been cancelled
ci / tests-unit (push) Has been cancelled
ci / tests-types (push) Has been cancelled
ci / int-cosmosdb (push) Has been cancelled
ci / int-documentdb (push) Has been cancelled
ci / int-firestore (push) Has been cancelled
ci / int-mongodb (push) Has been cancelled
ci / int-postgres (push) Has been cancelled
ci / int-postgres-custom-schema (push) Has been cancelled
ci / int-postgres-uuid (push) Has been cancelled
ci / int-sqlite (push) Has been cancelled
ci / int-sqlite-uuid (push) Has been cancelled
ci / int-supabase (push) Has been cancelled
ci / e2e-_community (push) Has been cancelled
ci / e2e-access-control (push) Has been cancelled
ci / e2e-admin-bar (push) Has been cancelled
ci / e2e-admin-root (push) Has been cancelled
ci / e2e-admin__e2e__document-view (push) Has been cancelled
ci / e2e-admin__e2e__general (push) Has been cancelled
ci / e2e-admin__e2e__list-view (push) Has been cancelled
ci / e2e-auth (push) Has been cancelled
ci / e2e-auth-basic (push) Has been cancelled
ci / e2e-bulk-edit (push) Has been cancelled
ci / e2e-field-error-states (push) Has been cancelled
ci / e2e-fields-relationship (push) Has been cancelled
ci / e2e-fields__collections__Array (push) Has been cancelled
ci / e2e-fields__collections__Blocks#config.blockreferences.ts (push) Has been cancelled
ci / e2e-fields__collections__Blocks (push) Has been cancelled
ci / e2e-fields__collections__Checkbox (push) Has been cancelled
ci / e2e-fields__collections__Collapsible (push) Has been cancelled
ci / e2e-fields__collections__ConditionalLogic (push) Has been cancelled
ci / e2e-fields__collections__CustomID (push) Has been cancelled
ci / e2e-fields__collections__Date (push) Has been cancelled
ci / e2e-fields__collections__Email (push) Has been cancelled
ci / e2e-fields__collections__Indexed (push) Has been cancelled
ci / e2e-fields__collections__JSON (push) Has been cancelled
ci / e2e-fields__collections__Number (push) Has been cancelled
ci / e2e-fields__collections__Point (push) Has been cancelled
ci / e2e-fields__collections__Radio (push) Has been cancelled
ci / e2e-fields__collections__Relationship (push) Has been cancelled
ci / e2e-fields__collections__Row (push) Has been cancelled
ci / e2e-fields__collections__Select (push) Has been cancelled
ci / e2e-fields__collections__Tabs (push) Has been cancelled
ci / e2e-fields__collections__Tabs2 (push) Has been cancelled
ci / e2e-fields__collections__Text (push) Has been cancelled
ci / e2e-fields__collections__UI (push) Has been cancelled
ci / e2e-fields__collections__Upload (push) Has been cancelled
ci / e2e-folders (push) Has been cancelled
ci / e2e-form-state (push) Has been cancelled
ci / e2e-group-by (push) Has been cancelled
ci / e2e-hooks (push) Has been cancelled
ci / e2e-i18n (push) Has been cancelled
ci / e2e-joins (push) Has been cancelled
ci / e2e-lexical__collections__LexicalHeadingFeature (push) Has been cancelled
ci / e2e-lexical__collections__LexicalJSXConverter (push) Has been cancelled
ci / e2e-lexical__collections__LexicalLinkFeature (push) Has been cancelled
ci / e2e-lexical__collections__Lexical__e2e__blocks#config.blockreferences.ts (push) Has been cancelled
ci / e2e-lexical__collections__Lexical__e2e__blocks (push) Has been cancelled
ci / e2e-lexical__collections__Lexical__e2e__main (push) Has been cancelled
ci / e2e-lexical__collections__OnDemandForm (push) Has been cancelled
ci / e2e-lexical__collections__RichText (push) Has been cancelled
ci / e2e-lexical__collections___LexicalFullyFeatured (push) Has been cancelled
ci / e2e-lexical__collections___LexicalFullyFeatured__db (push) Has been cancelled
ci / e2e-live-preview (push) Has been cancelled
ci / e2e-localization (push) Has been cancelled
ci / e2e-locked-documents (push) Has been cancelled
ci / e2e-plugin-cloud-storage (push) Has been cancelled
ci / e2e-plugin-form-builder (push) Has been cancelled
ci / e2e-plugin-import-export (push) Has been cancelled
ci / e2e-plugin-multi-tenant (push) Has been cancelled
ci / e2e-plugin-nested-docs (push) Has been cancelled
ci / e2e-plugin-seo (push) Has been cancelled
ci / e2e-query-presets (push) Has been cancelled
ci / e2e-sort (push) Has been cancelled
ci / e2e-trash (push) Has been cancelled
ci / e2e-uploads (push) Has been cancelled
ci / e2e-versions (push) Has been cancelled
ci / e2e-turbo-_community (push) Has been cancelled
ci / e2e-turbo-access-control (push) Has been cancelled
ci / e2e-turbo-admin-bar (push) Has been cancelled
ci / e2e-turbo-admin-root (push) Has been cancelled
ci / e2e-turbo-admin__e2e__document-view (push) Has been cancelled
ci / e2e-turbo-admin__e2e__general (push) Has been cancelled
ci / e2e-turbo-admin__e2e__list-view (push) Has been cancelled
ci / e2e-turbo-auth (push) Has been cancelled
ci / e2e-turbo-auth-basic (push) Has been cancelled
ci / e2e-turbo-bulk-edit (push) Has been cancelled
ci / e2e-turbo-field-error-states (push) Has been cancelled
ci / e2e-turbo-fields-relationship (push) Has been cancelled
ci / e2e-turbo-fields__collections__Array (push) Has been cancelled
ci / e2e-turbo-fields__collections__Blocks#config.blockreferences.ts (push) Has been cancelled
ci / e2e-turbo-fields__collections__Blocks (push) Has been cancelled
ci / e2e-turbo-fields__collections__Checkbox (push) Has been cancelled
ci / e2e-turbo-fields__collections__Collapsible (push) Has been cancelled
ci / e2e-turbo-fields__collections__ConditionalLogic (push) Has been cancelled
ci / e2e-turbo-fields__collections__CustomID (push) Has been cancelled
ci / e2e-turbo-fields__collections__Date (push) Has been cancelled
ci / e2e-turbo-fields__collections__Email (push) Has been cancelled
ci / e2e-turbo-fields__collections__Indexed (push) Has been cancelled
ci / e2e-turbo-fields__collections__JSON (push) Has been cancelled
ci / e2e-turbo-fields__collections__Number (push) Has been cancelled
ci / e2e-turbo-fields__collections__Point (push) Has been cancelled
ci / e2e-turbo-fields__collections__Radio (push) Has been cancelled
ci / e2e-turbo-fields__collections__Relationship (push) Has been cancelled
ci / e2e-turbo-fields__collections__Row (push) Has been cancelled
ci / e2e-turbo-fields__collections__Select (push) Has been cancelled
ci / e2e-turbo-fields__collections__Tabs (push) Has been cancelled
ci / e2e-turbo-fields__collections__Tabs2 (push) Has been cancelled
ci / e2e-turbo-fields__collections__Text (push) Has been cancelled
ci / e2e-turbo-fields__collections__UI (push) Has been cancelled
ci / e2e-turbo-fields__collections__Upload (push) Has been cancelled
ci / e2e-turbo-folders (push) Has been cancelled
ci / e2e-turbo-form-state (push) Has been cancelled
ci / e2e-turbo-group-by (push) Has been cancelled
ci / e2e-turbo-hooks (push) Has been cancelled
ci / e2e-turbo-i18n (push) Has been cancelled
ci / e2e-turbo-joins (push) Has been cancelled
ci / e2e-turbo-lexical__collections__LexicalHeadingFeature (push) Has been cancelled
ci / e2e-turbo-lexical__collections__LexicalJSXConverter (push) Has been cancelled
ci / e2e-turbo-lexical__collections__LexicalLinkFeature (push) Has been cancelled
ci / e2e-turbo-lexical__collections__Lexical__e2e__blocks#config.blockreferences.ts (push) Has been cancelled
ci / e2e-turbo-lexical__collections__Lexical__e2e__blocks (push) Has been cancelled
ci / e2e-turbo-lexical__collections__Lexical__e2e__main (push) Has been cancelled
ci / e2e-turbo-lexical__collections__OnDemandForm (push) Has been cancelled
ci / e2e-turbo-lexical__collections__RichText (push) Has been cancelled
ci / e2e-turbo-lexical__collections___LexicalFullyFeatured (push) Has been cancelled
ci / e2e-turbo-lexical__collections___LexicalFullyFeatured__db (push) Has been cancelled
ci / e2e-turbo-live-preview (push) Has been cancelled
ci / e2e-turbo-localization (push) Has been cancelled
ci / e2e-turbo-locked-documents (push) Has been cancelled
ci / e2e-turbo-plugin-cloud-storage (push) Has been cancelled
ci / e2e-turbo-plugin-form-builder (push) Has been cancelled
ci / e2e-turbo-plugin-import-export (push) Has been cancelled
ci / e2e-turbo-plugin-multi-tenant (push) Has been cancelled
ci / e2e-turbo-plugin-nested-docs (push) Has been cancelled
ci / e2e-turbo-plugin-seo (push) Has been cancelled
ci / e2e-turbo-query-presets (push) Has been cancelled
ci / e2e-turbo-sort (push) Has been cancelled
ci / e2e-turbo-trash (push) Has been cancelled
ci / e2e-turbo-uploads (push) Has been cancelled
ci / e2e-turbo-versions (push) Has been cancelled
ci / build-template-blank-mongodb (push) Has been cancelled
ci / build-template-website-mongodb (push) Has been cancelled
ci / build-template-with-payload-cloud-mongodb (push) Has been cancelled
ci / build-template-with-vercel-mongodb-mongodb (push) Has been cancelled
ci / build-template-plugin- (push) Has been cancelled
ci / build-template-with-postgres-postgres (push) Has been cancelled
ci / build-template-with-vercel-postgres-postgres (push) Has been cancelled
ci / tests-type-generation (push) Has been cancelled
ci / All Green (push) Has been cancelled
ci / Publish Canary (push) Has been cancelled
ci / analyze (push) Has been cancelled
publish-prerelease / publish-prerelease-${{ github.ref_name }}-${{ github.sha }} (push) Has been cancelled
lock-issues / lock_issues (push) Has been cancelled
stale / stale (push) Has been cancelled
audit-dependencies / audit (push) Has been cancelled
activity-notifications / run (push) Has been cancelled
chore: Update code to new repo
2025-10-08 23:27:45 +02:00

310 lines
8.4 KiB
TypeScript

import type { Payload } from 'tbsh-cms'
import { randomUUID } from 'crypto'
import path from 'path'
import { fileURLToPath } from 'url'
import type { TypedPayloadSDK } from '../helpers/getSDK.js'
import type { Post } from './payload-types.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
import { createStreamableFile } from '../uploads/createStreamableFile.js'
let payload: Payload
let post: Post
let sdk: TypedPayloadSDK
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const testUserCredentials = {
email: 'test@payloadcms.com',
password: '123456',
}
describe('@payloadcms/sdk', () => {
beforeAll(async () => {
;({ payload, sdk } = await initPayloadInt(dirname))
post = await payload.create({ collection: 'posts', data: { number: 1, number2: 3 } })
await payload.create({
collection: 'users',
data: { ...testUserCredentials },
})
await payload.updateGlobal({ slug: 'global', data: { text: 'some-global' } })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
it('should execute find', async () => {
const result = await sdk.find({ collection: 'posts', where: { id: { equals: post.id } } })
expect(result.docs[0].id).toBe(post.id)
})
it('should execute findVersions', async () => {
const result = await sdk.findVersions({
collection: 'posts',
where: { parent: { equals: post.id } },
})
expect(result.docs[0].parent).toBe(post.id)
})
it('should execute findByID', async () => {
const result = await sdk.findByID({ collection: 'posts', id: post.id })
expect(result.id).toBe(post.id)
})
it('should execute findByID with disableErrors: true', async () => {
const result = await sdk.findByID({
disableErrors: true,
collection: 'posts',
// eslint-disable-next-line jest/no-conditional-in-test
id: typeof post.id === 'string' ? randomUUID() : 999,
})
expect(result).toBeNull()
})
it('should execute findVersionByID', async () => {
const {
docs: [version],
} = await payload.findVersions({ collection: 'posts', where: { parent: { equals: post.id } } })
const result = await sdk.findVersionByID({ collection: 'posts', id: version.id })
expect(result.id).toBe(version.id)
})
it('should execute create', async () => {
const result = await sdk.create({ collection: 'posts', data: { text: 'text' } })
expect(result.text).toBe('text')
})
it('should execute create with file', async () => {
const filePath = path.join(dirname, './image.jpg')
const { file, handle } = await createStreamableFile(filePath)
const res = await sdk.create({ collection: 'media', file, data: {} })
expect(res.id).toBeTruthy()
await handle.close()
})
it('should execute count', async () => {
const result = await sdk.count({ collection: 'posts', where: { id: { equals: post.id } } })
expect(result.totalDocs).toBe(1)
})
it('should execute update (by ID)', async () => {
const result = await sdk.update({
collection: 'posts',
id: post.id,
data: { text: 'updated-text' },
})
expect(result.text).toBe('updated-text')
})
it('should execute update (bulk)', async () => {
const result = await sdk.update({
collection: 'posts',
where: {
id: {
equals: post.id,
},
},
data: { text: 'updated-text-bulk' },
})
expect(result.docs[0].text).toBe('updated-text-bulk')
})
it('should execute delete (by ID)', async () => {
const post = await payload.create({ collection: 'posts', data: {} })
const result = await sdk.delete({ id: post.id, collection: 'posts' })
expect(result.id).toBe(post.id)
const resultLocal = await payload.findByID({
collection: 'posts',
id: post.id,
disableErrors: true,
})
expect(resultLocal).toBeNull()
})
it('should execute delete (bulk)', async () => {
const post = await payload.create({ collection: 'posts', data: {} })
const result = await sdk.delete({ where: { id: { equals: post.id } }, collection: 'posts' })
expect(result.docs[0].id).toBe(post.id)
const resultLocal = await payload.findByID({
collection: 'posts',
id: post.id,
disableErrors: true,
})
expect(resultLocal).toBeNull()
})
it('should execute restoreVersion', async () => {
const post = await payload.create({ collection: 'posts', data: { text: 'old' } })
const {
docs: [currentVersion],
} = await payload.findVersions({ collection: 'posts', where: { parent: { equals: post.id } } })
await payload.update({ collection: 'posts', id: post.id, data: { text: 'new' } })
const result = await sdk.restoreVersion({
collection: 'posts',
id: currentVersion.id,
})
expect(result.text).toBe('old')
const resultDB = await payload.findByID({ collection: 'posts', id: post.id })
expect(resultDB.text).toBe('old')
})
it('should execute findGlobal', async () => {
const result = await sdk.findGlobal({ slug: 'global' })
expect(result.text).toBe('some-global')
})
it('should execute findGlobalVersions', async () => {
const result = await sdk.findGlobalVersions({
slug: 'global',
})
expect(result.docs[0].version).toBeTruthy()
})
it('should execute findGlobalVersionByID', async () => {
const {
docs: [version],
} = await payload.findGlobalVersions({
slug: 'global',
})
const result = await sdk.findGlobalVersionByID({ id: version.id, slug: 'global' })
expect(result.id).toBe(version.id)
})
it('should execute updateGlobal', async () => {
const result = await sdk.updateGlobal({ slug: 'global', data: { text: 'some-updated-global' } })
expect(result.text).toBe('some-updated-global')
})
it('should execute restoreGlobalVersion', async () => {
await payload.updateGlobal({ slug: 'global', data: { text: 'old' } })
const {
docs: [currentVersion],
} = await payload.findGlobalVersions({
slug: 'global',
})
await payload.updateGlobal({ slug: 'global', data: { text: 'new' } })
const { version: result } = await sdk.restoreGlobalVersion({
slug: 'global',
id: currentVersion.id,
})
expect(result.text).toBe('old')
const resultDB = await payload.findGlobal({ slug: 'global' })
expect(resultDB.text).toBe('old')
})
it('should execute login', async () => {
const res = await sdk.login({
collection: 'users',
data: { email: testUserCredentials.email, password: testUserCredentials.password },
})
expect(res.user.email).toBe(testUserCredentials.email)
})
it('should execute me', async () => {
const { token } = await sdk.login({
collection: 'users',
data: { email: testUserCredentials.email, password: testUserCredentials.password },
})
const res = await sdk.me(
{ collection: 'users' },
{ headers: { Authorization: `JWT ${token}` } },
)
expect(res.user.email).toBe(testUserCredentials.email)
})
it('should execute refreshToken', async () => {
const { token } = await sdk.login({
collection: 'users',
data: { email: testUserCredentials.email, password: testUserCredentials.password },
})
const res = await sdk.refreshToken(
{ collection: 'users' },
{ headers: { Authorization: `JWT ${token}` } },
)
expect(res.user.email).toBe(testUserCredentials.email)
})
it('should execute forgotPassword and resetPassword', async () => {
const user = await payload.create({
collection: 'users',
data: { email: 'new@payloadcms.com', password: 'HOW TO rEmeMber this password' },
})
const resForgotPassword = await sdk.forgotPassword({
collection: 'users',
data: { email: user.email },
})
expect(resForgotPassword.message).toBeTruthy()
const afterForgotPassword = await payload.findByID({
showHiddenFields: true,
collection: 'users',
id: user.id,
})
expect(afterForgotPassword.resetPasswordToken).toBeTruthy()
const verifyEmailResult = await sdk.resetPassword({
collection: 'users',
data: { password: '1234567', token: afterForgotPassword.resetPasswordToken },
})
expect(verifyEmailResult.user.email).toBe(user.email)
const {
user: { email },
} = await sdk.login({
collection: 'users',
data: { email: user.email, password: '1234567' },
})
expect(email).toBe(user.email)
})
})