Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontends/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@ebay/nice-modal-react": "^1.2.13",
"@emotion/cache": "^11.13.1",
"@mitodl/course-search-utils": "mitodl/course-search-utils.git#jk/5244-support-for-nextjs",
"@mitodl/course-search-utils": "^3.2.3",
"@remixicon/react": "^4.2.0",
"@tanstack/react-query": "^4.36.1",
"api": "workspace:*",
Expand Down
32 changes: 31 additions & 1 deletion frontends/ol-test-utilities/src/mocks/nextNavigation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook } from "@testing-library/react"
import { act, renderHook } from "@testing-library/react"
import { nextNavigationMocks } from "./nextNavigation"
import mockRouter from "next-router-mock"
import { createDynamicRouteParser } from "next-router-mock/dynamic-routes"
Expand Down Expand Up @@ -51,4 +51,34 @@ describe("Mock Navigation", () => {
const { result } = renderHook(() => nextNavigationMocks.useParams())
expect(result.current).toEqual({ id: "bar", other: "baz" })
})

test("router.push", () => {
mockRouter.useParser(createDynamicRouteParser(["x"]))
mockRouter.setCurrentUrl("/dynamic/bar/baz?a=1&b=2")
const { result } = renderHook(() => nextNavigationMocks.useRouter())
act(() => {
result.current.push(
"/dynamic/foo",
// @ts-expect-error The type signature of mockRouter.push is for old pages router.
// The 2nd arg here is for what our application uses, the app router
{ scroll: false },
)
})
expect(mockRouter.asPath).toBe("/dynamic/foo")
})

test("router.replace", () => {
mockRouter.useParser(createDynamicRouteParser(["x"]))
mockRouter.setCurrentUrl("/dynamic/bar/baz?a=1&b=2")
const { result } = renderHook(() => nextNavigationMocks.useRouter())
act(() => {
result.current.replace(
"/dynamic/foo",
// @ts-expect-error The type signature of mockRouter.replace is for old pages router.
// The 2nd arg here is for what our application uses, the app router
{ scroll: false },
)
})
expect(mockRouter.asPath).toBe("/dynamic/foo")
})
})
15 changes: 15 additions & 0 deletions frontends/ol-test-utilities/src/mocks/nextNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ const convertObjectToUrlParams = (obj: ParsedUrlQuery): [string, string][] =>
: [[key, value] as [string, string]],
)

/**
* memoryRouter is a mock for the older pages router
* this file adapts it for the app router
* but older router had 3-arg push and replace (url, as, options)
* new router has 2-arg (url, options).
*
* Our application code may call the 2-arg version which 2nd arg as options,
* which causes problems for the mock. Let's just limit the mock to the first
* argument. The options don't really make sense in a JSDom environment anyway.
*/
const originalPush = mocks.memoryRouter.push
const originalReplace = mocks.memoryRouter.replace
mocks.memoryRouter.push = (url) => originalPush(url)
mocks.memoryRouter.replace = (url) => originalReplace(url)

export const nextNavigationMocks = {
...mocks,
notFound: jest.fn(),
Expand Down
65 changes: 12 additions & 53 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading