1
- import { createStore , combineReducers } from '../'
1
+ import {
2
+ createStore ,
3
+ combineReducers ,
4
+ Reducer ,
5
+ StoreEnhancer ,
6
+ Action ,
7
+ Store
8
+ } from '..'
2
9
import {
3
10
addTodo ,
4
11
dispatchInMiddle ,
@@ -9,7 +16,7 @@ import {
9
16
unknownAction
10
17
} from './helpers/actionCreators'
11
18
import * as reducers from './helpers/reducers'
12
- import { from } from 'rxjs'
19
+ import { from , ObservableInput } from 'rxjs'
13
20
import { map } from 'rxjs/operators'
14
21
import $$observable from 'symbol-observable'
15
22
@@ -26,11 +33,11 @@ describe('createStore', () => {
26
33
} )
27
34
28
35
it ( 'throws if reducer is not a function' , ( ) => {
29
- expect ( ( ) => createStore ( ) ) . toThrow ( )
36
+ expect ( ( ) => createStore ( undefined ) ) . toThrow ( )
30
37
31
- expect ( ( ) => createStore ( 'test' ) ) . toThrow ( )
38
+ expect ( ( ) => createStore ( ( 'test' as unknown ) as Reducer ) ) . toThrow ( )
32
39
33
- expect ( ( ) => createStore ( { } ) ) . toThrow ( )
40
+ expect ( ( ) => createStore ( ( { } as unknown ) as Reducer ) ) . toThrow ( )
34
41
35
42
expect ( ( ) => createStore ( ( ) => { } ) ) . not . toThrow ( )
36
43
} )
@@ -568,13 +575,23 @@ describe('createStore', () => {
568
575
} )
569
576
570
577
it ( 'throws if enhancer is neither undefined nor a function' , ( ) => {
571
- expect ( ( ) => createStore ( reducers . todos , undefined , { } ) ) . toThrow ( )
578
+ expect ( ( ) =>
579
+ createStore ( reducers . todos , undefined , ( { } as unknown ) as StoreEnhancer )
580
+ ) . toThrow ( )
572
581
573
- expect ( ( ) => createStore ( reducers . todos , undefined , [ ] ) ) . toThrow ( )
582
+ expect ( ( ) =>
583
+ createStore ( reducers . todos , undefined , ( [ ] as unknown ) as StoreEnhancer )
584
+ ) . toThrow ( )
574
585
575
586
expect ( ( ) => createStore ( reducers . todos , undefined , null ) ) . toThrow ( )
576
587
577
- expect ( ( ) => createStore ( reducers . todos , undefined , false ) ) . toThrow ( )
588
+ expect ( ( ) =>
589
+ createStore (
590
+ reducers . todos ,
591
+ undefined ,
592
+ ( false as unknown ) as StoreEnhancer
593
+ )
594
+ ) . toThrow ( )
578
595
579
596
expect ( ( ) =>
580
597
createStore ( reducers . todos , undefined , undefined )
@@ -586,25 +603,27 @@ describe('createStore', () => {
586
603
587
604
expect ( ( ) => createStore ( reducers . todos , [ ] ) ) . not . toThrow ( )
588
605
589
- expect ( ( ) => createStore ( reducers . todos , { } ) ) . not . toThrow ( )
606
+ expect ( ( ) =>
607
+ createStore < { } , Action , { } , { } > ( reducers . todos , { } )
608
+ ) . not . toThrow ( )
590
609
} )
591
610
592
611
it ( 'throws if nextReducer is not a function' , ( ) => {
593
612
const store = createStore ( reducers . todos )
594
613
595
- expect ( ( ) => store . replaceReducer ( ) ) . toThrow (
614
+ expect ( ( ) => store . replaceReducer ( undefined ) ) . toThrow (
596
615
'Expected the nextReducer to be a function.'
597
616
)
598
617
599
- expect ( ( ) => store . replaceReducer ( ( ) => { } ) ) . not . toThrow ( )
618
+ expect ( ( ) => store . replaceReducer ( ( ) => [ ] ) ) . not . toThrow ( )
600
619
} )
601
620
602
621
it ( 'throws if listener is not a function' , ( ) => {
603
622
const store = createStore ( reducers . todos )
604
623
605
- expect ( ( ) => store . subscribe ( ) ) . toThrow ( )
624
+ expect ( ( ) => store . subscribe ( undefined ) ) . toThrow ( )
606
625
607
- expect ( ( ) => store . subscribe ( '' ) ) . toThrow ( )
626
+ expect ( ( ) => store . subscribe ( ( '' as unknown ) as ( ) => void ) ) . toThrow ( )
608
627
609
628
expect ( ( ) => store . subscribe ( null ) ) . toThrow ( )
610
629
@@ -654,11 +673,11 @@ describe('createStore', () => {
654
673
} )
655
674
656
675
it ( 'should pass an integration test with no unsubscribe' , ( ) => {
657
- function foo ( state = 0 , action ) {
676
+ function foo ( state = 0 , action : Action ) {
658
677
return action . type === 'foo' ? 1 : state
659
678
}
660
679
661
- function bar ( state = 0 , action ) {
680
+ function bar ( state = 0 , action : Action ) {
662
681
return action . type === 'bar' ? 2 : state
663
682
}
664
683
@@ -667,7 +686,7 @@ describe('createStore', () => {
667
686
const results = [ ]
668
687
669
688
observable . subscribe ( {
670
- next ( state ) {
689
+ next ( state : any ) {
671
690
results . push ( state )
672
691
}
673
692
} )
@@ -683,11 +702,11 @@ describe('createStore', () => {
683
702
} )
684
703
685
704
it ( 'should pass an integration test with an unsubscribe' , ( ) => {
686
- function foo ( state = 0 , action ) {
705
+ function foo ( state = 0 , action : Action ) {
687
706
return action . type === 'foo' ? 1 : state
688
707
}
689
708
690
- function bar ( state = 0 , action ) {
709
+ function bar ( state = 0 , action : Action ) {
691
710
return action . type === 'bar' ? 2 : state
692
711
}
693
712
@@ -696,7 +715,7 @@ describe('createStore', () => {
696
715
const results = [ ]
697
716
698
717
const sub = observable . subscribe ( {
699
- next ( state ) {
718
+ next ( state : any ) {
700
719
results . push ( state )
701
720
}
702
721
} )
@@ -709,25 +728,26 @@ describe('createStore', () => {
709
728
} )
710
729
711
730
it ( 'should pass an integration test with a common library (RxJS)' , ( ) => {
712
- function foo ( state = 0 , action ) {
731
+ function foo ( state = 0 , action : Action ) {
713
732
return action . type === 'foo' ? 1 : state
714
733
}
715
734
716
- function bar ( state = 0 , action ) {
735
+ function bar ( state = 0 , action : Action ) {
717
736
return action . type === 'bar' ? 2 : state
718
737
}
719
738
720
- const store = createStore ( combineReducers ( { foo, bar } ) )
739
+ const store : ObservableInput < { foo : number ; bar : number } > = createStore (
740
+ combineReducers ( { foo, bar } )
741
+ )
721
742
const observable = from ( store )
722
743
const results = [ ]
723
744
724
745
const sub = observable
725
746
. pipe ( map ( state => ( { fromRx : true , ...state } ) ) )
726
747
. subscribe ( state => results . push ( state ) )
727
-
728
- store . dispatch ( { type : 'foo' } )
748
+ ; ( store as Store ) . dispatch ( { type : 'foo' } )
729
749
sub . unsubscribe ( )
730
- store . dispatch ( { type : 'bar' } )
750
+ ; ( store as Store ) . dispatch ( { type : 'bar' } )
731
751
732
752
expect ( results ) . toEqual ( [
733
753
{ foo : 0 , bar : 0 , fromRx : true } ,
@@ -758,23 +778,30 @@ describe('createStore', () => {
758
778
} )
759
779
)
760
780
761
- expect ( console . error . mock . calls . length ) . toBe ( 0 )
781
+ expect ( ( console . error as any ) . mock . calls . length ) . toBe ( 0 )
762
782
console . error = originalConsoleError
763
783
} )
764
784
765
785
it ( 'throws if passing several enhancer functions without preloaded state' , ( ) => {
766
786
const rootReducer = combineReducers ( reducers )
767
787
const dummyEnhancer = f => f
768
788
expect ( ( ) =>
769
- createStore ( rootReducer , dummyEnhancer , dummyEnhancer )
789
+ // use a fake pre-loaded state to get a valid createStore signature
790
+ createStore ( rootReducer , ( dummyEnhancer as unknown ) as { } , dummyEnhancer )
770
791
) . toThrow ( )
771
792
} )
772
793
773
794
it ( 'throws if passing several enhancer functions with preloaded state' , ( ) => {
774
795
const rootReducer = combineReducers ( reducers )
775
- const dummyEnhancer = f => f
796
+ const dummyEnhancer = ( f : any ) => f
776
797
expect ( ( ) =>
777
- createStore ( rootReducer , { todos : [ ] } , dummyEnhancer , dummyEnhancer )
798
+ // work around the type so we can call this poorly
799
+ ( createStore as any ) (
800
+ rootReducer ,
801
+ { todos : [ ] } ,
802
+ dummyEnhancer ,
803
+ dummyEnhancer
804
+ )
778
805
) . toThrow ( )
779
806
} )
780
807
} )
0 commit comments