Skip to content

Commit 3777a86

Browse files
committed
chore(ComboBox): better story tests
1 parent 2dbc001 commit 3777a86

File tree

1 file changed

+98
-10
lines changed

1 file changed

+98
-10
lines changed

src/components/fields/ComboBox/ComboBox.stories.tsx

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DollarCircleOutlined } from '@ant-design/icons';
22
import { Meta, StoryFn } from '@storybook/react';
3+
import { expect, jest } from '@storybook/jest';
34
import { userEvent, within } from '@storybook/test';
45

56
import { SELECTED_KEY_ARG } from '../../../stories/FormFieldArgs';
@@ -40,13 +41,20 @@ const TemplateForm: StoryFn<CubeComboBoxProps<any>> = (
4041
args: CubeComboBoxProps<any>,
4142
) => {
4243
const [form] = Form.useForm();
44+
const onSubmit = (data: any) => {
45+
console.log('! submit', data);
46+
if (args.onSubmitSpy) {
47+
args.onSubmitSpy(data);
48+
}
49+
return data;
50+
};
4351

4452
return (
4553
<Flow gap="2x">
4654
<Form
4755
form={form}
4856
defaultValues={{ combobox: args.allowsCustomValue ? 'unknown' : 'red' }}
49-
onSubmit={(data) => console.log('! submit', data)}
57+
onSubmit={onSubmit}
5058
>
5159
<ComboBox
5260
name="combobox"
@@ -83,6 +91,9 @@ const TemplateForm: StoryFn<CubeComboBoxProps<any>> = (
8391
{args.allowsCustomValue ? 'violet' : 'Violet'}
8492
</ComboBox.Item>
8593
</ComboBox>
94+
<Button type="submit" data-testid="SubmitButton">
95+
Submit
96+
</Button>
8697
</Form>
8798
<Button>Focus</Button>
8899
</Flow>
@@ -93,13 +104,20 @@ const TemplateFormPropagation: StoryFn<CubeComboBoxProps<any>> = (
93104
args: CubeComboBoxProps<any>,
94105
) => {
95106
const [form] = Form.useForm();
107+
const onSubmit = (data: any) => {
108+
console.log('! submit', data);
109+
if (args.onSubmitSpy) {
110+
args.onSubmitSpy(data);
111+
}
112+
return data;
113+
};
96114

97115
return (
98116
<Flow gap="2x">
99117
<Form
100118
form={form}
101119
defaultValues={{ combobox: args.allowsCustomValue ? 'unknown' : 'red' }}
102-
onSubmit={(data) => console.log('! submit', data)}
120+
onSubmit={onSubmit}
103121
>
104122
<ComboBox
105123
name="combobox"
@@ -136,7 +154,7 @@ const TemplateFormPropagation: StoryFn<CubeComboBoxProps<any>> = (
136154
{args.allowsCustomValue ? 'violet' : 'Violet'}
137155
</ComboBox.Item>
138156
</ComboBox>
139-
<Form.Submit>Submit</Form.Submit>
157+
<Form.Submit data-testid="FormSubmit">Submit</Form.Submit>
140158
</Form>
141159
</Flow>
142160
);
@@ -146,13 +164,20 @@ const TemplateLegacyForm: StoryFn<CubeComboBoxProps<any>> = (
146164
args: CubeComboBoxProps<any>,
147165
) => {
148166
const [form] = Form.useForm();
167+
const onSubmit = (data: any) => {
168+
console.log('! Submit', data);
169+
if (args.onSubmitSpy) {
170+
args.onSubmitSpy(data);
171+
}
172+
return data;
173+
};
149174

150175
return (
151176
<Flow gap="2x">
152177
<Form
153178
form={form}
154179
defaultValues={{ combobox: args.allowsCustomValue ? 'unknown' : 'red' }}
155-
onSubmit={(data) => console.log('! Submit', data)}
180+
onSubmit={onSubmit}
156181
>
157182
<Field
158183
name="combobox"
@@ -188,6 +213,9 @@ const TemplateLegacyForm: StoryFn<CubeComboBoxProps<any>> = (
188213
</ComboBox.Item>
189214
</ComboBox>
190215
</Field>
216+
<Button type="submit" data-testid="SubmitButton">
217+
Submit
218+
</Button>
191219
</Form>
192220
<Button>Focus</Button>
193221
</Flow>
@@ -265,13 +293,17 @@ With1LongOptionFiltered.play = async ({ canvasElement }) => {
265293
};
266294

267295
export const WithinForm = TemplateForm.bind({});
268-
WithinForm.play = async ({ canvasElement }) => {
296+
WithinForm.args = {
297+
onSubmitSpy: jest.fn(),
298+
};
299+
WithinForm.play = async ({ canvasElement, args }) => {
269300
const { getByRole, getByTestId } = within(canvasElement);
270301

271302
const combobox = getByRole('combobox');
272303
const trigger = getByTestId('ComboBoxTrigger');
273304
const button = getByTestId('Button');
274305
const input = getByTestId('Input');
306+
const submitButton = getByTestId('SubmitButton');
275307

276308
await userEvent.click(combobox);
277309
await userEvent.click(trigger);
@@ -281,16 +313,31 @@ WithinForm.play = async ({ canvasElement }) => {
281313
input,
282314
'{backspace}{backspace}{backspace}{backspace}{backspace}{backspace}{backspace}blue',
283315
);
316+
317+
// Submit the form
318+
await userEvent.click(submitButton);
319+
320+
// Check that the onSubmit callback was called with the expected data
321+
await wait(100);
322+
expect(args.onSubmitSpy).toHaveBeenCalledWith(
323+
expect.objectContaining({
324+
combobox: 'blue',
325+
}),
326+
);
284327
};
285328

286329
export const WithinFormSelected = TemplateForm.bind({});
287-
WithinFormSelected.play = async ({ canvasElement }) => {
330+
WithinFormSelected.args = {
331+
onSubmitSpy: jest.fn(),
332+
};
333+
WithinFormSelected.play = async ({ canvasElement, args }) => {
288334
const { getByRole, getByTestId } = within(canvasElement);
289335

290336
const combobox = getByRole('combobox');
291337
const trigger = getByTestId('ComboBoxTrigger');
292338
const button = getByTestId('Button');
293339
const input = getByTestId('Input');
340+
const submitButton = getByTestId('SubmitButton');
294341

295342
await userEvent.click(combobox);
296343
await userEvent.click(trigger);
@@ -300,13 +347,25 @@ WithinFormSelected.play = async ({ canvasElement }) => {
300347
input,
301348
'{backspace}{backspace}{backspace}{backspace}{backspace}{backspace}{backspace}blue{enter}',
302349
);
350+
351+
// Submit the form
352+
await userEvent.click(submitButton);
353+
354+
// Check that the onSubmit callback was called with the expected data
355+
await wait(100);
356+
expect(args.onSubmitSpy).toHaveBeenCalledWith(
357+
expect.objectContaining({
358+
combobox: 'blue',
359+
}),
360+
);
303361
};
304362

305363
export const WithinFormWithCustomValue = TemplateForm.bind({});
306364
WithinFormWithCustomValue.play = WithinForm.play;
307365
WithinFormWithCustomValue.args = {
308366
...TemplateForm.args,
309367
allowsCustomValue: true,
368+
onSubmitSpy: jest.fn(),
310369
};
311370

312371
export const WithinFormWithLegacyFieldAndCustomValue = TemplateLegacyForm.bind(
@@ -316,10 +375,14 @@ WithinFormWithLegacyFieldAndCustomValue.play = WithinForm.play;
316375
WithinFormWithLegacyFieldAndCustomValue.args = {
317376
...TemplateForm.args,
318377
allowsCustomValue: true,
378+
onSubmitSpy: jest.fn(),
319379
};
320380

321381
export const WithinFormStopEnterPropagation = TemplateFormPropagation.bind({});
322-
WithinFormStopEnterPropagation.play = async ({ canvasElement }) => {
382+
WithinFormStopEnterPropagation.args = {
383+
onSubmitSpy: jest.fn(),
384+
};
385+
WithinFormStopEnterPropagation.play = async ({ canvasElement, args }) => {
323386
const { getByTestId } = within(canvasElement);
324387

325388
const input = getByTestId('Input');
@@ -328,15 +391,40 @@ WithinFormStopEnterPropagation.play = async ({ canvasElement }) => {
328391
input,
329392
'{backspace}{backspace}{backspace}{backspace}{backspace}{backspace}{backspace}blurring{enter}',
330393
);
394+
395+
// The enter key should submit the form
396+
await wait(100);
397+
398+
// Check that the onSubmit callback was called with the expected data
399+
expect(args.onSubmitSpy).toHaveBeenCalledWith(
400+
expect.objectContaining({
401+
combobox: 'blurring',
402+
}),
403+
);
331404
};
332405

333406
export const WithinFormStopBlurPropagation = TemplateFormPropagation.bind({});
334-
WithinFormStopBlurPropagation.play = async ({ canvasElement }) => {
407+
WithinFormStopBlurPropagation.args = {
408+
onSubmitSpy: jest.fn(),
409+
};
410+
WithinFormStopBlurPropagation.play = async ({ canvasElement, args }) => {
335411
const { getByTestId } = within(canvasElement);
336412

337413
const input = getByTestId('Input');
414+
const submitButton = getByTestId('Button');
338415

339416
await userEvent.type(input, '!');
340-
const button = getByTestId('Button');
341-
await userEvent.click(button);
417+
await userEvent.click(submitButton);
418+
419+
// Now submit the form
420+
const formSubmit = getByTestId('FormSubmit');
421+
await userEvent.click(formSubmit);
422+
423+
// Check that the onSubmit callback was called with the expected data
424+
await wait(100);
425+
expect(args.onSubmitSpy).toHaveBeenCalledWith(
426+
expect.objectContaining({
427+
combobox: 'red!',
428+
}),
429+
);
342430
};

0 commit comments

Comments
 (0)