-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
153 lines (128 loc) · 3.75 KB
/
app.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
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
import axiosist from "axiosist";
import makeApp from "../src/app";
const createSuite = (call) => () => {
it("returns a pdf", async () => {
const response = await call("/demo", null, {
a: "a",
b: "b",
});
expect(response.status).toBe(200);
expect(response.headers["content-disposition"]).toBe(
'attachment; filename="demo.pdf"',
);
expect(response.headers["content-type"]).toBe("application/pdf");
});
it("allows customising the filename", async () => {
const filename = "abc.pdf";
const response = await call("/demo", filename, {
a: "a",
b: "b",
});
expect(response.status).toBe(200);
expect(response.headers["content-disposition"]).toBe(
`attachment; filename="${filename}"`,
);
expect(response.headers["content-type"]).toBe("application/pdf");
});
it("returns a 404 for inexistent files", async () => {
expect.hasAssertions();
const response = await call("/nope");
expect(response.status).toBe(404);
expect(response.data).toBe("document not found");
});
it("accepts props", async () => {
const response = await call("/demo", null, {
a: "Hello",
b: "World",
});
expect(response.status).toBe(200);
expect(response.headers["content-disposition"]).toBe(
'attachment; filename="demo.pdf"',
);
expect(response.headers["content-type"]).toBe("application/pdf");
});
it("returns a 400 when prop types errors", async () => {
expect.hasAssertions();
const response = await call("/demo", null, {
a: 1,
});
expect(response.status).toBe(400);
expect(response.data).toHaveProperty("errors");
expect(response.data.errors).toContain(
"The prop `b` is marked as required in `Test`, but its value is `undefined`.",
);
});
it("returns a 404 for non-document components", async () => {
expect.hasAssertions();
const response = await call("/Side", null, {
a: 1,
});
expect(response.status).toBe(404);
expect(response.data).toBe("document not found");
});
it("returns a 500 when rendering fails", async () => {
expect.hasAssertions();
const response = await call("/broken", null, {
a: 1,
});
expect(response.status).toBe(500);
expect(response.data).toBe("internal server error");
});
it("calls getAsyncProps when it is present", async () => {
expect.hasAssertions();
const response = await call("/asyncProps", null, {});
// When getAsyncProps is called, it sets the value of the `a` prop.
// If it wasn't called, the prop would be missing and we'd get a 400.
expect(response.status).toBe(200);
});
};
describe("httpdf", () => {
let app, axios;
beforeAll(async () => {
app = await makeApp();
axios = axiosist(app);
});
describe(
"GET",
createSuite((url, filename, props) =>
axios(url, {
method: "GET",
params: { filename, ...props },
}),
),
);
describe(
"POST",
createSuite((url, filename, props) =>
axios(url, {
method: "POST",
params: { filename },
data: props,
}),
),
);
describe(
"PUT",
createSuite((url, filename, props) =>
axios(url, {
method: "PUT",
params: { filename },
data: props,
}),
),
);
it("returns a 405 for unsupported methods", async () => {
expect.hasAssertions();
const response = await axios("/demo", {
method: "DELETE",
data: {},
});
expect(response.status).toBe(405);
expect(response.data).toBe("method not allowed");
});
it("returns a 200 on /health", async () => {
const response = await axios.get("/health");
expect(response.status).toBe(200);
expect(response.data).toEqual({ status: "pass" });
});
});