Skip to content

Commit

Permalink
add update ADD todo item test for #48
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 3, 2018
1 parent dba0be7 commit 4571db0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
14 changes: 10 additions & 4 deletions examples/todo-list/todo-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ var initial_model = {
* @return {Object} updated_model - the transformed model.
*/
function update(action, model, data) {
// var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
// console.log('model:', model);
var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
// console.log('new_model:', new_model);
switch(action) { // and an action (String) runs a switch
// case 'CREATE':
// new_model.counters[index] = model.counters[index] + 1;
// case 'ADD':
// new_model.todos.push({
// id: model.todos.length + 1,
// title: data,
// done: false
// });
// break;
// case Dec:
// new_model.counters[index] = model.counters[index] - 1;
Expand All @@ -33,7 +39,7 @@ function update(action, model, data) {
default: // if action unrecognised or undefined,
return model; // return model unmodified
} // see: https://softwareengineering.stackexchange.com/a/201786/211301
// return new_model;
return new_model;
}

// function view(signal, model, root) {
Expand Down
10 changes: 10 additions & 0 deletions test/todo-app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ test('todo `update` default case should return model unmodified', function (t) {
t.deepEqual(model, unmodified_model, "model returned unmodified");
t.end();
});

test('`ADD` a new todo item to model.todos Array via `update`', function (t) {
const model = JSON.parse(JSON.stringify(app.model)); // initial state
t.equal(model.todos.length, 0, "initial model.todos.length is 0");
const updated_model = app.update('ADD', model, "Add Todo List Item");
const expected = { id: 1, title: "Add Todo List Item", done: false };
t.equal(updated_model.todos.length, 1, "updated_model.todos.length is 1");
t.deepEqual(expected, updated_model.todos[0], "Todo list item added.");
t.end();
});
23 changes: 11 additions & 12 deletions todo-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,10 @@ Without making _any_ other changes, re-run the tests:
```sh
node test/todo-app.test.js
```
You should see this assertion pass:
_Everything_ should still pass:
![update-default-branch-test-passing](https://user-images.githubusercontent.com/194400/43581137-c6aa236e-964f-11e8-96d0-ef724659761e.png)




Congratulations! You just _refactored_ a function (_signature_).


#### `ADD` item _Test_
Expand All @@ -495,30 +493,31 @@ Append following test code to your `test/todo-app.test.js` file:
```js
test('`ADD` a new todo item to model.todos Array via `update`', function (t) {
const model = JSON.parse(JSON.stringify(app.model)); // initial state
t.equal(model.todos.length, 0, "initial model.todos.length is 0");
const updated_model = app.update('ADD', model, "Add Todo List Item");
const expected = { id: 1, title: "Add Todo List Item", done: false }
t.deepEqual(model, unmodified_model, "model returned unmodified");
const expected = { id: 1, title: "Add Todo List Item", done: false };
t.equal(updated_model.todos.length, 1, "updated_model.todos.length is 1");
t.deepEqual(expected, updated_model.todos[0], "Todo list item added.");
t.end();
});
```







If you _run_ this test in your terminal:
```sh
node test/todo-app.test.js
```
You should see the assertion _fail_:

![update-add-item-test-failing](https://user-images.githubusercontent.com/194400/43639131-206b632c-9713-11e8-83ee-d0ecab0ac4ef.png)




#### `ADD` item _Implementation_




<!--
## What _Next_?
Expand Down

0 comments on commit 4571db0

Please sign in to comment.