-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.test.js
More file actions
30 lines (21 loc) · 990 Bytes
/
book.test.js
File metadata and controls
30 lines (21 loc) · 990 Bytes
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
const { getBook } = require("./controllers/book.controller");
const Book = require("./models/book.model");
// Making mock req, res
const req = {params: {id: '65e505573be2ff278a96c3ad'}};
const res = { status: jest.fn().mockReturnThis(), json: jest.fn()}
// Unit test
describe("testing getBook function", () => {
it("Book ID exists, return book information", async() => {
const bookFromDB = await Book.findById('65e505573be2ff278a96c3ad');
await getBook(req, res);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(bookFromDB);
});
it("Book ID doesn't exist, return error message", async() => {
const message = "Book ID does not exist";
Book.findById.mockRejectedValue(new Error(message));
await getBook(req, res);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({message: "Book ID does not exist in DB"});
});
})