Skip to content

Commit 193d55e

Browse files
committed
add screenshot of 2. New Todo tests passing #57
1 parent 9471c34 commit 193d55e

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

Diff for: elmish.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,7 @@ If you get "stuck", checkout:
18621862

18631863
<br />
18641864

1865-
### `subscriptions` for event listeners
1865+
### `subscriptions` for Event Listeners
18661866

18671867
In Elm, when we want to "listen" for an event or "external input"
18681868
we use `subscriptions`. <br />
@@ -1988,6 +1988,8 @@ so the last assertion always passes.
19881988
(_this will not be the case
19891989
once you have the [Up] arrow event listener working_).
19901990

1991+
Recommended reading: https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically
1992+
19911993
#### `subscriptions`_Implementation_: Keyboard Keys Increment/Decrement Counter!
19921994

19931995
Once again, try to _think_ of how you would implement
@@ -2006,7 +2008,7 @@ Your tests should pass:
20062008

20072009
![counter-reset-keyboard-subscriptions-tests-passing](https://user-images.githubusercontent.com/194400/43981911-b6413dda-9ceb-11e8-8514-44fc1f88c3fe.png)
20082010

2009-
Well done!
2011+
Well done!
20102012

20112013
<br />
20122014

Diff for: examples/counter-reset-keyboard/index.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
<script>
1919
// Initialise the app by "mounting" it passing in MUV Object & "root" DOM node
2020
mount(0, update, view, 'app', subscriptions);
21-
console.log('subs:', subscriptions.toString());
2221
</script>
23-
22+
2423
</body>
2524
</html>

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ function mount (model, update, view, root_element_id, subscriptions) {
3131
root.appendChild(view(mod, sig)) // render view based on model & signal
3232
}
3333

34-
function signal(action) { // signal function takes action
34+
function signal(action, data) { // signal function takes action
3535
return function callback() { // and returns callback
3636
model = JSON.parse(localStorage.getItem(store_name)) //|| model;
37-
var updatedModel = update(action, model); // update model for the action
37+
var updatedModel = update(action, model, data); // update model for the action
3838
render(updatedModel, signal, ROOT);
3939
};
4040
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function subscriptions (signal) {
197197
var ESCAPE_KEY = 27; // used for "escaping" when editing a Todo item
198198
var new_todo = document.getElementById('new-todo');
199199

200-
new_todo.addEventListener('keypress', function (e) {
200+
new_todo.addEventListener('keyup', function (e) {
201201
if (e.keyCode === ENTER_KEY && new_todo.value.length > 0) {
202202
signal('ADD')(); // invoke the singal function and inner callback
203203
new_todo.value = ''; // reset <input> so we can add another todo

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ test('2. New Todo, should allow me to add todo items', function (t) {
220220
const todo_text = 'Make Everything Awesome! '; // deliberate whitespace!
221221
new_todo.value = todo_text;
222222
// trigger the [Enter] keyboard key to ADD the new todo:
223-
new_todo.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 13}));
223+
new_todo.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 13}));
224224
const items = document.querySelectorAll('.view');
225225
t.equal(items.length, 1, "should allow me to add todo items");
226226
// check if the new todo was added to the DOM:
@@ -229,7 +229,7 @@ test('2. New Todo, should allow me to add todo items', function (t) {
229229

230230
// subscription keyCode trigger "branch" test (should NOT fire the signal):
231231
const clone = document.getElementById(id).cloneNode(true);
232-
new_todo.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 42}));
232+
new_todo.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 42}));
233233
t.deepEqual(document.getElementById(id), clone, "#" + id + " no change");
234234

235235
// check that the <input id="new-todo"> was reset after the new item was added

Diff for: todo-list.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ test('2. New Todo, should allow me to add todo items', function (t) {
14731473
const todo_text = 'Make Everything Awesome! '; // deliberate whitespace!
14741474
new_todo.value = todo_text;
14751475
// trigger the [Enter] keyboard key to ADD the new todo:
1476-
new_todo.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 13}));
1476+
new_todo.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 13}));
14771477
const items = document.querySelectorAll('.view');
14781478

14791479
t.equal(items.length, 1, "should allow me to add todo items");
@@ -1483,7 +1483,7 @@ test('2. New Todo, should allow me to add todo items', function (t) {
14831483

14841484
// subscription keyCode trigger "branch" test (should NOT fire the signal):
14851485
const clone = document.getElementById(id).cloneNode(true);
1486-
new_todo.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 42}));
1486+
new_todo.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 42}));
14871487
t.deepEqual(document.getElementById(id), clone, "#" + id + " no change");
14881488

14891489
// check that the <input id="new-todo"> was reset after the new item was added
@@ -1510,7 +1510,28 @@ You should see the following output:
15101510

15111511
![test-failing](https://user-images.githubusercontent.com/194400/43929259-1880b41e-9c2c-11e8-9615-1372928c905d.png)
15121512

1513-
Reading: https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically
1513+
1514+
#### Todo List `subscriptions`
1515+
1516+
So far in the Todo List App
1517+
we have not implemented any **`subscriptions`**,
1518+
however, in order to "listen" for the **`[Enter]`** key "event"
1519+
(_to add a Todo List item_), we need to dive into event listeners.
1520+
1521+
Thankfully, we touched upon this while building `Elm`(_ish_),
1522+
if you need a recap, see:
1523+
[**elmish.md#subscriptions-for-event-listeners**](https://github.com/dwyl/learn-elm-architecture-in-javascript/blob/master/elmish.md#subscriptions-for-event-listeners)
1524+
1525+
Try to make the "**2. New Todo**" batch of tests _pass_
1526+
by creating (_and exporting_) a **`subscriptions`** function
1527+
in your **`examples/todo-list/todo-app.js`** file.
1528+
1529+
If you get "_stuck_", checkout the sample code:
1530+
[**`todo-app.js > subscriptions`**](https://github.com/dwyl/learn-elm-architecture-in-javascript/pull/45/files#diff-6be3e16fe7cfb4c00788d4d587374afdR189)
1531+
1532+
0nce you see the tests passing:
1533+
1534+
![add-todo-tests-passing](https://user-images.githubusercontent.com/194400/43982691-08d81eee-9cef-11e8-92c3-341433884092.png)
15141535

15151536

15161537
<!--

0 commit comments

Comments
 (0)