-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [#52] Manage the appropriate form field values depending on openFor…
…ms.dataSrc
- Loading branch information
1 parent
fee85ae
commit 6184b95
Showing
6 changed files
with
227 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
185 changes: 185 additions & 0 deletions
185
src/components/builder/values/values-config.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
import {RadioComponentSchema, SelectboxesComponentSchema} from '@open-formulieren/types'; | ||
import {expect, jest} from '@storybook/jest'; | ||
import {Meta, StoryObj} from '@storybook/react'; | ||
import {userEvent, within} from '@storybook/testing-library'; | ||
import {Form, Formik} from 'formik'; | ||
|
||
import {withFormik} from '@/sb-decorators'; | ||
|
||
import ValuesConfig from './values-config'; | ||
|
||
export default { | ||
title: 'Formio/Builder/Values/ValuesConfig', | ||
component: ValuesConfig, | ||
parameters: { | ||
controls: {hideNoControlsWarning: true}, | ||
modal: {noModal: true}, | ||
formik: { | ||
initialValues: { | ||
openForms: { | ||
dataSrc: '', | ||
}, | ||
}, | ||
}, | ||
}, | ||
args: { | ||
name: 'values', | ||
}, | ||
argTypes: { | ||
name: {control: {disable: true}}, | ||
}, | ||
tags: ['autodocs'], | ||
} as Meta<typeof ValuesConfig>; | ||
|
||
type SelectboxesStory = StoryObj<typeof ValuesConfig<SelectboxesComponentSchema>>; | ||
type RadioStory = StoryObj<typeof ValuesConfig<RadioComponentSchema>>; | ||
|
||
/** | ||
* Variant pinned to the `SelectboxesComponentSchema` component type. | ||
*/ | ||
export const SelectBoxes: SelectboxesStory = { | ||
decorators: [withFormik], | ||
}; | ||
|
||
export const SelectBoxesManual: SelectboxesStory = { | ||
decorators: [withFormik], | ||
parameters: { | ||
formik: { | ||
initialValues: { | ||
openForms: { | ||
dataSrc: 'manual', | ||
}, | ||
values: [ | ||
{ | ||
value: 'a', | ||
label: 'A', | ||
}, | ||
{ | ||
value: 'b', | ||
label: 'B', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const SelectBoxesVariable: SelectboxesStory = { | ||
decorators: [withFormik], | ||
parameters: { | ||
formik: { | ||
initialValues: { | ||
openForms: { | ||
dataSrc: 'variable', | ||
itemsExpression: {var: 'someVariable'}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const SelectBoxesResetState: StoryObj<{ | ||
onSubmit: (values: any) => void; | ||
}> = { | ||
render: ({onSubmit}) => { | ||
return ( | ||
<Formik | ||
initialValues={{openForms: {dataSrc: ''}}} | ||
onSubmit={values => { | ||
onSubmit(values); | ||
}} | ||
> | ||
<Form> | ||
<ValuesConfig<SelectboxesComponentSchema> name="values" /> | ||
<button type="submit" className="btn btn-primary"> | ||
Submit | ||
</button> | ||
</Form> | ||
</Formik> | ||
); | ||
}, | ||
args: { | ||
onSubmit: jest.fn(), | ||
}, | ||
play: async ({canvasElement, step, args}) => { | ||
const canvas = within(canvasElement); | ||
|
||
await step('Nothing selected', async () => { | ||
await userEvent.click(canvas.getByRole('button', {name: 'Submit'})); | ||
|
||
expect(args.onSubmit).toHaveBeenCalledWith({openForms: {dataSrc: ''}}); | ||
// @ts-expect-error jest mocks + TS doesn't play nice together | ||
args.onSubmit.mockClear(); | ||
}); | ||
|
||
await step('Manual values', async () => { | ||
// Open the dropdown | ||
const dataSrcSelect = canvas.getByLabelText('Data source'); | ||
await userEvent.click(dataSrcSelect); | ||
await userEvent.keyboard('[ArrowDown]'); | ||
|
||
await userEvent.click(await canvas.findByText('Manually fill in')); | ||
const addBtn = await canvas.findByRole('button', {name: 'Add another'}); | ||
await expect(addBtn).toBeVisible(); | ||
await userEvent.click(addBtn); | ||
|
||
await userEvent.click(canvas.getByRole('button', {name: 'Submit'})); | ||
expect(args.onSubmit).toHaveBeenCalledWith({ | ||
openForms: {dataSrc: 'manual'}, | ||
values: [{value: '', label: '', openForms: {translations: {}}}], | ||
}); | ||
// @ts-expect-error jest mocks + TS doesn't play nice together | ||
args.onSubmit.mockClear(); | ||
}); | ||
|
||
await step('Set variable source', async () => { | ||
// Open the dropdown | ||
const dataSrcSelect = canvas.getByLabelText('Data source'); | ||
await userEvent.click(dataSrcSelect); | ||
await userEvent.keyboard('[ArrowDown]'); | ||
|
||
await userEvent.click(await canvas.findByText('From variable')); | ||
|
||
const expressionInput = await canvas.findByLabelText('Items expression'); | ||
await userEvent.clear(expressionInput); | ||
// { needs to be escaped: https://github.com/testing-library/user-event/issues/584 | ||
const expression = '{"var": "someVar"}'.replace(/[{[]/g, '$&$&'); | ||
await userEvent.type(expressionInput, expression); | ||
|
||
await userEvent.click(canvas.getByRole('button', {name: 'Submit'})); | ||
await expect(args.onSubmit).toHaveBeenCalledWith({ | ||
openForms: { | ||
dataSrc: 'variable', | ||
itemsExpression: {var: 'someVar'}, | ||
}, | ||
}); | ||
// @ts-expect-error jest mocks + TS doesn't play nice together | ||
args.onSubmit.mockClear(); | ||
}); | ||
|
||
await step('Reset back to manual values', async () => { | ||
// Open the dropdown | ||
const dataSrcSelect = canvas.getByLabelText('Data source'); | ||
await userEvent.click(dataSrcSelect); | ||
await userEvent.keyboard('[ArrowDown]'); | ||
|
||
await userEvent.click(await canvas.findByText('Manually fill in')); | ||
await expect(await canvas.findByText('Manually fill in')).toBeVisible(); | ||
|
||
await userEvent.click(canvas.getByRole('button', {name: 'Submit'})); | ||
expect(args.onSubmit).toHaveBeenCalledWith({ | ||
openForms: {dataSrc: 'manual'}, | ||
values: [], // all pre-existing items have been cleared | ||
}); | ||
// @ts-expect-error jest mocks + TS doesn't play nice together | ||
args.onSubmit.mockClear(); | ||
}); | ||
}, | ||
}; | ||
|
||
/** | ||
* Variant pinned to the `RadioComponentSchema` component type. | ||
*/ | ||
export const Radio: RadioStory = { | ||
decorators: [withFormik], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters