-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
196 lines (181 loc) · 8.14 KB
/
index.spec.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* tslint:disable:max-classes-per-file object-literal-sort-keys */
import { expect } from "chai";
import { Component, RouteDefs, RouteResolver, Vnode } from "mithril";
import { SinonSpy, spy } from "sinon";
import { mockReq, mockRes } from "sinon-express-mock";
import middleware from "./index";
describe(middleware.name, () => {
let view: Component<any, any>["view"] & SinonSpy;
let onmatch: RouteResolver<any, any>["onmatch"] & SinonSpy;
let render: RouteResolver<any, any>["render"] & SinonSpy;
let resolver: RouteResolver<any, any>;
let cmp: Component<any, any>;
let routes: RouteDefs;
beforeEach(() => {
view = spy(() => `test`);
cmp = { view };
onmatch = spy(() => cmp);
render = spy((vnode: Vnode<any, any>) => vnode);
resolver = { onmatch, render };
routes = {
"/": resolver,
"/:test": resolver,
"/:test...": resolver,
};
});
it("should return RequestHandler function", () => {
expect(middleware(routes)).to.be.a("function");
expect(middleware(routes)).to.have.property("length").lt(4);
});
it("should be working with express res and req", async () => {
const handler = middleware(routes);
const res = mockRes();
const req = mockReq({ path: "/" });
const next = spy();
await handler(req, res, next);
expect(res.send.called).to.be.equal(true);
expect(res.end.called).to.be.equal(true);
expect(next.called).to.be.equal(false);
});
it("should be working with express next - non-existing route", async () => {
const handler = middleware(routes);
const res = mockRes();
const req = mockReq({ path: "" });
const next = spy();
await handler(req, res, next);
expect(res.send.called).to.be.equal(false);
expect(res.end.called).to.be.equal(false);
expect(next.called).to.be.equal(true);
});
it("should be working with express - default route", async () => {
const handler = middleware(routes, { defaultRoute: "/" });
const res = mockRes();
const req = mockReq({ path: "" });
const next = spy();
await handler(req, res, next);
expect(res.send.called).to.be.equal(true);
expect(res.end.called).to.be.equal(true);
expect(next.called).to.be.equal(false);
});
it("should be working with express - default attrs", async () => {
const handler = middleware(routes, { attrs: { test: "test" } });
const res = mockRes();
const req = mockReq({ path: "/" });
const next = spy();
await handler(req, res, next);
expect(onmatch.firstCall.args[0]).to.be.eql({ test: "test" });
expect(render.firstCall.args[0].attrs).to.be.eql({ test: "test" });
expect(view.firstCall.args[0].attrs).to.be.eql({ test: "test" });
});
it("should be working with express - request data as attrs", async () => {
const handler = middleware(routes, {
attrsBody: true,
attrsCookies: true,
attrsQuery: true,
});
const res = mockRes();
const req = mockReq({
body: { body: `test` },
cookies: { cookies: `test` },
query: { query: `test` },
path: "/",
});
const next = spy();
await handler(req, res, next);
expect(next.called).to.be.equal(false, `next was called, an exception was thrown`);
expect(onmatch.firstCall.args[0]).to.be.eql({ body: "test", cookies: "test", query: "test" });
expect(render.firstCall.args[0].attrs).to.be.eql({ body: "test", cookies: "test", query: "test" });
expect(view.firstCall.args[0].attrs).to.be.eql({ body: "test", cookies: "test", query: "test" });
});
it("should be working with express - attrs overwrite request data", async () => {
const handler = middleware(routes, {
attrs: { body: `attrs` },
attrsBody: true,
attrsCookies: true,
attrsQuery: true,
});
const res = mockRes();
const req = mockReq({
body: { body: `test` },
cookies: { cookies: `test` },
query: { query: `test` },
path: "/",
});
const next = spy();
await handler(req, res, next);
expect(next.called).to.be.equal(false, `next was called, an exception was thrown`);
expect(onmatch.firstCall.args[0]).to.be.eql({ body: "attrs", cookies: "test", query: "test" });
expect(render.firstCall.args[0].attrs).to.be.eql({ body: "attrs", cookies: "test", query: "test" });
expect(view.firstCall.args[0].attrs).to.be.eql({ body: "attrs", cookies: "test", query: "test" });
});
it("should be working with express - route params", async () => {
const handler = middleware(routes);
const res = mockRes();
const req = mockReq({ path: "/val" });
const next = spy();
await handler(req, res, next);
expect(onmatch.firstCall.args[0]).to.be.eql({ test: "val" });
expect(render.firstCall.args[0].attrs).to.be.eql({ test: "val" });
expect(view.firstCall.args[0].attrs).to.be.eql({ test: "val" });
});
it("should be working with express - route variadic params", async () => {
const handler = middleware(routes);
const res = mockRes();
const req = mockReq({ path: "/val/val" });
const next = spy();
await handler(req, res, next);
expect(onmatch.firstCall.args[0]).to.be.eql({ test: "val/val" });
expect(render.firstCall.args[0].attrs).to.be.eql({ test: "val/val" });
expect(view.firstCall.args[0].attrs).to.be.eql({ test: "val/val" });
});
it("should be working with express - path params", async () => {
const handler = middleware(routes);
const res = mockRes();
const req = mockReq({ path: "/?attr1=val#attr2=val" });
const next = spy();
await handler(req, res, next);
expect(onmatch.firstCall.args[0]).to.be.eql({ attr1: "val", attr2: "val" });
expect(render.firstCall.args[0].attrs).to.be.eql({ attr1: "val", attr2: "val" });
expect(view.firstCall.args[0].attrs).to.be.eql({ attr1: "val", attr2: "val" });
});
it("should have working html function", async () => {
const html = spy(async (partial: Promise<string>) => `test${await partial}`);
const handler = middleware(routes, { html });
const res = mockRes();
const req = mockReq({ path: "/" });
const next = spy();
await handler(req, res, next);
expect(html.calledOnce).to.be.equal(true);
expect(html.firstCall.args[0]).to.be.instanceOf(Promise);
expect(await html.firstCall.args[0]).to.be.a("string");
expect(res.send.called).to.be.equal(true);
expect(next.called).to.be.equal(false);
expect(await res.send.firstCall.args[0]).to.be.not.equal(`test`);
expect(await res.send.firstCall.args[0]).to.be.equal(`testtest`);
});
it("should this in res methods be instance of Response", async () => {
const handler = middleware(routes);
const res = mockRes();
const req = mockReq({ path: "/" });
const next = spy();
await handler(req, res, next);
expect(res.send.called).to.be.equal(true, `'res.send' was not called`);
expect(res.send.firstCall.thisValue).to.be.not.a("undefined", `'this' is undefined`);
expect(res.send.firstCall.thisValue).to.be.equal(res, `'res.send' not called with the 'res' as 'this'`);
});
it("should 'skipError' flag be working", async () => {
const handler = middleware(routes, {
skipError: true,
});
const res = mockRes();
const req = mockReq({ path: "" });
const next = spy();
await handler(req, res, next);
expect(res.send.called).to.be.equal(false, `'res.send' was called`);
expect(next.called).to.be.equal(true, `'next' was not called`);
expect(next.firstCall.args).to.have.property("length").equal(
0,
`'next' was called with args: ${JSON.stringify(next.firstCall.args)}`,
);
});
});