@@ -452,54 +452,37 @@ test('elmish.add_attributes onclick=signal(action) events!', function (t) {
452
452
453
453
454
454
test . only ( 'subscriptions test using counter-reset-keyaboard ⌨️' , function ( t ) {
455
- const { view, update } = require ( '../examples/counter-reset/counter.js' ) ;
456
-
455
+ const { view, update, subscriptions } =
456
+ require ( '../examples/counter-reset-keyboard/counter.js' ) ;
457
457
const root = document . getElementById ( id ) ;
458
- elmish . mount ( 0 , update , view , id ) ;
459
- // the "model" stored in localStorage should be 7 now:
458
+
459
+ // mount the counter-reset-keyboard example app WITH subscriptions:
460
+ elmish . mount ( 0 , update , view , id , subscriptions ) ;
461
+ // keep a "handle" on the count to reduce test length:
462
+ const count = document . getElementById ( 'count' ) ;
463
+ // counter starts off at 0 (zero):
464
+ t . equal ( parseInt ( count . textContent , 10 ) , 0 , "Up key press increment 0 -> 1" ) ;
460
465
t . equal ( JSON . parse ( localStorage . getItem ( 'elmish_' + id ) ) , 0 ,
461
466
"elmish_store is 0 (as expected). initial state saved to localStorage." ) ;
462
467
463
- // trigger the [Enter] keyboard key to ADD the new todo:
464
- document . dispatchEvent ( new KeyboardEvent ( 'keypress' , { 'keyCode' : 38 } ) ) ; // ↑
465
- const items = document . querySelectorAll ( '.view' ) ;
466
-
467
- // subscription keyCode trigger "branch" test (should NOT fire the signal):
468
- document . dispatchEvent ( new KeyboardEvent ( 'keypress' , { 'keyCode' : 40 } ) ) ; // ↓
469
- // t.deepEqual(document.getElementById(id), clone, "#" + id + " no change");
468
+ // 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" ) ;
471
+ t . equal ( JSON . parse ( localStorage . getItem ( 'elmish_' + id ) ) , 1 ,
472
+ "elmish_store 1 (as expected). incremented state saved to localStorage." ) ;
470
473
474
+ // 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
+ t . equal ( JSON . parse ( localStorage . getItem ( 'elmish_' + id ) ) , 0 ,
478
+ "elmish_store 0. keyboard down key decrement state saved to localStorage." ) ;
471
479
472
480
// subscription keyCode trigger "branch" test (should NOT fire the signal):
473
481
const clone = document . getElementById ( id ) . cloneNode ( true ) ;
474
- document . dispatchEvent ( new KeyboardEvent ( 'keypress ' , { 'keyCode' : 42 } ) ) ; // ↓
482
+ document . dispatchEvent ( new KeyboardEvent ( 'keyup ' , { 'keyCode' : 42 } ) ) ; //
475
483
t . deepEqual ( document . getElementById ( id ) , clone , "#" + id + " no change" ) ;
476
484
477
-
478
- // // test that mount still works as expected (check initial state of counter):
479
- // const actual = document.getElementById(id).textContent;
480
- // const actual_stripped = parseInt(actual.replace('+', '')
481
- // .replace('-Reset', ''), 10);
482
- // const expected = 7;
483
- // t.equal(expected, actual_stripped, "Inital state set to 7.");
484
- // // attempting to "re-mount" with a different model value should not work
485
- // // because mount should retrieve the value from localStorage
486
- // elmish.mount(42, update, view, id); // model (42) should be ignored this time!
487
- // t.equal(JSON.parse(localStorage.getItem('elmish_store')), 7,
488
- // "elmish_store is 7 (as expected). initial state saved to localStorage.");
489
- // // increment the counter
490
- // const btn = root.getElementsByClassName("inc")[0]; // click increment button
491
- // btn.click(); // Click the Increment button!
492
- // const state = parseInt(root.getElementsByClassName('count')[0]
493
- // .textContent, 10);
494
- // t.equal(state, 8, "State is 8 after increment.");
495
- // // the "model" stored in localStorage should also be 8 now:
496
- // t.equal(JSON.parse(localStorage.getItem('elmish_store')), 8,
497
- // "elmish_store is 8 (as expected).");
498
- // elmish.empty(root); // reset the DOM to simulate refreshing a browser window
499
- // elmish.mount(5, update, view, id); // 5 ignored! read model from localStorage
500
- // // clearing DOM does NOT clear the localStorage (this is desired behaviour!)
501
- // t.equal(JSON.parse(localStorage.getItem('elmish_store')), 8,
502
- // "elmish_store still 8 from increment (above) saved in localStorage");
503
485
localStorage . removeItem ( 'elmish_store' ) ;
486
+ elmish . empty ( root ) ;
504
487
t . end ( )
505
488
} ) ;
0 commit comments