-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathauthor.controller.spec.ts
More file actions
71 lines (61 loc) · 1.99 KB
/
author.controller.spec.ts
File metadata and controls
71 lines (61 loc) · 1.99 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
import request from 'supertest';
import { app, DI, init } from '../server';
describe('author 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('/author')
.send({ name: 'a1', email: 'e1', books: [{ title: 'b1' }, { title: 'b2' }] })
.then(res => {
expect(res.status).toBe(200);
expect(res.body.id).toBeDefined();
expect(res.body.name).toBe('a1');
expect(res.body.email).toBe('e1');
expect(res.body.termsAccepted).toBe(false);
expect(res.body.books).toHaveLength(2);
id = res.body.id;
});
await request(app.callback())
.get('/author')
.then(res => {
expect(res.status).toBe(200);
expect(res.body).toHaveLength(1);
expect(res.body[0].id).toBeDefined();
expect(res.body[0].name).toBe('a1');
expect(res.body[0].email).toBe('e1');
expect(res.body[0].termsAccepted).toBe(false);
expect(res.body[0].books).toHaveLength(2);
});
await request(app.callback())
.get('/author/' + id)
.then(res => {
expect(res.status).toBe(200);
expect(res.body.id).toBeDefined();
expect(res.body.name).toBe('a1');
expect(res.body.email).toBe('e1');
expect(res.body.books).toHaveLength(2);
});
await request(app.callback())
.put('/author/' + id)
.send({ name: 'a2' })
.then(res => {
expect(res.status).toBe(200);
expect(res.body.id).toBeDefined();
expect(res.body.name).toBe('a2');
expect(res.body.email).toBe('e1');
expect(res.body.termsAccepted).toBe(false);
});
});
});