Files
payload/test/_community/int.spec.ts
2024-03-19 00:59:56 -04:00

73 lines
1.8 KiB
TypeScript

import type { Payload } from 'payload'
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import { devUser } from '../credentials.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
import { postsSlug } from './collections/Posts/index.js'
import configPromise from './config.js'
let payload: Payload
let token: string
let restClient: NextRESTClient
const { email, password } = devUser
describe('_Community Tests', () => {
// --__--__--__--__--__--__--__--__--__
// Boilerplate test setup/teardown
// --__--__--__--__--__--__--__--__--__
beforeAll(async () => {
const initialized = await initPayloadInt(configPromise)
;({ payload, restClient } = initialized)
const data = await restClient
.POST('/users/login', {
body: JSON.stringify({
email,
password,
}),
})
.then((res) => res.json())
token = data.token
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
// --__--__--__--__--__--__--__--__--__
// You can run tests against the local API or the REST API
// use the tests below as a guide
// --__--__--__--__--__--__--__--__--__
it('local API example', async () => {
const newPost = await payload.create({
collection: postsSlug,
data: {
text: 'LOCAL API EXAMPLE',
},
})
expect(newPost.text).toEqual('LOCAL API EXAMPLE')
})
it('rest API example', async () => {
const data = await restClient
.POST(`/${postsSlug}`, {
body: JSON.stringify({
text: 'REST API EXAMPLE',
}),
headers: {
Authorization: `JWT ${token}`,
},
})
.then((res) => res.json())
expect(data.doc.text).toEqual('REST API EXAMPLE')
})
})