@storybook/experimental-nextjs-vite 0.0.0-pr-30943-sha-eba6de84 → 0.0.0-pr-31494-sha-c99bda03
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/dist/{chunk-HFILGZ5B.mjs → chunk-3Z6MACOM.mjs} +1 -1
- package/dist/export-mocks/cache/index.d.ts +1 -1
- package/dist/export-mocks/cache/index.js +1 -1
- package/dist/export-mocks/cache/index.mjs +1 -1
- package/dist/export-mocks/headers/index.d.ts +1 -1
- package/dist/export-mocks/headers/index.js +1 -1
- package/dist/export-mocks/headers/index.mjs +1 -1
- package/dist/export-mocks/navigation/index.d.ts +2 -2
- package/dist/export-mocks/navigation/index.js +1 -1
- package/dist/export-mocks/navigation/index.mjs +1 -1
- package/dist/export-mocks/router/index.d.ts +2 -2
- package/dist/export-mocks/router/index.js +1 -1
- package/dist/export-mocks/router/index.mjs +1 -1
- package/dist/index.d-5a935e77.d.ts +266 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -1
- package/dist/index.mjs +3 -4
- package/dist/preview.d.ts +3 -4
- package/dist/preview.js +1 -1
- package/dist/preview.mjs +1 -1
- package/package.json +11 -6
- package/template/cli/js/Button.jsx +2 -0
- package/template/cli/js/Button.stories.js +1 -1
- package/template/cli/js/Header.jsx +2 -0
- package/template/cli/js/Header.stories.js +1 -1
- package/template/cli/js/Page.stories.js +1 -1
- package/template/cli/ts-3-8/Button.stories.ts +53 -0
- package/template/cli/ts-3-8/Button.tsx +41 -0
- package/template/cli/ts-3-8/Configure.mdx +446 -0
- package/template/cli/ts-3-8/Header.stories.ts +33 -0
- package/template/cli/ts-3-8/Header.tsx +56 -0
- package/template/cli/ts-3-8/Page.stories.ts +32 -0
- package/template/cli/ts-3-8/Page.tsx +73 -0
- package/template/cli/ts-4-9/Button.stories.ts +2 -3
- package/template/cli/ts-4-9/Button.tsx +2 -0
- package/template/cli/ts-4-9/Header.stories.ts +2 -3
- package/template/cli/ts-4-9/Header.tsx +2 -0
- package/template/cli/ts-4-9/Page.stories.ts +2 -3
- package/dist/index.d-98b9eb06.d.ts +0 -296
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
import { fn } from '@storybook/test';
|
|
3
|
+
|
|
4
|
+
import { Header } from './Header';
|
|
5
|
+
|
|
6
|
+
const meta: Meta<typeof Header> = {
|
|
7
|
+
title: 'Example/Header',
|
|
8
|
+
component: Header,
|
|
9
|
+
// This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
|
|
10
|
+
tags: ['autodocs'],
|
|
11
|
+
parameters: {
|
|
12
|
+
// More on how to position stories at: https://storybook.js.org/docs/configure/story-layout
|
|
13
|
+
layout: 'fullscreen',
|
|
14
|
+
},
|
|
15
|
+
args: {
|
|
16
|
+
onLogin: fn(),
|
|
17
|
+
onLogout: fn(),
|
|
18
|
+
onCreateAccount: fn(),
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default meta;
|
|
23
|
+
type Story = StoryObj<typeof Header>;
|
|
24
|
+
|
|
25
|
+
export const LoggedIn: Story = {
|
|
26
|
+
args: {
|
|
27
|
+
user: {
|
|
28
|
+
name: 'Jane Doe',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const LoggedOut: Story = {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
import { Button } from './Button';
|
|
4
|
+
import './header.css';
|
|
5
|
+
|
|
6
|
+
type User = {
|
|
7
|
+
name: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export interface HeaderProps {
|
|
11
|
+
user?: User;
|
|
12
|
+
onLogin?: () => void;
|
|
13
|
+
onLogout?: () => void;
|
|
14
|
+
onCreateAccount?: () => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const Header = ({ user, onLogin, onLogout, onCreateAccount }: HeaderProps) => (
|
|
18
|
+
<header>
|
|
19
|
+
<div className="storybook-header">
|
|
20
|
+
<div>
|
|
21
|
+
<svg width="32" height="32" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
|
|
22
|
+
<g fill="none" fillRule="evenodd">
|
|
23
|
+
<path
|
|
24
|
+
d="M10 0h12a10 10 0 0110 10v12a10 10 0 01-10 10H10A10 10 0 010 22V10A10 10 0 0110 0z"
|
|
25
|
+
fill="#FFF"
|
|
26
|
+
/>
|
|
27
|
+
<path
|
|
28
|
+
d="M5.3 10.6l10.4 6v11.1l-10.4-6v-11zm11.4-6.2l9.7 5.5-9.7 5.6V4.4z"
|
|
29
|
+
fill="#555AB9"
|
|
30
|
+
/>
|
|
31
|
+
<path
|
|
32
|
+
d="M27.2 10.6v11.2l-10.5 6V16.5l10.5-6zM15.7 4.4v11L6 10l9.7-5.5z"
|
|
33
|
+
fill="#91BAF8"
|
|
34
|
+
/>
|
|
35
|
+
</g>
|
|
36
|
+
</svg>
|
|
37
|
+
<h1>Acme</h1>
|
|
38
|
+
</div>
|
|
39
|
+
<div>
|
|
40
|
+
{user ? (
|
|
41
|
+
<>
|
|
42
|
+
<span className="welcome">
|
|
43
|
+
Welcome, <b>{user.name}</b>!
|
|
44
|
+
</span>
|
|
45
|
+
<Button size="small" onClick={onLogout} label="Log out" />
|
|
46
|
+
</>
|
|
47
|
+
) : (
|
|
48
|
+
<>
|
|
49
|
+
<Button size="small" onClick={onLogin} label="Log in" />
|
|
50
|
+
<Button primary size="small" onClick={onCreateAccount} label="Sign up" />
|
|
51
|
+
</>
|
|
52
|
+
)}
|
|
53
|
+
</div>
|
|
54
|
+
</div>
|
|
55
|
+
</header>
|
|
56
|
+
);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
import { expect, userEvent, within } from '@storybook/test';
|
|
3
|
+
|
|
4
|
+
import { Page } from './Page';
|
|
5
|
+
|
|
6
|
+
const meta: Meta<typeof Page> = {
|
|
7
|
+
title: 'Example/Page',
|
|
8
|
+
component: Page,
|
|
9
|
+
parameters: {
|
|
10
|
+
// More on how to position stories at: https://storybook.js.org/docs/configure/story-layout
|
|
11
|
+
layout: 'fullscreen',
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default meta;
|
|
16
|
+
type Story = StoryObj<typeof Page>;
|
|
17
|
+
|
|
18
|
+
export const LoggedOut: Story = {};
|
|
19
|
+
|
|
20
|
+
// More on component testing: https://storybook.js.org/docs/writing-tests/component-testing
|
|
21
|
+
export const LoggedIn: Story = {
|
|
22
|
+
play: async ({ canvasElement }) => {
|
|
23
|
+
const canvas = within(canvasElement);
|
|
24
|
+
const loginButton = canvas.getByRole('button', { name: /Log in/i });
|
|
25
|
+
await expect(loginButton).toBeInTheDocument();
|
|
26
|
+
await userEvent.click(loginButton);
|
|
27
|
+
await expect(loginButton).not.toBeInTheDocument();
|
|
28
|
+
|
|
29
|
+
const logoutButton = canvas.getByRole('button', { name: /Log out/i });
|
|
30
|
+
await expect(logoutButton).toBeInTheDocument();
|
|
31
|
+
},
|
|
32
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
import { Header } from './Header';
|
|
4
|
+
import './page.css';
|
|
5
|
+
|
|
6
|
+
type User = {
|
|
7
|
+
name: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const Page: React.FC = () => {
|
|
11
|
+
const [user, setUser] = React.useState<User>();
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<article>
|
|
15
|
+
<Header
|
|
16
|
+
user={user}
|
|
17
|
+
onLogin={() => setUser({ name: 'Jane Doe' })}
|
|
18
|
+
onLogout={() => setUser(undefined)}
|
|
19
|
+
onCreateAccount={() => setUser({ name: 'Jane Doe' })}
|
|
20
|
+
/>
|
|
21
|
+
|
|
22
|
+
<section className="storybook-page">
|
|
23
|
+
<h2>Pages in Storybook</h2>
|
|
24
|
+
<p>
|
|
25
|
+
We recommend building UIs with a{' '}
|
|
26
|
+
<a href="https://componentdriven.org" target="_blank" rel="noopener noreferrer">
|
|
27
|
+
<strong>component-driven</strong>
|
|
28
|
+
</a>{' '}
|
|
29
|
+
process starting with atomic components and ending with pages.
|
|
30
|
+
</p>
|
|
31
|
+
<p>
|
|
32
|
+
Render pages with mock data. This makes it easy to build and review page states without
|
|
33
|
+
needing to navigate to them in your app. Here are some handy patterns for managing page
|
|
34
|
+
data in Storybook:
|
|
35
|
+
</p>
|
|
36
|
+
<ul>
|
|
37
|
+
<li>
|
|
38
|
+
Use a higher-level connected component. Storybook helps you compose such data from the
|
|
39
|
+
"args" of child component stories
|
|
40
|
+
</li>
|
|
41
|
+
<li>
|
|
42
|
+
Assemble data in the page component from your services. You can mock these services out
|
|
43
|
+
using Storybook.
|
|
44
|
+
</li>
|
|
45
|
+
</ul>
|
|
46
|
+
<p>
|
|
47
|
+
Get a guided tutorial on component-driven development at{' '}
|
|
48
|
+
<a href="https://storybook.js.org/tutorials/" target="_blank" rel="noopener noreferrer">
|
|
49
|
+
Storybook tutorials
|
|
50
|
+
</a>
|
|
51
|
+
. Read more in the{' '}
|
|
52
|
+
<a href="https://storybook.js.org/docs" target="_blank" rel="noopener noreferrer">
|
|
53
|
+
docs
|
|
54
|
+
</a>
|
|
55
|
+
.
|
|
56
|
+
</p>
|
|
57
|
+
<div className="tip-wrapper">
|
|
58
|
+
<span className="tip">Tip</span> Adjust the width of the canvas with the{' '}
|
|
59
|
+
<svg width="10" height="10" viewBox="0 0 12 12" xmlns="http://www.w3.org/2000/svg">
|
|
60
|
+
<g fill="none" fillRule="evenodd">
|
|
61
|
+
<path
|
|
62
|
+
d="M1.5 5.2h4.8c.3 0 .5.2.5.4v5.1c-.1.2-.3.3-.4.3H1.4a.5.5 0 01-.5-.4V5.7c0-.3.2-.5.5-.5zm0-2.1h6.9c.3 0 .5.2.5.4v7a.5.5 0 01-1 0V4H1.5a.5.5 0 010-1zm0-2.1h9c.3 0 .5.2.5.4v9.1a.5.5 0 01-1 0V2H1.5a.5.5 0 010-1zm4.3 5.2H2V10h3.8V6.2z"
|
|
63
|
+
id="a"
|
|
64
|
+
fill="#999"
|
|
65
|
+
/>
|
|
66
|
+
</g>
|
|
67
|
+
</svg>
|
|
68
|
+
Viewports addon in the toolbar
|
|
69
|
+
</div>
|
|
70
|
+
</section>
|
|
71
|
+
</article>
|
|
72
|
+
);
|
|
73
|
+
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { Meta, StoryObj } from '@storybook/
|
|
2
|
-
|
|
3
|
-
import { expect, userEvent, within } from 'storybook/test';
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
import { expect, userEvent, within } from '@storybook/test';
|
|
4
3
|
|
|
5
4
|
import { Page } from './Page';
|
|
6
5
|
|
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
interface MockResultReturn<T> {
|
|
2
|
-
type: "return";
|
|
3
|
-
/**
|
|
4
|
-
* The value that was returned from the function. If function returned a Promise, then this will be a resolved value.
|
|
5
|
-
*/
|
|
6
|
-
value: T;
|
|
7
|
-
}
|
|
8
|
-
interface MockResultIncomplete {
|
|
9
|
-
type: "incomplete";
|
|
10
|
-
value: undefined;
|
|
11
|
-
}
|
|
12
|
-
interface MockResultThrow {
|
|
13
|
-
type: "throw";
|
|
14
|
-
/**
|
|
15
|
-
* An error that was thrown during function execution.
|
|
16
|
-
*/
|
|
17
|
-
value: any;
|
|
18
|
-
}
|
|
19
|
-
interface MockSettledResultFulfilled<T> {
|
|
20
|
-
type: "fulfilled";
|
|
21
|
-
value: T;
|
|
22
|
-
}
|
|
23
|
-
interface MockSettledResultRejected {
|
|
24
|
-
type: "rejected";
|
|
25
|
-
value: any;
|
|
26
|
-
}
|
|
27
|
-
type MockResult<T> = MockResultReturn<T> | MockResultThrow | MockResultIncomplete;
|
|
28
|
-
type MockSettledResult<T> = MockSettledResultFulfilled<T> | MockSettledResultRejected;
|
|
29
|
-
interface MockContext<T extends Procedure> {
|
|
30
|
-
/**
|
|
31
|
-
* This is an array containing all arguments for each call. One item of the array is the arguments of that call.
|
|
32
|
-
*
|
|
33
|
-
* @see https://vitest.dev/api/mock#mock-calls
|
|
34
|
-
* @example
|
|
35
|
-
* const fn = vi.fn()
|
|
36
|
-
*
|
|
37
|
-
* fn('arg1', 'arg2')
|
|
38
|
-
* fn('arg3')
|
|
39
|
-
*
|
|
40
|
-
* fn.mock.calls === [
|
|
41
|
-
* ['arg1', 'arg2'], // first call
|
|
42
|
-
* ['arg3'], // second call
|
|
43
|
-
* ]
|
|
44
|
-
*/
|
|
45
|
-
calls: Parameters<T>[];
|
|
46
|
-
/**
|
|
47
|
-
* 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.
|
|
48
|
-
* @see https://vitest.dev/api/mock#mock-instances
|
|
49
|
-
*/
|
|
50
|
-
instances: ReturnType<T>[];
|
|
51
|
-
/**
|
|
52
|
-
* An array of `this` values that were used during each call to the mock function.
|
|
53
|
-
* @see https://vitest.dev/api/mock#mock-contexts
|
|
54
|
-
*/
|
|
55
|
-
contexts: ThisParameterType<T>[];
|
|
56
|
-
/**
|
|
57
|
-
* The order of mock's execution. This returns an array of numbers which are shared between all defined mocks.
|
|
58
|
-
*
|
|
59
|
-
* @see https://vitest.dev/api/mock#mock-invocationcallorder
|
|
60
|
-
* @example
|
|
61
|
-
* const fn1 = vi.fn()
|
|
62
|
-
* const fn2 = vi.fn()
|
|
63
|
-
*
|
|
64
|
-
* fn1()
|
|
65
|
-
* fn2()
|
|
66
|
-
* fn1()
|
|
67
|
-
*
|
|
68
|
-
* fn1.mock.invocationCallOrder === [1, 3]
|
|
69
|
-
* fn2.mock.invocationCallOrder === [2]
|
|
70
|
-
*/
|
|
71
|
-
invocationCallOrder: number[];
|
|
72
|
-
/**
|
|
73
|
-
* This is an array containing all values that were `returned` from the function.
|
|
74
|
-
*
|
|
75
|
-
* 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.
|
|
76
|
-
*
|
|
77
|
-
* @see https://vitest.dev/api/mock#mock-results
|
|
78
|
-
* @example
|
|
79
|
-
* const fn = vi.fn()
|
|
80
|
-
* .mockReturnValueOnce('result')
|
|
81
|
-
* .mockImplementationOnce(() => { throw new Error('thrown error') })
|
|
82
|
-
*
|
|
83
|
-
* const result = fn()
|
|
84
|
-
*
|
|
85
|
-
* try {
|
|
86
|
-
* fn()
|
|
87
|
-
* }
|
|
88
|
-
* catch {}
|
|
89
|
-
*
|
|
90
|
-
* fn.mock.results === [
|
|
91
|
-
* {
|
|
92
|
-
* type: 'return',
|
|
93
|
-
* value: 'result',
|
|
94
|
-
* },
|
|
95
|
-
* {
|
|
96
|
-
* type: 'throw',
|
|
97
|
-
* value: Error,
|
|
98
|
-
* },
|
|
99
|
-
* ]
|
|
100
|
-
*/
|
|
101
|
-
results: MockResult<ReturnType<T>>[];
|
|
102
|
-
/**
|
|
103
|
-
* An array containing all values that were `resolved` or `rejected` from the function.
|
|
104
|
-
*
|
|
105
|
-
* This array will be empty if the function was never resolved or rejected.
|
|
106
|
-
*
|
|
107
|
-
* @see https://vitest.dev/api/mock#mock-settledresults
|
|
108
|
-
* @example
|
|
109
|
-
* const fn = vi.fn().mockResolvedValueOnce('result')
|
|
110
|
-
*
|
|
111
|
-
* const result = fn()
|
|
112
|
-
*
|
|
113
|
-
* fn.mock.settledResults === []
|
|
114
|
-
* fn.mock.results === [
|
|
115
|
-
* {
|
|
116
|
-
* type: 'return',
|
|
117
|
-
* value: Promise<'result'>,
|
|
118
|
-
* },
|
|
119
|
-
* ]
|
|
120
|
-
*
|
|
121
|
-
* await result
|
|
122
|
-
*
|
|
123
|
-
* fn.mock.settledResults === [
|
|
124
|
-
* {
|
|
125
|
-
* type: 'fulfilled',
|
|
126
|
-
* value: 'result',
|
|
127
|
-
* },
|
|
128
|
-
* ]
|
|
129
|
-
*/
|
|
130
|
-
settledResults: MockSettledResult<Awaited<ReturnType<T>>>[];
|
|
131
|
-
/**
|
|
132
|
-
* This contains the arguments of the last call. If spy wasn't called, will return `undefined`.
|
|
133
|
-
* @see https://vitest.dev/api/mock#mock-lastcall
|
|
134
|
-
*/
|
|
135
|
-
lastCall: Parameters<T> | undefined;
|
|
136
|
-
}
|
|
137
|
-
type Procedure = (...args: any[]) => any;
|
|
138
|
-
type NormalizedProcedure<T extends Procedure> = (...args: Parameters<T>) => ReturnType<T>;
|
|
139
|
-
interface MockInstance<T extends Procedure = Procedure> {
|
|
140
|
-
/**
|
|
141
|
-
* Use it to return the name assigned to the mock with the `.mockName(name)` method. By default, it will return `vi.fn()`.
|
|
142
|
-
* @see https://vitest.dev/api/mock#getmockname
|
|
143
|
-
*/
|
|
144
|
-
getMockName(): string;
|
|
145
|
-
/**
|
|
146
|
-
* Sets the internal mock name. This is useful for identifying the mock when an assertion fails.
|
|
147
|
-
* @see https://vitest.dev/api/mock#mockname
|
|
148
|
-
*/
|
|
149
|
-
mockName(name: string): this;
|
|
150
|
-
/**
|
|
151
|
-
* Current context of the mock. It stores information about all invocation calls, instances, and results.
|
|
152
|
-
*/
|
|
153
|
-
mock: MockContext<T>;
|
|
154
|
-
/**
|
|
155
|
-
* 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.
|
|
156
|
-
*
|
|
157
|
-
* To automatically call this method before each test, enable the [`clearMocks`](https://vitest.dev/config/#clearmocks) setting in the configuration.
|
|
158
|
-
* @see https://vitest.dev/api/mock#mockclear
|
|
159
|
-
*/
|
|
160
|
-
mockClear(): this;
|
|
161
|
-
/**
|
|
162
|
-
* Does what `mockClear` does and resets inner implementation to the original function. This also resets all "once" implementations.
|
|
163
|
-
*
|
|
164
|
-
* Note that resetting a mock from `vi.fn()` will set implementation to an empty function that returns `undefined`.
|
|
165
|
-
* Resetting a mock from `vi.fn(impl)` will set implementation to `impl`. It is useful for completely resetting a mock to its default state.
|
|
166
|
-
*
|
|
167
|
-
* To automatically call this method before each test, enable the [`mockReset`](https://vitest.dev/config/#mockreset) setting in the configuration.
|
|
168
|
-
* @see https://vitest.dev/api/mock#mockreset
|
|
169
|
-
*/
|
|
170
|
-
mockReset(): this;
|
|
171
|
-
/**
|
|
172
|
-
* Does what `mockReset` does and restores original descriptors of spied-on objects.
|
|
173
|
-
*
|
|
174
|
-
* 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`.
|
|
175
|
-
* @see https://vitest.dev/api/mock#mockrestore
|
|
176
|
-
*/
|
|
177
|
-
mockRestore(): void;
|
|
178
|
-
/**
|
|
179
|
-
* Returns current permanent mock implementation if there is one.
|
|
180
|
-
*
|
|
181
|
-
* If mock was created with `vi.fn`, it will consider passed down method as a mock implementation.
|
|
182
|
-
*
|
|
183
|
-
* If mock was created with `vi.spyOn`, it will return `undefined` unless a custom implementation was provided.
|
|
184
|
-
*/
|
|
185
|
-
getMockImplementation(): NormalizedProcedure<T> | undefined;
|
|
186
|
-
/**
|
|
187
|
-
* Accepts a function to be used as the mock implementation. TypeScript expects the arguments and return type to match those of the original function.
|
|
188
|
-
* @see https://vitest.dev/api/mock#mockimplementation
|
|
189
|
-
* @example
|
|
190
|
-
* const increment = vi.fn().mockImplementation(count => count + 1);
|
|
191
|
-
* expect(increment(3)).toBe(4);
|
|
192
|
-
*/
|
|
193
|
-
mockImplementation(fn: NormalizedProcedure<T>): this;
|
|
194
|
-
/**
|
|
195
|
-
* 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.
|
|
196
|
-
*
|
|
197
|
-
* 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.
|
|
198
|
-
* @see https://vitest.dev/api/mock#mockimplementationonce
|
|
199
|
-
* @example
|
|
200
|
-
* const fn = vi.fn(count => count).mockImplementationOnce(count => count + 1);
|
|
201
|
-
* expect(fn(3)).toBe(4);
|
|
202
|
-
* expect(fn(3)).toBe(3);
|
|
203
|
-
*/
|
|
204
|
-
mockImplementationOnce(fn: NormalizedProcedure<T>): this;
|
|
205
|
-
/**
|
|
206
|
-
* Overrides the original mock implementation temporarily while the callback is being executed.
|
|
207
|
-
*
|
|
208
|
-
* Note that this method takes precedence over the [`mockImplementationOnce`](https://vitest.dev/api/mock#mockimplementationonce).
|
|
209
|
-
* @see https://vitest.dev/api/mock#withimplementation
|
|
210
|
-
* @example
|
|
211
|
-
* const myMockFn = vi.fn(() => 'original')
|
|
212
|
-
*
|
|
213
|
-
* myMockFn.withImplementation(() => 'temp', () => {
|
|
214
|
-
* myMockFn() // 'temp'
|
|
215
|
-
* })
|
|
216
|
-
*
|
|
217
|
-
* myMockFn() // 'original'
|
|
218
|
-
*/
|
|
219
|
-
withImplementation<T2>(fn: NormalizedProcedure<T>, cb: () => T2): T2 extends Promise<unknown> ? Promise<this> : this;
|
|
220
|
-
/**
|
|
221
|
-
* Use this if you need to return the `this` context from the method without invoking the actual implementation.
|
|
222
|
-
* @see https://vitest.dev/api/mock#mockreturnthis
|
|
223
|
-
*/
|
|
224
|
-
mockReturnThis(): this;
|
|
225
|
-
/**
|
|
226
|
-
* 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.
|
|
227
|
-
* @see https://vitest.dev/api/mock#mockreturnvalue
|
|
228
|
-
* @example
|
|
229
|
-
* const mock = vi.fn()
|
|
230
|
-
* mock.mockReturnValue(42)
|
|
231
|
-
* mock() // 42
|
|
232
|
-
* mock.mockReturnValue(43)
|
|
233
|
-
* mock() // 43
|
|
234
|
-
*/
|
|
235
|
-
mockReturnValue(value: ReturnType<T>): this;
|
|
236
|
-
/**
|
|
237
|
-
* 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.
|
|
238
|
-
*
|
|
239
|
-
* 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.
|
|
240
|
-
* @example
|
|
241
|
-
* const myMockFn = vi
|
|
242
|
-
* .fn()
|
|
243
|
-
* .mockReturnValue('default')
|
|
244
|
-
* .mockReturnValueOnce('first call')
|
|
245
|
-
* .mockReturnValueOnce('second call')
|
|
246
|
-
*
|
|
247
|
-
* // 'first call', 'second call', 'default'
|
|
248
|
-
* console.log(myMockFn(), myMockFn(), myMockFn())
|
|
249
|
-
*/
|
|
250
|
-
mockReturnValueOnce(value: ReturnType<T>): this;
|
|
251
|
-
/**
|
|
252
|
-
* 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.
|
|
253
|
-
* @example
|
|
254
|
-
* const asyncMock = vi.fn().mockResolvedValue(42)
|
|
255
|
-
* asyncMock() // Promise<42>
|
|
256
|
-
*/
|
|
257
|
-
mockResolvedValue(value: Awaited<ReturnType<T>>): this;
|
|
258
|
-
/**
|
|
259
|
-
* 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.
|
|
260
|
-
* @example
|
|
261
|
-
* const myMockFn = vi
|
|
262
|
-
* .fn()
|
|
263
|
-
* .mockResolvedValue('default')
|
|
264
|
-
* .mockResolvedValueOnce('first call')
|
|
265
|
-
* .mockResolvedValueOnce('second call')
|
|
266
|
-
*
|
|
267
|
-
* // Promise<'first call'>, Promise<'second call'>, Promise<'default'>
|
|
268
|
-
* console.log(myMockFn(), myMockFn(), myMockFn())
|
|
269
|
-
*/
|
|
270
|
-
mockResolvedValueOnce(value: Awaited<ReturnType<T>>): this;
|
|
271
|
-
/**
|
|
272
|
-
* Accepts an error that will be rejected when async function is called.
|
|
273
|
-
* @example
|
|
274
|
-
* const asyncMock = vi.fn().mockRejectedValue(new Error('Async error'))
|
|
275
|
-
* await asyncMock() // throws Error<'Async error'>
|
|
276
|
-
*/
|
|
277
|
-
mockRejectedValue(error: unknown): this;
|
|
278
|
-
/**
|
|
279
|
-
* Accepts a value that will be rejected during the next function call. If chained, each consecutive call will reject the specified value.
|
|
280
|
-
* @example
|
|
281
|
-
* const asyncMock = vi
|
|
282
|
-
* .fn()
|
|
283
|
-
* .mockResolvedValueOnce('first call')
|
|
284
|
-
* .mockRejectedValueOnce(new Error('Async error'))
|
|
285
|
-
*
|
|
286
|
-
* await asyncMock() // first call
|
|
287
|
-
* await asyncMock() // throws Error<'Async error'>
|
|
288
|
-
*/
|
|
289
|
-
mockRejectedValueOnce(error: unknown): this;
|
|
290
|
-
}
|
|
291
|
-
interface Mock<T extends Procedure = Procedure> extends MockInstance<T> {
|
|
292
|
-
new (...args: Parameters<T>): ReturnType<T>;
|
|
293
|
-
(...args: Parameters<T>): ReturnType<T>;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
export { Mock as M };
|