-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathtodos.test.mjs
More file actions
162 lines (131 loc) · 5.25 KB
/
todos.test.mjs
File metadata and controls
162 lines (131 loc) · 5.25 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
158
159
160
161
162
// The tests is prepared to demonstrate we can test the functions
// in a module independently.
// Command to execute this script:
// npm test todos.test.mjs
// Import all the exported members through an object
import * as Todos from "./todos.mjs";
// Return a mock ToDo List data with exactly 4 elements.
function createMockTodos() {
return [
{ task: "Task 1 description", completed: true },
{ task: "Task 2 description", completed: false },
{ task: "Task 3 description", completed: true },
{ task: "Task 4 description", completed: false },
];
}
// A mock task to simulate user input
const theTask = { task: "The Task", completed: false };
describe("addTask()", () => {
test("Add a task to an empty ToDo list", () => {
let todos = [];
Todos.addTask(todos, theTask.task, theTask.completed);
expect(todos).toHaveLength(1);
expect(todos[0]).toEqual(theTask);
});
test("Should append a new task to the end of a ToDo list", () => {
const todos = createMockTodos();
const lengthBeforeAddition = todos.length;
Todos.addTask(todos, theTask.task, theTask.completed);
// todos should now have one more task
expect(todos).toHaveLength(lengthBeforeAddition + 1);
// New task should be appended to the todos
expect(todos[todos.length - 1]).toEqual(theTask);
});
});
describe("deleteTask()", () => {
test("Delete the first task", () => {
const todos = createMockTodos();
const todosBeforeDeletion = createMockTodos();
const lengthBeforeDeletion = todos.length;
Todos.deleteTask(todos, 0);
expect(todos).toHaveLength(lengthBeforeDeletion - 1);
expect(todos[0]).toEqual(todosBeforeDeletion[1]);
expect(todos[1]).toEqual(todosBeforeDeletion[2]);
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
});
test("Delete the second task (a middle task)", () => {
const todos = createMockTodos();
const todosBeforeDeletion = createMockTodos();
const lengthBeforeDeletion = todos.length;
Todos.deleteTask(todos, 1);
expect(todos).toHaveLength(lengthBeforeDeletion - 1);
expect(todos[0]).toEqual(todosBeforeDeletion[0]);
expect(todos[1]).toEqual(todosBeforeDeletion[2]);
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
});
test("Delete the last task", () => {
const todos = createMockTodos();
const todosBeforeDeletion = createMockTodos();
const lengthBeforeDeletion = todos.length;
Todos.deleteTask(todos, todos.length - 1);
expect(todos).toHaveLength(lengthBeforeDeletion - 1);
expect(todos[0]).toEqual(todosBeforeDeletion[0]);
expect(todos[1]).toEqual(todosBeforeDeletion[1]);
expect(todos[2]).toEqual(todosBeforeDeletion[2]);
});
test("Delete a non-existing task", () => {
const todos = createMockTodos();
const todosBeforeDeletion = createMockTodos();
Todos.deleteTask(todos, 10);
expect(todos).toEqual(todosBeforeDeletion);
Todos.deleteTask(todos, -1);
expect(todos).toEqual(todosBeforeDeletion);
});
});
describe("toggleCompletedOnTask()", () => {
test("Expect the 'completed' property to toggle on an existing task", () => {
const todos = createMockTodos();
const taskIndex = 1;
const completedStateBeforeToggle = todos[taskIndex].completed;
Todos.toggleCompletedOnTask(todos, taskIndex);
expect(todos[taskIndex].completed).toEqual(!completedStateBeforeToggle);
// Toggle again
Todos.toggleCompletedOnTask(todos, taskIndex);
expect(todos[taskIndex].completed).toEqual(completedStateBeforeToggle);
});
test("Expect toggling on a task does not affect other tasks", () => {
const todos = createMockTodos();
const todosBeforeToggle = createMockTodos();
Todos.toggleCompletedOnTask(todos, 1);
expect(todos[0]).toEqual(todosBeforeToggle[0]);
expect(todos[2]).toEqual(todosBeforeToggle[2]);
expect(todos[3]).toEqual(todosBeforeToggle[3]);
});
test("Expect no change when toggling on a non-existing task", () => {
const todos = createMockTodos();
const todosBeforeToggle = createMockTodos();
Todos.toggleCompletedOnTask(todos, 10);
expect(todos).toEqual(todosBeforeToggle);
Todos.toggleCompletedOnTask(todos, -1);
expect(todos).toEqual(todosBeforeToggle);
});
});
describe("deleteCompleted()", () => {
test("Expect all completed tasks to be deleted", () => {
const todos = createMockTodos();
Todos.deleteCompleted(todos);
expect(todos).toHaveLength(2);
expect(todos[0]).toEqual({ task: "Task 2 description", completed: false });
expect(todos[1]).toEqual({ task: "Task 4 description", completed: false });
});
test("Expect no change if there is no completed task", () => {
const todos = [
{ task: "Task A description", completed: false },
{ task: "Task B description", completed: false },
];
const todosBeforeDeletion = [
{ task: "Task A description", completed: false },
{ task: "Task B description", completed: false },
];
Todos.deleteCompleted(todos);
expect(todos).toEqual(todosBeforeDeletion);
});
test("Expect all tasks to be deleted if all tasks are completed", () => {
const todos = [
{ task: "Task A description", completed: true },
{ task: "Task B description", completed: true },
];
Todos.deleteCompleted(todos);
expect(todos).toHaveLength(0);
});
});