-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.test.js
More file actions
157 lines (126 loc) · 4.81 KB
/
app.test.js
File metadata and controls
157 lines (126 loc) · 4.81 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
// Use jest test
// Supertest
import request from "supertest";
import app from "./server.js";
describe("API Tests", () => {
// Check if the server is running or not
test("get /check-alive", async () =>{
const response = await request(app).get("/check-alive");
expect([200, 404, 500]).toContain(response.statusCode); // Check server status (200 is good, other repsonse is offline)
});
// Get the global settings of the server -- currently only one settings -> reaction attempts
test("get /get-reaction-attempts", async () =>{
const response = await request(app).get("/get-reaction-attempts");
// Expect the correct status code
expect(response.statusCode).toBe(200);
// Expect the correct data
expect(response.body).toEqual({
reactionAttempts: expect.any(Number)
});
});
test("get /api/get-leaderboard", async () =>{
const response = await request(app).get("/api/get-leaderboard");
// Check if the database fails
if (response.statusCode === 500){
expect(response.body).toHaveProperty("error");
}
// Check if the resposne is an array
expect(Array.isArray(response.body)).toBe(true);
// Check if each array is holding the correct type of data
response.body.forEach(item => {
expect(item).toEqual({
name: expect.any(String),
wpm: expect.any(Number),
date: expect.any(String)
});
});
});
test("post /save-reaction-attempts Check if the correct data is saved", async () => {
// Create the data to send
const newAttempts = {
reactionAttempts: 10
};
// Send the request
const response = await request(app)
.post("/save-reaction-attempts")
.send(newAttempts)
.set("Content-Type", "application/json");
// Check what the data sent back should be
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
message: expect.any(String)
});
});
test("post /save-reaction-attempts Check if the incorrect data is not saved", async () => {
// Create the data to send
const newAttempts = {
attempts: 1 // Should be reactionAttempts: any(Number)
};
// Send the request
const response = await request(app)
.post("/save-reaction-attempts")
.send(newAttempts)
.set("Content-Type", "application/json");
// Check what the data sent back should be
expect(response.statusCode).toBe(400);
expect(response.body).toEqual({
error: expect.any(String)
});
});
test("post /api/submit-score Check if the correct data is saved", async () => {
const newScore = {
name: "test-delete",
wpm: 100,
};
const response = await request(app)
.post("/api/submit-score")
.send(newScore)
.set("Content-Type", "application/json");
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
status: expect.any(String),
message: expect.any(String)
});
});
test("post /api/submit-score Check if the data is not saved without the WPM", async () => {
const newScore = {
name: "test",
};
const response = await request(app)
.post("/api/submit-score")
.send(newScore)
.set("Content-Type", "application/json");
expect(response.statusCode).toBe(400);
expect(response.body).toEqual({
status: expect.any(String),
message: expect.any(String)
});
});
test("post /api/submit-score Check if the data is not saved without the name", async () => {
const newScore = {
wpm: 100
};
const response = await request(app)
.post("/api/submit-score")
.send(newScore)
.set("Content-Type", "application/json");
expect(response.statusCode).toBe(400);
expect(response.body).toEqual({
status: expect.any(String),
message: expect.any(String)
});
});
test("post /api/submit-score Check if the data is not saved without any data", async () => {
const newScore = {
};
const response = await request(app)
.post("/api/submit-score")
.send(newScore)
.set("Content-Type", "application/json");
expect(response.statusCode).toBe(400);
expect(response.body).toEqual({
status: expect.any(String),
message: expect.any(String)
});
});
});