-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.mjs
102 lines (84 loc) · 2.4 KB
/
test.mjs
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
import {resolve, reject} from 'fluture';
import S from 'sinon';
import {
deepStrictEqual as eq,
throws,
} from 'assert';
import {
Typed,
Empty,
NotFound,
Redirect,
Serialized,
handler,
} from '.';
function mock() {
return {
code: S.stub ().returnsThis (),
type: S.stub ().returnsThis (),
send: S.stub ().returnsThis (),
serializer: S.stub ().returnsThis (),
};
}
const K = x => _ => x;
const req = {};
throws (
_ => handler ('noresponse', K (resolve (null))) (req, {}),
new TypeError (
'The Future returned by "noresponse" did not resolve to a Response, '
+ 'instead saw:\n\nnull\n'
)
);
throws (
_ => handler ('nofuture', K (null)) (req, {}),
new TypeError (
'The action "nofuture" did not return a Future, instead saw:\n\nnull\n'
)
);
{
const serializer = K (null);
const action = K (resolve (
Serialized (serializer, 'application/null', 200, 'Something')
));
const reply = mock ();
handler ('serialized', action) (req, reply);
eq (reply.serializer.calledOnceWithExactly (serializer), true);
eq (reply.type.calledOnceWithExactly ('application/null'), true);
eq (reply.code.calledOnceWithExactly (200), true);
eq (reply.send.calledOnceWithExactly ('Something'), true);
}
{
const action = K (resolve (Typed ('application/json', 200, null)));
const reply = mock ();
handler ('json', action) (req, reply);
eq (reply.type.calledOnceWithExactly ('application/json'), true);
eq (reply.code.calledOnceWithExactly (200), true);
eq (reply.send.calledOnceWithExactly (null), true);
}
{
const action = K (resolve (Empty));
const reply = mock ();
handler ('empty', action) (req, reply);
eq (reply.code.calledOnceWithExactly (204), true);
eq (reply.send.calledOnceWithExactly (null), true);
}
{
const action = K (resolve (NotFound));
const reply = {callNotFound: S.spy ()};
handler ('notfound', action) (req, reply);
eq (reply.callNotFound.calledOnce, true);
}
{
const action = K (resolve (Redirect (418, 'imateapot')));
const reply = {redirect: S.spy ()};
handler ('notfound', action) (req, reply);
eq (reply.redirect.calledOnceWithExactly (418, 'imateapot'), true);
}
{
const error = new Error ('imanerror');
const action = K (reject (error));
const reply = mock ();
handler ('error', action) (req, reply);
eq (reply.code.calledOnceWithExactly (500), true);
eq (reply.send.calledOnceWithExactly (error), true);
}