@@ -3,6 +3,7 @@ import { fn } from "@storybook/test";
3
3
import * as actual from "next/dist/client/components/navigation.js" ;
4
4
import { RedirectStatusCode } from "next/dist/client/components/redirect-status-code.js" ;
5
5
import { getRedirectError } from "next/dist/client/components/redirect.js" ;
6
+ import type { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime" ;
6
7
import { NextjsRouterMocksNotAvailable } from "storybook/internal/preview-errors" ;
7
8
8
9
let navigationAPI : {
@@ -63,7 +64,9 @@ export const getRouter = () => {
63
64
export * from "next/dist/client/components/navigation.js" ;
64
65
65
66
// mock utilities/overrides (as of Next v14.2.0)
66
- export const redirect = fn (
67
+ export const redirect : Mock <
68
+ ( url : string , type ?: actual . RedirectType ) => never
69
+ > = fn (
67
70
(
68
71
url : string ,
69
72
type : actual . RedirectType = actual . RedirectType . push ,
@@ -72,7 +75,9 @@ export const redirect = fn(
72
75
} ,
73
76
) . mockName ( "next/navigation::redirect" ) ;
74
77
75
- export const permanentRedirect = fn (
78
+ export const permanentRedirect : Mock <
79
+ ( url : string , type ?: actual . RedirectType ) => never
80
+ > = fn (
76
81
(
77
82
url : string ,
78
83
type : actual . RedirectType = actual . RedirectType . push ,
@@ -82,32 +87,38 @@ export const permanentRedirect = fn(
82
87
) . mockName ( "next/navigation::permanentRedirect" ) ;
83
88
84
89
// passthrough mocks - keep original implementation but allow for spying
85
- export const useSearchParams = fn ( actual . useSearchParams ) . mockName (
86
- "next/navigation:: useSearchParams" ,
87
- ) ;
88
- export const usePathname = fn ( actual . usePathname ) . mockName (
90
+ export const useSearchParams : Mock < ( ) => actual . ReadonlyURLSearchParams > = fn (
91
+ actual . useSearchParams ,
92
+ ) . mockName ( "next/navigation::useSearchParams" ) ;
93
+ export const usePathname : Mock < ( ) => string > = fn ( actual . usePathname ) . mockName (
89
94
"next/navigation::usePathname" ,
90
95
) ;
91
- export const useSelectedLayoutSegment = fn (
92
- actual . useSelectedLayoutSegment ,
93
- ) . mockName ( "next/navigation::useSelectedLayoutSegment" ) ;
94
- export const useSelectedLayoutSegments = fn (
95
- actual . useSelectedLayoutSegments ,
96
- ) . mockName ( "next/navigation::useSelectedLayoutSegments" ) ;
97
- export const useRouter = fn ( actual . useRouter ) . mockName (
98
- "next/navigation::useRouter" ,
96
+ export const useSelectedLayoutSegment : Mock <
97
+ ( parallelRouteKey ?: string ) => string | null
98
+ > = fn ( actual . useSelectedLayoutSegment ) . mockName (
99
+ "next/navigation::useSelectedLayoutSegment" ,
100
+ ) ;
101
+ export const useSelectedLayoutSegments : Mock <
102
+ ( parallelRouteKey ?: string ) => string [ ]
103
+ > = fn ( actual . useSelectedLayoutSegments ) . mockName (
104
+ "next/navigation::useSelectedLayoutSegments" ,
99
105
) ;
100
- export const useServerInsertedHTML = fn ( actual . useServerInsertedHTML ) . mockName (
106
+ export const useRouter : Mock < ( ) => AppRouterInstance > = fn (
107
+ actual . useRouter ,
108
+ ) . mockName ( "next/navigation::useRouter" ) ;
109
+ export const useServerInsertedHTML : Mock <
110
+ ( callback : ( ) => React . ReactNode ) => void
111
+ > = fn ( actual . useServerInsertedHTML ) . mockName (
101
112
"next/navigation::useServerInsertedHTML" ,
102
113
) ;
103
- export const notFound = fn ( actual . notFound ) . mockName (
114
+ export const notFound : Mock < ( ) => never > = fn ( actual . notFound ) . mockName (
104
115
"next/navigation::notFound" ,
105
116
) ;
106
117
107
118
// Params, not exported by Next.js, is manually declared to avoid inference issues.
108
119
interface Params {
109
120
[ key : string ] : string | string [ ] ;
110
121
}
111
- export const useParams = fn < [ ] , Params > ( actual . useParams ) . mockName (
112
- "next/navigation:: useParams" ,
113
- ) ;
122
+ export const useParams : Mock < ( ) => Params > = fn < [ ] , Params > (
123
+ actual . useParams ,
124
+ ) . mockName ( "next/navigation::useParams" ) ;
0 commit comments