@@ -1038,3 +1038,86 @@ The following assertions:
1038
1038
✓ should allow me to display all items
1039
1039
✓ should highlight the currently applied filter
1040
1040
```
1041
+
1042
+ + ` 'SHOW_ALL' ` the default view.
1043
+ + ` 'SHOW_ACTIVE' ` item.done === false
1044
+ + ` 'SHOW_COMPLETED' ` item.done === true
1045
+
1046
+
1047
+ #### 9. Routing _ Test_
1048
+
1049
+ Append following test code to your ` test/todo-app.test.js ` file:
1050
+
1051
+ ``` js
1052
+ test .only (' 9. Routing > should allow me to display active/completed/all items' ,
1053
+ function (t ) {
1054
+ elmish .empty (document .getElementById (id));
1055
+ const model = {
1056
+ todos: [
1057
+ { id: 0 , title: " Make something people want." , done: false },
1058
+ { id: 1 , title: " Bootstrap for as long as you can" , done: true },
1059
+ { id: 2 , title: " Let's solve our own problem" , done: true }
1060
+ ],
1061
+ hash: ' #/active' // ONLY ACTIVE items
1062
+ };
1063
+ // render the view and append it to the DOM inside the `test-app` node:
1064
+ elmish .mount (model, app .update , app .view , id, app .subscriptions );
1065
+ t .equal (document .querySelectorAll (' .view' ).length , 1 , " one active item" );
1066
+ let selected = document .querySelectorAll (' .selected' )[0 ]
1067
+ t .equal (selected .id , ' active' , " active footer filter is selected" );
1068
+
1069
+ // empty:
1070
+ elmish .empty (document .getElementById (id));
1071
+ localStorage .removeItem (' todos-elmish_' + id);
1072
+ // show COMPLTED items:
1073
+ model .hash = ' #/completed' ;
1074
+ elmish .mount (model, app .update , app .view , id, app .subscriptions );
1075
+ t .equal (document .querySelectorAll (' .view' ).length , 2 ,
1076
+ " two completed items" );
1077
+ selected = document .querySelectorAll (' .selected' )[0 ]
1078
+ t .equal (selected .id , ' completed' , " completed footer filter is selected" );
1079
+
1080
+ // empty:
1081
+ elmish .empty (document .getElementById (id));
1082
+ localStorage .removeItem (' todos-elmish_' + id);
1083
+ // show ALL items:
1084
+ model .hash = ' #/' ;
1085
+ elmish .mount (model, app .update , app .view , id, app .subscriptions );
1086
+ t .equal (document .querySelectorAll (' .view' ).length , 3 ,
1087
+ " three items total" );
1088
+ selected = document .querySelectorAll (' .selected' )[0 ]
1089
+ t .equal (selected .id , ' all' , " all footer filter is selected" );
1090
+
1091
+ elmish .empty (document .getElementById (id)); // clear DOM ready for next test
1092
+ localStorage .removeItem (' todos-elmish_' + id);
1093
+ t .end ();
1094
+ });
1095
+ ```
1096
+
1097
+
1098
+ #### 9. Routing _ Implementation_
1099
+
1100
+ Since this is the _ final_ quest in the TodoMVC/Todo List App,
1101
+ the solution is _ not_ included here.
1102
+ Spend some time trying to make the test assertions pass.
1103
+
1104
+ If you get "_ stuck_ " consult the code in ` todo-app.js ` .
1105
+
1106
+ #### 9.1 Routing _ Bonus_
1107
+
1108
+ As a _ bonus_ level,
1109
+ you can add the following event listener to your ` subscriptions `
1110
+ to make the router work when the url (hash) changes in the browser:
1111
+
1112
+ ``` js
1113
+ window .onhashchange = function route () {
1114
+ signal (' ROUTE' )();
1115
+ }
1116
+ ```
1117
+ And the ` 'ROUTE' ` ` case ` to your ` update ` function:
1118
+ ``` js
1119
+ case ' ROUTE' :
1120
+ new_model .hash = (window && window .location && window .location .hash ) ?
1121
+ window .location .hash : ' #/' ;
1122
+ break ;
1123
+ ```
0 commit comments