generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathapp.test.js
More file actions
58 lines (46 loc) · 1.57 KB
/
app.test.js
File metadata and controls
58 lines (46 loc) · 1.57 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
const request = require("supertest");
const app = require("./app");
describe("POST /subjects", () => {
test("responds with authenticated message", async () => {
const response = await request(app)
.post("/subjects")
.set("X-Username", "Ahmed")
.send(["Birds", "Bats"]);
expect(response.statusCode).toBe(200);
expect(response.text).toContain("You are authenticated as Ahmed.");
expect(response.text).toContain(
"You have requested information about 2 subjects: Birds, Bats."
);
});
test("responds with not authenticated when header missing", async () => {
const response = await request(app)
.post("/subjects")
.send(["Bees"]);
expect(response.statusCode).toBe(200);
expect(response.text).toContain("You are not authenticated.");
expect(response.text).toContain(
"You have requested information about 1 subject: Bees."
);
});
test("rejects non-array body", async () => {
const response = await request(app)
.post("/subjects")
.send({ wrong: "format" });
expect(response.statusCode).toBe(400);
});
test("rejects array with non-strings", async () => {
const response = await request(app)
.post("/subjects")
.send([1, 2, 3]);
expect(response.statusCode).toBe(400);
});
test("responds with empty subjects message", async () => {
const response = await request(app)
.post("/subjects")
.send([]);
expect(response.statusCode).toBe(200);
expect(response.text).toContain(
"You have requested information about 0 subjects."
);
});
});