-
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.
- Loading branch information
Showing
3 changed files
with
89 additions
and
13 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
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,63 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
|
||
import { useRowAnchorHandler } from "./useRowAnchorHandler"; | ||
|
||
describe("useRowAnchorHandler", () => { | ||
it("should navigate to the given path", () => { | ||
// Arrange | ||
const navigate = jest.fn(); | ||
const navigatorOpts = { replace: true }; | ||
const handler = renderHook(() => useRowAnchorHandler(navigate, 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(); | ||
const handler = renderHook(() => useRowAnchorHandler(navigate)).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(); | ||
const handler = renderHook(() => useRowAnchorHandler(navigate)).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,23 @@ | ||
import { NavigatorOpts, UseNavigatorResult } from "@dashboard/hooks/useNavigator"; | ||
import { MouseEvent } from "react"; | ||
|
||
export const useRowAnchorHandler = ( | ||
navigate: UseNavigatorResult, | ||
navigatorOpts?: NavigatorOpts, | ||
) => { | ||
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); | ||
} | ||
}; | ||
}; |