1
- import useContextDevTools from './index' ;
2
-
3
1
const devInit = jest . fn ( ) ;
4
2
const devSend = jest . fn ( ) ;
5
3
const devDisconnect = jest . fn ( ) ;
6
4
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
+
7
26
const devToolsMock = {
8
27
connect : jest . fn ( ( ) => ( {
9
28
init : devInit ,
10
29
send : devSend ,
11
30
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 ) ,
12
50
} ) ) ,
13
51
} ;
14
52
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
+ } ) ;
19
96
20
97
test ( 'Should initialize devTools' , ( ) => {
21
98
dispatchContext . sendDispatch ( { } ) ;
@@ -29,9 +106,61 @@ describe('Dev Tools Extension', () => {
29
106
expect ( devSend ) . toHaveBeenCalled ( ) ;
30
107
} ) ;
31
108
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' ) ) ;
34
116
expect ( devDisconnect ) . toHaveBeenCalled ( ) ;
35
117
} ) ;
36
118
} )
37
119
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