-
Notifications
You must be signed in to change notification settings - Fork 131
/
test.mjs
139 lines (119 loc) · 4.61 KB
/
test.mjs
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
import chai from "chai";
import chaiHttp from "chai-http";
import app from "./app.mjs";
chai.use(chaiHttp);
const expect = chai.expect;
describe("Reading List API", () => {
describe("POST /reading-list/books", () => {
it("should add a book to the reading list", async () => {
const newBook = {
title: "Test Book",
author: "Test Author",
status: "to_read",
};
const request = chai.request(app);
const res = await request.post("/reading-list/books").send(newBook);
expect(res).to.have.status(201);
expect(res.body).to.have.property("uuid");
expect(res.body.title).to.equal(newBook.title);
expect(res.body.author).to.equal(newBook.author);
});
it("should return a 400 Bad Request when the status is invalid", async () => {
const invalidBook = {
title: "Invalid Book",
author: "Invalid Author",
status: "invalid_status",
};
const request = chai.request(app);
const res = await request.post("/reading-list/books").send(invalidBook);
expect(res).to.have.status(400);
expect(res.body.error).to.equal(
"Status is invalid. Accepted statuses: read | to_read | reading"
);
});
it("should return a 400 Bad Request when title, author, or status is empty", async () => {
const emptyFieldsBook = {};
const request = chai.request(app);
const res = await request
.post("/reading-list/books")
.send(emptyFieldsBook);
expect(res).to.have.status(400);
expect(res.body.error).to.equal(
"Status is invalid. Accepted statuses: read | to_read | reading"
);
});
});
describe("PUT /reading-list/books/:uuid", () => {
it("should update the status of a book", async () => {
const newBook = {
title: "Test Book",
author: "Test Author",
status: "to_read",
};
const requestOne = chai.request(app);
const addRes = await requestOne.post("/reading-list/books").send(newBook);
const updatedStatus = "reading";
const requestTwo = chai.request(app);
const updateRes = await requestTwo
.put(`/reading-list/books/${addRes.body.uuid}`)
.send({ status: updatedStatus });
expect(updateRes).to.have.status(200);
expect(updateRes.body.uuid).to.equal(addRes.body.uuid);
expect(updateRes.body.status).to.equal(updatedStatus);
});
it("should return a 404 Not Found when the UUID does not exist", async () => {
const nonExistentUuid = "non-existent-uuid";
const updatedStatus = "reading";
const request = chai.request(app);
const res = await request
.put(`/reading-list/books/${nonExistentUuid}`)
.send({ status: updatedStatus });
expect(res).to.have.status(404);
expect(res.body.error).to.equal("UUID does not exist");
});
it("should return a 400 Bad Request when the status is invalid", async () => {
const newBook = {
title: "Test Book",
author: "Test Author",
status: "to_read",
};
const requestOne = chai.request(app);
const addRes = await requestOne.post("/reading-list/books").send(newBook);
const updatedStatus = "invalid_status";
const requestTwo = chai.request(app);
const updateRes = await requestTwo
.put(`/reading-list/books/${addRes.body.uuid}`)
.send({ status: updatedStatus });
expect(updateRes).to.have.status(400);
expect(updateRes.body.error).to.equal(
"Status is invalid. Accepted statuses: read | to_read | reading"
);
});
});
describe("GET /reading-list/books/:uuid", () => {
it("should return a book by UUID", async () => {
const newBook = {
title: "Test Book",
author: "Test Author",
status: "to_read",
};
const requestOne = chai.request(app);
const addRes = await requestOne.post("/reading-list/books").send(newBook);
const requestTwo = chai.request(app);
const res = await requestTwo.get(
`/reading-list/books/${addRes.body.uuid}`
);
expect(res).to.have.status(200);
expect(res.body.uuid).to.equal(addRes.body.uuid);
expect(res.body.title).to.equal(newBook.title);
expect(res.body.author).to.equal(newBook.author);
});
it("should return a 404 Not Found when the UUID does not exist", async () => {
const nonExistentUuid = "non-existent-uuid";
const request = chai.request(app);
const res = await request.get(`/reading-list/books/${nonExistentUuid}`);
expect(res).to.have.status(404);
expect(res.body.error).to.equal("UUID does not exist");
});
});
});