Skip to content

Commit a3cf67e

Browse files
committed
add test (failing) for "2. New Todo" #55
1 parent 3b8eb11 commit a3cf67e

File tree

1 file changed

+71
-12
lines changed

1 file changed

+71
-12
lines changed

todo-list.md

+71-12
Original file line numberDiff line numberDiff line change
@@ -1338,38 +1338,38 @@ https://github.com/tastejs/todomvc/tree/master/tests#example-output
13381338

13391339
```
13401340
TodoMVC
1341-
No Todos
1341+
1. No Todos
13421342
✓ should hide #main and #footer (201ms)
1343-
New Todo
1343+
2. New Todo
13441344
✓ should allow me to add todo items (548ms)
13451345
✓ should clear text input field when an item is added (306ms)
13461346
✓ should trim text input (569ms)
13471347
✓ should show #main and #footer when items added (405ms)
1348-
Mark all as completed
1348+
3. Mark all as completed
13491349
✓ should allow me to mark all items as completed (1040ms)
13501350
✓ should allow me to clear the completion state of all items (1014ms)
13511351
✓ complete all checkbox should update state when items are completed (1413ms)
1352-
Item
1352+
4. Item
13531353
✓ should allow me to mark items as complete (843ms)
13541354
✓ should allow me to un-mark items as complete (978ms)
13551355
✓ should allow me to edit an item (1155ms)
13561356
✓ should show the remove button on hover
1357-
Editing
1357+
5. Editing
13581358
✓ should hide other controls when editing (718ms)
13591359
✓ should save edits on enter (1093ms)
13601360
✓ should save edits on blur (1256ms)
13611361
✓ should trim entered text (1163ms)
13621362
✓ should remove the item if an empty text string was entered (1033ms)
13631363
✓ should cancel edits on escape (1115ms)
1364-
Counter
1364+
6. Counter
13651365
✓ should display the current number of todo items (462ms)
1366-
Clear completed button
1366+
7. Clear completed button
13671367
✓ should display the number of completed items (873ms)
13681368
✓ should remove completed items when clicked (898ms)
13691369
✓ should be hidden when there are no items that are completed (893ms)
1370-
Persistence
1370+
8. Persistence
13711371
✓ should persist its data (3832ms)
1372-
Routing
1372+
9. Routing
13731373
✓ should allow me to display active items (871ms)
13741374
✓ should allow me to display completed items (960ms)
13751375
✓ should allow me to display all items (1192ms)
@@ -1384,7 +1384,7 @@ We are going to write each one of these tests and then
13841384

13851385
Add the following test to your `test/todo-app.test.js` file:
13861386
```js
1387-
test.only('No Todos, should hide #footer and #main', function (t) {
1387+
test.only('1. No Todos, should hide #footer and #main', function (t) {
13881388
// render the view and append it to the DOM inside the `test-app` node:
13891389
document.getElementById(id).appendChild(app.view({todos: []})); // No Todos
13901390

@@ -1406,7 +1406,6 @@ node test/todo-app.js
14061406
You should see the following output:
14071407
![image](https://user-images.githubusercontent.com/194400/43868621-59e1aba0-9b66-11e8-95c1-0034892128cd.png)
14081408

1409-
14101409
##### Make it Pass!
14111410

14121411
Simply replace the instances of `"style=display: block;"` in the view code
@@ -1421,12 +1420,72 @@ var display = "style=display:"
14211420
You should see:
14221421
![no-todos-test-passing](https://user-images.githubusercontent.com/194400/43868724-e3e2249c-9b66-11e8-8228-a5c1528c17b0.png)
14231422

1423+
Testing it in your web browser you should see the desired result:
14241424

1425+
![no-todos-hide-main-and-footer](https://user-images.githubusercontent.com/194400/43869170-1982648e-9b69-11e8-8f7a-4730edbc07ca.png)
14251426

1427+
> If you get stuck trying to make the test pass, see:
1428+
[todo-app.js](https://github.com/dwyl/learn-elm-architecture-in-javascript/pull/45/commits/d1bd85e4d75afdc69fcf38b9d58947cdce18a9cf#diff-6be3e16fe7cfb4c00788d4d587374afdR85)
14261429

1427-
Recommended reading:
1430+
Recommended reading on CSS `visibility:hidden` vs. `display:none` :
14281431
https://stackoverflow.com/questions/133051/what-is-the-difference-between-visibilityhidden-and-displaynone
14291432

1433+
<br />
1434+
1435+
#### 2. New Todo, should allow me to add todo items
1436+
1437+
The second batch of tests involves adding a new todo item to the list:
1438+
1439+
```
1440+
2. New Todo
1441+
✓ should allow me to add todo items (548ms)
1442+
✓ should clear text input field when an item is added (306ms)
1443+
✓ should trim text input (569ms)
1444+
✓ should show #main and #footer when items added (405ms)
1445+
```
1446+
Let's create a test with these 4 assertions.
1447+
1448+
Add the following code/test to your `test/todo-app.test.js` file:
1449+
```js
1450+
test.only('2. New Todo, should allow me to add todo items', function (t) {
1451+
// render the view and append it to the DOM inside the `test-app` node:
1452+
elmish.mount({todos: []}, app.update, app.view, id, app.subscriptions);
1453+
const new_todo = document.getElementById('new-todo');
1454+
// "type" content in the <input id="new-todo">:
1455+
const todo_text = 'Make Everything Awesome! '; // deliberate whitespace!
1456+
new_todo.value = todo_text;
1457+
// trigger the [Enter] keyboard key to ADD the new todo:
1458+
new_todo.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 13}));
1459+
const items = document.querySelectorAll('.view')
1460+
t.equal(items.length, 1, "✓ should allow me to add todo items");
1461+
// check if the new todo was added to the DOM:
1462+
const actual = document.getElementById('1').textContent;
1463+
t.equal(todo_text.trim(), actual, "✓ should trim text input")
1464+
1465+
// check that the <input id="new-todo"> was reset after the new item was added
1466+
t.equal(new_todo.value, '',
1467+
"✓ should clear text input field when an item is added")
1468+
1469+
const main_display = window.getComputedStyle(document.getElementById('main'));
1470+
t.equal('block', main_display._values.display,
1471+
"✓ should show #main and #footer when items added");
1472+
const main_footer= window.getComputedStyle(document.getElementById('footer'));
1473+
t.equal('block', main_footer._values.display, "show #footer");
1474+
1475+
elmish.empty(document.getElementById(id)); // clear DOM ready for next test
1476+
t.end();
1477+
});
1478+
```
1479+
1480+
Run the test with:
1481+
```sh
1482+
node test/todo-app.js
1483+
```
1484+
You should see the following output:
1485+
1486+
![test-failing](https://user-images.githubusercontent.com/194400/43929259-1880b41e-9c2c-11e8-9615-1372928c905d.png)
1487+
1488+
14301489

14311490

14321491
<!--

0 commit comments

Comments
 (0)