forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: [app dir bootstrapping 8]
useParamsWithFallback
hook and add t…
…ests (calcom#12041) * fix: first solution using RouterContext * fix: second solution by importing router from next/compat/router * fix return type
- Loading branch information
Showing
3 changed files
with
81 additions
and
6 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,63 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import { vi } from "vitest"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { useParamsWithFallback } from "./useParamsWithFallback"; | ||
|
||
describe("useParamsWithFallback hook", () => { | ||
it("should return router.query when param is null", () => { | ||
vi.mock("next/navigation", () => ({ | ||
useParams: vi.fn().mockReturnValue(null), | ||
})); | ||
|
||
vi.mock("next/compat/router", () => ({ | ||
useRouter: vi.fn().mockReturnValue({ query: { id: 1 } }), | ||
})); | ||
|
||
const { result } = renderHook(() => useParamsWithFallback()); | ||
|
||
expect(result.current).toEqual({ id: 1 }); | ||
}); | ||
|
||
it("should return router.query when param is undefined", () => { | ||
vi.mock("next/navigation", () => ({ | ||
useParams: vi.fn().mockReturnValue(undefined), | ||
})); | ||
|
||
vi.mock("next/compat/router", () => ({ | ||
useRouter: vi.fn().mockReturnValue({ query: { id: 1 } }), | ||
})); | ||
|
||
const { result } = renderHook(() => useParamsWithFallback()); | ||
|
||
expect(result.current).toEqual({ id: 1 }); | ||
}); | ||
|
||
it("should return useParams() if it exists", () => { | ||
vi.mock("next/navigation", () => ({ | ||
useParams: vi.fn().mockReturnValue({ id: 1 }), | ||
})); | ||
|
||
vi.mock("next/compat/router", () => ({ | ||
useRouter: vi.fn().mockReturnValue(null), | ||
})); | ||
|
||
const { result } = renderHook(() => useParamsWithFallback()); | ||
|
||
expect(result.current).toEqual({ id: 1 }); | ||
}); | ||
|
||
it("should return useParams() if it exists", () => { | ||
vi.mock("next/navigation", () => ({ | ||
useParams: vi.fn().mockReturnValue({ id: 1 }), | ||
})); | ||
|
||
vi.mock("next/compat/router", () => ({ | ||
useRouter: vi.fn().mockReturnValue({ query: { id: 2 } }), | ||
})); | ||
|
||
const { result } = renderHook(() => useParamsWithFallback()); | ||
|
||
expect(result.current).toEqual({ id: 1 }); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,13 +1,18 @@ | ||
"use client"; | ||
|
||
import { useRouter as useCompatRouter } from "next/compat/router"; | ||
import { useParams } from "next/navigation"; | ||
import { useRouter } from "next/router"; | ||
import type { ParsedUrlQuery } from "querystring"; | ||
|
||
interface Params { | ||
[key: string]: string | string[]; | ||
} | ||
|
||
/** | ||
* This hook is a workaround until pages are migrated to app directory. | ||
*/ | ||
export function useParamsWithFallback() { | ||
const router = useRouter(); | ||
const params = useParams(); | ||
return params || router.query; | ||
export function useParamsWithFallback(): Params | ParsedUrlQuery { | ||
const params = useParams(); // always `null` in pages router | ||
const router = useCompatRouter(); // always `null` in app router | ||
return params ?? router?.query ?? {}; | ||
} |
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