Skip to content

Commit 3096d81

Browse files
committed
add view function implementation to pass tests for #54
1 parent aae0490 commit 3096d81

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

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

+33-15
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,46 @@ function render_footer (model) {
132132
)
133133
}
134134

135-
136-
// function view(signal, model, root) {
137-
// empty(root); // clear root element before re-rendering the App (DOM).
138-
// model.counters.map(function(counter, index) {
139-
// return container(index, [ // wrap DOM nodes in an "container"
140-
// button('+', signal, Inc + '-' + index), // append index to action
141-
// div('count', counter), // create div w/ count as text
142-
// button('-', signal, Dec + '-' + index), // decrement counter
143-
// button('Reset', signal, Res + '-' + index) // reset counter
144-
// ]);
145-
// }).forEach(function (el) { root.appendChild(el) }); // forEach is ES5 so IE9+
146-
// }
135+
/**
136+
* `view` renders the entire Todo List App
137+
* which contains count of items to (still) to be done and a `<ul>` "menu"
138+
* with links to filter which todo items appear in the list view.
139+
* @param {Object} model - the App's (current) model (or "state").
140+
* @return {Object} <section> DOM Tree which containing all other DOM elements.
141+
* @example
142+
* // returns <section class="todo-app"> DOM element with other DOM els nested:
143+
* var DOM = view(model);
144+
*/
145+
function view(model) {
146+
return (
147+
section(["class=todoapp"], [ // array of "child" elements
148+
header(["class=header"], [
149+
h1([], [
150+
text("todos")
151+
]), // </h1>
152+
input([
153+
"id=new-todo",
154+
"class=new-todo",
155+
"placeholder=What needs to be done?",
156+
"autofocus"
157+
], []) // <input> is "self-closing"
158+
]), // </header>
159+
render_main(model),
160+
render_footer(model)
161+
]) // <section>
162+
);
163+
}
147164

148165
/* module.exports is needed to run the functions using Node.js for testing! */
149166
/* istanbul ignore next */
150167
if (typeof module !== 'undefined' && module.exports) {
151168
module.exports = {
152169
model: initial_model,
153170
update: update,
154-
render_item: render_item, // export so that we can unit test
155-
render_main: render_main, // export for unit testing
156-
render_footer: render_footer // export for unit testing
171+
render_item: render_item, // export so that we can unit test
172+
render_main: render_main, // export for unit testing
173+
render_footer: render_footer, // export for unit testing
174+
view: view
157175
}
158176
}
159177

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,16 @@ test('render_footer 1 item left (pluarisation test)', function (t) {
162162
t.end();
163163
});
164164

165-
test.only('view renders the whole todo app using "partials"', function (t) {
165+
test('view renders the whole todo app using "partials"', function (t) {
166166
// render the view and append it to the DOM inside the `test-app` node:
167167
document.getElementById(id).appendChild(app.view(app.model)); // initial_model
168168

169-
t.equal(document.querySelectorAll('h1').textContent, "todos", "<h1>todos");
169+
t.equal(document.querySelectorAll('h1')[0].textContent, "todos", "<h1>todos");
170170
// placeholder:
171171
const placeholder = document.getElementById('new-todo')
172172
.getAttribute("placeholder");
173173
t.equal(placeholder, "What needs to be done?", "paceholder set on <input>");
174174

175-
const empty = document.querySelectorAll('.todo-list')
176-
console.log('empty', empty);
177-
178175
// todo-count should display 0 items left (based on initial_model):
179176
const left = document.getElementById('count').innerHTML;
180177
t.equal(left, "<strong>0</strong> items left", "Todos remaining: " + left);

Diff for: todo-list.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ Here is a sample JSDOC comment you can add to your **`todo-app.js`** file:
12181218
These should be pretty familiar to you by now.
12191219
If you feel comfortable extending it with more detail, go for it!
12201220

1221-
#### `view` Tests
1221+
#### `view` _Tests_
12221222

12231223
A sample test for the `view` function
12241224
you can add to your `test/todo-app.test.js` file:
@@ -1230,15 +1230,12 @@ test.only('view renders the whole todo app using "partials"', function (t) {
12301230
// render the view and append it to the DOM inside the `test-app` node:
12311231
document.getElementById(id).appendChild(app.view(app.model)); // initial_model
12321232

1233-
t.equal(document.querySelectorAll('h1').textContent, "todos", "<h1>todos");
1233+
t.equal(document.querySelectorAll('h1')[0].textContent, "todos", "<h1>todos");
12341234
// placeholder:
12351235
const placeholder = document.getElementById('new-todo')
12361236
.getAttribute("placeholder");
12371237
t.equal(placeholder, "What needs to be done?", "paceholder set on <input>");
12381238

1239-
const empty = document.querySelectorAll('.todo-list')
1240-
console.log('empty', empty);
1241-
12421239
// todo-count should display 0 items left (based on initial_model):
12431240
const left = document.getElementById('count').innerHTML;
12441241
t.equal(left, "<strong>0</strong> items left", "Todos remaining: " + left);
@@ -1254,7 +1251,15 @@ node test/todo-app.test.js
12541251
```
12551252

12561253
you will see something like this ("_Red_"):
1254+
![app.view-not-a-function](https://user-images.githubusercontent.com/194400/43782111-721805c2-9a56-11e8-970b-681b1499b3a8.png)
1255+
1256+
#### `view` Function _Implementation_
1257+
1258+
1259+
12571260

1261+
When you run `npm test` you should see something like this:
1262+
![image](https://user-images.githubusercontent.com/194400/43782895-48496f22-9a58-11e8-9fde-dbb5554f43a0.png)
12581263

12591264
<!--
12601265

0 commit comments

Comments
 (0)