-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblog.test.js
109 lines (87 loc) · 2.81 KB
/
blog.test.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import test from 'ava';
import chronicle, { Axios } from '../../tests/entry';
import express from './app';
const axios = new Axios();
const app = {};
function contextBuilder(context) {
return {
title : context.chronicle.title,
group : context.chronicle.group
};
}
test.before(async () => {
chronicle.setContextBuilder(contextBuilder);
chronicle.setConfig({
headers : {
request : { include: ['Content-Type'] },
}
});
await new Promise(res => {
const server = express.listen(0, () => {
const { port } = server.address();
app.port = port;
app.url = `http://localhost:${port}`;
app.server = server;
res();
});
});
});
test.beforeEach(t => {
t.context.chronicle = {
group : 'Posts',
title : t.title.replace('beforeEach hook for ', '')
};
});
test('Positive: create blog post', async function (t) {
const res = await axios
.with(t.context)
.post(`${app.url}/posts`, {
data : {
title : 'quite cotton although shadow',
body : 'mood income built field throw badly finest seat accurate ago seldom allow invented exactly past garage baseball',
thumbnail : 'http://lakkik.ng/kupa'
}
});
t.is(res.status, 201);
t.assert(res.data);
});
test('Positive: show first post', async function (t) {
const res = await axios
.with(t.context)
.get(`${app.url}/posts/1`);
t.is(res.status, 200);
t.assert(res.data);
});
test('Positive: show single post by id', async function (t) {
const res = await axios
.with(t.context)
.get(`${app.url}/posts/:id`, { params: { id: 2 } });
t.is(res.status, 200);
t.assert(res.data);
});
test('Negative: post not found', async function (t) {
const res = await axios
.with(t.context)
.get(`${app.url}/posts/:id`, { params: { id: 16 } })
.catch(error => error);
t.is(res.response.status, 404);
});
test('Negative: bad id', async function (t) {
const res = await axios
.with(t.context)
.get(`${app.url}/posts/:id`, { params: { id: 'abcd' } })
.catch(error => error);
t.is(res.response.status, 404);
});
test.after('cleanup', async () => {
const docsDir = process.env.DOCS || './documentation';
await chronicle.save(`${docsDir}/swagger.json`, {
reporter : 'swagger',
hash : action => action.context.title.replace(/\W/g, ' ').replace(/\s+/g, '_').toLowerCase()
});
await chronicle.save(`${docsDir}/raml.yaml`, {
reporter : 'raml',
hash : action => action.context.title.split(':').pop().trim().replace(/\W/g, ' ').replace(/\s+/g, '_').toLowerCase()
});
chronicle.clear();
});