From fd60ff65314982bb033eb52c316f320b2aae5b24 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Thu, 6 Sep 2018 16:25:10 +0100 Subject: [PATCH] add screenshot of render_item ("edit mode") tests passing for #60 --- edit-todo.md | 363 +++++++++------------------------ examples/todo-list/todo-app.js | 4 +- todo-list.md | 114 +---------- 3 files changed, 105 insertions(+), 376 deletions(-) diff --git a/edit-todo.md b/edit-todo.md index ca872d4b..8f488f4b 100644 --- a/edit-todo.md +++ b/edit-todo.md @@ -1,267 +1,3 @@ - - -#### 3. Mark all as completed - -The third batch of tests involves "Toggling" all todos as "done=true": - -``` -3. Mark all as completed - ✓ should allow me to mark all items as completed - ✓ should allow me to clear the completion state of all items - ✓ complete all checkbox should update state when items are completed -``` -Luckily, given that we know how to use a _boolean_ value, -these _three_ assertions can be "solved" with _minimal_ code. -Let's create a test with these 3 assertions. - -Add the following code/test to your `test/todo-app.test.js` file: -```js -test.only('3. Mark all as completed ("TOGGLE_ALL")', function (t) { - elmish.empty(document.getElementById(id)); - localStorage.removeItem('elmish_' + id); - const model = { - todos: [ - { id: 0, title: "Learn Elm Architecture", done: true }, - { id: 1, title: "Build Todo List App", done: false }, - { id: 2, title: "Win the Internet!", done: false } - ], - hash: '#/' // the "route" to display - }; - // render the view and append it to the DOM inside the `test-app` node: - elmish.mount(model, app.update, app.view, id, app.subscriptions); - // confirm that the ONLY the first todo item is done=true: - const items = document.querySelectorAll('.view'); - - document.querySelectorAll('.toggle').forEach(function(item, index) { - t.equal(item.checked, model.todos[index].done, - "Todo #" + index + " is done=" + item.checked - + " text: " + items[index].textContent) - }) - - // click the toggle-all checkbox to trigger TOGGLE_ALL: >> true - document.getElementById('toggle-all').click(); // click toggle-all checkbox - document.querySelectorAll('.toggle').forEach(function(item, index) { - t.equal(item.checked, true, - "TOGGLE each Todo #" + index + " is done=" + item.checked - + " text: " + items[index].textContent) - }); - t.equal(document.getElementById('toggle-all').checked, true, - "should allow me to mark all items as completed") - - - // click the toggle-all checkbox to TOGGLE_ALL (again!) true >> false - document.getElementById('toggle-all').click(); // click toggle-all checkbox - document.querySelectorAll('.toggle').forEach(function(item, index) { - t.equal(item.checked, false, - "TOGGLE_ALL Todo #" + index + " is done=" + item.checked - + " text: " + items[index].textContent) - }) - t.equal(document.getElementById('toggle-all').checked, false, - "should allow me to clear the completion state of all items") - - // *manually* "click" each todo item: - document.querySelectorAll('.toggle').forEach(function(item, index) { - item.click(); // this should "toggle" the todo checkbox to done=true - t.equal(item.checked, true, - ".toggle.click() (each) Todo #" + index + " which is done=" + item.checked - + " text: " + items[index].textContent) - }); - // the toggle-all checkbox should be "checked" as all todos are done=true! - t.equal(document.getElementById('toggle-all').checked, true, - "complete all checkbox should update state when items are completed") - - elmish.empty(document.getElementById(id)); // clear DOM ready for next test - localStorage.removeItem('elmish_store'); - t.end(); -}); -``` - -If you attempt to run the test file: -```sh -node test/todo-app.test.js -``` -You will see something like this: - -![toggle-all-test-failing](https://user-images.githubusercontent.com/194400/43985804-3c8dbe02-9d02-11e8-9876-cd7e35602754.png) - -While there may _appear_ to be "_many_" assertions in this test, -in reality there are only two bits of functionality. - -_Firstly_, we need a new `case` -in the `update` `switch` statement: `TOGGLE_ALL`.
-and _second_ we need to add a couple of lines to our `TOGGLE` -block to _check_ if _all_ todos are `done=true` or `done=false`. -In the case where _all_ todos are `done=true` we should reflect -this in the "state" of the `toggle-all` checkbox. -The _easiest_ way of representing this in the `model` is -with a new property, e.g: `model.all_done=true` -when _all_ todos are `done=true`. - -The only other thing we need to update is the `render_main` -function to include `signal('TOGGLE_ALL')` in the attributes array. - -Try and make this test pass by yourself before consulting the -sample code: -[**`examples/todo-list/todo-app.js`**](https://github.com/dwyl/learn-elm-architecture-in-javascript/pull/45/files#diff-6be3e16fe7cfb4c00788d4d587374afdR46) - - -### 4. Item (Toggle, Edit & Delete) - -``` -4. Item - ✓ should allow me to mark items as complete (843ms) - ✓ should allow me to un-mark items as complete (978ms) - ✓ should allow me to edit an item (1155ms) - ✓ should show the remove button on hover -``` - -Of these requirements, we already have the first two "_covered_" -because we implemented the `TOGGLE` feature (_above_). - -We can add another "proxy" test just for "_completeness_": - -```js -test.only('4. Item: should allow me to mark items as complete', function (t) { - elmish.empty(document.getElementById(id)); - localStorage.removeItem('elmish_' + id); - const model = { - todos: [ - { id: 0, title: "Make something people want.", done: false } - ], - hash: '#/' // the "route" to display - }; - // render the view and append it to the DOM inside the `test-app` node: - elmish.mount(model, app.update, app.view, id, app.subscriptions); - const item = document.getElementById('0') - t.equal(item.textContent, model.todos[0].title, 'Item contained in model.'); - // confirm that the todo item is NOT done (done=false): - t.equal(document.querySelectorAll('.toggle')[0].checked, false, - 'Item starts out "active" (done=false)'); - - - // click the checkbox to toggle it to done=true - document.querySelectorAll('.toggle')[0].click() - t.equal(document.querySelectorAll('.toggle')[0].checked, true, - 'Item should allow me to mark items as complete'); - - // click the checkbox to toggle it to done=false "undo" - document.querySelectorAll('.toggle')[0].click() - t.equal(document.querySelectorAll('.toggle')[0].checked, false, - 'Item should allow me to un-mark items as complete'); - t.end(); -}); -``` -You should not need to write any additional code -in order to make this test pass; just run it and move on. - -![toggle-todo-tests-passing](https://user-images.githubusercontent.com/194400/43992979-a4d00ab6-9d7e-11e8-891b-9f699f474dd5.png) - - - -#### 4.1 `DELETE` an Item - -``` -should show the remove button on hover -``` - -##### Acceptance Criteria - -+ [ ] should show the ` - - -
  • -
    - - -
    - -
  • - -``` - - -```js - -``` - -There are _two_ steps to Editing a Todo List item: - -+ [ ] Receiving the `singal('EDIT', item.id)` "activates" editing mode. - - - - -BEFORE: -```js -function render_item (item, signal) { - return ( - li([ - "data-id=" + item.id, - "id=" + item.id, - item.done ? "class=completed" : "" - ], [ - div(["class=view"], [ - input([ - item.done ? "checked=true" : "", - "class=toggle", - "type=checkbox", - typeof signal === 'function' ? signal('TOGGLE', item.id) : '' - ], - []), // does not have any nested elements - label([], [text(item.title)]), - button(["class=destroy"]) - ]) // - ]) // - ) -} -``` -