-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into merx-1310-add-source-header
- Loading branch information
Showing
4 changed files
with
111 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
Opening item in new tab using cmd key on datagrid now takes into account mounting point |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import useNavigator from "@dashboard/hooks/useNavigator"; | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
|
||
import { useRowAnchorHandler } from "./useRowAnchorHandler"; | ||
|
||
jest.mock("@dashboard/hooks/useNavigator", () => jest.fn()); | ||
|
||
jest.mock("@dashboard/hooks/useNavigator", () => ({ | ||
__esModule: true, | ||
default: jest.fn(() => jest.fn()), | ||
})); | ||
|
||
describe("useRowAnchorHandler", () => { | ||
it("should navigate to the given path", () => { | ||
// Arrange | ||
const navigate = jest.fn(); | ||
|
||
(useNavigator as jest.Mock).mockReturnValue(navigate); | ||
|
||
const navigatorOpts = { replace: true }; | ||
const handler = renderHook(() => useRowAnchorHandler(navigatorOpts)).result.current; | ||
const event = { | ||
preventDefault: jest.fn(), | ||
currentTarget: { | ||
dataset: { | ||
reactRouterPath: "/some-path", | ||
}, | ||
}, | ||
}; | ||
|
||
// Act | ||
handler(event as any); | ||
|
||
// Assert | ||
expect(event.preventDefault).toHaveBeenCalled(); | ||
expect(navigate).toHaveBeenCalledWith("/some-path", navigatorOpts); | ||
}); | ||
|
||
it("should not prevent default when CMD key is pressed", () => { | ||
// Arrange | ||
const navigate = jest.fn(); | ||
|
||
(useNavigator as jest.Mock).mockReturnValue(navigate); | ||
|
||
const handler = renderHook(() => useRowAnchorHandler()).result.current; | ||
const event = { | ||
preventDefault: jest.fn(), | ||
metaKey: true, | ||
ctrlKey: false, | ||
}; | ||
|
||
// Act | ||
handler(event as any); | ||
|
||
// Assert | ||
expect(event.preventDefault).not.toHaveBeenCalled(); | ||
expect(navigate).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should not prevent default when CTRL key is pressed", () => { | ||
// Arrange | ||
const navigate = jest.fn(); | ||
|
||
(useNavigator as jest.Mock).mockReturnValue(navigate); | ||
|
||
const handler = renderHook(() => useRowAnchorHandler()).result.current; | ||
const event = { | ||
preventDefault: jest.fn(), | ||
metaKey: false, | ||
ctrlKey: true, | ||
}; | ||
|
||
// Act | ||
handler(event as any); | ||
|
||
// Assert | ||
expect(event.preventDefault).not.toHaveBeenCalled(); | ||
expect(navigate).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import useNavigator, { NavigatorOpts } from "@dashboard/hooks/useNavigator"; | ||
import { MouseEvent } from "react"; | ||
|
||
export const useRowAnchorHandler = (navigatorOpts?: NavigatorOpts) => { | ||
const navigate = useNavigator(); | ||
|
||
return (e: MouseEvent<HTMLAnchorElement>) => { | ||
// When someone clicks with CMD key to open in new tab, we should not prevent default | ||
if (e.metaKey || e.ctrlKey) { | ||
return; | ||
} | ||
|
||
// Prevent default when navigate with browser router | ||
e.preventDefault(); | ||
|
||
if (e.currentTarget.dataset.reactRouterPath) { | ||
// Navigate gets only a path to navigate, for example, /products/1 | ||
// Navigate use browser router and cover case when url is with /dashboard or not | ||
navigate(e.currentTarget.dataset.reactRouterPath, navigatorOpts); | ||
} | ||
}; | ||
}; |