/
public-types.test.ts
192 lines (163 loc) · 6.1 KB
/
public-types.test.ts
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
import type { ComponentAnnotations, StoryAnnotations } from '@storybook/types';
import { expectTypeOf } from 'expect-type';
import type { SetOptional } from 'type-fest';
import type { ComponentOptions, FunctionalComponent } from 'vue';
import { h } from 'vue';
import type { DecoratorFn, Meta, StoryObj } from './public-types';
import type { VueFramework } from './types';
import Button from './__tests__/Button.vue';
import DecoratorTsVue from './__tests__/Decorator.vue';
import Decorator2TsVue from './__tests__/Decorator2.vue';
describe('Meta', () => {
test('Generic parameter of Meta can be a component', () => {
const meta: Meta<typeof Button> = {
component: Button,
args: { label: 'good', disabled: false },
};
expectTypeOf(meta).toEqualTypeOf<
ComponentAnnotations<
VueFramework,
{
readonly disabled: boolean;
readonly label: string;
onMyChangeEvent?: (id: number) => any;
onMyClickEvent?: (id: number) => any;
}
>
>();
});
test('Generic parameter of Meta can be the props of the component', () => {
const meta: Meta<{ disabled: boolean; label: string }> = {
component: Button,
args: { label: 'good', disabled: false },
};
expectTypeOf(meta).toEqualTypeOf<
ComponentAnnotations<VueFramework, { disabled: boolean; label: string }>
>();
});
test('Events are inferred from component', () => {
({
component: Button,
args: {
label: 'good',
disabled: false,
onMyChangeEvent: (value) => {
expectTypeOf(value).toEqualTypeOf<number>();
},
},
render: (args) => {
return h(Button, {
...args,
onMyChangeEvent: (value) => {
expectTypeOf(value).toEqualTypeOf<number>();
},
});
},
}) satisfies Meta<typeof Button>;
});
});
describe('StoryObj', () => {
type ButtonProps = {
readonly disabled: boolean;
readonly label: string;
onMyChangeEvent?: ((id: number) => any) | undefined;
onMyClickEvent?: ((id: number) => any) | undefined;
};
test('✅ Required args may be provided partial in meta and the story', () => {
const meta = {
component: Button,
args: { label: 'good' },
} satisfies Meta<typeof Button>;
type Actual = StoryObj<typeof meta>;
type Expected = StoryAnnotations<VueFramework, ButtonProps, SetOptional<ButtonProps, 'label'>>;
expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});
test('❌ The combined shape of meta args and story args must match the required args.', () => {
{
const meta = { component: Button } satisfies Meta<typeof Button>;
type Expected = StoryAnnotations<VueFramework, ButtonProps, ButtonProps>;
expectTypeOf<StoryObj<typeof meta>>().toEqualTypeOf<Expected>();
}
{
const meta = {
component: Button,
args: { label: 'good' },
} satisfies Meta<typeof Button>;
// @ts-expect-error disabled not provided ❌
const Basic: StoryObj<typeof meta> = {};
type Expected = StoryAnnotations<
VueFramework,
ButtonProps,
SetOptional<ButtonProps, 'label'>
>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
}
{
const meta = { component: Button } satisfies Meta<{ label: string; disabled: boolean }>;
const Basic: StoryObj<typeof meta> = {
// @ts-expect-error disabled not provided ❌
args: { label: 'good' },
};
type Expected = StoryAnnotations<VueFramework, ButtonProps, ButtonProps>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
}
});
test('Component can be used as generic parameter for StoryObj', () => {
expectTypeOf<StoryObj<typeof Button>>().toEqualTypeOf<
StoryAnnotations<VueFramework, ButtonProps>
>();
});
});
type ThemeData = 'light' | 'dark';
type ComponentProps<Component> = Component extends ComponentOptions<infer P>
? P
: Component extends FunctionalComponent<infer P>
? P
: never;
describe('Story args can be inferred', () => {
test('Correct args are inferred when type is widened for render function', () => {
type Props = ComponentProps<typeof Button> & { theme: ThemeData };
const meta = {
component: Button,
args: { disabled: false },
render: (args) => {
return h('div', [h('div', `Use the theme ${args.theme}`), h(Button, args)]);
},
} satisfies Meta<Props>;
const Basic: StoryObj<typeof meta> = { args: { theme: 'light', label: 'good' } };
type Expected = StoryAnnotations<VueFramework, Props, SetOptional<Props, 'disabled'>>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
});
const withDecorator: DecoratorFn<{ decoratorArg: string }> = (
storyFn,
{ args: { decoratorArg } }
) => h(DecoratorTsVue, { decoratorArg }, h(storyFn()));
test('Correct args are inferred when type is widened for decorators', () => {
type Props = ComponentProps<typeof Button> & { decoratorArg: string };
const meta = {
component: Button,
args: { disabled: false },
decorators: [withDecorator],
} satisfies Meta<Props>;
const Basic: StoryObj<typeof meta> = { args: { decoratorArg: 'title', label: 'good' } };
type Expected = StoryAnnotations<VueFramework, Props, SetOptional<Props, 'disabled'>>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
});
test('Correct args are inferred when type is widened for multiple decorators', () => {
type Props = ComponentProps<typeof Button> & { decoratorArg: string; decoratorArg2: string };
const secondDecorator: DecoratorFn<{ decoratorArg2: string }> = (
storyFn,
{ args: { decoratorArg2 } }
) => h(Decorator2TsVue, { decoratorArg2 }, h(storyFn()));
const meta = {
component: Button,
args: { disabled: false },
decorators: [withDecorator, secondDecorator],
} satisfies Meta<Props>;
const Basic: StoryObj<typeof meta> = {
args: { decoratorArg: '', decoratorArg2: '', label: 'good' },
};
type Expected = StoryAnnotations<VueFramework, Props, SetOptional<Props, 'disabled'>>;
expectTypeOf(Basic).toEqualTypeOf<Expected>();
});
});