Skip to content

Commit 34c2a05

Browse files
committed
add code to pass default branch update test for #44
1 parent caf8165 commit 34c2a05

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed

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

+24-17
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ var initial_model = {
1111
hash: "#/"
1212
}
1313

14-
function update(model, action) { // Update function takes the current state
15-
var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
14+
/**
15+
* `update` transforms the `model` based on the `action`.
16+
* @param {String} action - the desired action to perform on the model.
17+
* @param {Object} model - the App's data ("state").
18+
* @return {Object} updated_model - the transformed model.
19+
*/
20+
function update(action, model) { // Update function takes the current state
21+
// var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
1622
switch(action) { // and an action (String) runs a switch
1723
// case 'CREATE':
1824
// new_model.counters[index] = model.counters[index] + 1;
@@ -23,30 +29,31 @@ function update(model, action) { // Update function takes the current state
2329
// case Res: // use ES6 Array.fill to create a new array with values set to 0:
2430
// new_model.counters[index] = 0;
2531
// break;
26-
default: return model; // if action not defined, return curent state.
27-
}
28-
return new_model;
32+
default: // if action unrecognised or undefined,
33+
return model; // return model unmodified
34+
} // see: https://softwareengineering.stackexchange.com/a/201786/211301
35+
// return new_model;
2936
}
3037

31-
function view(signal, model, root) {
32-
empty(root); // clear root element before re-rendering the App (DOM).
33-
model.counters.map(function(counter, index) {
34-
return container(index, [ // wrap DOM nodes in an "container"
35-
button('+', signal, Inc + '-' + index), // append index to action
36-
div('count', counter), // create div w/ count as text
37-
button('-', signal, Dec + '-' + index), // decrement counter
38-
button('Reset', signal, Res + '-' + index) // reset counter
39-
]);
40-
}).forEach(function (el) { root.appendChild(el) }); // forEach is ES5 so IE9+
41-
}
38+
// function view(signal, model, root) {
39+
// empty(root); // clear root element before re-rendering the App (DOM).
40+
// model.counters.map(function(counter, index) {
41+
// return container(index, [ // wrap DOM nodes in an "container"
42+
// button('+', signal, Inc + '-' + index), // append index to action
43+
// div('count', counter), // create div w/ count as text
44+
// button('-', signal, Dec + '-' + index), // decrement counter
45+
// button('Reset', signal, Res + '-' + index) // reset counter
46+
// ]);
47+
// }).forEach(function (el) { root.appendChild(el) }); // forEach is ES5 so IE9+
48+
// }
4249

4350
/* module.exports is needed to run the functions using Node.js for testing! */
4451
/* istanbul ignore next */
4552
if (typeof module !== 'undefined' && module.exports) {
4653
module.exports = {
4754
model: initial_model,
4855
update: update,
49-
view: view
56+
// view: view
5057
}
5158
}
5259

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

+7
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ test('todo `model` (Object) has desired keys', function (t) {
1313
t.true(Array.isArray(app.model.todos), "model.todos is an Array")
1414
t.end();
1515
});
16+
17+
test('todo `update` default case should return model unmodified', function (t) {
18+
const model = JSON.parse(JSON.stringify(app.model));
19+
const unmodified_model = app.update('UNKNOWN_ACTION', model);
20+
t.deepEqual(model, unmodified_model, "model returned unmodified");
21+
t.end();
22+
});

Diff for: todo-list.md

+54-2
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ The **`JSDOC`** for our `update` function is:
304304
* @param {Object} model - the App's data ("state").
305305
* @return {Object} updated_model - the transformed model.
306306
*/
307-
308307
```
309308

310309
#### `update` Test
@@ -314,10 +313,63 @@ the function body is a `switch` statement
314313
that "decides" how to handle a request based on the `action` (_also known as the "message"_).
315314

316315
Given that we _know_ that our `update` function "skeleton"
317-
(_because this is the "TEA" pattern_)
316+
will be a `switch` statement
317+
(_because that is the "TEA" pattern_)
318318
good test to _start_ with is the `default case`.
319319

320+
Append following test code in `test/todo-app.test.js`:
321+
322+
```js
323+
test('todo `update` default case should return model unmodified', function (t) {
324+
const model = JSON.parse(JSON.stringify(app.model));
325+
const unmodified_model = app.update('UNKNOWN_ACTION', model);
326+
t.deepEqual(model, unmodified_model, "model returned unmodified");
327+
t.end();
328+
});
329+
```
330+
331+
If you _run_ this test in your terminal:
332+
```sh
333+
node test/todo-app.test.js
334+
```
335+
You should see the assertion _fail_:
336+
![update-default-branch-test-failing](https://user-images.githubusercontent.com/194400/43580847-b78105c0-964e-11e8-81ac-61a1dd8ec535.png)
337+
338+
#### `update` Function Implementation > `switch default case`
339+
340+
Write the _minimum_ code necessary to pass the test.
341+
342+
> Yes, we could just write:
343+
```js
344+
function update (action, model) { return model; }
345+
```
346+
And that _would_ make the test _pass_. <br />
347+
But, in light of the fact that we **know** the `update`
348+
function body will contain a `switch` statement,
349+
make the test pass by returning the `model` _unmodified_ in the `default` case.
350+
351+
e.g:
352+
```js
353+
/**
354+
* `update` transforms the `model` based on the `action`.
355+
* @param {String} action - the desired action to perform on the model.
356+
* @param {Object} model - the App's data ("state").
357+
* @return {Object} updated_model - the transformed model.
358+
*/
359+
function update(action, model) { // Update function takes the current state
360+
switch (action) { // and an action (String) runs a switch
361+
default: // if action unrecognised or undefined,
362+
return model; // return model unmodified
363+
} // see: https://softwareengineering.stackexchange.com/a/201786/211301
364+
}
365+
```
320366

367+
When you re-run the test(s) in your terminal:
368+
```sh
369+
node test/todo-app.test.js
370+
```
371+
You should see this assertion pass:
372+
![update-default-branch-test-passing](https://user-images.githubusercontent.com/194400/43581137-c6aa236e-964f-11e8-96d0-ef724659761e.png)
321373

322374
<!--
323375

0 commit comments

Comments
 (0)