@@ -587,18 +587,94 @@ test.only('5.3 [ENTER] Key in edit mode triggers SAVE action', function (t) {
587
587
// render the view and append it to the DOM inside the `test-app` node:
588
588
elmish .mount (model, app .update , app .view , id, app .subscriptions );
589
589
// change the
590
- const updated_title = " Do things that don\' t scale!"
590
+ const updated_title = " Do things that don\' t scale! "
591
591
// apply the updated_title to the <input class="edit">:
592
592
document .querySelectorAll (' .edit' )[0 ].value = updated_title;
593
593
// trigger the [Enter] keyboard key to ADD the new todo:
594
594
document .dispatchEvent (new KeyboardEvent (' keyup' , {' keyCode' : 13 }));
595
- // confirm that the todo item title was updated to the updated_title
595
+ // confirm that the todo item title was updated to the updated_title:
596
596
const label = document .querySelectorAll (' .view > label' )[1 ].textContent ;
597
- t .equal (label, updated_title, " item title updated to:" + updated_title)
597
+ t .equal (label, updated_title .trim (),
598
+ " item title updated to:" + updated_title + ' (trimmed)' );
598
599
t .end ();
599
600
});
600
601
```
601
602
If you attempt to run this test: ` node test/todo-app.test.js `
602
603
you will see output similar to the following:
603
604
604
- ![ edit-double-click-test-failing] ( https://user-images.githubusercontent.com/194400/45183202-54b7e880-b21b-11e8-84d8-7b3b50162113.png )
605
+ ![ save-edit-test-fails] ( https://user-images.githubusercontent.com/194400/45187886-2c83b600-b22a-11e8-8782-6d3fcaa240df.png )
606
+
607
+
608
+ ### 5.3 ` 'SAVE' update case ` _ Implementation_
609
+
610
+ The _ first_ step in the implementation is to create the ` 'SAVE' ` case
611
+ in ` update ` function:
612
+
613
+ ``` js
614
+ case ' SAVE' :
615
+ var edit = document .getElementsByClassName (' edit' )[0 ];
616
+ var value = edit .value ;
617
+ var id = parseInt (edit .id , 10 );
618
+ // End Editing
619
+ new_model .clicked = false ;
620
+ new_model .editing = false ;
621
+
622
+ if (! value || value .length === 0 ) { // delete item if title is blank:
623
+ return update (' DELETE' , new_model, id);
624
+ }
625
+ // update the value of the item.title that has been edited:
626
+ new_model .todos = new_model .todos .map (function (item ) {
627
+ if (item .id === id && value && value .length > 0 ) {
628
+ item .title = value .trim ();
629
+ }
630
+ return item; // return all todo items.
631
+ });
632
+ break ;
633
+ ```
634
+
635
+ The _ second_ step is _ triggering_ this ` case ` in the ` subscriptions `
636
+ event listener for ` keyup ` :
637
+
638
+ _ Before_ :
639
+
640
+ ``` js
641
+ document .addEventListener (' keyup' , function handler (e ) {
642
+ switch (e .keyCode ) {
643
+ case ENTER_KEY :
644
+ var new_todo = document .getElementById (' new-todo' );
645
+ if (new_todo .value .length > 0 ) {
646
+ signal (' ADD' )(); // invoke singal inner callback
647
+ new_todo .value = ' ' ; // reset <input> so we can add another todo
648
+ document .getElementById (' new-todo' ).focus ();
649
+ }
650
+ break ;
651
+ }
652
+ });
653
+ ```
654
+
655
+
656
+ _ After_ :
657
+
658
+ ``` js
659
+ document .addEventListener (' keyup' , function handler (e ) {
660
+ switch (e .keyCode ) {
661
+ case ENTER_KEY :
662
+ var editing = document .getElementsByClassName (' editing' );
663
+ if (editing && editing .length > 0 ) {
664
+ signal (' SAVE' )(); // invoke singal inner callback
665
+ }
666
+
667
+ var new_todo = document .getElementById (' new-todo' );
668
+ if (new_todo .value .length > 0 ) {
669
+ signal (' ADD' )(); // invoke singal inner callback
670
+ new_todo .value = ' ' ; // reset <input> so we can add another todo
671
+ document .getElementById (' new-todo' ).focus ();
672
+ }
673
+ break ;
674
+ }
675
+ });
676
+ ```
677
+
678
+ When you run the tests: ` node test/todo-app.test.js `
679
+ they should now _ pass_ :
680
+ ![ save-update-test-pass] ( https://user-images.githubusercontent.com/194400/45188350-d879d100-b22b-11e8-8669-94e080a25ef7.png )
0 commit comments