Skip to content

Commit

Permalink
add some test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jerader committed Apr 1, 2024
1 parent 1222e2e commit af80700
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Offset = 'x' | 'y' | 'z'
interface PositionSpec {
name: StepFieldName
value: number | null
updateValue: (val: number | null | undefined) => void
updateValue: (val?: number | null) => void
}
export type PositionSpecs = Record<Offset, PositionSpec>

Expand Down Expand Up @@ -108,24 +108,17 @@ export const TipPositionModal = (
wellXWidthMm
)

const zErrors = utils.getErrors({
isDefault,
minMm: minMmFromBottom,
maxMm: maxMmFromBottom,
value: zValue,
})
const xErrors = utils.getErrors({
isDefault,
minMm: xMinWidth,
maxMm: xMaxWidth,
value: xValue,
})
const yErrors = utils.getErrors({
isDefault,
minMm: yMinWidth,
maxMm: yMaxWidth,
value: yValue,
})
const createErrors = (
value: string | null,
min: number,
max: number
): utils.Error[] => {
return utils.getErrors({ isDefault, minMm: min, maxMm: max, value })
}
const zErrors = createErrors(zValue, minMmFromBottom, maxMmFromBottom)
const xErrors = createErrors(xValue, xMinWidth, xMaxWidth)
const yErrors = createErrors(yValue, yMinWidth, yMaxWidth)

const hasErrors =
zErrors.length > 0 || xErrors.length > 0 || yErrors.length > 0
const hasVisibleErrors = isPristine
Expand All @@ -134,33 +127,20 @@ export const TipPositionModal = (
yErrors.includes(TOO_MANY_DECIMALS)
: hasErrors

const zErrorText = utils.getErrorText({
errors: zErrors,
maxMm: maxMmFromBottom,
minMm: minMmFromBottom,
isPristine,
t,
})
const createErrorText = (
errors: utils.Error[],
min: number,
max: number
): string | null => {
return utils.getErrorText({ errors, minMm: min, maxMm: max, isPristine, t })
}

const xErrorText = utils.getErrorText({
errors: xErrors,
minMm: xMinWidth,
maxMm: xMaxWidth,
isPristine,
t,
})

const yErrorText = utils.getErrorText({
errors: yErrors,
minMm: yMinWidth,
maxMm: yMaxWidth,
isPristine,
t,
})
const zErrorText = createErrorText(zErrors, minMmFromBottom, maxMmFromBottom)
const xErrorText = createErrorText(xErrors, xMinWidth, xMaxWidth)
const yErrorText = createErrorText(yErrors, yMinWidth, yMaxWidth)

const handleDone = (): void => {
setPristine(false)

if (!hasErrors) {
if (isDefault) {
zSpec?.updateValue(null)
Expand Down Expand Up @@ -246,7 +226,10 @@ export const TipPositionModal = (
{t('tip_position.field_titles.x_position')}
</StyledText>
<InputField
caption={`between ${xMinWidth} and ${xMaxWidth}`}
caption={t('tip_position.caption', {
min: xMinWidth,
max: xMaxWidth,
})}
error={xErrorText}
className={styles.position_from_bottom_input}
id="TipPositionModal_x_custom_input"
Expand All @@ -260,7 +243,10 @@ export const TipPositionModal = (
{t('tip_position.field_titles.y_position')}
</StyledText>
<InputField
caption={`between ${yMinWidth} and ${yMaxWidth}`}
caption={t('tip_position.caption', {
min: yMinWidth,
max: yMaxWidth,
})}
error={yErrorText}
className={styles.position_from_bottom_input}
id="TipPositionModal_y_custom_input"
Expand All @@ -274,7 +260,10 @@ export const TipPositionModal = (
{t('tip_position.field_titles.z_position')}
</StyledText>
<InputField
caption={`between ${minMmFromBottom} and ${maxMmFromBottom}`}
caption={t('tip_position.caption', {
min: minMmFromBottom,
max: maxMmFromBottom,
})}
error={zErrorText}
className={styles.position_from_bottom_input}
id="TipPositionModal_z_custom_input"
Expand Down Expand Up @@ -320,12 +309,14 @@ export const TipPositionModal = (
options={[
{
name: isMixAspDispField
? `Aspirate 1mm, Dispense 0.5mm from the bottom center (default)`
: `${defaultMmFromBottom} mm from the bottom center (default)`,
? t('tip_position.radio_button.mix')
: t('tip_position.radio_button.default', {
defaultMmFromBottom,
}),
value: 'default',
},
{
name: 'Custom',
name: t('tip_position.radio_button.custom'),
value: 'custom',
},
]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ export const ZTipPositionModal = (props: Props): JSX.Element => {

const TipPositionInputField = !isDefault && (
<InputField
caption={`between ${minMmFromBottom} and ${maxMmFromBottom}`}
caption={t('tip_position.caption', {
min: minMmFromBottom,
max: maxMmFromBottom,
})}
className={styles.position_from_bottom_input}
error={errorText}
id={'TipPositionModal_custom_input'}
Expand Down Expand Up @@ -200,11 +203,13 @@ export const ZTipPositionModal = (props: Props): JSX.Element => {
}}
options={[
{
name: `${defaultMmFromBottom} mm from the bottom (default)`,
name: t('tip_position.radio_button.default', {
defaultMmFromBottom,
}),
value: 'default',
},
{
name: 'Custom',
name: t('tip_position.radio_button.custom'),
value: 'custom',
},
]}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import * as React from 'react'
import { fireEvent, screen } from '@testing-library/react'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderWithProviders } from '../../../../../__testing-utils__'
import { i18n } from '../../../../../localization'
import { TipPositionModal } from '../TipPositionModal'
import { TipPositionField } from '../index'
import { getLabwareEntities } from '../../../../../step-forms/selectors'
import { LabwareDefinition2, fixture96Plate } from '@opentrons/shared-data'
import { ZTipPositionModal } from '../ZTipPositionModal'

vi.mock('../../../../../step-forms/selectors')
vi.mock('../ZTipPositionModal')
vi.mock('../TipPositionModal')
const render = (props: React.ComponentProps<typeof TipPositionField>) => {
return renderWithProviders(<TipPositionField {...props} />, {
i18nInstance: i18n,
})[0]
}
const mockDelay = 'aspirate_delay_mmFromBottom'
const mockAspirate = 'aspirate_mmFromBottom'
const mockLabwareId = 'mockId'
describe('TipPositionField', () => {
let props: React.ComponentProps<typeof TipPositionField>

beforeEach(() => {
props = {
zField: mockDelay,
labwareId: mockLabwareId,
propsForFields: {
[mockDelay]: {
name: mockDelay,
value: null,
updateValue: vi.fn(),
tooltipContent: 'mock content',
isIndeterminate: false,
disabled: false,
} as any,
},
}
vi.mocked(TipPositionModal).mockReturnValue(
<div>mock TipPositionModal</div>
)
vi.mocked(ZTipPositionModal).mockReturnValue(
<div>mock ZTipPositionModal</div>
)
vi.mocked(getLabwareEntities).mockReturnValue({
[mockLabwareId]: {
id: mockLabwareId,
labwareDefURI: 'mock uri',
def: fixture96Plate as LabwareDefinition2,
},
})
})
it('renders the input field and header when x and y fields are not provided', () => {
render(props)
screen.getByText('mm')
fireEvent.click(screen.getByRole('textbox', { name: '' }))
expect(screen.getByRole('textbox', { name: '' })).not.toBeDisabled()
screen.getByText('mock ZTipPositionModal')
})
it('renders the input field but it is disabled', () => {
props = {
...props,
propsForFields: {
[mockDelay]: {
name: mockDelay,
value: null,
updateValue: vi.fn(),
tooltipContent: 'mock content',
isIndeterminate: false,
disabled: true,
} as any,
},
}
render(props)
expect(screen.getByRole('textbox', { name: '' })).toBeDisabled()
})
it('renders the icon when x,y, and z fields are provided', () => {
const mockX = 'aspirate_x_position'
const mockY = 'aspirate_y_position'
props = {
zField: mockAspirate,
xField: mockX,
yField: mockY,
labwareId: mockLabwareId,
propsForFields: {
[mockAspirate]: {
name: mockAspirate,
value: null,
updateValue: vi.fn(),
tooltipContent: 'mock content',
isIndeterminate: false,
disabled: false,
} as any,
[mockX]: {
name: mockX,
value: null,
updateValue: vi.fn(),
} as any,
[mockY]: {
name: mockY,
value: null,
updateValue: vi.fn(),
} as any,
},
}
render(props)
fireEvent.click(screen.getByTestId('TipPositionIcon_aspirate_mmFromBottom'))
screen.getByText('mock TipPositionModal')
})
})
Loading

0 comments on commit af80700

Please sign in to comment.