Skip to content

Commit fbca2df

Browse files
committed
The deleteCompleted function has been added to todos.mjs to match the test expectations
1 parent bf2eb25 commit fbca2df

2 files changed

Lines changed: 57 additions & 67 deletions

File tree

Sprint-3/todo-list/todos.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ export function toggleCompletedOnTask(todos, taskIndex) {
2626
todos[taskIndex].completed = !todos[taskIndex].completed;
2727
}
2828
}
29+
30+
// Delete all completed tasks
31+
export function deleteCompleted(todos) {
32+
for (let i = todos.length - 1; i >= 0; i--) {
33+
if (todos[i].completed) {
34+
todos.splice(i, 1);
35+
}
36+
}
37+
}

Sprint-3/todo-list/todos.test.mjs

Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
// The tests is prepared to demonstrate we can test the functions
2-
// in a module independently.
3-
41
// Command to execute this script:
5-
// npm test todos.test.mjs
2+
// node --test todos.test.mjs
63

7-
// Import all the exported members through an object
84
import * as Todos from "./todos.mjs";
5+
import { describe, it } from "node:test";
6+
import assert from "node:assert/strict";
97

108
// Return a mock ToDo List data with exactly 4 elements.
119
function createMockTodos() {
@@ -21,153 +19,136 @@ function createMockTodos() {
2119
const theTask = { task: "The Task", completed: false, deadline: null };
2220

2321
describe("addTask()", () => {
24-
test("Add a task to an empty ToDo list", () => {
22+
it("Add a task to an empty ToDo list", () => {
2523
let todos = [];
2624
Todos.addTask(todos, theTask.task, theTask.completed);
27-
expect(todos).toHaveLength(1);
28-
expect(todos[0]).toEqual(theTask);
25+
assert.strictEqual(todos.length, 1);
26+
assert.deepEqual(todos[0], theTask);
2927
});
3028

31-
test("Should append a new task to the end of a ToDo list", () => {
32-
29+
it("Should append a new task to the end of a ToDo list", () => {
3330
const todos = createMockTodos();
3431
const lengthBeforeAddition = todos.length;
3532
Todos.addTask(todos, theTask.task, theTask.completed);
36-
// todos should now have one more task
37-
expect(todos).toHaveLength(lengthBeforeAddition + 1);
38-
39-
// New task should be appended to the todos
40-
expect(todos[todos.length - 1]).toEqual(theTask);
33+
assert.strictEqual(todos.length, lengthBeforeAddition + 1);
34+
assert.deepEqual(todos[todos.length - 1], theTask);
4135
});
4236
});
4337

4438
describe("deleteTask()", () => {
45-
46-
test("Delete the first task", () => {
39+
it("Delete the first task", () => {
4740
const todos = createMockTodos();
4841
const todosBeforeDeletion = createMockTodos();
4942
const lengthBeforeDeletion = todos.length;
5043
Todos.deleteTask(todos, 0);
5144

52-
expect(todos).toHaveLength(lengthBeforeDeletion - 1);
53-
54-
expect(todos[0]).toEqual(todosBeforeDeletion[1]);
55-
expect(todos[1]).toEqual(todosBeforeDeletion[2]);
56-
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
45+
assert.strictEqual(todos.length, lengthBeforeDeletion - 1);
46+
assert.deepEqual(todos[0], todosBeforeDeletion[1]);
47+
assert.deepEqual(todos[1], todosBeforeDeletion[2]);
48+
assert.deepEqual(todos[2], todosBeforeDeletion[3]);
5749
});
5850

59-
test("Delete the second task (a middle task)", () => {
51+
it("Delete the second task (a middle task)", () => {
6052
const todos = createMockTodos();
6153
const todosBeforeDeletion = createMockTodos();
6254
const lengthBeforeDeletion = todos.length;
6355
Todos.deleteTask(todos, 1);
6456

65-
expect(todos).toHaveLength(lengthBeforeDeletion - 1);
66-
67-
expect(todos[0]).toEqual(todosBeforeDeletion[0]);
68-
expect(todos[1]).toEqual(todosBeforeDeletion[2]);
69-
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
57+
assert.strictEqual(todos.length, lengthBeforeDeletion - 1);
58+
assert.deepEqual(todos[0], todosBeforeDeletion[0]);
59+
assert.deepEqual(todos[1], todosBeforeDeletion[2]);
60+
assert.deepEqual(todos[2], todosBeforeDeletion[3]);
7061
});
7162

72-
test("Delete the last task", () => {
63+
it("Delete the last task", () => {
7364
const todos = createMockTodos();
7465
const todosBeforeDeletion = createMockTodos();
7566
const lengthBeforeDeletion = todos.length;
7667
Todos.deleteTask(todos, todos.length - 1);
7768

78-
expect(todos).toHaveLength(lengthBeforeDeletion - 1);
79-
80-
expect(todos[0]).toEqual(todosBeforeDeletion[0]);
81-
expect(todos[1]).toEqual(todosBeforeDeletion[1]);
82-
expect(todos[2]).toEqual(todosBeforeDeletion[2]);
69+
assert.strictEqual(todos.length, lengthBeforeDeletion - 1);
70+
assert.deepEqual(todos[0], todosBeforeDeletion[0]);
71+
assert.deepEqual(todos[1], todosBeforeDeletion[1]);
72+
assert.deepEqual(todos[2], todosBeforeDeletion[2]);
8373
});
8474

85-
test("Delete a non-existing task", () => {
75+
it("Delete a non-existing task", () => {
8676
const todos = createMockTodos();
8777
const todosBeforeDeletion = createMockTodos();
8878
Todos.deleteTask(todos, 10);
89-
expect(todos).toEqual(todosBeforeDeletion);
79+
assert.deepEqual(todos, todosBeforeDeletion);
9080

9181
Todos.deleteTask(todos, -1);
92-
expect(todos).toEqual(todosBeforeDeletion);
82+
assert.deepEqual(todos, todosBeforeDeletion);
9383
});
9484
});
9585

9686
describe("toggleCompletedOnTask()", () => {
97-
98-
test("Expect the 'completed' property to toggle on an existing task", () => {
87+
it("Expect the 'completed' property to toggle on an existing task", () => {
9988
const todos = createMockTodos();
10089
const taskIndex = 1;
10190
const completedStateBeforeToggle = todos[taskIndex].completed;
10291
Todos.toggleCompletedOnTask(todos, taskIndex);
103-
expect(todos[taskIndex].completed).toEqual(!completedStateBeforeToggle);
92+
assert.strictEqual(todos[taskIndex].completed, !completedStateBeforeToggle);
10493

10594
// Toggle again
10695
Todos.toggleCompletedOnTask(todos, taskIndex);
107-
expect(todos[taskIndex].completed).toEqual(completedStateBeforeToggle);
96+
assert.strictEqual(todos[taskIndex].completed, completedStateBeforeToggle);
10897
});
10998

110-
test("Expect toggling on a task does not affect other tasks", () => {
99+
it("Expect toggling on a task does not affect other tasks", () => {
111100
const todos = createMockTodos();
112101
const todosBeforeToggle = createMockTodos();
113102
Todos.toggleCompletedOnTask(todos, 1);
114-
115-
expect(todos[0]).toEqual(todosBeforeToggle[0]);
116-
expect(todos[2]).toEqual(todosBeforeToggle[2]);
117-
expect(todos[3]).toEqual(todosBeforeToggle[3]);
118-
});
119103

104+
assert.deepEqual(todos[0], todosBeforeToggle[0]);
105+
assert.deepEqual(todos[2], todosBeforeToggle[2]);
106+
assert.deepEqual(todos[3], todosBeforeToggle[3]);
107+
});
120108

121-
test("Expect no change when toggling on a non-existing task", () => {
109+
it("Expect no change when toggling on a non-existing task", () => {
122110
const todos = createMockTodos();
123111
const todosBeforeToggle = createMockTodos();
124112

125113
Todos.toggleCompletedOnTask(todos, 10);
126-
expect(todos).toEqual(todosBeforeToggle);
114+
assert.deepEqual(todos, todosBeforeToggle);
127115

128116
Todos.toggleCompletedOnTask(todos, -1);
129-
expect(todos).toEqual(todosBeforeToggle);
117+
assert.deepEqual(todos, todosBeforeToggle);
130118
});
131119
});
132120

133121
describe("deleteCompleted()", () => {
134-
135-
test("Remove all completed tasks", () => {
122+
it("Remove all completed tasks", () => {
136123
const todos = createMockTodos();
137124

138125
Todos.deleteCompleted(todos);
139126

140-
// Only incomplete tasks should remain
141-
expect(todos).toHaveLength(2);
142-
expect(todos[0].completed).toBe(false);
143-
expect(todos[1].completed).toBe(false);
144-
145-
expect(todos[0].task).toBe("Task 2 description");
146-
expect(todos[1].task).toBe("Task 4 description");
127+
assert.strictEqual(todos.length, 2);
128+
assert.strictEqual(todos[0].completed, false);
129+
assert.strictEqual(todos[1].completed, false);
130+
assert.strictEqual(todos[0].task, "Task 2 description");
131+
assert.strictEqual(todos[1].task, "Task 4 description");
147132
});
148133

149-
test("No change if no tasks are completed", () => {
134+
it("No change if no tasks are completed", () => {
150135
const todos = [
151136
{ task: "Task A", completed: false },
152137
{ task: "Task B", completed: false }
153138
];
154139

155140
const before = JSON.parse(JSON.stringify(todos));
156-
157141
Todos.deleteCompleted(todos);
158-
159-
expect(todos).toEqual(before);
142+
assert.deepEqual(todos, before);
160143
});
161144

162-
test("All tasks removed if all are completed", () => {
145+
it("All tasks removed if all are completed", () => {
163146
const todos = [
164147
{ task: "Task A", completed: true },
165148
{ task: "Task B", completed: true }
166149
];
167150

168151
Todos.deleteCompleted(todos);
169-
170-
expect(todos).toHaveLength(0);
152+
assert.strictEqual(todos.length, 0);
171153
});
172-
173154
});

0 commit comments

Comments
 (0)