@@ -56,7 +56,7 @@ describe("AppActionsHandler", function () {
56
56
jest . clearAllMocks ( ) ;
57
57
} ) ;
58
58
/**
59
- * jsdom doesnt allow src code to write to window.location.href,
59
+ * jsdom doesn't allow src code to write to window.location.href,
60
60
* so totally replace this object so its writeable
61
61
*
62
62
* @see https://wildwolf.name/jest-how-to-mock-window-location-href/
@@ -75,12 +75,14 @@ describe("AppActionsHandler", function () {
75
75
} ) ;
76
76
describe ( "useHandleNotificationAction" , ( ) => {
77
77
it ( "Calls useNotifier with payload from action" , ( ) => {
78
+ // Arrange
78
79
const {
79
80
result : {
80
81
current : { handle } ,
81
82
} ,
82
83
} = renderHook ( ( ) => AppActionsHandler . useHandleNotificationAction ( ) ) ;
83
84
85
+ // Act
84
86
handle ( {
85
87
type : "notification" ,
86
88
payload : {
@@ -90,6 +92,8 @@ describe("AppActionsHandler", function () {
90
92
title : "Test title" ,
91
93
} ,
92
94
} ) ;
95
+
96
+ // Assert
93
97
expect ( mockNotify ) . toHaveBeenCalledTimes ( 1 ) ;
94
98
expect ( mockNotify ) . toHaveBeenCalledWith ( {
95
99
status : "success" ,
@@ -100,6 +104,7 @@ describe("AppActionsHandler", function () {
100
104
} ) ;
101
105
describe ( "useUpdateRoutingAction" , ( ) => {
102
106
it ( "Updates dashboard url properly" , ( ) => {
107
+ // Arrange
103
108
const mockHistoryPushState = jest . fn ( ) ;
104
109
105
110
jest . spyOn ( window . history , "pushState" ) . mockImplementation ( mockHistoryPushState ) ;
@@ -110,20 +115,49 @@ describe("AppActionsHandler", function () {
110
115
} ,
111
116
} = renderHook ( ( ) => AppActionsHandler . useHandleUpdateRoutingAction ( "XYZ" ) ) ;
112
117
118
+ // Act
113
119
handle ( {
114
120
type : "updateRouting" ,
115
121
payload : {
116
122
actionId : "123" ,
117
123
newRoute : "/foo/bar" ,
118
124
} ,
119
125
} ) ;
126
+
127
+ // Assert
120
128
expect ( mockHistoryPushState ) . toHaveBeenCalledTimes ( 1 ) ;
121
129
expect ( mockHistoryPushState ) . toHaveBeenCalledWith (
122
130
null ,
123
131
"" ,
124
132
"/dashboard/apps/XYZ/app/foo/bar" ,
125
133
) ;
126
134
} ) ;
135
+
136
+ it ( "Does not update url if it's already updated" , ( ) => {
137
+ // Arrange
138
+ const mockHistoryPushState = jest . fn ( ) ;
139
+
140
+ window . location . pathname = "/dashboard/apps/XYZ/app/foo/bar" ;
141
+ jest . spyOn ( window . history , "pushState" ) . mockImplementation ( mockHistoryPushState ) ;
142
+
143
+ const {
144
+ result : {
145
+ current : { handle } ,
146
+ } ,
147
+ } = renderHook ( ( ) => AppActionsHandler . useHandleUpdateRoutingAction ( "XYZ" ) ) ;
148
+
149
+ // Act
150
+ handle ( {
151
+ type : "updateRouting" ,
152
+ payload : {
153
+ actionId : "123" ,
154
+ newRoute : "/foo/bar" ,
155
+ } ,
156
+ } ) ;
157
+
158
+ // Assert
159
+ expect ( mockHistoryPushState ) . not . toHaveBeenCalled ( ) ;
160
+ } ) ;
127
161
} ) ;
128
162
describe ( "useHandleRedirectAction" , ( ) => {
129
163
describe ( "Open in the new browser context" , ( ) => {
@@ -137,6 +171,7 @@ describe("AppActionsHandler", function () {
137
171
jest . spyOn ( window , "open" ) . mockImplementation ( mockWindowOpen ) ;
138
172
} ) ;
139
173
it ( "Opens external URL in new browser context" , ( ) => {
174
+ // Arrange & Act
140
175
hookRenderResult . result . current . handle ( {
141
176
type : "redirect" ,
142
177
payload : {
@@ -145,10 +180,13 @@ describe("AppActionsHandler", function () {
145
180
newContext : true ,
146
181
} ,
147
182
} ) ;
183
+
184
+ // Assert
148
185
expect ( mockWindowOpen ) . toHaveBeenCalledTimes ( 1 ) ;
149
186
expect ( mockWindowOpen ) . toHaveBeenCalledWith ( "https://google.com" ) ;
150
187
} ) ;
151
188
it ( "Opens another dashboard url in new browser context" , ( ) => {
189
+ // Arrange & Act
152
190
hookRenderResult . result . current . handle ( {
153
191
type : "redirect" ,
154
192
payload : {
@@ -157,6 +195,8 @@ describe("AppActionsHandler", function () {
157
195
newContext : true ,
158
196
} ,
159
197
} ) ;
198
+
199
+ // Assert
160
200
expect ( mockWindowOpen ) . toHaveBeenCalledTimes ( 1 ) ;
161
201
expect ( mockWindowOpen ) . toHaveBeenCalledWith ( "/dashboard/orders" ) ;
162
202
} ) ;
@@ -166,6 +206,7 @@ describe("AppActionsHandler", function () {
166
206
* TODO Drop this behavior, updateRouting action can do that explicitely
167
207
*/
168
208
it ( "Opens another app route in new browser context" , ( ) => {
209
+ // Arrange & Act
169
210
hookRenderResult . result . current . handle ( {
170
211
type : "redirect" ,
171
212
payload : {
@@ -174,6 +215,8 @@ describe("AppActionsHandler", function () {
174
215
newContext : true ,
175
216
} ,
176
217
} ) ;
218
+
219
+ // Assert
177
220
expect ( mockWindowOpen ) . toHaveBeenCalledTimes ( 1 ) ;
178
221
expect ( mockWindowOpen ) . toHaveBeenCalledWith ( "/dashboard/apps/XYZ/app/config" ) ;
179
222
} ) ;
@@ -184,6 +227,7 @@ describe("AppActionsHandler", function () {
184
227
const hookRenderResult = renderHook ( ( ) => AppActionsHandler . useHandleRedirectAction ( "XYZ" ) ) ;
185
228
186
229
it ( "Redirects to external URL after confirmation" , ( ) => {
230
+ // Arrange & Act
187
231
hookRenderResult . result . current . handle ( {
188
232
type : "redirect" ,
189
233
payload : {
@@ -192,9 +236,12 @@ describe("AppActionsHandler", function () {
192
236
newContext : false ,
193
237
} ,
194
238
} ) ;
239
+
240
+ // Assert
195
241
expect ( window . location . href ) . toBe ( "https://google.com" ) ;
196
242
} ) ;
197
243
it ( "Opens another dashboard url" , ( ) => {
244
+ // Arrange & Act
198
245
hookRenderResult . result . current . handle ( {
199
246
type : "redirect" ,
200
247
payload : {
@@ -203,14 +250,19 @@ describe("AppActionsHandler", function () {
203
250
newContext : false ,
204
251
} ,
205
252
} ) ;
253
+
254
+ // Assert
206
255
expect ( mockNavigate ) . toHaveBeenCalledTimes ( 1 ) ;
207
256
expect ( mockNavigate ) . toHaveBeenCalledWith ( "/orders" ) ;
208
257
} ) ;
209
258
it ( "Update route within the same app" , ( ) => {
259
+ // Arrange
210
260
const mockHistoryPushState = jest . fn ( ) ;
211
261
212
262
jest . spyOn ( window . history , "pushState" ) . mockImplementation ( mockHistoryPushState ) ;
213
263
window . location . pathname = "/apps/XYZ/app/foo" ;
264
+
265
+ // Act
214
266
hookRenderResult . result . current . handle ( {
215
267
type : "redirect" ,
216
268
payload : {
@@ -219,6 +271,8 @@ describe("AppActionsHandler", function () {
219
271
newContext : false ,
220
272
} ,
221
273
} ) ;
274
+
275
+ // Assert
222
276
expect ( mockHistoryPushState ) . toHaveBeenCalledTimes ( 1 ) ;
223
277
expect ( mockHistoryPushState ) . toHaveBeenCalledWith (
224
278
null ,
@@ -230,10 +284,12 @@ describe("AppActionsHandler", function () {
230
284
} ) ;
231
285
describe ( "useHandlePermissionRequest" , ( ) => {
232
286
it ( "Redirects to a dedicated page with params from action" , ( ) => {
287
+ // Arrange
233
288
const hookRenderResult = renderHook ( ( ) =>
234
289
AppActionsHandler . useHandlePermissionRequest ( "XYZ" ) ,
235
290
) ;
236
291
292
+ // Act
237
293
hookRenderResult . result . current . handle ( {
238
294
type : "requestPermissions" ,
239
295
payload : {
@@ -242,6 +298,8 @@ describe("AppActionsHandler", function () {
242
298
redirectPath : "/permissions-result" ,
243
299
} ,
244
300
} ) ;
301
+
302
+ // Assert
245
303
expect ( mockNavigate ) . toHaveBeenCalledTimes ( 1 ) ;
246
304
expect ( mockNavigate ) . toHaveBeenCalledWith (
247
305
"/apps/XYZ/permissions?redirectPath=%2Fpermissions-result&requestedPermissions=MANAGE_ORDERS%2CMANAGE_CHANNELS" ,
0 commit comments