Skip to content

Commit

Permalink
refactor(labware-creator): Break out grid and volume sections
Browse files Browse the repository at this point in the history
addresses #7707
  • Loading branch information
Kadee80 committed May 4, 2021
1 parent ac1c004 commit 1a423dc
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 58 deletions.
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'))
})
})
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 labware-library/src/labware-creator/components/sections/Grid.tsx
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 labware-library/src/labware-creator/components/sections/Volume.tsx
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>
)
}
63 changes: 5 additions & 58 deletions labware-library/src/labware-creator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import { reportEvent } from '../analytics'
import { reportErrors } from './analyticsUtils'
import { AlertModal, PrimaryButton } from '@opentrons/components'
import labwareSchema from '@opentrons/shared-data/labware/schemas/2.json'
import { makeMaskToDecimal, maskToInteger, maskLoadName } from './fieldMasks'
import { makeMaskToDecimal, maskLoadName } from './fieldMasks'
import {
tubeRackInsertOptions,
aluminumBlockAutofills,
aluminumBlockTypeOptions,
aluminumBlockChildTypeOptions,
getDefaultFormState,
getImplicitAutofillValues,
yesNoOptions,
tubeRackAutofills,
} from './fields'
import { labwareDefToFields } from './labwareDefToFields'
Expand All @@ -43,8 +42,9 @@ import { Regularity } from './components/sections/Regularity'

import { Footprint } from './components/sections/Footprint'
import { Height } from './components/sections/Height'
import { Grid } from './components/sections/Grid'
import { Volume } from './components/sections/Volume'
import {
GridImg,
WellXYImg,
XYSpacingImg,
DepthImg,
Expand Down Expand Up @@ -470,62 +470,9 @@ export const LabwareCreator = (): JSX.Element => {
<Regularity />
<Footprint />
<Height />
<Section
label="Grid"
fieldList={[
'gridRows',
'gridColumns',
'regularRowSpacing',
'regularColumnSpacing',
]}
>
<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>
</Section>
<Grid />
{/* PAGE 2 */}
<Section label="Volume" fieldList={['wellVolume']}>
<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>
</Section>
<Volume />
<Section
label="Well Shape & Sides"
fieldList={[
Expand Down

0 comments on commit 1a423dc

Please sign in to comment.