-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathint.spec.ts
More file actions
90 lines (70 loc) · 2.37 KB
/
int.spec.ts
File metadata and controls
90 lines (70 loc) · 2.37 KB
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
/* eslint-disable no-console */
/**
* Here are your integration tests for the plugin.
* They don't require running your Next.js so they are fast
* Yet they still can test the Local API and custom endpoints using NextRESTClient helper.
*/
import type { Payload } from 'payload'
import dotenv from 'dotenv'
import { MongoMemoryReplSet } from 'mongodb-memory-server'
import path from 'path'
import { getPayload } from 'payload'
import { fileURLToPath } from 'url'
import { NextRESTClient } from './helpers/NextRESTClient.js'
const dirname = path.dirname(fileURLToPath(import.meta.url))
let payload: Payload
let restClient: NextRESTClient
let memoryDB: MongoMemoryReplSet | undefined
describe('Plugin tests', () => {
beforeAll(async () => {
process.env.DISABLE_PAYLOAD_HMR = 'true'
process.env.PAYLOAD_DROP_DATABASE = 'true'
dotenv.config({
path: path.resolve(dirname, './.env'),
})
if (!process.env.DATABASE_URI) {
console.log('Starting memory database')
memoryDB = await MongoMemoryReplSet.create({
replSet: {
count: 3,
dbName: 'payloadmemory',
},
})
console.log('Memory database started')
process.env.DATABASE_URI = `${memoryDB.getUri()}&retryWrites=true`
}
const { default: config } = await import('./payload.config.js')
payload = await getPayload({ config })
restClient = new NextRESTClient(payload.config)
})
afterAll(async () => {
if (payload.db.destroy) {
await payload.db.destroy()
}
if (memoryDB) {
await memoryDB.stop()
}
})
it('should query added by plugin custom endpoint', async () => {
const response = await restClient.GET('/my-plugin-endpoint')
expect(response.status).toBe(200)
const data = await response.json()
expect(data).toMatchObject({
message: 'Hello from custom endpoint',
})
})
it('can create post with a custom text field added by plugin', async () => {
const post = await payload.create({
collection: 'posts',
data: {
addedByPlugin: 'added by plugin',
},
})
expect(post.addedByPlugin).toBe('added by plugin')
})
it('plugin creates and seeds plugin-collection', async () => {
expect(payload.collections['plugin-collection']).toBeDefined()
const { docs } = await payload.find({ collection: 'plugin-collection' })
expect(docs).toHaveLength(1)
})
})