@@ -2,6 +2,8 @@ import expect from 'expect'
2
2
import { createStore , combineReducers } from '../src/index'
3
3
import { addTodo , dispatchInMiddle , throwError , unknownAction } from './helpers/actionCreators'
4
4
import * as reducers from './helpers/reducers'
5
+ import * as Rx from 'rxjs'
6
+ import $$observable from 'symbol-observable'
5
7
6
8
describe ( 'createStore' , ( ) => {
7
9
it ( 'exposes the public API' , ( ) => {
@@ -610,4 +612,118 @@ describe('createStore', () => {
610
612
store . subscribe ( undefined )
611
613
) . toThrow ( )
612
614
} )
615
+
616
+ describe ( 'Symbol.observable interop point' , ( ) => {
617
+ it ( 'should exist' , ( ) => {
618
+ const store = createStore ( ( ) => { } )
619
+ expect ( typeof store [ $$observable ] ) . toBe ( 'function' )
620
+ } )
621
+
622
+ describe ( 'returned value' , ( ) => {
623
+ it ( 'should be subscribable' , ( ) => {
624
+ const store = createStore ( ( ) => { } )
625
+ const obs = store [ $$observable ] ( )
626
+ expect ( typeof obs . subscribe ) . toBe ( 'function' )
627
+ } )
628
+
629
+ it ( 'should throw a TypeError if an observer object is not supplied to subscribe' , ( ) => {
630
+ const store = createStore ( ( ) => { } )
631
+ const obs = store [ $$observable ] ( )
632
+
633
+ expect ( function ( ) {
634
+ obs . subscribe ( )
635
+ } ) . toThrow ( )
636
+
637
+ expect ( function ( ) {
638
+ obs . subscribe ( ( ) => { } )
639
+ } ) . toThrow ( )
640
+
641
+ expect ( function ( ) {
642
+ obs . subscribe ( { } )
643
+ } ) . toNotThrow ( )
644
+ } )
645
+
646
+ it ( 'should return a subscription object when subscribed' , ( ) => {
647
+ const store = createStore ( ( ) => { } )
648
+ const obs = store [ $$observable ] ( )
649
+ const sub = obs . subscribe ( { } )
650
+ expect ( typeof sub . unsubscribe ) . toBe ( 'function' )
651
+ } )
652
+ } )
653
+
654
+ it ( 'should pass an integration test with no unsubscribe' , ( ) => {
655
+ function foo ( state = 0 , action ) {
656
+ return action . type === 'foo' ? 1 : state
657
+ }
658
+
659
+ function bar ( state = 0 , action ) {
660
+ return action . type === 'bar' ? 2 : state
661
+ }
662
+
663
+ const store = createStore ( combineReducers ( { foo, bar } ) )
664
+ const observable = store [ $$observable ] ( )
665
+ const results = [ ]
666
+
667
+ observable . subscribe ( {
668
+ next ( state ) {
669
+ results . push ( state )
670
+ }
671
+ } )
672
+
673
+ store . dispatch ( { type : 'foo' } )
674
+ store . dispatch ( { type : 'bar' } )
675
+
676
+ expect ( results ) . toEqual ( [ { foo : 0 , bar : 0 } , { foo : 1 , bar : 0 } , { foo : 1 , bar : 2 } ] )
677
+ } )
678
+
679
+ it ( 'should pass an integration test with an unsubscribe' , ( ) => {
680
+ function foo ( state = 0 , action ) {
681
+ return action . type === 'foo' ? 1 : state
682
+ }
683
+
684
+ function bar ( state = 0 , action ) {
685
+ return action . type === 'bar' ? 2 : state
686
+ }
687
+
688
+ const store = createStore ( combineReducers ( { foo, bar } ) )
689
+ const observable = store [ $$observable ] ( )
690
+ const results = [ ]
691
+
692
+ const sub = observable . subscribe ( {
693
+ next ( state ) {
694
+ results . push ( state )
695
+ }
696
+ } )
697
+
698
+ store . dispatch ( { type : 'foo' } )
699
+ sub . unsubscribe ( )
700
+ store . dispatch ( { type : 'bar' } )
701
+
702
+ expect ( results ) . toEqual ( [ { foo : 0 , bar : 0 } , { foo : 1 , bar : 0 } ] )
703
+ } )
704
+
705
+ it ( 'should pass an integration test with a common library (RxJS)' , ( ) => {
706
+ function foo ( state = 0 , action ) {
707
+ return action . type === 'foo' ? 1 : state
708
+ }
709
+
710
+ function bar ( state = 0 , action ) {
711
+ return action . type === 'bar' ? 2 : state
712
+ }
713
+
714
+ const store = createStore ( combineReducers ( { foo, bar } ) )
715
+ const observable = Rx . Observable . from ( store )
716
+ const results = [ ]
717
+
718
+ const sub = observable
719
+ . map ( state => ( { fromRx : true , ...state } ) )
720
+ . subscribe ( state => results . push ( state ) )
721
+
722
+ store . dispatch ( { type : 'foo' } )
723
+ sub . unsubscribe ( )
724
+ store . dispatch ( { type : 'bar' } )
725
+
726
+ expect ( results ) . toEqual ( [ { foo : 0 , bar : 0 , fromRx : true } , { foo : 1 , bar : 0 , fromRx : true } ] )
727
+ } )
728
+ } )
613
729
} )
0 commit comments