generated from getir-nodejs-bootcamp/getirgraduationproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.test.js
169 lines (156 loc) · 5.14 KB
/
app.test.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const request = require("supertest");
const app = require("./app");
describe("POST /api/v1/records", () => {
//should response with 200 status code
//should specify json in the content type header
//should response with a json object contaning code, msg, records
describe("check when given startDate, endDate, maxCount and minCount as wanted", () => {
test("should response with a 200 status code", () => {
request(app)
.post("/api/v1/records")
.send({
startDate: "2016-10-04",
endDate: "2018-07-21",
minCount: 200,
maxCount: 1000,
})
.expect(200);
});
test("should specify json in the content type header", () => {
request(app)
.post("/api/v1/records")
.send({
startDate: "2016-10-04",
endDate: "2018-07-21",
minCount: 200,
maxCount: 1000,
})
.expect("Content-Type", /json/);
});
test("should response with json object including 3 properties which are code, msg, records", () => {
request(app)
.post("/api/v1/records")
.send({
startDate: "2016-10-04",
endDate: "2018-07-21",
minCount: 200,
maxCount: 1000,
})
.then((res) => {
expect(res.body).toEqual(
expect.objectContaining({
code: 0,
msg: "Success",
records: expect.any(Array),
})
);
});
});
});
//check records has 3 properties which are key, createdAt, totalCount
//check records are between given dates
//check records are between min and max counts
describe("check if return values are true", () => {
test("check records are between given dates", async () => {
let startDate = "2016-10-04";
let endDate = "2017-07-21";
const response = await request(app).post("/api/v1/records").send({
startDate,
endDate,
minCount: 200,
maxCount: 1000,
});
const records = response.body.records.splice(0, 3);
startDate = new Date(startDate);
endDate = new Date(endDate);
const isTrue = records.every((record) => {
return (
new Date(record.createdAt) >= startDate &&
new Date(record.createdAt) <= endDate
);
});
expect(isTrue).toBe(true);
}, 15000);
test("check records are between min and max counts", async () => {
const minCount = 200;
const maxCount = 1000;
const response = await request(app).post("/api/v1/records").send({
startDate: "2016-10-04",
endDate: "2018-07-21",
minCount,
maxCount,
});
const records = response.body.records.splice(0, 3);
const isTrue = records.every((record) => {
const number = record.totalCount;
return number >= minCount && number <= maxCount;
});
expect(isTrue).toBe(true);
}, 15000);
test("objects in records array should have three properties which are key, createdAt, totalCount ", async () => {
const expected = ["key", "createdAt", "totalCount"];
const response = await request(app).post("/api/v1/records").send({
startDate: "2016-10-04",
endDate: "2017-07-21",
minCount: 500,
maxCount: 1000,
});
const records = response.body.records.splice(0, 2); //limit the array with 10 items to speed the test
const isTrue = records.every((record) =>
Object.keys(record).every((item) => expected.includes(item))
);
expect(isTrue).toBe(true);
}, 15000);
});
//startDate is after endDate should response with 400 status code
//endDate or startDate after current Date should response with 400 status code
//minCount grater than maxCount should response with 400 status code
describe("check if irrational inputs given, and return bad request", () => {
test("check when startDate is after endDate", async () => {
request(app)
.post("/api/v1/records")
.send({
startDate: "2017-10-04",
endDate: "2016-07-21",
minCount: 200,
maxCount: 1000,
})
.expect(400);
});
test("check when endDate or startDate after current Date", async () => {
request(app)
.post("/api/v1/records")
.send({
startDate: "2027-10-04",
endDate: "2016-07-21",
minCount: 200,
maxCount: 1000,
})
.expect(400);
});
test("check when minCount grater than maxCount", async () => {
request(app)
.post("/api/v1/records")
.send({
startDate: "2015-10-04",
endDate: "2016-07-21",
minCount: 500,
maxCount: 200,
})
.expect(400);
});
});
//when any of the payloads is missed,should respond with a status code of 400
describe("when any of the payload is missing", () => {
test("when startDate is missing , check if response 400 (Bad Request)", () => {
request(app)
.post("/api/v1/records")
.send({
endDate: "2018-07-21",
minCount: 200,
maxCount: 1000,
})
.expect(400);
});
});
});