@storybook/tanstack-react 0.0.0-pr-34403-sha-53a142e3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/dist/_browser-chunks/chunk-4BE7D4DS.js +9 -0
- package/dist/_browser-chunks/chunk-4CKCBIXI.js +7 -0
- package/dist/_browser-chunks/chunk-LJRZDJ5L.js +113 -0
- package/dist/export-mocks/react-router.d.ts +349 -0
- package/dist/export-mocks/react-router.js +90 -0
- package/dist/export-mocks/start.d.ts +18 -0
- package/dist/export-mocks/start.js +37 -0
- package/dist/index.d.ts +408 -0
- package/dist/index.js +17 -0
- package/dist/node/index.d.ts +28 -0
- package/dist/node/index.js +19 -0
- package/dist/preset.d.ts +8 -0
- package/dist/preset.js +74 -0
- package/dist/preview.d.ts +69 -0
- package/dist/preview.js +11 -0
- package/package.json +111 -0
- package/template/cli/ts/Button.stories.ts +54 -0
- package/template/cli/ts/Button.tsx +35 -0
- package/template/cli/ts/Header.stories.ts +34 -0
- package/template/cli/ts/Header.tsx +55 -0
- package/template/cli/ts/Page.stories.ts +33 -0
- package/template/cli/ts/Page.tsx +159 -0
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Storybook for TanStack (React + Vite)
|
|
2
|
+
|
|
3
|
+
Develop, document, and test UI components in isolation with built-in TanStack Router and TanStack Query support.
|
|
4
|
+
|
|
5
|
+
See [documentation](https://storybook.js.org/docs/get-started?ref=readme) for installation instructions, usage examples, APIs, and more.
|
|
6
|
+
|
|
7
|
+
Learn more about Storybook at [storybook.js.org](https://storybook.js.org/?ref=readme).
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__export
|
|
3
|
+
} from "./chunk-4BE7D4DS.js";
|
|
4
|
+
|
|
5
|
+
// src/preview.tsx
|
|
6
|
+
var preview_exports = {};
|
|
7
|
+
__export(preview_exports, {
|
|
8
|
+
decorators: () => decorators,
|
|
9
|
+
loaders: () => loaders,
|
|
10
|
+
parameters: () => parameters
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// src/routing/decorator.tsx
|
|
14
|
+
import React from "react";
|
|
15
|
+
import {
|
|
16
|
+
createMemoryHistory,
|
|
17
|
+
createRootRoute,
|
|
18
|
+
createRouter,
|
|
19
|
+
Route,
|
|
20
|
+
RouterProvider,
|
|
21
|
+
createRoute,
|
|
22
|
+
RootRoute,
|
|
23
|
+
interpolatePath,
|
|
24
|
+
defaultStringifySearch
|
|
25
|
+
} from "@tanstack/react-router";
|
|
26
|
+
function isRoute(value) {
|
|
27
|
+
return value instanceof Route || value instanceof RootRoute;
|
|
28
|
+
}
|
|
29
|
+
function getRouteFromContext(Story, context) {
|
|
30
|
+
let metaRoute = context.route, routerParameters = context.parameters.tanstack?.router ?? {}, routerParameterRoute = routerParameters.route, { route: _route, ...routeOverrides } = routerParameters ?? {}, resolvedRoute = isRoute(routerParameterRoute) ? routerParameterRoute : isRoute(metaRoute) ? metaRoute : void 0;
|
|
31
|
+
if (resolvedRoute instanceof RootRoute) {
|
|
32
|
+
let children = resolvedRoute.children;
|
|
33
|
+
return children?.length ? children[0].update({ component: () => React.createElement(Story, null), ...routeOverrides }) : resolvedRoute.update({ component: () => React.createElement(Story, null), ...routeOverrides }), resolvedRoute;
|
|
34
|
+
}
|
|
35
|
+
if (resolvedRoute instanceof Route) {
|
|
36
|
+
let root2 = createRootRoute(), { id: _id, ...routeOpts } = resolvedRoute.options ?? {}, child2 = createRoute({
|
|
37
|
+
...routeOpts,
|
|
38
|
+
...routeOverrides,
|
|
39
|
+
component: () => React.createElement(Story, null),
|
|
40
|
+
getParentRoute: () => root2
|
|
41
|
+
});
|
|
42
|
+
return root2.addChildren([child2]), root2;
|
|
43
|
+
}
|
|
44
|
+
let root = createRootRoute(), child = createRoute({
|
|
45
|
+
component: () => React.createElement(Story, null),
|
|
46
|
+
...routerParameterRoute,
|
|
47
|
+
path: routerParameterRoute?.path ?? "/",
|
|
48
|
+
getParentRoute: () => root
|
|
49
|
+
});
|
|
50
|
+
return root.addChildren([child]), root;
|
|
51
|
+
}
|
|
52
|
+
function createStoryRouter({ Story, context }) {
|
|
53
|
+
let routerParameters = context.parameters.tanstack?.router ?? {}, routeTree = getRouteFromContext(Story, context), initialPath = (routerParameters?.path || routeTree.children[0]?.fullPath || routeTree.children[0]?.path || routeTree.children[0]?.options?.path) ?? "/", resolvedPath = interpolatePath({
|
|
54
|
+
path: initialPath,
|
|
55
|
+
params: routerParameters?.params ?? {}
|
|
56
|
+
}).interpolatedPath, search = routerParameters?.query ? defaultStringifySearch(routerParameters.query) : "";
|
|
57
|
+
search && (resolvedPath += search);
|
|
58
|
+
let history = createMemoryHistory({
|
|
59
|
+
initialEntries: [resolvedPath]
|
|
60
|
+
});
|
|
61
|
+
return history.replace(resolvedPath), createRouter({
|
|
62
|
+
routeTree,
|
|
63
|
+
history,
|
|
64
|
+
defaultNotFoundComponent(props) {
|
|
65
|
+
return React.createElement("div", null, "Route not found: ", props.routeId);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
var tanstackRouteDecorator = (Story, context) => {
|
|
70
|
+
let router = createStoryRouter({
|
|
71
|
+
Story,
|
|
72
|
+
context
|
|
73
|
+
});
|
|
74
|
+
return React.createElement(RouterProvider, { router });
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/routing/loader.ts
|
|
78
|
+
import { Route as Route2, RootRoute as RootRoute2 } from "@tanstack/react-router";
|
|
79
|
+
function isRoute2(value) {
|
|
80
|
+
return value instanceof Route2 || value instanceof RootRoute2;
|
|
81
|
+
}
|
|
82
|
+
var routeComponentLoader = (context) => {
|
|
83
|
+
if (isRoute2(context.component)) {
|
|
84
|
+
let route = context.component, component = route.options?.component;
|
|
85
|
+
component && (context.component = component), context.route || (context.route = route);
|
|
86
|
+
let routeOptionKeys = [
|
|
87
|
+
"loader",
|
|
88
|
+
"beforeLoad",
|
|
89
|
+
"validateSearch",
|
|
90
|
+
"loaderDeps",
|
|
91
|
+
"context",
|
|
92
|
+
"params",
|
|
93
|
+
"head",
|
|
94
|
+
"search",
|
|
95
|
+
"parseParams"
|
|
96
|
+
], overrides = {};
|
|
97
|
+
for (let key of routeOptionKeys)
|
|
98
|
+
key in (context.args ?? {}) && context.args[key] !== void 0 && (overrides[key] = context.args[key]);
|
|
99
|
+
Object.keys(overrides).length > 0 && route.update(overrides);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// src/preview.tsx
|
|
104
|
+
var loaders = [routeComponentLoader], decorators = [tanstackRouteDecorator], parameters = {
|
|
105
|
+
tanstack: {}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export {
|
|
109
|
+
loaders,
|
|
110
|
+
decorators,
|
|
111
|
+
parameters,
|
|
112
|
+
preview_exports
|
|
113
|
+
};
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import * as _tanstack_react_router from '@tanstack/react-router';
|
|
2
|
+
import { useNavigate as useNavigate$1, useRouter as useRouter$1, useBlocker as useBlocker$1, useMatch as useMatch$1, useSearch as useSearch$1, useParams as useParams$1, useLocation as useLocation$1, useRouterState as useRouterState$1, useMatchRoute as useMatchRoute$1, useLoaderData as useLoaderData$1, useLoaderDeps as useLoaderDeps$1, useRouteContext as useRouteContext$1, useMatches as useMatches$1, useParentMatches as useParentMatches$1, useChildMatches as useChildMatches$1, useCanGoBack as useCanGoBack$1, useLinkProps as useLinkProps$1, Navigate as Navigate$1 } from '@tanstack/react-router';
|
|
3
|
+
export * from '@tanstack/react-router';
|
|
4
|
+
import * as _tanstack_router_core from '@tanstack/router-core';
|
|
5
|
+
import React from 'react';
|
|
6
|
+
|
|
7
|
+
interface MockResultReturn<T> {
|
|
8
|
+
type: "return";
|
|
9
|
+
/**
|
|
10
|
+
* The value that was returned from the function. If function returned a Promise, then this will be a resolved value.
|
|
11
|
+
*/
|
|
12
|
+
value: T;
|
|
13
|
+
}
|
|
14
|
+
interface MockResultIncomplete {
|
|
15
|
+
type: "incomplete";
|
|
16
|
+
value: undefined;
|
|
17
|
+
}
|
|
18
|
+
interface MockResultThrow {
|
|
19
|
+
type: "throw";
|
|
20
|
+
/**
|
|
21
|
+
* An error that was thrown during function execution.
|
|
22
|
+
*/
|
|
23
|
+
value: any;
|
|
24
|
+
}
|
|
25
|
+
interface MockSettledResultFulfilled<T> {
|
|
26
|
+
type: "fulfilled";
|
|
27
|
+
value: T;
|
|
28
|
+
}
|
|
29
|
+
interface MockSettledResultRejected {
|
|
30
|
+
type: "rejected";
|
|
31
|
+
value: any;
|
|
32
|
+
}
|
|
33
|
+
type MockResult<T> = MockResultReturn<T> | MockResultThrow | MockResultIncomplete;
|
|
34
|
+
type MockSettledResult<T> = MockSettledResultFulfilled<T> | MockSettledResultRejected;
|
|
35
|
+
interface MockContext<T extends Procedure> {
|
|
36
|
+
/**
|
|
37
|
+
* This is an array containing all arguments for each call. One item of the array is the arguments of that call.
|
|
38
|
+
*
|
|
39
|
+
* @see https://vitest.dev/api/mock#mock-calls
|
|
40
|
+
* @example
|
|
41
|
+
* const fn = vi.fn()
|
|
42
|
+
*
|
|
43
|
+
* fn('arg1', 'arg2')
|
|
44
|
+
* fn('arg3')
|
|
45
|
+
*
|
|
46
|
+
* fn.mock.calls === [
|
|
47
|
+
* ['arg1', 'arg2'], // first call
|
|
48
|
+
* ['arg3'], // second call
|
|
49
|
+
* ]
|
|
50
|
+
*/
|
|
51
|
+
calls: Parameters<T>[];
|
|
52
|
+
/**
|
|
53
|
+
* This is an array containing all instances that were instantiated when mock was called with a `new` keyword. Note that this is an actual context (`this`) of the function, not a return value.
|
|
54
|
+
* @see https://vitest.dev/api/mock#mock-instances
|
|
55
|
+
*/
|
|
56
|
+
instances: ReturnType<T>[];
|
|
57
|
+
/**
|
|
58
|
+
* An array of `this` values that were used during each call to the mock function.
|
|
59
|
+
* @see https://vitest.dev/api/mock#mock-contexts
|
|
60
|
+
*/
|
|
61
|
+
contexts: ThisParameterType<T>[];
|
|
62
|
+
/**
|
|
63
|
+
* The order of mock's execution. This returns an array of numbers which are shared between all defined mocks.
|
|
64
|
+
*
|
|
65
|
+
* @see https://vitest.dev/api/mock#mock-invocationcallorder
|
|
66
|
+
* @example
|
|
67
|
+
* const fn1 = vi.fn()
|
|
68
|
+
* const fn2 = vi.fn()
|
|
69
|
+
*
|
|
70
|
+
* fn1()
|
|
71
|
+
* fn2()
|
|
72
|
+
* fn1()
|
|
73
|
+
*
|
|
74
|
+
* fn1.mock.invocationCallOrder === [1, 3]
|
|
75
|
+
* fn2.mock.invocationCallOrder === [2]
|
|
76
|
+
*/
|
|
77
|
+
invocationCallOrder: number[];
|
|
78
|
+
/**
|
|
79
|
+
* This is an array containing all values that were `returned` from the function.
|
|
80
|
+
*
|
|
81
|
+
* The `value` property contains the returned value or thrown error. If the function returned a `Promise`, then `result` will always be `'return'` even if the promise was rejected.
|
|
82
|
+
*
|
|
83
|
+
* @see https://vitest.dev/api/mock#mock-results
|
|
84
|
+
* @example
|
|
85
|
+
* const fn = vi.fn()
|
|
86
|
+
* .mockReturnValueOnce('result')
|
|
87
|
+
* .mockImplementationOnce(() => { throw new Error('thrown error') })
|
|
88
|
+
*
|
|
89
|
+
* const result = fn()
|
|
90
|
+
*
|
|
91
|
+
* try {
|
|
92
|
+
* fn()
|
|
93
|
+
* }
|
|
94
|
+
* catch {}
|
|
95
|
+
*
|
|
96
|
+
* fn.mock.results === [
|
|
97
|
+
* {
|
|
98
|
+
* type: 'return',
|
|
99
|
+
* value: 'result',
|
|
100
|
+
* },
|
|
101
|
+
* {
|
|
102
|
+
* type: 'throw',
|
|
103
|
+
* value: Error,
|
|
104
|
+
* },
|
|
105
|
+
* ]
|
|
106
|
+
*/
|
|
107
|
+
results: MockResult<ReturnType<T>>[];
|
|
108
|
+
/**
|
|
109
|
+
* An array containing all values that were `resolved` or `rejected` from the function.
|
|
110
|
+
*
|
|
111
|
+
* This array will be empty if the function was never resolved or rejected.
|
|
112
|
+
*
|
|
113
|
+
* @see https://vitest.dev/api/mock#mock-settledresults
|
|
114
|
+
* @example
|
|
115
|
+
* const fn = vi.fn().mockResolvedValueOnce('result')
|
|
116
|
+
*
|
|
117
|
+
* const result = fn()
|
|
118
|
+
*
|
|
119
|
+
* fn.mock.settledResults === []
|
|
120
|
+
* fn.mock.results === [
|
|
121
|
+
* {
|
|
122
|
+
* type: 'return',
|
|
123
|
+
* value: Promise<'result'>,
|
|
124
|
+
* },
|
|
125
|
+
* ]
|
|
126
|
+
*
|
|
127
|
+
* await result
|
|
128
|
+
*
|
|
129
|
+
* fn.mock.settledResults === [
|
|
130
|
+
* {
|
|
131
|
+
* type: 'fulfilled',
|
|
132
|
+
* value: 'result',
|
|
133
|
+
* },
|
|
134
|
+
* ]
|
|
135
|
+
*/
|
|
136
|
+
settledResults: MockSettledResult<Awaited<ReturnType<T>>>[];
|
|
137
|
+
/**
|
|
138
|
+
* This contains the arguments of the last call. If spy wasn't called, will return `undefined`.
|
|
139
|
+
* @see https://vitest.dev/api/mock#mock-lastcall
|
|
140
|
+
*/
|
|
141
|
+
lastCall: Parameters<T> | undefined;
|
|
142
|
+
}
|
|
143
|
+
type Procedure = (...args: any[]) => any;
|
|
144
|
+
// pick a single function type from function overloads, unions, etc...
|
|
145
|
+
type NormalizedProcedure<T extends Procedure> = (...args: Parameters<T>) => ReturnType<T>;
|
|
146
|
+
/*
|
|
147
|
+
cf. https://typescript-eslint.io/rules/method-signature-style/
|
|
148
|
+
|
|
149
|
+
Typescript assignability is different between
|
|
150
|
+
{ foo: (f: T) => U } (this is "method-signature-style")
|
|
151
|
+
and
|
|
152
|
+
{ foo(f: T): U }
|
|
153
|
+
|
|
154
|
+
Jest uses the latter for `MockInstance.mockImplementation` etc... and it allows assignment such as:
|
|
155
|
+
const boolFn: Jest.Mock<() => boolean> = jest.fn<() => true>(() => true)
|
|
156
|
+
*/
|
|
157
|
+
/* eslint-disable ts/method-signature-style */
|
|
158
|
+
interface MockInstance<T extends Procedure = Procedure> extends Disposable {
|
|
159
|
+
/**
|
|
160
|
+
* Use it to return the name assigned to the mock with the `.mockName(name)` method. By default, it will return `vi.fn()`.
|
|
161
|
+
* @see https://vitest.dev/api/mock#getmockname
|
|
162
|
+
*/
|
|
163
|
+
getMockName(): string;
|
|
164
|
+
/**
|
|
165
|
+
* Sets the internal mock name. This is useful for identifying the mock when an assertion fails.
|
|
166
|
+
* @see https://vitest.dev/api/mock#mockname
|
|
167
|
+
*/
|
|
168
|
+
mockName(name: string): this;
|
|
169
|
+
/**
|
|
170
|
+
* Current context of the mock. It stores information about all invocation calls, instances, and results.
|
|
171
|
+
*/
|
|
172
|
+
mock: MockContext<T>;
|
|
173
|
+
/**
|
|
174
|
+
* Clears all information about every call. After calling it, all properties on `.mock` will return to their initial state. This method does not reset implementations. It is useful for cleaning up mocks between different assertions.
|
|
175
|
+
*
|
|
176
|
+
* To automatically call this method before each test, enable the [`clearMocks`](https://vitest.dev/config/#clearmocks) setting in the configuration.
|
|
177
|
+
* @see https://vitest.dev/api/mock#mockclear
|
|
178
|
+
*/
|
|
179
|
+
mockClear(): this;
|
|
180
|
+
/**
|
|
181
|
+
* Does what `mockClear` does and resets inner implementation to the original function. This also resets all "once" implementations.
|
|
182
|
+
*
|
|
183
|
+
* Note that resetting a mock from `vi.fn()` will set implementation to an empty function that returns `undefined`.
|
|
184
|
+
* Resetting a mock from `vi.fn(impl)` will set implementation to `impl`. It is useful for completely resetting a mock to its default state.
|
|
185
|
+
*
|
|
186
|
+
* To automatically call this method before each test, enable the [`mockReset`](https://vitest.dev/config/#mockreset) setting in the configuration.
|
|
187
|
+
* @see https://vitest.dev/api/mock#mockreset
|
|
188
|
+
*/
|
|
189
|
+
mockReset(): this;
|
|
190
|
+
/**
|
|
191
|
+
* Does what `mockReset` does and restores original descriptors of spied-on objects.
|
|
192
|
+
*
|
|
193
|
+
* Note that restoring mock from `vi.fn()` will set implementation to an empty function that returns `undefined`. Restoring a `vi.fn(impl)` will restore implementation to `impl`.
|
|
194
|
+
* @see https://vitest.dev/api/mock#mockrestore
|
|
195
|
+
*/
|
|
196
|
+
mockRestore(): void;
|
|
197
|
+
/**
|
|
198
|
+
* Returns current permanent mock implementation if there is one.
|
|
199
|
+
*
|
|
200
|
+
* If mock was created with `vi.fn`, it will consider passed down method as a mock implementation.
|
|
201
|
+
*
|
|
202
|
+
* If mock was created with `vi.spyOn`, it will return `undefined` unless a custom implementation was provided.
|
|
203
|
+
*/
|
|
204
|
+
getMockImplementation(): NormalizedProcedure<T> | undefined;
|
|
205
|
+
/**
|
|
206
|
+
* Accepts a function to be used as the mock implementation. TypeScript expects the arguments and return type to match those of the original function.
|
|
207
|
+
* @see https://vitest.dev/api/mock#mockimplementation
|
|
208
|
+
* @example
|
|
209
|
+
* const increment = vi.fn().mockImplementation(count => count + 1);
|
|
210
|
+
* expect(increment(3)).toBe(4);
|
|
211
|
+
*/
|
|
212
|
+
mockImplementation(fn: NormalizedProcedure<T>): this;
|
|
213
|
+
/**
|
|
214
|
+
* Accepts a function to be used as the mock implementation. TypeScript expects the arguments and return type to match those of the original function. This method can be chained to produce different results for multiple function calls.
|
|
215
|
+
*
|
|
216
|
+
* When the mocked function runs out of implementations, it will invoke the default implementation set with `vi.fn(() => defaultValue)` or `.mockImplementation(() => defaultValue)` if they were called.
|
|
217
|
+
* @see https://vitest.dev/api/mock#mockimplementationonce
|
|
218
|
+
* @example
|
|
219
|
+
* const fn = vi.fn(count => count).mockImplementationOnce(count => count + 1);
|
|
220
|
+
* expect(fn(3)).toBe(4);
|
|
221
|
+
* expect(fn(3)).toBe(3);
|
|
222
|
+
*/
|
|
223
|
+
mockImplementationOnce(fn: NormalizedProcedure<T>): this;
|
|
224
|
+
/**
|
|
225
|
+
* Overrides the original mock implementation temporarily while the callback is being executed.
|
|
226
|
+
*
|
|
227
|
+
* Note that this method takes precedence over the [`mockImplementationOnce`](https://vitest.dev/api/mock#mockimplementationonce).
|
|
228
|
+
* @see https://vitest.dev/api/mock#withimplementation
|
|
229
|
+
* @example
|
|
230
|
+
* const myMockFn = vi.fn(() => 'original')
|
|
231
|
+
*
|
|
232
|
+
* myMockFn.withImplementation(() => 'temp', () => {
|
|
233
|
+
* myMockFn() // 'temp'
|
|
234
|
+
* })
|
|
235
|
+
*
|
|
236
|
+
* myMockFn() // 'original'
|
|
237
|
+
*/
|
|
238
|
+
withImplementation<T2>(fn: NormalizedProcedure<T>, cb: () => T2): T2 extends Promise<unknown> ? Promise<this> : this;
|
|
239
|
+
/**
|
|
240
|
+
* Use this if you need to return the `this` context from the method without invoking the actual implementation.
|
|
241
|
+
* @see https://vitest.dev/api/mock#mockreturnthis
|
|
242
|
+
*/
|
|
243
|
+
mockReturnThis(): this;
|
|
244
|
+
/**
|
|
245
|
+
* Accepts a value that will be returned whenever the mock function is called. TypeScript will only accept values that match the return type of the original function.
|
|
246
|
+
* @see https://vitest.dev/api/mock#mockreturnvalue
|
|
247
|
+
* @example
|
|
248
|
+
* const mock = vi.fn()
|
|
249
|
+
* mock.mockReturnValue(42)
|
|
250
|
+
* mock() // 42
|
|
251
|
+
* mock.mockReturnValue(43)
|
|
252
|
+
* mock() // 43
|
|
253
|
+
*/
|
|
254
|
+
mockReturnValue(value: ReturnType<T>): this;
|
|
255
|
+
/**
|
|
256
|
+
* Accepts a value that will be returned whenever the mock function is called. TypeScript will only accept values that match the return type of the original function.
|
|
257
|
+
*
|
|
258
|
+
* When the mocked function runs out of implementations, it will invoke the default implementation set with `vi.fn(() => defaultValue)` or `.mockImplementation(() => defaultValue)` if they were called.
|
|
259
|
+
* @example
|
|
260
|
+
* const myMockFn = vi
|
|
261
|
+
* .fn()
|
|
262
|
+
* .mockReturnValue('default')
|
|
263
|
+
* .mockReturnValueOnce('first call')
|
|
264
|
+
* .mockReturnValueOnce('second call')
|
|
265
|
+
*
|
|
266
|
+
* // 'first call', 'second call', 'default'
|
|
267
|
+
* console.log(myMockFn(), myMockFn(), myMockFn())
|
|
268
|
+
*/
|
|
269
|
+
mockReturnValueOnce(value: ReturnType<T>): this;
|
|
270
|
+
/**
|
|
271
|
+
* Accepts a value that will be resolved when the async function is called. TypeScript will only accept values that match the return type of the original function.
|
|
272
|
+
* @example
|
|
273
|
+
* const asyncMock = vi.fn().mockResolvedValue(42)
|
|
274
|
+
* asyncMock() // Promise<42>
|
|
275
|
+
*/
|
|
276
|
+
mockResolvedValue(value: Awaited<ReturnType<T>>): this;
|
|
277
|
+
/**
|
|
278
|
+
* Accepts a value that will be resolved during the next function call. TypeScript will only accept values that match the return type of the original function. If chained, each consecutive call will resolve the specified value.
|
|
279
|
+
* @example
|
|
280
|
+
* const myMockFn = vi
|
|
281
|
+
* .fn()
|
|
282
|
+
* .mockResolvedValue('default')
|
|
283
|
+
* .mockResolvedValueOnce('first call')
|
|
284
|
+
* .mockResolvedValueOnce('second call')
|
|
285
|
+
*
|
|
286
|
+
* // Promise<'first call'>, Promise<'second call'>, Promise<'default'>
|
|
287
|
+
* console.log(myMockFn(), myMockFn(), myMockFn())
|
|
288
|
+
*/
|
|
289
|
+
mockResolvedValueOnce(value: Awaited<ReturnType<T>>): this;
|
|
290
|
+
/**
|
|
291
|
+
* Accepts an error that will be rejected when async function is called.
|
|
292
|
+
* @example
|
|
293
|
+
* const asyncMock = vi.fn().mockRejectedValue(new Error('Async error'))
|
|
294
|
+
* await asyncMock() // throws Error<'Async error'>
|
|
295
|
+
*/
|
|
296
|
+
mockRejectedValue(error: unknown): this;
|
|
297
|
+
/**
|
|
298
|
+
* Accepts a value that will be rejected during the next function call. If chained, each consecutive call will reject the specified value.
|
|
299
|
+
* @example
|
|
300
|
+
* const asyncMock = vi
|
|
301
|
+
* .fn()
|
|
302
|
+
* .mockResolvedValueOnce('first call')
|
|
303
|
+
* .mockRejectedValueOnce(new Error('Async error'))
|
|
304
|
+
*
|
|
305
|
+
* await asyncMock() // first call
|
|
306
|
+
* await asyncMock() // throws Error<'Async error'>
|
|
307
|
+
*/
|
|
308
|
+
mockRejectedValueOnce(error: unknown): this;
|
|
309
|
+
}
|
|
310
|
+
/* eslint-enable ts/method-signature-style */
|
|
311
|
+
interface Mock<T extends Procedure = Procedure> extends MockInstance<T> {
|
|
312
|
+
new (...args: Parameters<T>): ReturnType<T>;
|
|
313
|
+
(...args: Parameters<T>): ReturnType<T>;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
declare const useNavigate: Mock<typeof useNavigate$1>;
|
|
317
|
+
declare const useRouter: Mock<typeof useRouter$1>;
|
|
318
|
+
declare const useBlocker: Mock<typeof useBlocker$1>;
|
|
319
|
+
declare const useMatch: Mock<typeof useMatch$1>;
|
|
320
|
+
declare const useSearch: Mock<typeof useSearch$1>;
|
|
321
|
+
declare const useParams: Mock<typeof useParams$1>;
|
|
322
|
+
declare const useLocation: Mock<typeof useLocation$1>;
|
|
323
|
+
declare const useRouterState: Mock<typeof useRouterState$1>;
|
|
324
|
+
declare const useMatchRoute: Mock<typeof useMatchRoute$1>;
|
|
325
|
+
declare const useLoaderData: Mock<typeof useLoaderData$1>;
|
|
326
|
+
declare const useLoaderDeps: Mock<typeof useLoaderDeps$1>;
|
|
327
|
+
declare const useRouteContext: Mock<typeof useRouteContext$1>;
|
|
328
|
+
declare const useMatches: Mock<typeof useMatches$1>;
|
|
329
|
+
declare const useParentMatches: Mock<typeof useParentMatches$1>;
|
|
330
|
+
declare const useChildMatches: Mock<typeof useChildMatches$1>;
|
|
331
|
+
declare const useCanGoBack: Mock<typeof useCanGoBack$1>;
|
|
332
|
+
declare const useLinkProps: Mock<typeof useLinkProps$1>;
|
|
333
|
+
declare const Outlet: () => null;
|
|
334
|
+
declare const Navigate: typeof Navigate$1;
|
|
335
|
+
declare const Link: ({ to, children, ...props }: {
|
|
336
|
+
to: string;
|
|
337
|
+
children?: React.ReactNode;
|
|
338
|
+
[key: string]: unknown;
|
|
339
|
+
}) => React.DetailedReactHTMLElement<{
|
|
340
|
+
href: string;
|
|
341
|
+
onClick: (e: React.MouseEvent) => void;
|
|
342
|
+
}, HTMLElement>;
|
|
343
|
+
/**
|
|
344
|
+
* Override createFilezRoute from tanstack react router
|
|
345
|
+
* because the org `createFileRoute` doesn't set the path in the Route
|
|
346
|
+
*/
|
|
347
|
+
declare function createFileRoute(path: string): (options: any) => _tanstack_react_router.Route<unknown, _tanstack_router_core.AnyRoute, "/", "/" | `/${any}/`, string, "/" | `/${any}/`, undefined, _tanstack_router_core.ResolveParams<"/">, _tanstack_router_core.AnyContext, _tanstack_router_core.AnyContext, _tanstack_router_core.AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>;
|
|
348
|
+
|
|
349
|
+
export { Link, Navigate, Outlet, createFileRoute, useBlocker, useCanGoBack, useChildMatches, useLinkProps, useLoaderData, useLoaderDeps, useLocation, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useParentMatches, useRouteContext, useRouter, useRouterState, useSearch };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
onNavigate
|
|
3
|
+
} from "../_browser-chunks/chunk-4CKCBIXI.js";
|
|
4
|
+
import "../_browser-chunks/chunk-4BE7D4DS.js";
|
|
5
|
+
|
|
6
|
+
// src/export-mocks/react-router.ts
|
|
7
|
+
import { createRoute } from "@tanstack/react-router";
|
|
8
|
+
export * from "@tanstack/react-router";
|
|
9
|
+
import { fn } from "storybook/test";
|
|
10
|
+
import React from "react";
|
|
11
|
+
import { useEffect } from "storybook/internal/preview-api";
|
|
12
|
+
import {
|
|
13
|
+
useNavigate as _useNavigate,
|
|
14
|
+
useRouter as _useRouter,
|
|
15
|
+
useBlocker as _useBlocker,
|
|
16
|
+
useMatch as _useMatch,
|
|
17
|
+
useSearch as _useSearch,
|
|
18
|
+
useParams as _useParams,
|
|
19
|
+
useLocation as _useLocation,
|
|
20
|
+
useRouterState as _useRouterState,
|
|
21
|
+
useMatchRoute as _useMatchRoute,
|
|
22
|
+
useLoaderData as _useLoaderData,
|
|
23
|
+
useLoaderDeps as _useLoaderDeps,
|
|
24
|
+
useRouteContext as _useRouteContext,
|
|
25
|
+
useMatches as _useMatches,
|
|
26
|
+
useParentMatches as _useParentMatches,
|
|
27
|
+
useChildMatches as _useChildMatches,
|
|
28
|
+
useCanGoBack as _useCanGoBack,
|
|
29
|
+
useLinkProps as _useLinkProps
|
|
30
|
+
} from "@tanstack/react-router";
|
|
31
|
+
var useNavigate = fn(_useNavigate).mockName("@tanstack/react-router::useNavigate"), useRouter = fn(_useRouter).mockName("@tanstack/react-router::useRouter"), useBlocker = fn(_useBlocker).mockName("@tanstack/react-router::useBlocker"), useMatch = fn(_useMatch).mockName("@tanstack/react-router::useMatch"), useSearch = fn(_useSearch).mockName("@tanstack/react-router::useSearch"), useParams = fn(_useParams).mockName("@tanstack/react-router::useParams"), useLocation = fn(_useLocation).mockName("@tanstack/react-router::useLocation"), useRouterState = fn(_useRouterState).mockName(
|
|
32
|
+
"@tanstack/react-router::useRouterState"
|
|
33
|
+
), useMatchRoute = fn(_useMatchRoute).mockName("@tanstack/react-router::useMatchRoute"), useLoaderData = fn(_useLoaderData).mockName("@tanstack/react-router::useLoaderData"), useLoaderDeps = fn(_useLoaderDeps).mockName("@tanstack/react-router::useLoaderDeps"), useRouteContext = fn(_useRouteContext).mockName(
|
|
34
|
+
"@tanstack/react-router::useRouteContext"
|
|
35
|
+
), useMatches = fn(_useMatches).mockName("@tanstack/react-router::useMatches"), useParentMatches = fn(_useParentMatches).mockName(
|
|
36
|
+
"@tanstack/react-router::useParentMatches"
|
|
37
|
+
), useChildMatches = fn(_useChildMatches).mockName(
|
|
38
|
+
"@tanstack/react-router::useChildMatches"
|
|
39
|
+
), useCanGoBack = fn(_useCanGoBack).mockName("@tanstack/react-router::useCanGoBack"), useLinkProps = fn(_useLinkProps).mockName("@tanstack/react-router::useLinkProps"), Outlet = () => null, Navigate = ({ to, href }) => (useEffect(() => {
|
|
40
|
+
onNavigate(to || href);
|
|
41
|
+
}, []), null), Link = ({
|
|
42
|
+
to,
|
|
43
|
+
children,
|
|
44
|
+
...props
|
|
45
|
+
}) => React.createElement(
|
|
46
|
+
"a",
|
|
47
|
+
{
|
|
48
|
+
href: to,
|
|
49
|
+
onClick: (e) => {
|
|
50
|
+
e.preventDefault(), useNavigate({ from: useLocation().href });
|
|
51
|
+
},
|
|
52
|
+
...props
|
|
53
|
+
},
|
|
54
|
+
children
|
|
55
|
+
);
|
|
56
|
+
function createFileRoute(path) {
|
|
57
|
+
return (options) => createRoute({
|
|
58
|
+
path,
|
|
59
|
+
...options,
|
|
60
|
+
isRoot: !1
|
|
61
|
+
}).update({
|
|
62
|
+
id: path,
|
|
63
|
+
path,
|
|
64
|
+
fullPath: path
|
|
65
|
+
// any because tanstack router does that
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
export {
|
|
69
|
+
Link,
|
|
70
|
+
Navigate,
|
|
71
|
+
Outlet,
|
|
72
|
+
createFileRoute,
|
|
73
|
+
useBlocker,
|
|
74
|
+
useCanGoBack,
|
|
75
|
+
useChildMatches,
|
|
76
|
+
useLinkProps,
|
|
77
|
+
useLoaderData,
|
|
78
|
+
useLoaderDeps,
|
|
79
|
+
useLocation,
|
|
80
|
+
useMatch,
|
|
81
|
+
useMatchRoute,
|
|
82
|
+
useMatches,
|
|
83
|
+
useNavigate,
|
|
84
|
+
useParams,
|
|
85
|
+
useParentMatches,
|
|
86
|
+
useRouteContext,
|
|
87
|
+
useRouter,
|
|
88
|
+
useRouterState,
|
|
89
|
+
useSearch
|
|
90
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createServerFn as createServerFn$1 } from '@tanstack/start-client-core';
|
|
3
|
+
|
|
4
|
+
declare function useServerFn<T extends (...args: Array<any>) => Promise<any>>(serverFn: T): (...args: Parameters<T>) => ReturnType<T>;
|
|
5
|
+
declare const createServerFn: typeof createServerFn$1;
|
|
6
|
+
declare const Link: ({ to, children, ...props }: {
|
|
7
|
+
to: string;
|
|
8
|
+
children?: React.ReactNode;
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
}) => React.ReactElement<{
|
|
11
|
+
href: string;
|
|
12
|
+
}, string | React.JSXElementConstructor<any>>;
|
|
13
|
+
declare const Navigate: ({ to }: {
|
|
14
|
+
to: string;
|
|
15
|
+
}) => null;
|
|
16
|
+
declare const notFound: () => never;
|
|
17
|
+
|
|
18
|
+
export { Link, Navigate, createServerFn, notFound, useServerFn };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
onNavigate
|
|
3
|
+
} from "../_browser-chunks/chunk-4CKCBIXI.js";
|
|
4
|
+
import "../_browser-chunks/chunk-4BE7D4DS.js";
|
|
5
|
+
|
|
6
|
+
// src/export-mocks/start.ts
|
|
7
|
+
import React from "react";
|
|
8
|
+
import { fn } from "storybook/test";
|
|
9
|
+
function useServerFn(serverFn) {
|
|
10
|
+
return React.useCallback(
|
|
11
|
+
(...args) => serverFn(...args),
|
|
12
|
+
[serverFn]
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
function createMockServerFnBuilder() {
|
|
16
|
+
let builder = () => createMockServerFnBuilder();
|
|
17
|
+
return builder.options = {}, builder.middleware = () => createMockServerFnBuilder(), builder.inputValidator = () => createMockServerFnBuilder(), builder.handler = (handlerFn) => {
|
|
18
|
+
let mock = fn().mockName("@tanstack/start-client-core::createServerFn.handler()");
|
|
19
|
+
return handlerFn && mock.mockImplementation(async (opts) => handlerFn(opts)), mock;
|
|
20
|
+
}, builder;
|
|
21
|
+
}
|
|
22
|
+
var createServerFn = () => createMockServerFnBuilder(), Link = ({
|
|
23
|
+
to,
|
|
24
|
+
children,
|
|
25
|
+
...props
|
|
26
|
+
}) => React.createElement("a", { href: to, ...props }, children), Navigate = ({ to }) => (React.useEffect(() => {
|
|
27
|
+
onNavigate({ to });
|
|
28
|
+
}, [to]), null), notFound = () => {
|
|
29
|
+
throw new Error("Not found");
|
|
30
|
+
};
|
|
31
|
+
export {
|
|
32
|
+
Link,
|
|
33
|
+
Navigate,
|
|
34
|
+
createServerFn,
|
|
35
|
+
notFound,
|
|
36
|
+
useServerFn
|
|
37
|
+
};
|