Skip to content

Commit b277b6c

Browse files
committed
add test (passing) for 4. Item (toggle) done #44
1 parent b8bb500 commit b277b6c

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

Diff for: examples/todo-list/todo-app.js

-5
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@ var initial_model = {
1919
*/
2020
function update(action, model, data) {
2121
var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
22-
// console.log('> > > > > > > > > > > model', model);
2322
switch(action) { // and an action (String) runs a switch
2423
case 'ADD':
2524
// can you see an "issue" with this way of generating the todo id? Bug...?
2625
var id = (typeof model.todos !== 'undefined' && model.todos.length > 0) ?
2726
(model.todos.length + 1) : 1;
2827
var input = document.getElementById('new-todo');
29-
// console.log('new_model.todos', new_model.todos);
3028
new_model.todos = (new_model.todos && new_model.todos.length > 0)
3129
? new_model.todos : [];
32-
// console.log('new_model.todos', new_model.todos);
3330
new_model.todos.push({
3431
id: id,
3532
title: data || input.value.trim(),
@@ -44,10 +41,8 @@ function update(action, model, data) {
4441
});
4542
// if all todos are done=true then "check" the "toggle-all" checkbox:
4643
var all_done = new_model.todos.filter(function(item) {
47-
// console.log('> > > > > >item.done:', item.done);
4844
return item.done === false; // only care about items that are NOT done
4945
}).length;
50-
// console.log(' >>>> all_done', all_done);
5146
new_model.all_done = all_done === 0 ? true : false;
5247
break;
5348
case 'TOGGLE_ALL':

Diff for: test/todo-app.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,33 @@ test('3. Mark all as completed ("TOGGLE_ALL")', function (t) {
306306
localStorage.removeItem('elmish_store');
307307
t.end();
308308
});
309+
310+
test.only('4. Item: should allow me to mark items as complete', function (t) {
311+
elmish.empty(document.getElementById(id));
312+
localStorage.removeItem('elmish_' + id);
313+
const model = {
314+
todos: [
315+
{ id: 0, title: "Make something people want.", done: false }
316+
],
317+
hash: '#/' // the "route" to display
318+
};
319+
// render the view and append it to the DOM inside the `test-app` node:
320+
elmish.mount(model, app.update, app.view, id, app.subscriptions);
321+
const item = document.getElementById('0')
322+
t.equal(item.textContent, model.todos[0].title, 'Item contained in model.');
323+
// confirm that the todo item is NOT done (done=false):
324+
t.equal(document.querySelectorAll('.toggle')[0].checked, false,
325+
'Item starts out "active" (done=false)');
326+
327+
328+
// click the checkbox to toggle it to done=true
329+
document.querySelectorAll('.toggle')[0].click()
330+
t.equal(document.querySelectorAll('.toggle')[0].checked, true,
331+
'Item should allow me to mark items as complete');
332+
333+
// click the checkbox to toggle it to done=false "undo"
334+
document.querySelectorAll('.toggle')[0].click()
335+
t.equal(document.querySelectorAll('.toggle')[0].checked, false,
336+
'Item should allow me to un-mark items as complete');
337+
t.end();
338+
});

Diff for: todo-list.md

+68
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@ We are going to write each one of these tests and then
13821382

13831383
#### 1. No Todos, should hide #footer and #main
13841384

1385+
13851386
Add the following test to your `test/todo-app.test.js` file:
13861387
```js
13871388
test.only('1. No Todos, should hide #footer and #main', function (t) {
@@ -1638,6 +1639,73 @@ Try and make this test pass by yourself before consulting the
16381639
sample code:
16391640
[**`examples/todo-list/todo-app.js`**](https://github.com/dwyl/learn-elm-architecture-in-javascript/pull/45/files#diff-6be3e16fe7cfb4c00788d4d587374afdR46)
16401641

1642+
1643+
### 4. Item
1644+
1645+
```
1646+
4. Item
1647+
✓ should allow me to mark items as complete (843ms)
1648+
✓ should allow me to un-mark items as complete (978ms)
1649+
✓ should allow me to edit an item (1155ms)
1650+
✓ should show the remove button on hover
1651+
```
1652+
1653+
Of these requirements, we already have the first two "_covered_"
1654+
because we implemented the `TOGGLE` feature (_above_).
1655+
1656+
We can add another "proxy" test just for "_completeness_":
1657+
1658+
```js
1659+
test.only('4. Item: should allow me to mark items as complete', function (t) {
1660+
elmish.empty(document.getElementById(id));
1661+
localStorage.removeItem('elmish_' + id);
1662+
const model = {
1663+
todos: [
1664+
{ id: 0, title: "Make something people want.", done: false }
1665+
],
1666+
hash: '#/' // the "route" to display
1667+
};
1668+
// render the view and append it to the DOM inside the `test-app` node:
1669+
elmish.mount(model, app.update, app.view, id, app.subscriptions);
1670+
const item = document.getElementById('0')
1671+
t.equal(item.textContent, model.todos[0].title, 'Item contained in model.');
1672+
// confirm that the todo item is NOT done (done=false):
1673+
t.equal(document.querySelectorAll('.toggle')[0].checked, false,
1674+
'Item starts out "active" (done=false)');
1675+
1676+
1677+
// click the checkbox to toggle it to done=true
1678+
document.querySelectorAll('.toggle')[0].click()
1679+
t.equal(document.querySelectorAll('.toggle')[0].checked, true,
1680+
'Item should allow me to mark items as complete');
1681+
1682+
// click the checkbox to toggle it to done=false "undo"
1683+
document.querySelectorAll('.toggle')[0].click()
1684+
t.equal(document.querySelectorAll('.toggle')[0].checked, false,
1685+
'Item should allow me to un-mark items as complete');
1686+
t.end();
1687+
});
1688+
```
1689+
You should not need to write any additional code
1690+
in order to make this test pass; just run it and move on.
1691+
1692+
![toggle-todo-tests-passing](https://user-images.githubusercontent.com/194400/43992979-a4d00ab6-9d7e-11e8-891b-9f699f474dd5.png)
1693+
1694+
1695+
1696+
#### 4.2 `EDIT` an Item
1697+
1698+
```
1699+
should allow me to edit an item
1700+
```
1701+
1702+
#### 4.1 `DELETE` an Item
1703+
1704+
```
1705+
should show the remove button on hover
1706+
```
1707+
1708+
16411709
<!--
16421710
16431711
## What _Next_?

0 commit comments

Comments
 (0)