1
- import { renderHook , act } from '@testing-library/react-hooks ' ;
1
+ import { renderHook , act , waitFor } from '@testing-library/react' ;
2
2
import { useStationLogin } from '../src/helper' ;
3
3
4
4
// Mock webex instance
@@ -18,6 +18,10 @@ const loginCb = jest.fn();
18
18
const logoutCb = jest . fn ( ) ;
19
19
20
20
describe ( 'useStationLogin Hook' , ( ) => {
21
+
22
+ afterEach ( ( ) => {
23
+ jest . clearAllMocks ( ) ;
24
+ } ) ;
21
25
22
26
it ( 'should set loginSuccess on successful login' , async ( ) => {
23
27
const successResponse = {
@@ -45,7 +49,7 @@ describe('useStationLogin Hook', () => {
45
49
46
50
ccMock . stationLogin . mockResolvedValue ( successResponse ) ;
47
51
48
- const { result, waitForNextUpdate } = renderHook ( ( ) =>
52
+ const { result } = renderHook ( ( ) =>
49
53
useStationLogin ( { cc : ccMock , onLogin : loginCb , onLogout : logoutCb } )
50
54
) ;
51
55
@@ -57,25 +61,42 @@ describe('useStationLogin Hook', () => {
57
61
result . current . login ( ) ;
58
62
} ) ;
59
63
60
- await waitForNextUpdate ( ) ;
61
-
62
- expect ( ccMock . stationLogin ) . toHaveBeenCalledWith ( {
63
- teamId : loginParams . teamId ,
64
- loginOption : loginParams . loginOption ,
65
- dialNumber : loginParams . dialNumber ,
64
+ waitFor ( ( ) => {
65
+ expect ( ccMock . stationLogin ) . toHaveBeenCalledWith ( {
66
+ teamId : loginParams . teamId ,
67
+ loginOption : loginParams . loginOption ,
68
+ dialNumber : loginParams . dialNumber ,
69
+ } ) ;
70
+ expect ( loginCb ) . toHaveBeenCalledWith ( ) ;
71
+
72
+ expect ( result . current ) . toEqual ( {
73
+ name : 'StationLogin' ,
74
+ setDeviceType : expect . any ( Function ) ,
75
+ setDialNumber : expect . any ( Function ) ,
76
+ setTeam : expect . any ( Function ) ,
77
+ login : expect . any ( Function ) ,
78
+ logout : expect . any ( Function ) ,
79
+ loginSuccess : successResponse ,
80
+ loginFailure : undefined ,
81
+ logoutSuccess : undefined
82
+ } ) ;
66
83
} ) ;
67
- expect ( loginCb ) . toHaveBeenCalledWith ( ) ;
68
-
69
- expect ( result . current ) . toEqual ( {
70
- name : 'StationLogin' ,
71
- setDeviceType : expect . any ( Function ) ,
72
- setDialNumber : expect . any ( Function ) ,
73
- setTeam : expect . any ( Function ) ,
74
- login : expect . any ( Function ) ,
75
- logout : expect . any ( Function ) ,
76
- loginSuccess : successResponse ,
77
- loginFailure : undefined ,
78
- logoutSuccess : undefined
84
+ } ) ;
85
+
86
+ it ( 'should not call login callback if not present' , async ( ) => {
87
+
88
+ ccMock . stationLogin . mockResolvedValue ( { } ) ;
89
+
90
+ const { result } = renderHook ( ( ) =>
91
+ useStationLogin ( { cc : ccMock , onLogout : logoutCb } )
92
+ ) ;
93
+
94
+ act ( ( ) => {
95
+ result . current . login ( ) ;
96
+ } ) ;
97
+
98
+ waitFor ( ( ) => {
99
+ expect ( loginCb ) . not . toHaveBeenCalled ( ) ;
79
100
} ) ;
80
101
} ) ;
81
102
@@ -84,7 +105,7 @@ describe('useStationLogin Hook', () => {
84
105
ccMock . stationLogin . mockRejectedValue ( errorResponse ) ;
85
106
86
107
loginCb . mockClear ( ) ;
87
- const { result, waitForNextUpdate } = renderHook ( ( ) =>
108
+ const { result } = renderHook ( ( ) =>
88
109
useStationLogin ( { cc : ccMock , onLogin : loginCb , onLogout : logoutCb } )
89
110
) ;
90
111
@@ -96,26 +117,26 @@ describe('useStationLogin Hook', () => {
96
117
result . current . login ( ) ;
97
118
} ) ;
98
119
99
- await waitForNextUpdate ( ) ;
100
-
101
- expect ( ccMock . stationLogin ) . toHaveBeenCalledWith ( {
102
- teamId : loginParams . teamId ,
103
- loginOption : loginParams . loginOption ,
104
- dialNumber : loginParams . dialNumber ,
105
- } ) ;
106
-
107
- expect ( loginCb ) . not . toHaveBeenCalledWith ( ) ;
108
-
109
- expect ( result . current ) . toEqual ( {
110
- name : 'StationLogin' ,
111
- setDeviceType : expect . any ( Function ) ,
112
- setDialNumber : expect . any ( Function ) ,
113
- setTeam : expect . any ( Function ) ,
114
- login : expect . any ( Function ) ,
115
- logout : expect . any ( Function ) ,
116
- loginSuccess : undefined ,
117
- loginFailure : errorResponse ,
118
- logoutSuccess : undefined
120
+ waitFor ( ( ) => {
121
+ expect ( ccMock . stationLogin ) . toHaveBeenCalledWith ( {
122
+ teamId : loginParams . teamId ,
123
+ loginOption : loginParams . loginOption ,
124
+ dialNumber : loginParams . dialNumber ,
125
+ } ) ;
126
+
127
+ expect ( loginCb ) . not . toHaveBeenCalledWith ( ) ;
128
+
129
+ expect ( result . current ) . toEqual ( {
130
+ name : 'StationLogin' ,
131
+ setDeviceType : expect . any ( Function ) ,
132
+ setDialNumber : expect . any ( Function ) ,
133
+ setTeam : expect . any ( Function ) ,
134
+ login : expect . any ( Function ) ,
135
+ logout : expect . any ( Function ) ,
136
+ loginSuccess : undefined ,
137
+ loginFailure : errorResponse ,
138
+ logoutSuccess : undefined
139
+ } ) ;
119
140
} ) ;
120
141
} ) ;
121
142
@@ -137,30 +158,46 @@ describe('useStationLogin Hook', () => {
137
158
138
159
ccMock . stationLogout . mockResolvedValue ( successResponse ) ;
139
160
140
- const { result, waitForNextUpdate } = renderHook ( ( ) =>
161
+ const { result} = renderHook ( ( ) =>
141
162
useStationLogin ( { cc : ccMock , onLogin : loginCb , onLogout : logoutCb } )
142
163
) ;
143
164
144
165
act ( ( ) => {
145
166
result . current . logout ( ) ;
146
167
} ) ;
147
168
148
- await waitForNextUpdate ( ) ;
169
+ waitFor ( ( ) => {
170
+ expect ( ccMock . stationLogout ) . toHaveBeenCalledWith ( { logoutReason : 'User requested logout' } ) ;
171
+ expect ( logoutCb ) . toHaveBeenCalledWith ( ) ;
172
+
173
+
174
+ expect ( result . current ) . toEqual ( {
175
+ name : 'StationLogin' ,
176
+ setDeviceType : expect . any ( Function ) ,
177
+ setDialNumber : expect . any ( Function ) ,
178
+ setTeam : expect . any ( Function ) ,
179
+ login : expect . any ( Function ) ,
180
+ logout : expect . any ( Function ) ,
181
+ loginSuccess : undefined ,
182
+ loginFailure : undefined ,
183
+ logoutSuccess : successResponse
184
+ } ) ;
185
+ } ) ;
186
+ } ) ;
149
187
150
- expect ( ccMock . stationLogout ) . toHaveBeenCalledWith ( { logoutReason : 'User requested logout' } ) ;
151
- expect ( logoutCb ) . toHaveBeenCalledWith ( ) ;
188
+ it ( 'should not call logout callback if not present' , async ( ) => {
189
+ ccMock . stationLogout . mockResolvedValue ( { } ) ;
152
190
191
+ const { result} = renderHook ( ( ) =>
192
+ useStationLogin ( { cc : ccMock , onLogin : loginCb } )
193
+ ) ;
194
+
195
+ act ( ( ) => {
196
+ result . current . logout ( ) ;
197
+ } ) ;
153
198
154
- expect ( result . current ) . toEqual ( {
155
- name : 'StationLogin' ,
156
- setDeviceType : expect . any ( Function ) ,
157
- setDialNumber : expect . any ( Function ) ,
158
- setTeam : expect . any ( Function ) ,
159
- login : expect . any ( Function ) ,
160
- logout : expect . any ( Function ) ,
161
- loginSuccess : undefined ,
162
- loginFailure : undefined ,
163
- logoutSuccess : successResponse
199
+ waitFor ( ( ) => {
200
+ expect ( logoutCb ) . not . toHaveBeenCalled ( ) ;
164
201
} ) ;
165
202
} ) ;
166
203
} )
0 commit comments