-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmiddleware.test.ts
More file actions
135 lines (120 loc) · 3.49 KB
/
middleware.test.ts
File metadata and controls
135 lines (120 loc) · 3.49 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
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
import { expect } from "chai";
import sinon from "sinon";
import proxyquire from "proxyquire";
import {
specifyCreateRequest,
specifyCreateResponse,
} from "../../test-utils/mock-express";
const createResponse = specifyCreateResponse();
const createRequest = specifyCreateRequest();
const fsReadDirStub = { readdir: sinon.stub() };
const pagesAndContextsStub = {
pagesAndContexts: {
"some-template": [],
"another-template": ["context", undefined],
},
};
const middleware = proxyquire("./middleware", {
"../../config/pages-and-contexts": pagesAndContextsStub,
"fs/promises": fsReadDirStub,
});
beforeEach(() => {
fsReadDirStub.readdir.resolves(["some-template.njk", "another-template.njk"]);
});
describe("allTemplatesGet", () => {
// Arrange
const req = createRequest();
const res = createResponse();
it("should render all-templates page with correct options", async () => {
// Act
await middleware.allTemplatesGet(req, res);
// Assert
expect(res.render).to.have.been.calledWith(
"development/all-templates.njk",
{
templatesWithContextRadioOptions: {
"some-template": [],
"another-template": [
{ text: "context", value: "context" },
{ text: "No context", value: "" },
],
},
csrfToken: undefined,
},
);
});
});
describe("allTemplatesPost", () => {
const res = createResponse();
[
{
testCase: "a template is not chosen",
req: createRequest({ body: { template: undefined } }),
},
{
testCase:
"a template is chosen but a context is not if there are context options",
req: createRequest({
body: { template: "another-template", pageContext: undefined },
}),
},
].forEach(({ testCase, req }) => {
it(`should render the all-templates page when ${testCase}`, async () => {
// Act
middleware.allTemplatesPost(req, res);
// Assert
expect(res.render).to.have.been.calledWith(
"development/all-templates.njk",
{
templatesWithContextRadioOptions: {
"some-template": [],
"another-template": [
{ text: "context", value: "context" },
{ text: "No context", value: "" },
],
},
csrfToken: undefined,
errorState: true,
},
);
expect(res.redirect).to.not.be.called;
});
});
it("should redirect to the correct url if template and context have been chosen", async () => {
// Arrange
const req = createRequest({
body: {
template: "another-template",
language: "en",
pageContext: "context",
hasErrorState: false,
},
});
const res = createResponse();
// Act
await middleware.allTemplatesPost(req, res);
// Assert
expect(res.render).to.not.have.been.called;
expect(res.redirect).to.have.been.calledWith(
"/dev/template/another-template/en?context=context",
);
});
it("should redirect to the correct url if template is chosen with no available contexts", async () => {
// Arrange
const req = createRequest({
body: {
template: "some-template",
language: "en",
hasErrorState: false,
},
});
const res = createResponse();
// Act
await middleware.allTemplatesPost(req, res);
// Assert
expect(res.render).to.not.have.been.called;
expect(res.redirect).to.have.been.calledWith(
"/dev/template/some-template/en",
);
});
});