-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(labware-creator): Break out grid and volume sections
addresses #7707
- Loading branch information
Showing
5 changed files
with
242 additions
and
58 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
labware-library/src/labware-creator/components/__tests__/sections/Grid.test.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,94 @@ | ||
import React from 'react' | ||
import { FormikConfig } from 'formik' | ||
import { when } from 'jest-when' | ||
import { render, screen } from '@testing-library/react' | ||
import { | ||
getDefaultFormState, | ||
LabwareFields, | ||
yesNoOptions, | ||
} from '../../../fields' | ||
import { Grid } from '../../sections/Grid' | ||
import { getFormAlerts } from '../../utils/getFormAlerts' | ||
import { TextField } from '../../TextField' | ||
import { RadioField } from '../../RadioField' | ||
import { wrapInFormik } from '../../utils/wrapInFormik' | ||
|
||
jest.mock('../../TextField') | ||
jest.mock('../../RadioField') | ||
jest.mock('../../utils/getFormAlerts') | ||
|
||
const getFormAlertsMock = getFormAlerts as jest.MockedFunction< | ||
typeof getFormAlerts | ||
> | ||
|
||
const textFieldMock = TextField as jest.MockedFunction<typeof TextField> | ||
|
||
const radioFieldMock = RadioField as jest.MockedFunction<typeof RadioField> | ||
|
||
const formikConfig: FormikConfig<LabwareFields> = { | ||
initialValues: getDefaultFormState(), | ||
onSubmit: jest.fn(), | ||
} | ||
|
||
describe('Grid', () => { | ||
beforeEach(() => { | ||
radioFieldMock.mockImplementation(args => { | ||
if (args.name === 'regularRowSpacing') { | ||
expect(args).toEqual({ | ||
name: 'regularRowSpacing', | ||
options: yesNoOptions, | ||
}) | ||
return <div>regularRowSpacing radio group</div> | ||
} | ||
if (args.name === 'regularColumnSpacing') { | ||
expect(args).toEqual({ | ||
name: 'regularColumnSpacing', | ||
options: yesNoOptions, | ||
}) | ||
return <div>regularColumnSpacing radio group</div> | ||
} | ||
throw new Error( | ||
`Text field should have been called with regularRowSpacing or regularColumnSpacing, instead got ${args.name} ` | ||
) | ||
}) | ||
|
||
textFieldMock.mockImplementation(args => { | ||
if (args.name === 'gridRows') { | ||
return <div>gridRows text field</div> | ||
} | ||
if (args.name === 'gridColumns') { | ||
return <div>gridColumns text field</div> | ||
} | ||
throw new Error( | ||
`Text field should have been called with gridRows or gridColumns, instead got ${args.name} ` | ||
) | ||
}) | ||
|
||
when(getFormAlertsMock) | ||
.expectCalledWith({ | ||
values: getDefaultFormState(), | ||
touched: {}, | ||
errors: {}, | ||
fieldList: [ | ||
'gridRows', | ||
'gridColumns', | ||
'regularRowSpacing', | ||
'regularColumnSpacing', | ||
], | ||
}) | ||
.mockReturnValue([<div key="mock key">mock alerts</div>]) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
it('should render with the correct information', () => { | ||
render(wrapInFormik(<Grid />, formikConfig)) | ||
expect(screen.getByText('Grid')) | ||
expect(screen.getByText('mock alerts')) | ||
expect(screen.getByText('gridRows text field')) | ||
expect(screen.getByText('regularRowSpacing radio group')) | ||
expect(screen.getByText('gridColumns text field')) | ||
expect(screen.getByText('regularColumnSpacing radio group')) | ||
}) | ||
}) |
51 changes: 51 additions & 0 deletions
51
labware-library/src/labware-creator/components/__tests__/sections/Volume.test.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,51 @@ | ||
import React from 'react' | ||
import { FormikConfig } from 'formik' | ||
import { when } from 'jest-when' | ||
import { render, screen } from '@testing-library/react' | ||
import { getDefaultFormState, LabwareFields } from '../../../fields' | ||
import { Volume } from '../../sections/Volume' | ||
import { getFormAlerts } from '../../utils/getFormAlerts' | ||
import { TextField } from '../../TextField' | ||
import { wrapInFormik } from '../../utils/wrapInFormik' | ||
|
||
jest.mock('../../TextField') | ||
jest.mock('../../utils/getFormAlerts') | ||
|
||
const getFormAlertsMock = getFormAlerts as jest.MockedFunction< | ||
typeof getFormAlerts | ||
> | ||
const textFieldMock = TextField as jest.MockedFunction<typeof TextField> | ||
|
||
const formikConfig: FormikConfig<LabwareFields> = { | ||
initialValues: getDefaultFormState(), | ||
onSubmit: jest.fn(), | ||
} | ||
|
||
describe('Volume', () => { | ||
beforeEach(() => { | ||
textFieldMock.mockImplementation(args => { | ||
return <div>wellVolume text field</div> | ||
}) | ||
|
||
when(getFormAlertsMock) | ||
.calledWith({ | ||
values: getDefaultFormState(), | ||
touched: {}, | ||
errors: {}, | ||
fieldList: ['wellVolume'], | ||
}) | ||
.mockReturnValue([<div key="mock key">mock alerts</div>]) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
it('should render with the correct information', () => { | ||
render(wrapInFormik(<Volume />, formikConfig)) | ||
expect(screen.getByText('Volume')) | ||
expect(screen.getByText('Total maximum volume of each well.')) | ||
expect(screen.getByText('mock alerts')) | ||
expect(screen.getByText('wellVolume text field')) | ||
}) | ||
}) |
53 changes: 53 additions & 0 deletions
53
labware-library/src/labware-creator/components/sections/Grid.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,53 @@ | ||
import * as React from 'react' | ||
import { useFormikContext } from 'formik' | ||
import { maskToInteger } from '../../fieldMasks' | ||
import { LabwareFields, yesNoOptions } from '../../fields' | ||
import { getFormAlerts } from '../utils/getFormAlerts' | ||
import { TextField } from '../TextField' | ||
import { RadioField } from '../RadioField' | ||
import { GridImg } from '../diagrams' | ||
import { SectionBody } from './SectionBody' | ||
|
||
import styles from '../../styles.css' | ||
|
||
const getContent = (): JSX.Element => ( | ||
<div className={styles.flex_row}> | ||
<div className={styles.instructions_column}> | ||
<p> | ||
The grid of wells on your labware is arranged via rows and columns. Rows | ||
run horizontally across your labware (left to right). Columns run top to | ||
bottom. | ||
</p> | ||
</div> | ||
<div className={styles.diagram_column}> | ||
<GridImg /> | ||
</div> | ||
<div className={styles.form_fields_column}> | ||
<TextField name="gridRows" inputMasks={[maskToInteger]} /> | ||
<RadioField name="regularRowSpacing" options={yesNoOptions} /> | ||
<TextField name="gridColumns" inputMasks={[maskToInteger]} /> | ||
<RadioField name="regularColumnSpacing" options={yesNoOptions} /> | ||
</div> | ||
</div> | ||
) | ||
|
||
export const Grid = (): JSX.Element => { | ||
const fieldList: Array<keyof LabwareFields> = [ | ||
'gridRows', | ||
'gridColumns', | ||
'regularRowSpacing', | ||
'regularColumnSpacing', | ||
] | ||
const { values, errors, touched } = useFormikContext<LabwareFields>() | ||
|
||
return ( | ||
<div className={styles.new_definition_section}> | ||
<SectionBody label="Grid"> | ||
<> | ||
{getFormAlerts({ values, touched, errors, fieldList })} | ||
{getContent(values)} | ||
</> | ||
</SectionBody> | ||
</div> | ||
) | ||
} |
39 changes: 39 additions & 0 deletions
39
labware-library/src/labware-creator/components/sections/Volume.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,39 @@ | ||
import * as React from 'react' | ||
import { useFormikContext } from 'formik' | ||
import { makeMaskToDecimal } from '../../fieldMasks' | ||
import { LabwareFields } from '../../fields' | ||
import { getFormAlerts } from '../utils/getFormAlerts' | ||
import { TextField } from '../TextField' | ||
import { SectionBody } from './SectionBody' | ||
|
||
import styles from '../../styles.css' | ||
|
||
const maskTo2Decimal = makeMaskToDecimal(2) | ||
|
||
const getContent = (): JSX.Element => ( | ||
<div className={styles.flex_row}> | ||
<div className={styles.volume_instructions_column}> | ||
<p>Total maximum volume of each well.</p> | ||
</div> | ||
|
||
<div className={styles.form_fields_column}> | ||
<TextField name="wellVolume" inputMasks={[maskTo2Decimal]} units="μL" /> | ||
</div> | ||
</div> | ||
) | ||
|
||
export const Volume = (): JSX.Element => { | ||
const fieldList: Array<keyof LabwareFields> = ['wellVolume'] | ||
const { values, errors, touched } = useFormikContext<LabwareFields>() | ||
|
||
return ( | ||
<div className={styles.new_definition_section}> | ||
<SectionBody label="Volume"> | ||
<> | ||
{getFormAlerts({ values, touched, errors, fieldList })} | ||
{getContent()} | ||
</> | ||
</SectionBody> | ||
</div> | ||
) | ||
} |
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