@@ -29,7 +29,9 @@ <h1>Mocha Tests - Composi Runtime & Union</h1>
29
29
< div id ='component-click4 '> </ div >
30
30
< div id ='send-test '> </ div >
31
31
< div id ="test-effect "> </ div >
32
- < div id ="subscription-test "> </ div >
32
+ < div id ="subscription-test1 "> </ div >
33
+ < div id ="subscription-test2 "> </ div >
34
+ < div id ="subscription-test3 "> </ div >
33
35
</ div >
34
36
</ section >
35
37
@@ -453,21 +455,21 @@ <h1>Mocha Tests - Composi Runtime & Union</h1>
453
455
describe ( 'Should be able to launch subscriptions at start.' , function ( ) {
454
456
it ( 'subscription should run at startup.' , done => {
455
457
let effectResult = false
456
- function effect ( ) {
458
+ function effect ( getState , send ) {
457
459
effectResult = true
458
460
}
459
461
const program = {
460
462
init ( ) {
461
463
return [ 0 ]
462
464
} ,
463
- update ( ) {
464
- return [ state ]
465
+ update ( state , msg , send ) {
466
+ return [ msg . data ]
465
467
} ,
466
468
view ( ) {
467
469
return
468
470
} ,
469
- subscriptions ( ) {
470
- return effect
471
+ subscriptions ( getState , send ) {
472
+ return effect ( getState , send )
471
473
}
472
474
}
473
475
run ( program )
@@ -480,40 +482,41 @@ <h1>Mocha Tests - Composi Runtime & Union</h1>
480
482
481
483
describe ( 'Subscription should be able to access state and send message to action.' , function ( ) {
482
484
let state = 'initial state'
483
- let newState = ''
484
-
485
- let msgWasSent = false
486
- function effect ( state , send ) {
487
- send ( { type : 'update-state' , value : state } )
488
-
485
+ const test = document . querySelector ( '#subscription-test3' )
486
+ function Component ( { state, send} ) {
487
+ return (
488
+ h ( 'div' ,
489
+ { } ,
490
+ [ state ] )
491
+ )
492
+ }
493
+ function effect ( getState , send ) {
494
+ const currentState = getState ( )
495
+ if ( currentState === 'initial state' ) {
496
+ send ( { type : 'update-state' , value : 'new state' } )
497
+ }
489
498
}
490
499
const program = {
491
500
init ( ) {
492
501
return [ state ]
493
502
} ,
494
503
update ( state , msg ) {
495
504
if ( msg . type === 'update-state' ) {
496
- newState = msg . value
497
- msgWasSent = true
498
- return [ state ]
505
+ return [ msg . value ]
499
506
}
500
507
} ,
501
- view ( ) {
502
- return
508
+ view ( state , send ) {
509
+ return render ( Component ( { state , send } ) , test )
503
510
} ,
504
- subscriptions ( state , send ) {
505
- return ( state , send ) => {
506
- send ( { type : 'update-state' , value : state } )
507
- }
511
+ subscriptions ( getState , send ) {
512
+ return effect ( getState , send )
508
513
}
509
514
}
510
515
run ( program )
511
- it ( 'Subscription should have access to state.' , done => {
512
- expect ( state ) . to . equal ( 'initial state' )
513
- done ( )
514
- } )
515
- it ( 'Subscription should be able to send message to action.' , done => {
516
- expect ( msgWasSent ) . to . equal ( true )
516
+ it ( 'Subscription should have access to state and send message to program\'s update method.' , done => {
517
+ setTimeout ( ( ) => {
518
+ expect ( test . textContent ) . to . equal ( 'new state' )
519
+ } , 1000 )
517
520
done ( )
518
521
} )
519
522
} )
@@ -535,9 +538,9 @@ <h1>Mocha Tests - Composi Runtime & Union</h1>
535
538
return
536
539
} ,
537
540
subs ( getState , send ) {
538
- // Should be able to access state from subscription:
541
+ // Should be able to access program state from subscription:
539
542
expect ( getState ( ) ) . to . equal ( 1 )
540
- return effect
543
+ return effect ( getState , send )
541
544
}
542
545
}
543
546
run ( program )
@@ -548,94 +551,135 @@ <h1>Mocha Tests - Composi Runtime & Union</h1>
548
551
} )
549
552
} )
550
553
describe ( 'Should be able to run batched subscriptions.' , function ( ) {
554
+ const test = document . querySelector ( '#subscription-test2' )
551
555
552
- let result1 = ''
553
- let result2 = ''
554
- let count = 5
555
- const hello = ( ) => {
556
- result1 = 'Hello World!'
556
+ const state = {
557
+ count : 5 ,
558
+ result1 : '1' ,
559
+ result2 : '2'
557
560
}
558
- const secondEffect = ( ) => {
559
- result2 = 'Second effect ran!'
561
+
562
+ function hello ( getState , send ) {
563
+ send ( { type : 'result1' , value : 'Hello World!' } )
560
564
}
561
- const countDown = ( ) => {
565
+ const secondEffect = ( getState , send ) => {
566
+ send ( { type : 'result2' , value : 'Second effect ran!' } )
567
+ }
568
+ const countDown = ( getState , send ) => {
562
569
const id = setInterval ( ( ) => {
570
+ const { count} = getState ( )
563
571
if ( count === 0 ) {
564
572
clearInterval ( id )
565
573
} else {
566
- count --
574
+ send ( { type : 'reduce' } )
567
575
}
568
576
} , 100 )
569
577
}
570
- const subscriptions = batchEffects ( hello , secondEffect , countDown )
578
+
579
+
580
+ const subs = batchEffects ( hello , secondEffect , countDown )
581
+
582
+ function Component ( { state} ) {
583
+ return h (
584
+ 'ul' ,
585
+ { } ,
586
+ [
587
+ h ( 'li' , { } , state . result1 ) ,
588
+ h ( 'li' , { } , state . result2 ) ,
589
+ h ( 'li' , { } , state . count ) ,
590
+ ]
591
+ )
592
+ }
571
593
const program = {
572
594
init ( ) {
573
- return [ 'World' ]
595
+ return [ state ]
574
596
} ,
575
- update ( ) { } ,
576
- view ( ) { } ,
577
- subscriptions ( state , send ) {
578
- return subscriptions
597
+ view ( state , send ) {
598
+ return render ( Component ( { state} ) , test )
599
+ } ,
600
+ update ( state , msg , send ) {
601
+ if ( msg . type === 'result1' ) {
602
+ state . result1 = msg . value
603
+ return [ state ]
604
+ } else if ( msg . type === 'result2' ) {
605
+ state . result2 = msg . value
606
+ return [ state ]
607
+ } else if ( msg . type === 'reduce' ) {
608
+ state . count -= 1
609
+ return [ state ]
610
+ }
611
+ } ,
612
+ subscriptions ( getState , send ) {
613
+ return subs ( getState , send )
579
614
}
580
615
}
581
- const endProgram = run ( program )
616
+ run ( program )
582
617
583
618
it ( 'batched subsriptions should run at startup.' , done => {
584
- endProgram ( )
585
619
setTimeout ( ( ) => {
586
- expect ( result1 ) . to . equal ( 'Hello World!' )
587
- expect ( result2 ) . to . equal ( 'Second effect ran!' )
588
- expect ( count ) . to . equal ( 0 )
620
+ const children = test . firstElementChild . children
621
+ expect ( children [ 0 ] . textContent ) . to . equal ( 'Hello World!' )
622
+ expect ( children [ 1 ] . textContent ) . to . equal ( 'Second effect ran!' )
623
+ expect ( children [ 2 ] . textContent ) . to . equal ( '0' )
589
624
} , 1000 )
590
625
done ( )
591
626
} )
592
627
} )
593
628
594
629
describe ( 'Batched subscriptions should have access to getState and send functions.' , function ( ) {
595
- const subscriptionTest = document . querySelector ( '#subscription-test' )
596
- let state = 1
597
- let messageWasSent = false
630
+ const test = document . querySelector ( '#subscription-test1' )
631
+ let state = {
632
+ count : 1 ,
633
+ messageWasSent : 'false'
634
+ }
598
635
599
636
function effect1 ( getState , send ) {
600
- send ( { type : 'update-state-1' , value : getState ( ) + 1 } )
637
+ const count = getState ( ) . count
638
+ send ( { type : 'update-state-1' , value : parseInt ( count ) + 1 } )
601
639
}
602
640
function effect2 ( getState , send ) {
603
- setTimeout ( ( ) => send ( { type : 'update-state-2' , value : getState ( ) + 2 } ) , 1000 )
641
+ const count = getState ( ) . count
642
+ setTimeout ( ( ) => send ( { type : 'update-state-2' , value : parseInt ( count ) + 2 } ) , 2000 )
604
643
}
605
644
const batchedEffects = batchEffects ( effect1 , effect2 )
606
645
function subTest ( { state, send} ) {
607
- return h ( 'div' , { } , state )
646
+ return h ( 'div' , { } , [
647
+ h ( 'p' , { } , state . count ) ,
648
+ h ( 'p' , { } , state . messageWasSent )
649
+ ] )
608
650
}
609
651
const program = {
610
652
init ( ) {
611
653
return [ state ]
612
654
} ,
613
655
update ( state , msg ) {
614
656
if ( msg . type === 'update-state-1' ) {
615
- return [ msg . value ]
657
+ state . count = msg . value
658
+ return [ state ]
616
659
} else if ( msg . type === 'update-state-2' ) {
617
- messageWasSent = true
618
- return [ msg . value ]
660
+ state . messageWasSent = 'true'
661
+ state . count = msg . value
662
+ return [ state ]
619
663
}
620
664
} ,
621
665
view ( state , send ) {
622
- return render ( subTest ( { state, send} ) , subscriptionTest )
666
+ return render ( subTest ( { state, send} ) , test )
623
667
} ,
624
668
subscriptions ( getState , send ) {
625
- return batchedEffects
669
+ return batchedEffects ( getState , send )
626
670
}
627
671
}
628
672
run ( program )
629
673
it ( 'First effect should update state to 2.' , function ( ) {
630
- expect ( subscriptionTest . textContent ) . to . equal ( '2' )
674
+ expect ( test . firstElementChild . children [ 0 ] . textContent ) . to . equal ( '2' )
631
675
} )
632
- it ( 'Second effect should update state to 3 .' ,
676
+ it ( 'Second effect should update state to 4 .' ,
633
677
function ( done ) {
634
- setTimeout ( ( ) => expect ( subscriptionTest . textContent ) . to . equal ( '4' ) , 1200 )
678
+ setTimeout ( ( ) => expect ( test . firstElementChild . children [ 0 ] . textContent ) . to . equal ( '4' ) , 2000 )
635
679
done ( )
636
680
} )
637
681
it ( 'Second effect should send message to update action, setting messageWasSent to true.' , function ( done ) {
638
- setTimeout ( ( ) => expect ( messageWasSent ) . to . equal ( true ) , 1200 )
682
+ setTimeout ( ( ) => expect ( test . firstElementChild . children [ 1 ] . textContent ) . to . equal ( ' true' ) , 2500 )
639
683
done ( )
640
684
} )
641
685
} )
0 commit comments