Skip to content

Commit 5d4ebc5

Browse files
committed
add sample code for making TOGGLE todo item test pass for #48
1 parent de9797e commit 5d4ebc5

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Diff for: examples/todo-list/todo-app.js

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ function update(action, model, data) {
2929
done: false
3030
});
3131
break;
32+
case 'TOGGLE':
33+
new_model.todos.forEach(function (item) { // takes 1ms on a "slow mobile"
34+
if(item.id === data) { // this should only "match" one item.
35+
item.done = !item.done; // invert state of "done" e.g false >> true
36+
}
37+
});
38+
break;
3239
default: // if action unrecognised or undefined,
3340
return model; // return model unmodified
3441
} // see: https://softwareengineering.stackexchange.com/a/201786/211301

Diff for: test/todo-app.test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ test('`ADD` a new todo item to model.todos Array via `update`', function (t) {
2727
const updated_model = app.update('ADD', model, "Add Todo List Item");
2828
const expected = { id: 1, title: "Add Todo List Item", done: false };
2929
t.equal(updated_model.todos.length, 1, "updated_model.todos.length is 1");
30-
t.deepEqual(expected, updated_model.todos[0], "Todo list item added.");
30+
t.deepEqual(updated_model.todos[0], expected, "Todo list item added.");
31+
t.end();
32+
});
33+
34+
test('`TOGGLE` a todo item from done=false to done=true', function (t) {
35+
const model = JSON.parse(JSON.stringify(app.model)); // initial state
36+
const model_with_todo = app.update('ADD', model, "Toggle a todo list item");
37+
const item = model_with_todo.todos[0];
38+
const model_todo_done = app.update('TOGGLE', model_with_todo, item.id);
39+
const expected = { id: 1, title: "Toggle a todo list item", done: true };
40+
t.deepEqual(model_todo_done.todos[0], expected, "Todo list item Toggled.");
41+
t.end();
42+
});
43+
44+
test('`TOGGLE` (undo) a todo item from done=true to done=false', function (t) {
45+
const model = JSON.parse(JSON.stringify(app.model)); // initial state
46+
const model_with_todo = app.update('ADD', model, "Toggle a todo list item");
47+
const item = model_with_todo.todos[0];
48+
const model_todo_done = app.update('TOGGLE', model_with_todo, item.id);
49+
const expected = { id: 1, title: "Toggle a todo list item", done: true };
50+
t.deepEqual(model_todo_done.todos[0], expected, "Todo list item Toggled.");
51+
// add another item before "undoing" the original one:
52+
const model_second_item = app.update('ADD', model_todo_done, "Another todo");
53+
t.equal(model_second_item.todos.length, 2, "there are two todo items");
54+
// Toggle the original item such that: done=true >> done=false
55+
const model_todo_undone = app.update('TOGGLE', model_second_item, item.id);
56+
const undone = { id: 1, title: "Toggle a todo list item", done: false };
57+
t.deepEqual(model_todo_undone.todos[0],undone, "Todo item Toggled > undone!");
3158
t.end();
3259
});

0 commit comments

Comments
 (0)