-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.test.js
More file actions
188 lines (159 loc) · 5.2 KB
/
types.test.js
File metadata and controls
188 lines (159 loc) · 5.2 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
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
import {
dbConnect,
dbDisconnect,
resetDb,
} from "../../tests/dbHandler.utils.js";
import request from "supertest";
import {
NON_EXISTANT_ID,
NON_EXISTANT_NAME,
} from "../../tests/fixtures/index.js";
import dotenv from "dotenv";
import express from "express";
import typesRouter from "./types.js";
import activitiesRouter from "./activities.js";
import cors from "cors";
import {
expectActivityTypeArraysEqual,
expectActivityTypesEqual,
} from "../../tests/util/expect-helpers.js";
dotenv.config();
let app;
let server;
beforeAll(async () => {
await dbConnect();
app = express();
app.use(cors());
app.use(express.json());
app.use("/types", typesRouter);
app.use("/activities", activitiesRouter);
server = app.listen(process.env.TEST_SERVER_PORT);
});
afterAll(async () => {
await dbDisconnect();
server.close();
});
afterEach(async () => await resetDb());
/**
* Add a type using the API.
* @param {string} name The name of the type to add
* @returns The JSON representation of the response body
*/
const addTestType = async (name) => {
const responseBody = await request(app)
.post("/types/add")
.send({ name })
.expect("Content-Type", /json/)
.expect(200)
.then((response) => response.body);
return responseBody;
};
describe("get all activity types", () => {
test("should return types", async () => {
const type = await addTestType("TEST NAME");
const types = await request(app)
.get("/types")
.expect("Content-Type", /json/)
.expect(200)
.then((response) => response.body);
expectActivityTypeArraysEqual([type], types);
});
});
describe("get activity type by id", () => {
test("should return type given id", async () => {
const type = await addTestType("TEST NAME");
const returnedType = await request(app)
.get(`/types/${type._id}`)
.expect("Content-Type", /json/)
.expect(200)
.then((response) => response.body);
expectActivityTypesEqual(type, returnedType);
});
test("should return invalid request given invalid id", async () => {
await request(app)
.get(`/types/INVALID-ID`)
.expect("Content-Type", /html/)
.expect(400);
});
test("should return not found given non-existant id", async () => {
await request(app)
.get(`/types/${NON_EXISTANT_ID}`)
.expect("Content-Type", /html/)
.expect(404);
});
});
describe("get activity by name", () => {
test("should return type by name", async () => {
const name = "TEST NAME";
const type = await addTestType(name);
const returnedType = await request(app)
.get(`/types`)
.query({ name })
.expect("Content-Type", /json/)
.expect(200)
.then((response) => response.body);
expectActivityTypesEqual(type, returnedType);
});
test("should return not found given non-existant name", async () => {
await request(app)
.get(`/types`)
.query({ name: NON_EXISTANT_NAME })
.expect("Content-Type", /html/)
.expect(404)
.then((response) => response.body);
});
});
describe("add activity type", () => {
test("should return added type", async () => {
const type = { name: "TEST TYPE NAME" };
const addedType = await request(app)
.post("/types/add")
.send(type)
.expect("Content-Type", /json/)
.expect(200)
.then((response) => response.body);
expectActivityTypesEqual(type, addedType);
});
test("should return invalid request given no type", async () => {
await request(app)
.post(`/types/add`)
.send({ name: null })
.set("Accept", "application/json")
.expect("Content-Type", /html/)
.expect(400);
});
test("should return invalid request given duplicate type", async () => {
const name = "TEST TYPE NAME";
await addTestType(name);
await request(app)
.post(`/types/add`)
.send({ name })
.set("Accept", "application/json")
.expect("Content-Type", /html/)
.expect(400);
});
});
describe("remove activity type", () => {
test("should remove type", async () => {
const type = await addTestType("TEST TYPE NAME");
// Remove
await request(app).delete(`/types/remove/${type._id}`).expect(200);
// Expect to be removed
await request(app)
.get(`/types/${type._id}`)
.expect("Content-Type", /html/)
.expect(404);
});
test("should return invalid request given invalid id", async () => {
await request(app)
.delete(`/types/remove/INVALID-ID`)
.expect("Content-Type", /html/)
.expect(400);
});
test("should return not found given non-existant id", async () => {
await request(app)
.delete(`/types/remove/${NON_EXISTANT_ID}`)
.expect("Content-Type", /html/)
.expect(404);
});
});