forked from dptools/dpdash
-
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.
This commit: * Renames the ChartFilterForm (it was misspelled previously) * Adds a test for the ChartFiltersForm * Adds a new ControlledMultiSelect input using MUI components * Updates the ChartFiltersForm and ViewChartPage to use the new form conventions
- Loading branch information
1 parent
ba427d0
commit 9d5dc24
Showing
5 changed files
with
203 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import React from 'react' | ||
import { render, screen, waitFor, within } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
import ChartFilterForm from '.' | ||
|
||
describe('Chart Filter Form', () => { | ||
const defaultProps = { | ||
initialValues: { | ||
chrcrit_part: [ | ||
{ name: 'HC', value: 'false' }, | ||
{ name: 'CHR', value: 'false' }, | ||
{ name: 'Missing', value: 'false' }, | ||
], | ||
included_excluded: [ | ||
{ name: 'Included', value: 'false' }, | ||
{ name: 'Excluded', value: 'false' }, | ||
{ name: 'Missing', value: 'false' }, | ||
], | ||
sex_at_birth: [ | ||
{ name: 'Male', value: 'false' }, | ||
{ name: 'Female', value: 'false' }, | ||
{ name: 'Missing', value: 'false' }, | ||
], | ||
sites: ['CA', 'LA'], | ||
}, | ||
siteOptions: [ | ||
{ label: 'CA', value: 'CA' }, | ||
{ label: 'LA', value: 'LA' }, | ||
{ label: 'MA', value: 'MA' }, | ||
], | ||
onSubmit: () => {}, | ||
} | ||
const elements = { | ||
hcField: () => | ||
screen.getByRole('checkbox', { name: 'chrcrit_part.0.value' }), | ||
chrField: () => | ||
screen.getByRole('checkbox', { name: 'chrcrit_part.1.value' }), | ||
includedExcludedMissing: () => | ||
screen.getByRole('checkbox', { name: 'included_excluded.2.value' }), | ||
siteSelect: () => screen.getByLabelText('Sites'), | ||
siteOptions: () => screen.getByRole('listbox'), | ||
submitBtn: () => screen.getByText('Apply Filters'), | ||
} | ||
const renderForm = (props = defaultProps) => { | ||
render(<ChartFilterForm {...props} />) | ||
} | ||
|
||
test('calls the onSubmit prop when the form is submitted with valid data', async () => { | ||
const user = userEvent.setup() | ||
const onSubmit = jest.fn() | ||
const props = { ...defaultProps, onSubmit } | ||
|
||
renderForm(props) | ||
await user.click(elements.hcField()) | ||
await user.click(elements.includedExcludedMissing()) | ||
await user.click(elements.siteSelect()) | ||
await user.click(within(elements.siteOptions()).getByText('CA')) | ||
await user.click(within(elements.siteOptions()).getByText('MA')) | ||
await user.click(elements.submitBtn()) | ||
|
||
await waitFor(() => | ||
expect(onSubmit).toHaveBeenCalledWith( | ||
{ | ||
chrcrit_part: [ | ||
{ name: 'HC', value: true }, | ||
{ name: 'CHR', value: 'false' }, | ||
{ name: 'Missing', value: 'false' }, | ||
], | ||
included_excluded: [ | ||
{ name: 'Included', value: 'false' }, | ||
{ name: 'Excluded', value: 'false' }, | ||
{ name: 'Missing', value: true }, | ||
], | ||
sex_at_birth: [ | ||
{ name: 'Male', value: 'false' }, | ||
{ name: 'Female', value: 'false' }, | ||
{ name: 'Missing', value: 'false' }, | ||
], | ||
sites: ['LA', 'MA'], | ||
}, | ||
expect.anything() | ||
) | ||
) | ||
}) | ||
|
||
// test('displays errors and does not submit the form with invalid data', async () => { | ||
// const user = userEvent.setup() | ||
// const onSubmit = jest.fn() | ||
// const props = { ...defaultProps, onSubmit } | ||
|
||
// renderForm(props) | ||
// await user.type(elements.usernameField(), 'myUsername') | ||
// await user.type(elements.passwordField(), 'nope') | ||
// await user.type(elements.confirmPasswordField(), 'nope') | ||
// await user.type(elements.fullNameField(), 'Joe Schmoe') | ||
// await user.type(elements.emailField(), 'not-an-email') | ||
// await user.click(elements.submitBtn()) | ||
|
||
// await waitFor(() => | ||
// expect( | ||
// screen.getByText('password must be at least 8 characters') | ||
// ).toBeInTheDocument() | ||
// ) | ||
// expect(screen.getByText('email must be a valid email')).toBeInTheDocument() | ||
|
||
// expect(onSubmit).not.toHaveBeenCalled() | ||
// }) | ||
|
||
// test('displays errors and does not submit the form with unmatched passwords', async () => { | ||
// const user = userEvent.setup() | ||
// const onSubmit = jest.fn() | ||
// const props = { ...defaultProps, onSubmit } | ||
|
||
// renderForm(props) | ||
// await user.type(elements.usernameField(), 'myUsername') | ||
// await user.type(elements.passwordField(), 'thisIsMyPassword') | ||
// await user.type( | ||
// elements.confirmPasswordField(), | ||
// 'thisIsMyConfirmedPassword' | ||
// ) | ||
// await user.type(elements.fullNameField(), 'Joe Schmoe') | ||
// await user.type(elements.emailField(), 'joe@example.test') | ||
// await user.click(elements.submitBtn()) | ||
|
||
// await waitFor(() => | ||
// expect(screen.getByText('passwords do not match')).toBeInTheDocument() | ||
// ) | ||
|
||
// expect(onSubmit).not.toHaveBeenCalled() | ||
// }) | ||
|
||
// test('calls the onCancel prop when the cancel button is clicked', async () => { | ||
// const user = userEvent.setup() | ||
// const onCancel = jest.fn() | ||
// const props = { ...defaultProps, onCancel } | ||
|
||
// renderForm(props) | ||
// await user.click(elements.cancelBtn()) | ||
|
||
// await waitFor(() => expect(onCancel).toHaveBeenCalledWith()) | ||
// }) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react' | ||
import { Controller } from 'react-hook-form' | ||
import Select from '@mui/material/Select' | ||
import { Box, Chip, MenuItem } from '@mui/material' | ||
|
||
const ControlledMultiSelect = ({ name, control, options, ...rest }) => { | ||
return ( | ||
<Controller | ||
name={name} | ||
control={control} | ||
render={({ field }) => ( | ||
<Select | ||
{...rest} | ||
{...field} | ||
multiple | ||
renderValue={(selected) => ( | ||
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5 }}> | ||
{selected.map((value) => ( | ||
<Chip key={value} label={value} /> | ||
))} | ||
</Box> | ||
)} | ||
> | ||
{options.map((option) => ( | ||
<MenuItem key={option.value} value={option.value}> | ||
{option.label} | ||
</MenuItem> | ||
))} | ||
</Select> | ||
)} | ||
/> | ||
) | ||
} | ||
|
||
export default ControlledMultiSelect |
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