-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
public-types.test.tsx
276 lines (225 loc) · 8.15 KB
/
public-types.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { describe, test } from '@jest/globals';
import { satisfies } from '@storybook/core-common';
import type { Args, StoryAnnotations, StrictArgs } from '@storybook/types';
import { expectTypeOf } from 'expect-type';
import type { KeyboardEventHandler, ReactNode } from 'react';
import React from 'react';
import type { SetOptional } from 'type-fest';
import type { Decorator, Meta, StoryObj } from './public-types';
import type { ReactRenderer } from './types';
type ReactStory<TArgs, TRequiredArgs> = StoryAnnotations<ReactRenderer, TArgs, TRequiredArgs>;
type ButtonProps = { label: string; disabled: boolean };
const Button: (props: ButtonProps) => JSX.Element = () => <></>;
describe('Args can be provided in multiple ways', () => {
test('✅ All required args may be provided in meta', () => {
const meta = satisfies<Meta<typeof Button>>()({
component: Button,
args: { label: 'good', disabled: false },
});
type Story = StoryObj<typeof meta>;
const Basic: Story = {};
expectTypeOf(Basic).toEqualTypeOf<
ReactStory<ButtonProps, SetOptional<ButtonProps, 'label' | 'disabled'>>
>();
});
test('✅ Required args may be provided partial in meta and the story', () => {
const meta = satisfies<Meta<typeof Button>>()({
component: Button,
args: { label: 'good' },
});
const Basic: StoryObj<typeof meta> = {
args: { disabled: false },
};
type Expected = ReactStory<ButtonProps, SetOptional<ButtonProps, 'label'>>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
});
test('❌ The combined shape of meta args and story args must match the required args.', () => {
{
const meta = satisfies<Meta<typeof Button>>()({ component: Button });
const Basic: StoryObj<typeof meta> = {
// @ts-expect-error disabled not provided ❌
args: { label: 'good' },
};
type Expected = ReactStory<ButtonProps, ButtonProps>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
}
{
const meta = satisfies<Meta<typeof Button>>()({
component: Button,
args: { label: 'good' },
});
// @ts-expect-error disabled not provided ❌
const Basic: StoryObj<typeof meta> = {};
type Expected = ReactStory<ButtonProps, SetOptional<ButtonProps, 'label'>>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
}
{
const meta = satisfies<Meta<ButtonProps>>()({ component: Button });
const Basic: StoryObj<typeof meta> = {
// @ts-expect-error disabled not provided ❌
args: { label: 'good' },
};
type Expected = ReactStory<ButtonProps, ButtonProps>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
}
});
test('Component can be used as generic parameter for StoryObj', () => {
type Expected = ReactStory<ButtonProps, Partial<ButtonProps>>;
expectTypeOf<StoryObj<typeof Button>>().toEqualTypeOf<Expected>();
});
});
test('✅ All void functions are optional', () => {
interface CmpProps {
label: string;
disabled: boolean;
onClick(): void;
onKeyDown: KeyboardEventHandler;
onLoading: (s: string) => JSX.Element;
submitAction(): void;
}
const Cmp: (props: CmpProps) => JSX.Element = () => <></>;
const meta = satisfies<Meta<CmpProps>>()({
component: Cmp,
args: { label: 'good' },
});
const Basic: StoryObj<typeof meta> = {
args: { disabled: false, onLoading: () => <div>Loading...</div> },
};
type Expected = ReactStory<
CmpProps,
SetOptional<CmpProps, 'label' | 'onClick' | 'onKeyDown' | 'submitAction'>
>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
});
type ThemeData = 'light' | 'dark';
declare const Theme: (props: { theme: ThemeData; children?: ReactNode }) => JSX.Element;
describe('Story args can be inferred', () => {
test('Correct args are inferred when type is widened for render function', () => {
type Props = ButtonProps & { theme: ThemeData };
const meta = satisfies<Meta<Props>>()({
component: Button,
args: { disabled: false },
render: (args, { component }) => {
// component is not null as it is provided in meta
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const Component = component!;
return (
<Theme theme={args.theme}>
<Component {...args} />
</Theme>
);
},
});
const Basic: StoryObj<typeof meta> = { args: { theme: 'light', label: 'good' } };
type Expected = ReactStory<Props, SetOptional<Props, 'disabled'>>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
});
const withDecorator: Decorator<{ decoratorArg: number }> = (Story, { args }) => (
<>
Decorator: {args.decoratorArg}
<Story args={{ decoratorArg: 0 }} />
</>
);
test('Correct args are inferred when type is widened for decorators', () => {
type Props = ButtonProps & { decoratorArg: number };
const meta = satisfies<Meta<Props>>()({
component: Button,
args: { disabled: false },
decorators: [withDecorator],
});
const Basic: StoryObj<typeof meta> = { args: { decoratorArg: 0, label: 'good' } };
type Expected = ReactStory<Props, SetOptional<Props, 'disabled'>>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
});
test('Correct args are inferred when type is widened for multiple decorators', () => {
type Props = ButtonProps & { decoratorArg: number; decoratorArg2: string };
const secondDecorator: Decorator<{ decoratorArg2: string }> = (Story, { args }) => (
<>
Decorator: {args.decoratorArg2}
<Story />
</>
);
// decorator is not using args
const thirdDecorator: Decorator<Args> = (Story) => (
<>
<Story />
</>
);
// decorator is not using args
const fourthDecorator: Decorator<StrictArgs> = (Story) => (
<>
<Story />
</>
);
const meta = satisfies<Meta<Props>>()({
component: Button,
args: { disabled: false },
decorators: [withDecorator, secondDecorator, thirdDecorator, fourthDecorator],
});
const Basic: StoryObj<typeof meta> = {
args: { decoratorArg: 0, decoratorArg2: '', label: 'good' },
};
type Expected = ReactStory<Props, SetOptional<Props, 'disabled'>>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
});
});
test('StoryObj<typeof meta> is allowed when meta is upcasted to Meta<Props>', () => {
expectTypeOf<StoryObj<Meta<ButtonProps>>>().toEqualTypeOf<
ReactStory<
ButtonProps & { children?: ReactNode },
Partial<ButtonProps & { children?: ReactNode }>
>
>();
});
test('StoryObj<typeof meta> is allowed when meta is upcasted to Meta<typeof Cmp>', () => {
expectTypeOf<StoryObj<Meta<typeof Button>>>().toEqualTypeOf<
ReactStory<
ButtonProps & { children?: ReactNode },
Partial<ButtonProps & { children?: ReactNode }>
>
>();
});
test('StoryObj<typeof meta> is allowed when all arguments are optional', () => {
expectTypeOf<StoryObj<Meta<{ label?: string }>>>().toEqualTypeOf<
ReactStory<{ label?: string; children?: ReactNode }, { label?: string; children?: ReactNode }>
>();
});
test('Meta can be used without generic', () => {
expectTypeOf({ component: Button }).toMatchTypeOf<Meta>();
});
test('Props can be defined as interfaces, issue #21768', () => {
interface Props {
label: string;
}
const Component = ({ label }: Props) => <>{label}</>;
const withDecorator: Decorator = (Story) => (
<>
<Story />
</>
);
const meta = {
component: Component,
args: {
label: 'label',
},
decorators: [withDecorator],
} satisfies Meta<Props>;
const Basic: StoryObj<typeof meta> = {};
type Expected = ReactStory<Props, SetOptional<Props, 'label'>>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
});
test('Components without Props can be used, issue #21768', () => {
const Component = () => <>Foo</>;
const withDecorator: Decorator = (Story) => (
<>
<Story />
</>
);
const meta = {
component: Component,
decorators: [withDecorator],
} satisfies Meta<typeof Component>;
const Basic: StoryObj<typeof meta> = {};
type Expected = ReactStory<{}, {}>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
});