Skip to content

Commit 39d270b

Browse files
committed
add additional assertions for counter-reset-keyboard example for #57
1 parent 9a10d60 commit 39d270b

File tree

3 files changed

+47
-32
lines changed

3 files changed

+47
-32
lines changed

examples/counter-reset-keyboard/counter.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ function update (action, model) { // Update function takes the current state
1515
}
1616

1717
function view(model, signal) {
18-
console.log('model:', model);
1918
return div([], [
2019
button(["class=inc", "id=inc", signal('inc')], [text('+')]), // increment
2120
div(["class=count", "id=count"], [text(model.toString())]), // count
@@ -27,20 +26,24 @@ function view(model, signal) {
2726
function subscriptions (signal, root) {
2827
var UP_KEY = 38; // increment the counter when [↑] (up) key is pressed
2928
var DOWN_KEY = 40; // decrement the counter when [↓] (down) key is pressed
30-
console.log('root', root);
31-
root.addEventListener('keyup', function (e) {
32-
console.log('e.keyCode', e.keyCode);
33-
switch (e.keyCode) {
34-
case UP_KEY:
35-
signal('inc')(); // invoke the signal.callback function directly
36-
break;
37-
case DOWN_KEY:
38-
signal('dec')();
39-
break;
40-
default: // do nothing
41-
break;
42-
}
43-
});
29+
var called = false; // ensure event hanlder is only called once! #HelpWanted!
30+
31+
function handler (e) {
32+
if (!called) { // had issues with event handler being called multiple!
33+
called = true;
34+
switch (e.keyCode) {
35+
case UP_KEY:
36+
signal('inc')(); // invoke the signal.callback function directly
37+
break;
38+
case DOWN_KEY:
39+
signal('dec')();
40+
break;
41+
}
42+
} // only call event handler once!
43+
}
44+
// event listners were being fired multiple times so, remove before addding:
45+
root.removeEventListener('keypress', handler, false); // remove previous
46+
root.addEventListener('keypress', handler, false); // add fresh listeners
4447
}
4548

4649
/* The code block below ONLY Applies to tests run using Node.js */

examples/todo-list/elmish.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ function empty (node) {
2424
function mount (model, update, view, root_element_id, subscriptions) {
2525
var ROOT = document.getElementById(root_element_id); // root DOM element
2626
var store_name = 'elmish_' + root_element_id; // test-app !== app
27-
27+
console.log('store_name:', store_name);
2828
function render (mod, sig, root, subs) { // DRY rendering code (invoked twice)
29-
localStorage.setItem(store_name, JSON.stringify(mod)); // save model!
29+
localStorage.setItem(store_name, JSON.stringify(mod)); // save the model!
3030
empty(root); // clear root element (container) before (re)rendering
3131
root.appendChild(view(mod, sig)) // render view based on model & signal
3232
if (subs && typeof subs === 'function') { subs(sig, root); } // subscription
3333
}
3434

3535
function signal(action) { // signal function takes action
3636
return function callback() { // and returns callback
37-
console.log('signal action:', action);
3837
model = JSON.parse(localStorage.getItem(store_name)) //|| model;
3938
var updatedModel = update(action, model); // update model for the action
4039
render(updatedModel, signal, ROOT, subscriptions);

test/elmish.test.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -380,16 +380,16 @@ global.localStorage = global.localStorage ? global.localStorage : {
380380
delete this[key]
381381
}
382382
}
383-
localStorage.removeItem('elmish_store');
383+
localStorage.removeItem('elmish_' + id);
384384
// localStorage.setItem('hello', 'world!');
385385
// console.log('localStorage (polyfil) hello', localStorage.getItem('hello'));
386386

387387
// // Test mount's localStorage using view and update from counter-reset example
388388
// // to confirm that our elmish.mount localStorage works and is "generic".
389389
test('elmish.mount sets model in localStorage', function (t) {
390390
const { view, update } = require('../examples/counter-reset/counter.js');
391-
392391
const root = document.getElementById(id);
392+
393393
elmish.mount(7, update, view, id);
394394
// the "model" stored in localStorage should be 7 now:
395395
t.equal(JSON.parse(localStorage.getItem('elmish_' + id)), 7,
@@ -403,7 +403,8 @@ test('elmish.mount sets model in localStorage', function (t) {
403403
// attempting to "re-mount" with a different model value should not work
404404
// because mount should retrieve the value from localStorage
405405
elmish.mount(42, update, view, id); // model (42) should be ignored this time!
406-
t.equal(JSON.parse(localStorage.getItem('elmish_store')), 7,
406+
407+
t.equal(JSON.parse(localStorage.getItem('elmish_' + id)), 7,
407408
"elmish_store is 7 (as expected). initial state saved to localStorage.");
408409
// increment the counter
409410
const btn = root.getElementsByClassName("inc")[0]; // click increment button
@@ -419,7 +420,7 @@ test('elmish.mount sets model in localStorage', function (t) {
419420
// clearing DOM does NOT clear the localStorage (this is desired behaviour!)
420421
t.equal(JSON.parse(localStorage.getItem('elmish_' + id)), 8,
421422
"elmish_store still 8 from increment (above) saved in localStorage");
422-
localStorage.removeItem('elmish_store');
423+
localStorage.removeItem('elmish_' + id);
423424
t.end()
424425
});
425426

@@ -451,38 +452,50 @@ test('elmish.add_attributes onclick=signal(action) events!', function (t) {
451452
});
452453

453454

454-
test.only('subscriptions test using counter-reset-keyaboard ⌨️', function (t) {
455+
test('subscriptions test using counter-reset-keyaboard ⌨️', function (t) {
455456
const { view, update, subscriptions } =
456457
require('../examples/counter-reset-keyboard/counter.js');
457458
const root = document.getElementById(id);
458459

459460
// mount the counter-reset-keyboard example app WITH subscriptions:
460461
elmish.mount(0, update, view, id, subscriptions);
461-
// keep a "handle" on the count to reduce test length:
462-
const count = document.getElementById('count');
462+
463463
// counter starts off at 0 (zero):
464-
t.equal(parseInt(count.textContent, 10), 0, "Up key press increment 0 -> 1");
464+
t.equal(parseInt(document.getElementById('count') // always fresh DOM node!
465+
.textContent, 10), 0, "Count is 0 (Zero) at start.");
465466
t.equal(JSON.parse(localStorage.getItem('elmish_' + id)), 0,
466467
"elmish_store is 0 (as expected). initial state saved to localStorage.");
467468

468469
// trigger the [↑] (up) keyboard key to increment the counter:
469-
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 38})); // ↑
470-
t.equal(parseInt(count.textContent, 10), 1, "Up key press increment 0 -> 1");
470+
root.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 38})); // ↑
471+
t.equal(parseInt(document.getElementById('count')
472+
.textContent, 10), 1, "Up key press increment 0 -> 1");
471473
t.equal(JSON.parse(localStorage.getItem('elmish_' + id)), 1,
472474
"elmish_store 1 (as expected). incremented state saved to localStorage.");
473475

474476
// trigger the [↓] (down) keyboard key to increment the counter:
475-
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 40})); // ↓
476-
t.equal(parseInt(count.textContent, 10), 0, "Up key press dencrement 1 -> 0");
477+
root.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 40})); // ↓
478+
t.equal(parseInt(document.getElementById('count')
479+
.textContent, 10), 0, "Up key press dencrement 1 -> 0");
477480
t.equal(JSON.parse(localStorage.getItem('elmish_' + id)), 0,
478481
"elmish_store 0. keyboard down key decrement state saved to localStorage.");
479482

480483
// subscription keyCode trigger "branch" test (should NOT fire the signal):
481484
const clone = document.getElementById(id).cloneNode(true);
482-
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 42})); //
485+
document.dispatchEvent(new KeyboardEvent('keypress', {'keyCode': 42})); //
483486
t.deepEqual(document.getElementById(id), clone, "#" + id + " no change");
484487

485-
localStorage.removeItem('elmish_store');
488+
// default branch execution:
489+
document.getElementById('inc').click();
490+
t.equal(parseInt(document.getElementById('count')
491+
.textContent, 10), 1, "inc: 0 -> 1");
492+
document.getElementById('reset').click();
493+
t.equal(parseInt(document.getElementById('count')
494+
.textContent, 10), 0, "reset: 1 -> 0");
495+
const no_change = update(null, 7);
496+
t.equal(no_change, 7, "no change in model if action is unrecognised.");
497+
498+
localStorage.removeItem('elmish_' + id);
486499
elmish.empty(root);
487500
t.end()
488501
});

0 commit comments

Comments
 (0)