Skip to content

Commit 2968cd8

Browse files
committed
add test for [ENTER] key in edit mode to SAVE revised todo item title #60
1 parent 1a89e35 commit 2968cd8

File tree

3 files changed

+61
-26
lines changed

3 files changed

+61
-26
lines changed

Diff for: edit-todo.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ Let's work on the `case` (_handler_) for **`signal('EDIT', item.id)`**
369369
which will handle the "double-click" event and set `model.editing`.
370370

371371

372-
#### 5.2 Double-Click item `<label>` to Edit
372+
### 5.2 Double-Click item `<label>` to Edit
373373

374374
The TodoMVC ***spec*** for item
375375
https://github.com/tastejs/todomvc/blob/master/app-spec.md#item
@@ -560,3 +560,45 @@ time between clicks works; clicks spaced more than 300ms will not count
560560
as "double-click".
561561

562562
![edit-item-not-double-click](https://user-images.githubusercontent.com/194400/45184155-ff310b00-b21d-11e8-8f6c-ef6d699861cf.png)
563+
564+
565+
### 5.3 `'SAVE'` a Revised Todo Item Title after Editing it
566+
567+
568+
569+
570+
571+
### 5.3 `'SAVE' update case` _Test_
572+
573+
Append following test code to your `test/todo-app.test.js` file:
574+
575+
```js
576+
test.only('5.3 [ENTER] Key in edit mode triggers SAVE action', function (t) {
577+
elmish.empty(document.getElementById(id));
578+
localStorage.removeItem('todos-elmish_' + id);
579+
const model = {
580+
todos: [
581+
{ id: 0, title: "Make something people want.", done: false },
582+
{ id: 1, title: "Let's solve our own problem", done: false }
583+
],
584+
hash: '#/', // the "route" to display
585+
editing: 1 // edit the 3rd todo list item (which has id == 2)
586+
};
587+
// render the view and append it to the DOM inside the `test-app` node:
588+
elmish.mount(model, app.update, app.view, id, app.subscriptions);
589+
// change the
590+
const updated_title = "Do things that don\'t scale!"
591+
// apply the updated_title to the <input class="edit">:
592+
document.querySelectorAll('.edit')[0].value = updated_title;
593+
// trigger the [Enter] keyboard key to ADD the new todo:
594+
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 13}));
595+
// confirm that the todo item title was updated to the updated_title
596+
const label = document.querySelectorAll('.view > label')[1].textContent;
597+
t.equal(label, updated_title, "item title updated to:" + updated_title)
598+
t.end();
599+
});
600+
```
601+
If you attempt to run this test: `node test/todo-app.test.js`
602+
you will see output similar to the following:
603+
604+
![edit-double-click-test-failing](https://user-images.githubusercontent.com/194400/45183202-54b7e880-b21b-11e8-84d8-7b3b50162113.png)

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ function update(action, model, data) {
103103
return new_model;
104104
}
105105

106-
function edit_item (item, model, signal) {
107-
return [
108-
,
109-
]
110-
}
111106

112107

113108
/**
@@ -291,7 +286,6 @@ function subscriptions (signal) {
291286
case ENTER_KEY:
292287
var editing = document.getElementsByClassName('editing');
293288
if (editing && editing.length > 0) {
294-
console.log('editing:', editing);
295289
signal('SAVE')(); // invoke singal inner callback
296290
}
297291

@@ -302,9 +296,9 @@ function subscriptions (signal) {
302296
document.getElementById('new-todo').focus();
303297
}
304298
break;
305-
case ESCAPE_KEY:
306-
307-
break;
299+
// case ESCAPE_KEY: // COMING SOON!
300+
//
301+
// break;
308302
}
309303
});
310304
}

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

+15-16
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ test('4.1 DELETE item by clicking <button class="destroy">', function (t) {
395395
});
396396

397397

398-
test('5. Editing: > Render an item in "editing mode"', function (t) {
398+
test('5.1 Editing: > Render an item in "editing mode"', function (t) {
399399
elmish.empty(document.getElementById(id));
400400
localStorage.removeItem('todos-elmish_' + id);
401401
const model = {
@@ -474,28 +474,27 @@ test('5.2.2 Slow clicks do not count as double-click > no edit!', function (t) {
474474
});
475475

476476

477-
test.skip('5.1 Double-click an item <label> to edit it', function (t) {
477+
test.only('5.3 [ENTER] Key in edit mode triggers SAVE action', function (t) {
478478
elmish.empty(document.getElementById(id));
479479
localStorage.removeItem('todos-elmish_' + id);
480480
const model = {
481481
todos: [
482-
{ id: 0, title: "Make something people want.", done: false }
482+
{ id: 0, title: "Make something people want.", done: false },
483+
{ id: 1, title: "Let's solve our own problem", done: false }
483484
],
484-
hash: '#/' // the "route" to display
485+
hash: '#/', // the "route" to display
486+
editing: 1 // edit the 3rd todo list item (which has id == 2)
485487
};
486488
// render the view and append it to the DOM inside the `test-app` node:
487489
elmish.mount(model, app.update, app.view, id, app.subscriptions);
488-
// const todo_count = ;
489-
t.equal(document.querySelectorAll('.destroy').length, 1, "one destroy button")
490-
491-
const item = document.getElementById('0')
492-
t.equal(item.textContent, model.todos[0].title, 'Item contained in DOM.');
493-
// DELETE the item by clicking on the <button class="destroy">:
494-
const button = item.querySelectorAll('button.destroy')[0];
495-
button.click()
496-
// confirm that there is no loger a <button class="destroy">
497-
t.equal(document.querySelectorAll('button.destroy').length, 0,
498-
'there is no loger a <button class="destroy"> as the only item was DELETEd')
499-
t.equal(document.getElementById('0'), null, 'todo item successfully DELETEd');
490+
// change the
491+
const updated_title = "Do things that don\'t scale!"
492+
// apply the updated_title to the <input class="edit">:
493+
document.querySelectorAll('.edit')[0].value = updated_title;
494+
// trigger the [Enter] keyboard key to ADD the new todo:
495+
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 13}));
496+
// confirm that the todo item title was updated to the updated_title:
497+
const label = document.querySelectorAll('.view > label')[1].textContent;
498+
t.equal(label, updated_title, "item title updated to:" + updated_title)
500499
t.end();
501500
});

0 commit comments

Comments
 (0)