forked from payloadcms/payload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint.spec.ts
110 lines (95 loc) · 3.27 KB
/
int.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { initPayloadTest } from '../helpers/configHelpers'
import { RESTClient } from '../helpers/rest'
import {
applicationEndpoint,
collectionSlug,
globalEndpoint,
globalSlug,
noEndpointsCollectionSlug,
noEndpointsGlobalSlug,
rootEndpoint,
} from './shared'
require('isomorphic-fetch')
let client: RESTClient
describe('Endpoints', () => {
beforeAll(async () => {
const config = await initPayloadTest({ __dirname, init: { local: false } })
const { serverURL } = config
client = new RESTClient(config, { serverURL, defaultSlug: collectionSlug })
})
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)
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)
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)
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)
})
})
describe('Globals', () => {
it('should call custom endpoint', async () => {
const params = { globals: 'response' }
const { status, data } = await client.endpoint(
`/api/globals/${globalSlug}/${globalEndpoint}`,
'post',
params,
)
expect(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)
})
})
describe('API', () => {
it('should call custom endpoint', async () => {
const params = { app: 'response' }
const { status, data } = await client.endpoint(`/api/${applicationEndpoint}`, 'post', params)
expect(status).toBe(200)
expect(params).toMatchObject(data)
})
it('should have i18n on req', async () => {
const { status, data } = await client.endpoint(`/api/${applicationEndpoint}/i18n`, 'get')
expect(status).toBe(200)
expect(data.message).toStrictEqual('Back to Dashboard')
})
})
describe('Root', () => {
it('should call custom root endpoint', async () => {
const params = { root: 'response' }
const { status, data } = await client.endpoint(`/${rootEndpoint}`, 'post', params)
expect(status).toBe(200)
expect(params).toMatchObject(data)
})
})
})