1- import useContextDevTools from './index' ;
2-
31const devInit = jest . fn ( ) ;
42const devSend = jest . fn ( ) ;
53const devDisconnect = jest . fn ( ) ;
64
5+ const jumpToStateEvent = {
6+ payload : {
7+ type : 'JUMP_TO_STATE' ,
8+ } ,
9+ state : '{ "root": [] }'
10+ } ;
11+
12+ const jumpToActionEvent = {
13+ payload : {
14+ type : 'JUMP_TO_ACTION' ,
15+ } ,
16+ state : '{ "root": [] }'
17+ } ;
18+
19+ const importEvent = {
20+ type : 'DISPATCH' ,
21+ payload : {
22+ nextLiftedState : require ( './mocks.json' ) ,
23+ } ,
24+ } ;
25+
726const devToolsMock = {
827 connect : jest . fn ( ( ) => ( {
928 init : devInit ,
1029 send : devSend ,
1130 disconnect : devDisconnect ,
31+ subscribe : ( callback ) => callback ( jumpToStateEvent ) ,
32+ } ) ) ,
33+ }
34+
35+ const devToolsMock1 = {
36+ connect : jest . fn ( ( ) => ( {
37+ init : devInit ,
38+ send : devSend ,
39+ disconnect : devDisconnect ,
40+ subscribe : ( callback ) => callback ( jumpToActionEvent ) ,
41+ } ) ) ,
42+ } ;
43+
44+ const devToolsMock2 = {
45+ connect : jest . fn ( ( ) => ( {
46+ init : devInit ,
47+ send : devSend ,
48+ disconnect : devDisconnect ,
49+ subscribe : ( callback ) => callback ( importEvent ) ,
1250 } ) ) ,
1351} ;
1452
15- describe ( 'Dev Tools Extension' , ( ) => {
16- ( window as any ) . __REDUX_DEVTOOLS_EXTENSION__ = devToolsMock ;
17- const dispatchFn = jest . fn ( ) ;
18- const dispatchContext = useContextDevTools ( dispatchFn ) ;
53+ describe ( 'Dev Tools Extension (Without Extension)' , ( ) => {
54+ let dispatchFn = { } ;
55+ let dispatchContext : any = { } ;
56+
57+ beforeAll ( ( ) => {
58+ const useContextDevTools = require ( './index' ) . default ;
59+ dispatchFn = jest . fn ( ) ;
60+ dispatchContext = useContextDevTools ( dispatchFn ) ;
61+ } ) ;
62+
63+ test ( 'Should initialize devTools' , ( ) => {
64+ dispatchContext . sendDispatch ( { } ) ;
65+ expect ( dispatchFn ) . toHaveBeenCalledWith ( { } ) ;
66+ } ) ;
67+
68+ test ( 'Should be able to send updated state' , ( ) => {
69+ dispatchContext . sendUpdatedState ( { } ) ;
70+ expect ( devInit ) . not . toHaveBeenCalled ( ) ;
71+ dispatchContext . sendUpdatedState ( { } ) ;
72+ expect ( devSend ) . not . toHaveBeenCalled ( ) ;
73+ } ) ;
74+
75+ test ( 'Should be able to subscribe to new events (Jump To State)' , ( ) => {
76+ devToolsMock . connect ( ) . subscribe ( ( ) => jumpToStateEvent ) ;
77+ expect ( devSend ) . not . toHaveBeenCalled ( ) ;
78+ } ) ;
79+
80+ test ( 'Should be able to unsubscribe on tab close' , ( ) => {
81+ window . dispatchEvent ( new Event ( 'beforeunload' ) ) ;
82+ expect ( devDisconnect ) . not . toHaveBeenCalled ( ) ;
83+ } ) ;
84+ } )
85+
86+ describe ( 'Dev Tools Extension (JumpToState Event)' , ( ) => {
87+ let dispatchFn = { } ;
88+ let dispatchContext : any = { } ;
89+
90+ beforeAll ( ( ) => {
91+ ( window as any ) . __REDUX_DEVTOOLS_EXTENSION__ = devToolsMock ;
92+ const useContextDevTools = require ( './index' ) . default ;
93+ dispatchFn = jest . fn ( ) ;
94+ dispatchContext = useContextDevTools ( dispatchFn ) ;
95+ } ) ;
1996
2097 test ( 'Should initialize devTools' , ( ) => {
2198 dispatchContext . sendDispatch ( { } ) ;
@@ -29,9 +106,61 @@ describe('Dev Tools Extension', () => {
29106 expect ( devSend ) . toHaveBeenCalled ( ) ;
30107 } ) ;
31108
32- test ( 'Should be able to disconnect' , ( ) => {
33- dispatchContext . disconnectDevTools ( ) ;
109+ test ( 'Should be able to subscribe to new events (Jump To State)' , ( ) => {
110+ devToolsMock . connect ( ) . subscribe ( ( ) => jumpToStateEvent ) ;
111+ expect ( devSend ) . toHaveBeenCalled ( ) ;
112+ } ) ;
113+
114+ test ( 'Should be able to unsubscribe on tab close' , ( ) => {
115+ window . dispatchEvent ( new Event ( 'beforeunload' ) ) ;
34116 expect ( devDisconnect ) . toHaveBeenCalled ( ) ;
35117 } ) ;
36118} )
37119
120+
121+ describe ( 'Dev Tools Extension (Jump to Action Event)' , ( ) => {
122+ let dispatchFn = { } ;
123+ let dispatchContext : any = { } ;
124+
125+ beforeAll ( ( ) => {
126+ ( window as any ) . __REDUX_DEVTOOLS_EXTENSION__ = devToolsMock1 ;
127+ const useContextDevTools = require ( './index' ) . default ;
128+ dispatchFn = jest . fn ( ) ;
129+ dispatchContext = useContextDevTools ( dispatchFn ) ;
130+ } ) ;
131+
132+ test ( 'Should initialize devTools' , ( ) => {
133+ dispatchContext . sendDispatch ( { } ) ;
134+ expect ( dispatchFn ) . toHaveBeenCalledWith ( { } ) ;
135+ } ) ;
136+
137+ test ( 'Should be able to subscribe to new events (Jump To Action)' , ( ) => {
138+ devToolsMock1 . connect ( ) . subscribe ( ( ) => jumpToActionEvent ) ;
139+ window . dispatchEvent ( new Event ( 'beforeunload' ) ) ;
140+ expect ( devSend ) . toHaveBeenCalled ( ) ;
141+ } ) ;
142+ } )
143+
144+ describe ( 'Dev Tools Extension (Import Event)' , ( ) => {
145+ let dispatchFn = { } ;
146+ let dispatchContext : any = { } ;
147+
148+ beforeAll ( ( ) => {
149+ ( window as any ) . __REDUX_DEVTOOLS_EXTENSION__ = devToolsMock2 ;
150+ const useContextDevTools = require ( './index' ) . default ;
151+ dispatchFn = jest . fn ( ) ;
152+ dispatchContext = useContextDevTools ( dispatchFn ) ;
153+ } ) ;
154+
155+ test ( 'Should initialize devTools' , ( ) => {
156+ dispatchContext . sendDispatch ( { } ) ;
157+ expect ( dispatchFn ) . toHaveBeenCalledWith ( { } ) ;
158+ } ) ;
159+
160+ test ( 'Should be able to subscribe to new events (Import)' , ( ) => {
161+ devToolsMock2 . connect ( ) . subscribe ( ( ) => importEvent ) ;
162+ window . dispatchEvent ( new Event ( 'beforeunload' ) ) ;
163+ expect ( devSend ) . toHaveBeenCalled ( ) ;
164+ } ) ;
165+
166+ } )
0 commit comments