Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sprint-3/todo-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Each ToDo task has two properties:

To view the website, open `index.html` with Live Server in VS Code.

**Note**: The app is loaded **as ES modules** in the HTML file, and as such, the HTML file must be accessed via the HTTP or HTTPS protocol.
**Note**: The app is loaded **as ES modules** in the HTML file, and as such, the HTML file must be accessed via the HTTP or HTTPS protocol. In this project, we have included `http-server` as a dev-dependency, so after you `npm install`, you can run `npm run serve` from this directory to serve this project over HTTP.

## Understanding how the code is organized as ES modules

Expand Down
2 changes: 2 additions & 0 deletions Sprint-3/todo-list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"description": "You must update this package",
"type": "module",
"scripts": {
"serve": "http-server",
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
},
"repository": {
Expand All @@ -16,6 +17,7 @@
},
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"http-server": "^14.1.1",
"jest": "^30.0.4"
}
}
2 changes: 2 additions & 0 deletions Sprint-3/todo-list/package.json-windows
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"description": "You must update this package",
"type": "module",
"scripts": {
"serve": "http-server",
"test": "set NODE_OPTIONS=--experimental-vm-modules && jest"
},
"repository": {
Expand All @@ -16,6 +17,7 @@
},
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"http-server": "^14.1.1",
"jest": "^30.0.4"
}
}
5 changes: 3 additions & 2 deletions Sprint-3/todo-list/script.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as Todos from "./todos.mjs";

// To store the todo tasks
let todos = [];
const todos = [];

// Set up tasks to be performed once on page load
window.addEventListener("load", () => {
Expand Down Expand Up @@ -58,8 +58,9 @@ function createListItem(todo, index) {
const li = todoListItemTemplate.cloneNode(true); // true => Do a deep copy of the node

li.querySelector(".description").textContent = todo.task;
if (todo.completed)
if (todo.completed) {
li.classList.add("completed");
}

li.querySelector('.complete-btn').addEventListener("click", () => {
Todos.toggleCompletedOnTask(todos, index);
Expand Down
8 changes: 5 additions & 3 deletions Sprint-3/todo-list/todos.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ export function addTask(todos, task, completed = false) {

// Delete todos[taskIndex] if it exists
export function deleteTask(todos, taskIndex) {
if (todos[taskIndex])
if (todos[taskIndex]) {
todos.splice(taskIndex, 1);
}
}

// Toggle the "completed" property of todos[taskIndex] if the task exists.
export function toggleCompletedOnTask(todos, taskIndex) {
if (todos[taskIndex])
if (todos[taskIndex]) {
todos[taskIndex].completed = !todos[taskIndex].completed;
}
}
}
13 changes: 6 additions & 7 deletions Sprint-3/todo-list/todos.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe("addTask()", () => {
});

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);
Expand Down Expand Up @@ -97,14 +96,14 @@ describe("toggleCompletedOnTask()", () => {

test("Expect the 'completed' property to toggle on an existing task", () => {
const todos = createMockTodos();
const taskIdx = 1;
const completedStateBeforeToggle = todos[taskIdx].completed;
Todos.toggleCompletedOnTask(todos, taskIdx);
expect(todos[taskIdx].completed).toEqual(!completedStateBeforeToggle);
const taskIndex = 1;
const completedStateBeforeToggle = todos[taskIndex].completed;
Todos.toggleCompletedOnTask(todos, taskIndex);
expect(todos[taskIndex].completed).toEqual(!completedStateBeforeToggle);

// Toggle again
Todos.toggleCompletedOnTask(todos, taskIdx);
expect(todos[taskIdx].completed).toEqual(completedStateBeforeToggle);
Todos.toggleCompletedOnTask(todos, taskIndex);
expect(todos[taskIndex].completed).toEqual(completedStateBeforeToggle);
});

test("Expect toggling on a task does not affect other tasks", () => {
Expand Down
Loading