Skip to content

Commit 812248b

Browse files
committed
first draft of TodoMVC tutorial *complete*!! 🎉
1 parent f118ec4 commit 812248b

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

edit-todo.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,3 +1038,86 @@ The following assertions:
10381038
✓ should allow me to display all items
10391039
✓ should highlight the currently applied filter
10401040
```
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+
```

test/todo-app.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ test('2. New Todo, should allow me to add todo items', function (t) {
279279
t.end();
280280
});
281281

282-
283282
test('3. Mark all as completed ("TOGGLE_ALL")', function (t) {
284283
elmish.empty(document.getElementById(id));
285284
localStorage.removeItem('todos-elmish_' + id);
@@ -395,7 +394,6 @@ test('4.1 DELETE item by clicking <button class="destroy">', function (t) {
395394
t.end();
396395
});
397396

398-
399397
test('5.1 Editing: > Render an item in "editing mode"', function (t) {
400398
elmish.empty(document.getElementById(id));
401399
localStorage.removeItem('todos-elmish_' + id);
@@ -633,8 +631,9 @@ test('8. Persistence > should persist its data', function (t) {
633631
t.end();
634632
});
635633

636-
test.only('9. Routing > should allow me to display active/completed/all items',
634+
test('9. Routing > should allow me to display active/completed/all items',
637635
function (t) {
636+
localStorage.removeItem('todos-elmish_' + id);
638637
elmish.empty(document.getElementById(id));
639638
const model = {
640639
todos: [
@@ -646,6 +645,9 @@ test.only('9. Routing > should allow me to display active/completed/all items',
646645
};
647646
// render the view and append it to the DOM inside the `test-app` node:
648647
elmish.mount(model, app.update, app.view, id, app.subscriptions);
648+
const mod = app.update('ROUTE', model);
649+
// t.equal(mod.hash, '#/', 'default route is #/');
650+
649651
t.equal(document.querySelectorAll('.view').length, 1, "one active item");
650652
let selected = document.querySelectorAll('.selected')[0]
651653
t.equal(selected.id, 'active', "active footer filter is selected");

0 commit comments

Comments
 (0)