Skip to content

Commit 23c9d47

Browse files
committed
ADD item acceptance criteria for #48 #44
1 parent 34c2a05 commit 23c9d47

File tree

2 files changed

+114
-5
lines changed

2 files changed

+114
-5
lines changed

examples/todo-list/todo-app.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ var initial_model = {
1515
* `update` transforms the `model` based on the `action`.
1616
* @param {String} action - the desired action to perform on the model.
1717
* @param {Object} model - the App's data ("state").
18+
* @param {String} data - the data we want to "apply" to the item.
1819
* @return {Object} updated_model - the transformed model.
1920
*/
20-
function update(action, model) { // Update function takes the current state
21+
function update(action, model, data) {
2122
// var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
2223
switch(action) { // and an action (String) runs a switch
2324
// case 'CREATE':

todo-list.md

+112-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Elm(ish) Todo List (TodoMVC) App (_Real World TDD Tutorial!_)
22

3-
If you've made it this far, give yourself a pat on the back!
3+
If you've made it this far, give yourself a pat on the back! <br />
44
Your persistence is about to pay off as you
55
"_level up_" _both_ your **`JavaScript`** and "TEA" skills!
66

@@ -15,7 +15,7 @@ Documentation and Test Driven Development.
1515
This will _show_ you that it's not only _possible_
1616
to write docs and tests _first_,
1717
you will see _first hand_ that **`code`** is **more concise**,
18-
well-documented and thus **_easier_ to maintain**
18+
**well-documented** and thus **_easier_ to maintain**
1919
and you will get your "work" done ***much faster***.
2020

2121
These are _foundational_ skills that will
@@ -306,7 +306,7 @@ The **`JSDOC`** for our `update` function is:
306306
*/
307307
```
308308

309-
#### `update` Test
309+
#### `update` Test > `default case`
310310

311311
As with the `update` in our `counter` example
312312
the function body is a `switch` statement
@@ -335,7 +335,7 @@ node test/todo-app.test.js
335335
You should see the assertion _fail_:
336336
![update-default-branch-test-failing](https://user-images.githubusercontent.com/194400/43580847-b78105c0-964e-11e8-81ac-61a1dd8ec535.png)
337337

338-
#### `update` Function Implementation > `switch default case`
338+
#### `update` Function Implementation > `default case`
339339

340340
Write the _minimum_ code necessary to pass the test.
341341

@@ -371,6 +371,114 @@ node test/todo-app.test.js
371371
You should see this assertion pass:
372372
![update-default-branch-test-passing](https://user-images.githubusercontent.com/194400/43581137-c6aa236e-964f-11e8-96d0-ef724659761e.png)
373373

374+
Now that we have a _passing_ test
375+
for the `default case` in our `update` function,
376+
we can move on to
377+
thinking about the first (_and most fundamental_) piece
378+
of _functionality_ in the Todo List App: Adding an item to the list.
379+
380+
381+
### `ADD` an `item` to the Todo List
382+
383+
This is both the _first_ "feature" a "user" will encounter and
384+
_by_ far the most _used_ feature of a Todo List. <br />
385+
386+
#### `ADD` item _Acceptance Criteria_
387+
388+
Adding a new todo item's text should
389+
append the item `Object` to the `model.items` Array.
390+
Such that the `model` is transformed (_data is added_) in the following way:
391+
392+
_BEFORE_:
393+
```js
394+
{
395+
todos: [],
396+
hash: "#/"
397+
}
398+
```
399+
_AFTER_:
400+
```js
401+
{
402+
todos: [
403+
{id: 1, "Add Todo List Item", done: false }
404+
],
405+
hash: "#/"
406+
}
407+
```
408+
409+
410+
#### Hold On, How Does Todo Item _Text_ Get "Passed Into" `update`...?
411+
412+
While thinking of the "Acceptance Criteria"
413+
for adding an item to the Todo List,
414+
we _notice_ that our `update` **`JSDOC`**
415+
and corresponding function "signature" (_defined above_) as:
416+
```js
417+
/**
418+
* `update` transforms the `model` based on the `action`.
419+
* @param {String} action - the desired action to perform on the model.
420+
* @param {Object} model - the App's data ("state").
421+
* @return {Object} updated_model - the transformed model.
422+
*/
423+
function update(action, model) { // Update function takes the current state
424+
switch (action) { // and an action (String) runs a switch
425+
default: // if action unrecognised or undefined,
426+
return model; // return model unmodified
427+
} // default? https://softwareengineering.stackexchange.com/a/201786/211301
428+
}
429+
```
430+
does not have a **parameter** for passing in the Todo List item Text (`title`),
431+
i.e. how do we add "data" to the `model`...?
432+
433+
434+
That's "_Oh kay_"! (_don't panic_!) <br />
435+
If we **`try`** to think about implementation up-front,
436+
we would _invariably_ be "over-thinking"
437+
it's called "***Waterfall***"".
438+
439+
As you are _about_ to see, we can _easily_ change the function signature,
440+
in the _next_ test _without affecting_ our exiting (_passing_) test!
441+
442+
As you _practice_ "DDD" & "TDD" you will begin to _appreciate_
443+
and even _embrace_ the _mental agility_ that comes from
444+
_not_ "over-thinking" things.
445+
446+
Whenever you encounter a "New Requirement"
447+
(_or realise that you didn't **fully consider** the **original requirements**_),
448+
you know that your _suite_ of tests has
449+
["got your back"](https://www.urbandictionary.com/define.php?term=Got%20your%20back).
450+
451+
452+
453+
454+
#### `ADD` item _Test_
455+
456+
Append following test code to your `test/todo-app.test.js` file:
457+
458+
```js
459+
test('`ADD` a new todo item to model.todos Array via `update`', function (t) {
460+
const model = JSON.parse(JSON.stringify(app.model)); // initial state
461+
const updated_model = app.update('ADD', model, "Add Todo List Item");
462+
const expected = { id: 1, title: "Add Todo List Item", done: false }
463+
t.deepEqual(model, unmodified_model, "model returned unmodified");
464+
t.end();
465+
});
466+
```
467+
468+
469+
470+
471+
If you _run_ this test in your terminal:
472+
```sh
473+
node test/todo-app.test.js
474+
```
475+
You should see the assertion _fail_:
476+
477+
478+
#### `ADD` item _Implementation_
479+
480+
481+
374482
<!--
375483
376484
## What _Next_?

0 commit comments

Comments
 (0)