Files
payload/test/_community/int.spec.ts
2024-02-14 09:46:11 -05:00

81 lines
1.9 KiB
TypeScript

import type { Payload } from '../../packages/payload/src'
import { devUser } from '../credentials'
import { initPayloadTest } from '../helpers/configHelpers'
import { postsSlug } from './collections/Posts'
require('isomorphic-fetch')
let payload: Payload
let apiURL: string
let jwt
const headers = {
'Content-Type': 'application/json',
}
const { email, password } = devUser
describe('_Community Tests', () => {
// --__--__--__--__--__--__--__--__--__
// Boilerplate test setup/teardown
// --__--__--__--__--__--__--__--__--__
beforeAll(async () => {
const { payload: payloadClient, serverURL } = await initPayloadTest({
__dirname,
init: { local: false },
})
apiURL = `${serverURL}/api`
payload = payloadClient
const response = await fetch(`${apiURL}/users/login`, {
body: JSON.stringify({
email,
password,
}),
headers,
method: 'post',
})
const data = await response.json()
jwt = data.token
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload)
}
})
// --__--__--__--__--__--__--__--__--__
// 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 newPost = await fetch(`${apiURL}/${postsSlug}`, {
method: 'POST',
headers: {
...headers,
Authorization: `JWT ${jwt}`,
},
body: JSON.stringify({
text: 'REST API EXAMPLE',
}),
}).then((res) => res.json())
expect(newPost.doc.text).toEqual('REST API EXAMPLE')
})
})