1- import { renderHook , act } from '@testing-library/react-hooks ' ;
1+ import { renderHook , act , waitFor } from '@testing-library/react' ;
22import { useStationLogin } from '../src/helper' ;
33
44// Mock webex instance
@@ -18,6 +18,10 @@ const loginCb = jest.fn();
1818const logoutCb = jest . fn ( ) ;
1919
2020describe ( 'useStationLogin Hook' , ( ) => {
21+
22+ afterEach ( ( ) => {
23+ jest . clearAllMocks ( ) ;
24+ } ) ;
2125
2226 it ( 'should set loginSuccess on successful login' , async ( ) => {
2327 const successResponse = {
@@ -45,7 +49,7 @@ describe('useStationLogin Hook', () => {
4549
4650 ccMock . stationLogin . mockResolvedValue ( successResponse ) ;
4751
48- const { result, waitForNextUpdate } = renderHook ( ( ) =>
52+ const { result } = renderHook ( ( ) =>
4953 useStationLogin ( { cc : ccMock , onLogin : loginCb , onLogout : logoutCb } )
5054 ) ;
5155
@@ -57,25 +61,42 @@ describe('useStationLogin Hook', () => {
5761 result . current . login ( ) ;
5862 } ) ;
5963
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+ } ) ;
6683 } ) ;
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 ( ) ;
79100 } ) ;
80101 } ) ;
81102
@@ -84,7 +105,7 @@ describe('useStationLogin Hook', () => {
84105 ccMock . stationLogin . mockRejectedValue ( errorResponse ) ;
85106
86107 loginCb . mockClear ( ) ;
87- const { result, waitForNextUpdate } = renderHook ( ( ) =>
108+ const { result } = renderHook ( ( ) =>
88109 useStationLogin ( { cc : ccMock , onLogin : loginCb , onLogout : logoutCb } )
89110 ) ;
90111
@@ -96,26 +117,26 @@ describe('useStationLogin Hook', () => {
96117 result . current . login ( ) ;
97118 } ) ;
98119
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+ } ) ;
119140 } ) ;
120141 } ) ;
121142
@@ -137,30 +158,46 @@ describe('useStationLogin Hook', () => {
137158
138159 ccMock . stationLogout . mockResolvedValue ( successResponse ) ;
139160
140- const { result, waitForNextUpdate } = renderHook ( ( ) =>
161+ const { result} = renderHook ( ( ) =>
141162 useStationLogin ( { cc : ccMock , onLogin : loginCb , onLogout : logoutCb } )
142163 ) ;
143164
144165 act ( ( ) => {
145166 result . current . logout ( ) ;
146167 } ) ;
147168
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+ } ) ;
149187
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 ( { } ) ;
152190
191+ const { result} = renderHook ( ( ) =>
192+ useStationLogin ( { cc : ccMock , onLogin : loginCb } )
193+ ) ;
194+
195+ act ( ( ) => {
196+ result . current . logout ( ) ;
197+ } ) ;
153198
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 ( ) ;
164201 } ) ;
165202 } ) ;
166203} )
0 commit comments