-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.test.js
65 lines (52 loc) · 1.73 KB
/
utils.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
require("jest");
const fs = require("fs");
const { readExistingTodos, getSpecificTodo, saveTodos } = require("./utils");
jest.mock("fs");
describe("readExistingTodos", () => {
it("should read and parse the existing todos from the file", () => {
const mockTodos = [
{ id: 1, text: "Todo 1" },
{ id: 2, text: "Todo 2" },
];
fs.readFileSync.mockReturnValue(JSON.stringify(mockTodos));
const todos = readExistingTodos();
expect(fs.readFileSync).toHaveBeenCalledWith("./todos.json", "utf-8");
expect(todos).toEqual(mockTodos);
});
});
describe("getSpecificTodo", () => {
it("should return the todo with the specified id", () => {
const mockTodos = [
{ id: 1, text: "Todo 1" },
{ id: 2, text: "Todo 2" },
{ id: 3, text: "Todo 3" },
];
fs.readFileSync.mockReturnValue(JSON.stringify(mockTodos));
const todo = getSpecificTodo(2);
expect(fs.readFileSync).toHaveBeenCalledWith("./todos.json", "utf-8");
expect(todo).toEqual({ id: 2, text: "Todo 2" });
});
it("should return undefined if the todo with the specified id is not found", () => {
const mockTodos = [
{ id: 1, text: "Todo 1" },
{ id: 2, text: "Todo 2" },
];
fs.readFileSync.mockReturnValue(JSON.stringify(mockTodos));
const todo = getSpecificTodo(3);
expect(fs.readFileSync).toHaveBeenCalledWith("./todos.json", "utf-8");
expect(todo).toBeUndefined();
});
});
describe("saveTodos", () => {
it("should save the todos to the file", () => {
const mockTodos = [
{ id: 1, text: "Todo 1" },
{ id: 2, text: "Todo 2" },
];
saveTodos(mockTodos);
expect(fs.writeFileSync).toHaveBeenCalledWith(
"todos.json",
JSON.stringify(mockTodos)
);
});
});