Skip to content

Commit 171f34a

Browse files
committed
add screenshot and text reminding readers that we have 100% test coverage at the end of render_main #45 (comment) for #51
1 parent 82bd69d commit 171f34a

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function update(action, model, data) {
5454
* // returns <li> DOM element with <div>, <input>. <label> & <button> nested
5555
* var DOM = render_item({id: 1, title: "Build Todo List App", done: false});
5656
*/
57-
function render_item(item) {
57+
function render_item (item) {
5858
return (
5959
li([
6060
"data-id=" + item.id,
@@ -72,7 +72,13 @@ function render_item(item) {
7272
)
7373
}
7474

75-
function render_main(model) {
75+
/**
76+
* `render_main` renders the `<section class="main">` of the Todo List App
77+
* which contains all the "main" controls and the `<ul>` with the todo items.
78+
* @param {Object} model - the App's (current) model (or "state").
79+
* @return {Object} <section> DOM Tree which containing the todo list <ul>, etc.
80+
*/
81+
function render_main (model) {
7682
return (
7783
section(["class=main", "style=display: block;"], [
7884
input(["id=toggle-all", "class=toggle-all", "type=checkbox"], []),

Diff for: todo-list.md

+31-6
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,12 @@ you will see something like this:
829829
Given the test above, I added the following code to my `todo-app.js` file:
830830

831831
```js
832+
/* if require is available, it means we are in Node.js Land i.e. testing! */
833+
/* istanbul ignore next */
834+
const { a, button, div, empty, footer, input, h1, header, label, li, mount,
835+
route, section, span, strong, text, ul } =
836+
(typeof require !== 'undefined') ? require('./elmish.js') : {};
837+
832838
/**
833839
* `render_item` creates an DOM "tree" with a single Todo List Item
834840
* using the "elmish" DOM functions (`li`, `div`, `input`, `label` and `button`)
@@ -880,6 +886,8 @@ that renders a _single_ `<li>` (_todo list item_),
880886
we can create another function which _uses_ the `render_item` in a "loop",
881887
to create _several_ `<li>` nested in a `<ul>`.
882888

889+
#### `render_main` Test
890+
883891
Append following test code to your `test/todo-app.test.js` file:
884892

885893
```js
@@ -894,14 +902,16 @@ test('render "main" view using (elmish) HTML DOM functions', function (t) {
894902
};
895903
// render the "main" view and append it to the DOM inside the `test-app` node:
896904
document.getElementById(id).appendChild(app.render_main(model));
905+
// test that the title text in the model.todos was rendered to <label> nodes:
906+
document.querySelectorAll('.view').forEach(function (item, index) {
907+
t.equal(item.textContent, model.todos[index].title,
908+
"index #" + index + " <label> text: " + item.textContent)
909+
})
897910

898-
const done = document.querySelectorAll('.completed')[0].textContent;
899-
t.equal(done, 'Learn Elm Architecture', 'Done: Learn "TEA"');
900-
const todo = document.querySelectorAll('.view')[1].textContent;
901-
t.equal(todo, 'Build Todo List App', 'Todo: Build Todo List App');
902-
const todos = document.querySelectorAll('.toggle');
911+
const inputs = document.querySelectorAll('input'); // todo items are 1,2,3
903912
[true, false, false].forEach(function(state, index){
904-
t.equal(todos.checked, state, "Todo #" + index + " is done=" + state)
913+
t.equal(inputs[index + 1].checked, state,
914+
"Todo #" + index + " is done=" + state)
905915
})
906916
elmish.empty(document.getElementById(id)); // clear DOM ready for next test
907917
t.end();
@@ -917,6 +927,21 @@ you will see something like this:
917927
![main-test-failing](https://user-images.githubusercontent.com/194400/43741630-f03f1fe8-99c6-11e8-8b7b-e44ee397b38e.png)
918928

919929

930+
Given your knowledge of implementing the `render_item` function above,
931+
and your skills with JavaScript loops, create your `render_main` function,
932+
to make the tests pass.
933+
934+
> If you get "stuck" there is a _reference_ implementation in:
935+
[**`todo-app.js`**](https://github.com/dwyl/learn-elm-architecture-in-javascript/pull/45/commits/b6607478c3dbed048781932261af2981f4c6c405#diff-6be3e16fe7cfb4c00788d4d587374afdR76)
936+
937+
All our tests pass _and_ we have **100% test coverage**:
938+
939+
![render_main-tests-pass-100-coverage](https://user-images.githubusercontent.com/194400/43766409-4189ce4e-9a2a-11e8-8d73-3ea636b22928.png)
940+
941+
This means we are writing the "_bare minimum_" code necessary
942+
to meet all acceptance criteria (requirements),
943+
which is both faster and more maintainable! <br />
944+
Onwards!
920945

921946

922947

0 commit comments

Comments
 (0)