1
1
# Elm(ish) Todo List (TodoMVC) App (_ Real World TDD Tutorial!_ )
2
2
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 />
4
4
Your persistence is about to pay off as you
5
5
"_ level up_ " _ both_ your ** ` JavaScript ` ** and "TEA" skills!
6
6
@@ -15,7 +15,7 @@ Documentation and Test Driven Development.
15
15
This will _ show_ you that it's not only _ possible_
16
16
to write docs and tests _ first_ ,
17
17
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**
19
19
and you will get your "work" done *** much faster*** .
20
20
21
21
These are _ foundational_ skills that will
@@ -306,7 +306,7 @@ The **`JSDOC`** for our `update` function is:
306
306
*/
307
307
```
308
308
309
- #### ` update ` Test
309
+ #### ` update ` Test > ` default case `
310
310
311
311
As with the ` update ` in our ` counter ` example
312
312
the function body is a ` switch ` statement
@@ -335,7 +335,7 @@ node test/todo-app.test.js
335
335
You should see the assertion _ fail_ :
336
336
![ update-default-branch-test-failing] ( https://user-images.githubusercontent.com/194400/43580847-b78105c0-964e-11e8-81ac-61a1dd8ec535.png )
337
337
338
- #### ` update ` Function Implementation > ` switch default case`
338
+ #### ` update ` Function Implementation > ` default case `
339
339
340
340
Write the _ minimum_ code necessary to pass the test.
341
341
@@ -371,6 +371,114 @@ node test/todo-app.test.js
371
371
You should see this assertion pass:
372
372
![ update-default-branch-test-passing] ( https://user-images.githubusercontent.com/194400/43581137-c6aa236e-964f-11e8-96d0-ef724659761e.png )
373
373
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
+
374
482
<!--
375
483
376
484
## What _Next_?
0 commit comments