Skip to content

Commit e4f06c4

Browse files
fix tests
1 parent 50c13dd commit e4f06c4

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

frontends/ol-test-utilities/src/mocks/nextNavigation.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { renderHook } from "@testing-library/react"
1+
import { act, renderHook } from "@testing-library/react"
22
import { nextNavigationMocks } from "./nextNavigation"
33
import mockRouter from "next-router-mock"
44
import { createDynamicRouteParser } from "next-router-mock/dynamic-routes"
@@ -51,4 +51,24 @@ describe("Mock Navigation", () => {
5151
const { result } = renderHook(() => nextNavigationMocks.useParams())
5252
expect(result.current).toEqual({ id: "bar", other: "baz" })
5353
})
54+
55+
test("router.push", () => {
56+
mockRouter.useParser(createDynamicRouteParser(["x"]))
57+
mockRouter.setCurrentUrl("/dynamic/bar/baz?a=1&b=2")
58+
const { result } = renderHook(() => nextNavigationMocks.useRouter())
59+
act(() => {
60+
result.current.push("/dynamic/foo", { scroll: false })
61+
})
62+
expect(mockRouter.asPath).toBe("/dynamic/foo")
63+
})
64+
65+
test("router.replace", () => {
66+
mockRouter.useParser(createDynamicRouteParser(["x"]))
67+
mockRouter.setCurrentUrl("/dynamic/bar/baz?a=1&b=2")
68+
const { result } = renderHook(() => nextNavigationMocks.useRouter())
69+
act(() => {
70+
result.current.replace("/dynamic/foo", { scroll: false })
71+
})
72+
expect(mockRouter.asPath).toBe("/dynamic/foo")
73+
})
5474
})

frontends/ol-test-utilities/src/mocks/nextNavigation.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ const convertObjectToUrlParams = (obj: ParsedUrlQuery): [string, string][] =>
3737
: [[key, value] as [string, string]],
3838
)
3939

40+
/**
41+
* memoryRouter is a mock for the older pages router
42+
* this file adapts it for the app router
43+
* but older router had 3-arg push and replace (url, as, options)
44+
* new router has 2-arg (url, options).
45+
*
46+
* Our application code may call the 2-arg version which 2nd arg as options,
47+
* which causes problems for the mock. Let's just limit the mock to the first
48+
* argument. The options don't really make sense in a JSDom environment anyway.
49+
*/
50+
const originalPush = mocks.memoryRouter.push
51+
const originalReplace = mocks.memoryRouter.replace
52+
mocks.memoryRouter.push = (url) => originalPush(url)
53+
mocks.memoryRouter.replace = (url) => originalReplace(url)
54+
4055
export const nextNavigationMocks = {
4156
...mocks,
4257
notFound: jest.fn(),

0 commit comments

Comments
 (0)