@@ -610,4 +610,136 @@ describe('Store Devtools', () => {
610
610
expect ( fixture . getLiftedState ( ) ) . toEqual ( exportedState ) ;
611
611
} ) ;
612
612
} ) ;
613
+
614
+ describe ( 'Action and State Sanitizer' , ( ) => {
615
+ let fixture : Fixture < number > ;
616
+
617
+ const SANITIZED_TOKEN = 'SANITIZED_ACTION' ;
618
+ const SANITIZED_COUNTER = 42 ;
619
+ const testActionSanitizer = ( action : Action , id : number ) => {
620
+ return { type : SANITIZED_TOKEN } ;
621
+ } ;
622
+ const incrementActionSanitizer = ( action : Action , id : number ) => {
623
+ return { type : 'INCREMENT' } ;
624
+ } ;
625
+ const testStateSanitizer = ( state : any , index : number ) => {
626
+ return { state : SANITIZED_COUNTER } ;
627
+ } ;
628
+
629
+ afterEach ( ( ) => {
630
+ fixture . cleanup ( ) ;
631
+ } ) ;
632
+
633
+ it ( 'should function normally with no sanitizers' , ( ) => {
634
+ fixture = createStore ( counter ) ;
635
+
636
+ fixture . store . dispatch ( { type : 'INCREMENT' } ) ;
637
+
638
+ const liftedState = fixture . getLiftedState ( ) ;
639
+ const currentLiftedState =
640
+ liftedState . computedStates [ liftedState . currentStateIndex ] ;
641
+ expect ( Object . keys ( liftedState . actionsById ) . length ) . toBe (
642
+ Object . keys ( liftedState . sanitizedActionsById ) . length
643
+ ) ;
644
+ expect ( liftedState . actionsById ) . toEqual ( liftedState . sanitizedActionsById ) ;
645
+ expect ( currentLiftedState . state ) . toEqual ( { state : 1 } ) ;
646
+ expect ( currentLiftedState . sanitizedState ) . toBeUndefined ( ) ;
647
+ } ) ;
648
+
649
+ it ( 'should run the action sanitizer on actions' , ( ) => {
650
+ fixture = createStore ( counter , {
651
+ actionSanitizer : testActionSanitizer ,
652
+ } ) ;
653
+
654
+ fixture . store . dispatch ( { type : 'INCREMENT' } ) ;
655
+ fixture . store . dispatch ( { type : 'DECREMENT' } ) ;
656
+
657
+ const liftedState = fixture . getLiftedState ( ) ;
658
+ const sanitizedAction =
659
+ liftedState . sanitizedActionsById [ liftedState . nextActionId - 1 ] ;
660
+ const sanitizedAction2 =
661
+ liftedState . sanitizedActionsById [ liftedState . nextActionId - 2 ] ;
662
+ const action = liftedState . actionsById [ liftedState . nextActionId - 1 ] ;
663
+ const action2 = liftedState . actionsById [ liftedState . nextActionId - 2 ] ;
664
+
665
+ expect ( liftedState . actionsById ) . not . toEqual (
666
+ liftedState . sanitizedActionsById
667
+ ) ;
668
+ expect ( sanitizedAction . action ) . toEqual ( { type : SANITIZED_TOKEN } ) ;
669
+ expect ( sanitizedAction2 . action ) . toEqual ( { type : SANITIZED_TOKEN } ) ;
670
+ expect ( action . action ) . toEqual ( { type : 'DECREMENT' } ) ;
671
+ expect ( action2 . action ) . toEqual ( { type : 'INCREMENT' } ) ;
672
+ } ) ;
673
+
674
+ it ( 'should run the state sanitizer on store state' , ( ) => {
675
+ fixture = createStore ( counter , {
676
+ stateSanitizer : testStateSanitizer ,
677
+ } ) ;
678
+
679
+ let liftedState = fixture . getLiftedState ( ) ;
680
+ let currentLiftedState =
681
+ liftedState . computedStates [ liftedState . currentStateIndex ] ;
682
+ expect ( fixture . getState ( ) ) . toBe ( 0 ) ;
683
+ expect ( currentLiftedState . state ) . toEqual ( { state : 0 } ) ;
684
+ expect ( currentLiftedState . sanitizedState ) . toBeDefined ( ) ;
685
+ expect ( currentLiftedState . sanitizedState ) . toEqual ( {
686
+ state : SANITIZED_COUNTER ,
687
+ } ) ;
688
+
689
+ fixture . store . dispatch ( { type : 'INCREMENT' } ) ;
690
+
691
+ liftedState = fixture . getLiftedState ( ) ;
692
+ currentLiftedState =
693
+ liftedState . computedStates [ liftedState . currentStateIndex ] ;
694
+ expect ( fixture . getState ( ) ) . toBe ( 1 ) ;
695
+ expect ( currentLiftedState . state ) . toEqual ( { state : 1 } ) ;
696
+ expect ( currentLiftedState . sanitizedState ) . toEqual ( {
697
+ state : SANITIZED_COUNTER ,
698
+ } ) ;
699
+ } ) ;
700
+
701
+ it ( 'should run transparently to produce a new lifted store state' , ( ) => {
702
+ const devtoolsOptions : Partial < StoreDevtoolsConfig > = {
703
+ actionSanitizer : testActionSanitizer ,
704
+ stateSanitizer : testStateSanitizer ,
705
+ } ;
706
+ fixture = createStore ( counter , devtoolsOptions ) ;
707
+
708
+ fixture . store . dispatch ( { type : 'INCREMENT' } ) ;
709
+
710
+ const liftedState = fixture . getLiftedState ( ) ;
711
+ const sanitizedLiftedState = fixture . devtools . getSanitizedState (
712
+ liftedState ,
713
+ devtoolsOptions . stateSanitizer
714
+ ) ;
715
+ const originalAction =
716
+ liftedState . actionsById [ liftedState . nextActionId - 1 ] ;
717
+ const originalState =
718
+ liftedState . computedStates [ liftedState . currentStateIndex ] ;
719
+ const sanitizedAction =
720
+ sanitizedLiftedState . actionsById [ liftedState . nextActionId - 1 ] ;
721
+ const sanitizedState =
722
+ sanitizedLiftedState . computedStates [ liftedState . currentStateIndex ] ;
723
+
724
+ expect ( originalAction . action ) . toEqual ( { type : 'INCREMENT' } ) ;
725
+ expect ( originalState . state ) . toEqual ( { state : 1 } ) ;
726
+ expect ( sanitizedAction . action ) . toEqual ( { type : SANITIZED_TOKEN } ) ;
727
+ expect ( sanitizedState . state ) . toEqual ( { state : SANITIZED_COUNTER } ) ;
728
+ } ) ;
729
+
730
+ it ( 'sanitized actions should not affect the store state' , ( ) => {
731
+ fixture = createStore ( counter , {
732
+ actionSanitizer : incrementActionSanitizer ,
733
+ } ) ;
734
+
735
+ fixture . store . dispatch ( { type : 'DECREMENT' } ) ;
736
+ fixture . store . dispatch ( { type : 'DECREMENT' } ) ;
737
+
738
+ const liftedState = fixture . getLiftedState ( ) ;
739
+ expect ( fixture . getState ( ) ) . toBe ( - 2 ) ;
740
+ expect (
741
+ liftedState . computedStates [ liftedState . currentStateIndex ] . state
742
+ ) . toEqual ( { state : - 2 } ) ;
743
+ } ) ;
744
+ } ) ;
613
745
} ) ;
0 commit comments