Skip to content

Commit c48e50b

Browse files
andrzejewskypoulch
authored andcommitted
Fix app router push when triggered multiple times (#5266)
* Handle app route push * Handle app route push --------- Co-authored-by: Paweł Chyła <[email protected]>
1 parent 55e185e commit c48e50b

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

.changeset/empty-turkeys-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"saleor-dashboard": patch
3+
---
4+
5+
The app no longer changes the url when it is already been updated.

src/apps/components/AppFrame/appActionsHandler.test.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe("AppActionsHandler", function () {
5656
jest.clearAllMocks();
5757
});
5858
/**
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,
6060
* so totally replace this object so its writeable
6161
*
6262
* @see https://wildwolf.name/jest-how-to-mock-window-location-href/
@@ -75,12 +75,14 @@ describe("AppActionsHandler", function () {
7575
});
7676
describe("useHandleNotificationAction", () => {
7777
it("Calls useNotifier with payload from action", () => {
78+
// Arrange
7879
const {
7980
result: {
8081
current: { handle },
8182
},
8283
} = renderHook(() => AppActionsHandler.useHandleNotificationAction());
8384

85+
// Act
8486
handle({
8587
type: "notification",
8688
payload: {
@@ -90,6 +92,8 @@ describe("AppActionsHandler", function () {
9092
title: "Test title",
9193
},
9294
});
95+
96+
// Assert
9397
expect(mockNotify).toHaveBeenCalledTimes(1);
9498
expect(mockNotify).toHaveBeenCalledWith({
9599
status: "success",
@@ -100,6 +104,7 @@ describe("AppActionsHandler", function () {
100104
});
101105
describe("useUpdateRoutingAction", () => {
102106
it("Updates dashboard url properly", () => {
107+
// Arrange
103108
const mockHistoryPushState = jest.fn();
104109

105110
jest.spyOn(window.history, "pushState").mockImplementation(mockHistoryPushState);
@@ -110,20 +115,49 @@ describe("AppActionsHandler", function () {
110115
},
111116
} = renderHook(() => AppActionsHandler.useHandleUpdateRoutingAction("XYZ"));
112117

118+
// Act
113119
handle({
114120
type: "updateRouting",
115121
payload: {
116122
actionId: "123",
117123
newRoute: "/foo/bar",
118124
},
119125
});
126+
127+
// Assert
120128
expect(mockHistoryPushState).toHaveBeenCalledTimes(1);
121129
expect(mockHistoryPushState).toHaveBeenCalledWith(
122130
null,
123131
"",
124132
"/dashboard/apps/XYZ/app/foo/bar",
125133
);
126134
});
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+
});
127161
});
128162
describe("useHandleRedirectAction", () => {
129163
describe("Open in the new browser context", () => {
@@ -137,6 +171,7 @@ describe("AppActionsHandler", function () {
137171
jest.spyOn(window, "open").mockImplementation(mockWindowOpen);
138172
});
139173
it("Opens external URL in new browser context", () => {
174+
// Arrange & Act
140175
hookRenderResult.result.current.handle({
141176
type: "redirect",
142177
payload: {
@@ -145,10 +180,13 @@ describe("AppActionsHandler", function () {
145180
newContext: true,
146181
},
147182
});
183+
184+
// Assert
148185
expect(mockWindowOpen).toHaveBeenCalledTimes(1);
149186
expect(mockWindowOpen).toHaveBeenCalledWith("https://google.com");
150187
});
151188
it("Opens another dashboard url in new browser context", () => {
189+
// Arrange & Act
152190
hookRenderResult.result.current.handle({
153191
type: "redirect",
154192
payload: {
@@ -157,6 +195,8 @@ describe("AppActionsHandler", function () {
157195
newContext: true,
158196
},
159197
});
198+
199+
// Assert
160200
expect(mockWindowOpen).toHaveBeenCalledTimes(1);
161201
expect(mockWindowOpen).toHaveBeenCalledWith("/dashboard/orders");
162202
});
@@ -166,6 +206,7 @@ describe("AppActionsHandler", function () {
166206
* TODO Drop this behavior, updateRouting action can do that explicitely
167207
*/
168208
it("Opens another app route in new browser context", () => {
209+
// Arrange & Act
169210
hookRenderResult.result.current.handle({
170211
type: "redirect",
171212
payload: {
@@ -174,6 +215,8 @@ describe("AppActionsHandler", function () {
174215
newContext: true,
175216
},
176217
});
218+
219+
// Assert
177220
expect(mockWindowOpen).toHaveBeenCalledTimes(1);
178221
expect(mockWindowOpen).toHaveBeenCalledWith("/dashboard/apps/XYZ/app/config");
179222
});
@@ -184,6 +227,7 @@ describe("AppActionsHandler", function () {
184227
const hookRenderResult = renderHook(() => AppActionsHandler.useHandleRedirectAction("XYZ"));
185228

186229
it("Redirects to external URL after confirmation", () => {
230+
// Arrange & Act
187231
hookRenderResult.result.current.handle({
188232
type: "redirect",
189233
payload: {
@@ -192,9 +236,12 @@ describe("AppActionsHandler", function () {
192236
newContext: false,
193237
},
194238
});
239+
240+
// Assert
195241
expect(window.location.href).toBe("https://google.com");
196242
});
197243
it("Opens another dashboard url", () => {
244+
// Arrange & Act
198245
hookRenderResult.result.current.handle({
199246
type: "redirect",
200247
payload: {
@@ -203,14 +250,19 @@ describe("AppActionsHandler", function () {
203250
newContext: false,
204251
},
205252
});
253+
254+
// Assert
206255
expect(mockNavigate).toHaveBeenCalledTimes(1);
207256
expect(mockNavigate).toHaveBeenCalledWith("/orders");
208257
});
209258
it("Update route within the same app", () => {
259+
// Arrange
210260
const mockHistoryPushState = jest.fn();
211261

212262
jest.spyOn(window.history, "pushState").mockImplementation(mockHistoryPushState);
213263
window.location.pathname = "/apps/XYZ/app/foo";
264+
265+
// Act
214266
hookRenderResult.result.current.handle({
215267
type: "redirect",
216268
payload: {
@@ -219,6 +271,8 @@ describe("AppActionsHandler", function () {
219271
newContext: false,
220272
},
221273
});
274+
275+
// Assert
222276
expect(mockHistoryPushState).toHaveBeenCalledTimes(1);
223277
expect(mockHistoryPushState).toHaveBeenCalledWith(
224278
null,
@@ -230,10 +284,12 @@ describe("AppActionsHandler", function () {
230284
});
231285
describe("useHandlePermissionRequest", () => {
232286
it("Redirects to a dedicated page with params from action", () => {
287+
// Arrange
233288
const hookRenderResult = renderHook(() =>
234289
AppActionsHandler.useHandlePermissionRequest("XYZ"),
235290
);
236291

292+
// Act
237293
hookRenderResult.result.current.handle({
238294
type: "requestPermissions",
239295
payload: {
@@ -242,6 +298,8 @@ describe("AppActionsHandler", function () {
242298
redirectPath: "/permissions-result",
243299
},
244300
});
301+
302+
// Assert
245303
expect(mockNavigate).toHaveBeenCalledTimes(1);
246304
expect(mockNavigate).toHaveBeenCalledWith(
247305
"/apps/XYZ/permissions?redirectPath=%2Fpermissions-result&requestedPermissions=MANAGE_ORDERS%2CMANAGE_CHANNELS",

src/apps/components/AppFrame/appActionsHandler.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ const useHandleUpdateRoutingAction = (appId: string) => ({
150150
action.payload.newRoute,
151151
);
152152

153+
if (window.location.pathname === exactLocation) {
154+
debug("Current route is the same as requested change, skipping navigation.");
155+
156+
return createResponseStatus(actionId, true);
157+
}
158+
153159
debug(`Update to new nested route: %s`, exactLocation);
154160
window.history.pushState(null, "", exactLocation);
155161

0 commit comments

Comments
 (0)