-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbook.controller.spec.js
More file actions
57 lines (48 loc) · 1.57 KB
/
book.controller.spec.js
File metadata and controls
57 lines (48 loc) · 1.57 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
import request from 'supertest';
import { expect, describe, it, beforeAll, afterAll } from 'vitest';
import { app, DI, server } from '../server.js';
describe('book controller', () => {
beforeAll(async () => {
await DI.orm.reconnect({ dbName: ':memory:', debug: false });
await DI.orm.schema.create();
});
afterAll(async () => {
await DI.orm.close(true);
server.close();
});
it(`CRUD`, async () => {
let id;
await request(app)
.post('/book')
.send({ title: 'b1', author: { name: 'a1', email: 'e1' } })
.then(res => {
expect(res.status).toBe(200);
expect(res.body.id).toBeDefined();
expect(res.body.title).toBe('b1');
expect(res.body.author.name).toBe('a1');
expect(res.body.author.email).toBe('e1');
expect(res.body.author.termsAccepted).toBe(false);
expect(res.body.author.books).toHaveLength(1);
id = res.body.id;
});
await request(app)
.get('/book')
.then(res => {
expect(res.status).toBe(200);
expect(res.body[0].id).toBeDefined();
expect(res.body[0].title).toBe('b1');
expect(res.body[0].author.name).toBe('a1');
expect(res.body[0].author.email).toBe('e1');
expect(res.body[0].author.termsAccepted).toBe(false);
});
await request(app)
.put('/book/' + id)
.send({ title: 'b2' })
.then(res => {
expect(res.status).toBe(200);
expect(res.body.id).toBeDefined();
expect(res.body.title).toBe('b2');
expect(res.body.author).toBeDefined();
});
});
});