-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbook.controller.spec.ts
More file actions
60 lines (51 loc) · 1.66 KB
/
book.controller.spec.ts
File metadata and controls
60 lines (51 loc) · 1.66 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
import request from 'supertest';
import { app, DI, init } from '../server';
describe('book controller', () => {
beforeAll(async () => {
await init;
DI.orm.config.set('dbName', 'koa-test-db');
DI.orm.config.getLogger().setDebugMode(false);
await DI.orm.config.getDriver().reconnect();
await DI.orm.schema.drop();
await DI.orm.schema.create();
});
afterAll(async () => {
await DI.orm.close(true);
DI.server.close();
});
it(`CRUD`, async () => {
let id;
await request(app.callback())
.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.callback())
.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.callback())
.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();
});
});
});