-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
71 lines (57 loc) · 1.62 KB
/
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
const micro = require('micro');
const test = require('ava');
const listen = require('test-listen');
const got = require('got');
const app = require('./');
const getUrl = fn => {
const srv = micro(fn);
return listen(srv);
};
test('send 200 on valid input', async t => {
const url = await getUrl(app);
const res = await got(url, {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({cards: [{front: 'word', back: 'translation'}]})
});
t.is(res.statusCode, 200);
});
test('set deck name', async t => {
const url = await getUrl(app);
const res = await got(url, {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({
cards: [{front: 'word', back: 'translation'}],
deckName: 'test'
})
});
t.is(res.statusCode, 200);
t.is(res.headers['content-disposition'], 'attachment; filename="test.apkg"');
});
test('default deck name', async t => {
const url = await getUrl(app);
const res = await got(url, {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({
cards: [{front: 'word', back: 'translation'}]
})
});
t.is(res.statusCode, 200);
t.is(
res.headers['content-disposition'],
'attachment; filename="micro-anki-deck.apkg"'
);
});
test('send 400 on invalid input', async t => {
const url = await getUrl(app);
const error = await t.throws(
got(url, {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({wrong: [{front: 'word', back: 'translation'}]})
})
);
t.is(error.statusCode, 400);
});