chore: more passing int suites

This commit is contained in:
Jarrod Flesch
2024-02-16 15:09:51 -05:00
parent 504892ddb9
commit 28c4046300
9 changed files with 215 additions and 113 deletions

View File

@@ -1,5 +1,7 @@
import { initPayloadTest } from '../helpers/configHelpers'
import { RESTClient } from '../helpers/rest'
import { getPayload } from '../../packages/payload/src'
import { NextRESTClient } from '../helpers/NextRESTClient'
import { startMemoryDB } from '../startMemoryDB'
import configPromise from './config'
import {
applicationEndpoint,
collectionSlug,
@@ -10,90 +12,82 @@ import {
rootEndpoint,
} from './shared'
require('isomorphic-fetch')
let client: RESTClient
let restClient: NextRESTClient
describe('Endpoints', () => {
beforeAll(async () => {
const config = await initPayloadTest({ __dirname, init: { local: false } })
const { serverURL } = config
client = new RESTClient(config, { serverURL, defaultSlug: collectionSlug })
const config = await startMemoryDB(configPromise)
await getPayload({ config })
restClient = new NextRESTClient(config)
})
describe('Collections', () => {
it('should GET a static endpoint', async () => {
const { status, data } = await client.endpoint(`/api/${collectionSlug}/say-hello/joe-bloggs`)
expect(status).toBe(200)
const response = await restClient.GET(`/${collectionSlug}/say-hello/joe-bloggs`)
const data = await response.json()
expect(response.status).toBe(200)
expect(data.message).toStrictEqual('Hey Joey!')
})
it('should GET an endpoint with a parameter', async () => {
const name = 'George'
const { status, data } = await client.endpoint(`/api/${collectionSlug}/say-hello/${name}`)
expect(status).toBe(200)
const response = await restClient.GET(`/${collectionSlug}/say-hello/${name}`)
const data = await response.json()
expect(response.status).toBe(200)
expect(data.message).toStrictEqual(`Hello ${name}!`)
})
it('should POST an endpoint with data', async () => {
const params = { name: 'George', age: 29 }
const { status, data } = await client.endpoint(
`/api/${collectionSlug}/whoami`,
'post',
params,
)
expect(status).toBe(200)
const response = await restClient.POST(`/${collectionSlug}/whoami`, {
body: JSON.stringify(params),
})
const data = await response.json()
expect(response.status).toBe(200)
expect(data.name).toStrictEqual(params.name)
expect(data.age).toStrictEqual(params.age)
})
it('should disable built-in endpoints when false', async () => {
let result
try {
result = await client.endpoint(`/api/${noEndpointsCollectionSlug}`, 'get')
} catch (err: unknown) {
result = err
}
expect(result instanceof Error).toBe(true)
const response = await restClient.GET(`/${noEndpointsCollectionSlug}`)
expect(response.status).toBe(501)
})
})
describe('Globals', () => {
it('should call custom endpoint', async () => {
const params = { globals: 'response' }
const { status, data } = await client.endpoint(
`/api/globals/${globalSlug}/${globalEndpoint}`,
'post',
params,
)
const response = await restClient.POST(`/globals/${globalSlug}/${globalEndpoint}`, {
body: JSON.stringify(params),
})
const data = await response.json()
expect(status).toBe(200)
expect(response.status).toBe(200)
expect(params).toMatchObject(data)
})
it('should disable built-in endpoints when false', async () => {
let result
try {
result = await client.endpoint(`/api/globals/${noEndpointsGlobalSlug}`, 'get')
} catch (err: unknown) {
result = err
}
expect(result instanceof Error).toBe(true)
const response = await restClient.GET(`/globals/${noEndpointsGlobalSlug}`)
expect(response.status).toBe(501)
})
})
describe('API', () => {
it('should call custom endpoint', async () => {
const params = { app: 'response' }
const { status, data } = await client.endpoint(`/api/${applicationEndpoint}`, 'post', params)
const response = await restClient.POST(`/${applicationEndpoint}`, {
body: JSON.stringify(params),
})
const data = await response.json()
expect(status).toBe(200)
expect(response.status).toBe(200)
expect(params).toMatchObject(data)
})
it('should have i18n on req', async () => {
const { status, data } = await client.endpoint(`/api/${applicationEndpoint}/i18n`, 'get')
const response = await restClient.GET(`/${applicationEndpoint}/i18n`)
const data = await response.json()
expect(status).toBe(200)
expect(response.status).toBe(200)
expect(data.message).toStrictEqual('Back to Dashboard')
})
})
@@ -101,9 +95,12 @@ describe('Endpoints', () => {
describe('Root', () => {
it('should call custom root endpoint', async () => {
const params = { root: 'response' }
const { status, data } = await client.endpoint(`/${rootEndpoint}`, 'post', params)
const response = await restClient.POST(`/${rootEndpoint}`, {
body: JSON.stringify(params),
})
const data = await response.json()
expect(status).toBe(200)
expect(response.status).toBe(200)
expect(params).toMatchObject(data)
})
})