@@ -380,16 +380,16 @@ global.localStorage = global.localStorage ? global.localStorage : {
380
380
delete this [ key ]
381
381
}
382
382
}
383
- localStorage . removeItem ( 'elmish_store' ) ;
383
+ localStorage . removeItem ( 'elmish_' + id ) ;
384
384
// localStorage.setItem('hello', 'world!');
385
385
// console.log('localStorage (polyfil) hello', localStorage.getItem('hello'));
386
386
387
387
// // Test mount's localStorage using view and update from counter-reset example
388
388
// // to confirm that our elmish.mount localStorage works and is "generic".
389
389
test ( 'elmish.mount sets model in localStorage' , function ( t ) {
390
390
const { view, update } = require ( '../examples/counter-reset/counter.js' ) ;
391
-
392
391
const root = document . getElementById ( id ) ;
392
+
393
393
elmish . mount ( 7 , update , view , id ) ;
394
394
// the "model" stored in localStorage should be 7 now:
395
395
t . equal ( JSON . parse ( localStorage . getItem ( 'elmish_' + id ) ) , 7 ,
@@ -403,7 +403,8 @@ test('elmish.mount sets model in localStorage', function (t) {
403
403
// attempting to "re-mount" with a different model value should not work
404
404
// because mount should retrieve the value from localStorage
405
405
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 ,
407
408
"elmish_store is 7 (as expected). initial state saved to localStorage." ) ;
408
409
// increment the counter
409
410
const btn = root . getElementsByClassName ( "inc" ) [ 0 ] ; // click increment button
@@ -419,7 +420,7 @@ test('elmish.mount sets model in localStorage', function (t) {
419
420
// clearing DOM does NOT clear the localStorage (this is desired behaviour!)
420
421
t . equal ( JSON . parse ( localStorage . getItem ( 'elmish_' + id ) ) , 8 ,
421
422
"elmish_store still 8 from increment (above) saved in localStorage" ) ;
422
- localStorage . removeItem ( 'elmish_store' ) ;
423
+ localStorage . removeItem ( 'elmish_' + id ) ;
423
424
t . end ( )
424
425
} ) ;
425
426
@@ -451,38 +452,50 @@ test('elmish.add_attributes onclick=signal(action) events!', function (t) {
451
452
} ) ;
452
453
453
454
454
- test . only ( 'subscriptions test using counter-reset-keyaboard ⌨️' , function ( t ) {
455
+ test ( 'subscriptions test using counter-reset-keyaboard ⌨️' , function ( t ) {
455
456
const { view, update, subscriptions } =
456
457
require ( '../examples/counter-reset-keyboard/counter.js' ) ;
457
458
const root = document . getElementById ( id ) ;
458
459
459
460
// mount the counter-reset-keyboard example app WITH subscriptions:
460
461
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
+
463
463
// 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." ) ;
465
466
t . equal ( JSON . parse ( localStorage . getItem ( 'elmish_' + id ) ) , 0 ,
466
467
"elmish_store is 0 (as expected). initial state saved to localStorage." ) ;
467
468
468
469
// 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" ) ;
471
473
t . equal ( JSON . parse ( localStorage . getItem ( 'elmish_' + id ) ) , 1 ,
472
474
"elmish_store 1 (as expected). incremented state saved to localStorage." ) ;
473
475
474
476
// 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" ) ;
477
480
t . equal ( JSON . parse ( localStorage . getItem ( 'elmish_' + id ) ) , 0 ,
478
481
"elmish_store 0. keyboard down key decrement state saved to localStorage." ) ;
479
482
480
483
// subscription keyCode trigger "branch" test (should NOT fire the signal):
481
484
const clone = document . getElementById ( id ) . cloneNode ( true ) ;
482
- document . dispatchEvent ( new KeyboardEvent ( 'keyup ' , { 'keyCode' : 42 } ) ) ; //
485
+ document . dispatchEvent ( new KeyboardEvent ( 'keypress ' , { 'keyCode' : 42 } ) ) ; //
483
486
t . deepEqual ( document . getElementById ( id ) , clone , "#" + id + " no change" ) ;
484
487
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 ) ;
486
499
elmish . empty ( root ) ;
487
500
t . end ( )
488
501
} ) ;
0 commit comments