forked from payloadcms/payload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint.spec.ts
72 lines (61 loc) · 1.77 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
import payload from '../../packages/payload/src'
import { devUser } from '../credentials'
import { initPayloadTest } from '../helpers/configHelpers'
import { postsSlug } from './collections/Posts'
require('isomorphic-fetch')
let apiUrl
let jwt
const headers = {
'Content-Type': 'application/json',
}
const { email, password } = devUser
describe('_Community Tests', () => {
// --__--__--__--__--__--__--__--__--__
// Boilerplate test setup/teardown
// --__--__--__--__--__--__--__--__--__
beforeAll(async () => {
const { serverURL } = await initPayloadTest({ __dirname, init: { local: false } })
apiUrl = `${serverURL}/api`
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')
})
})