Skip to content

Commit

Permalink
update test for vitest migration
Browse files Browse the repository at this point in the history
  • Loading branch information
koji committed Mar 1, 2024
1 parent 8fb9f62 commit 26f7624
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import * as React from 'react'
import { fireEvent, screen } from '@testing-library/react'
import { describe, it, expect, vi } from 'vitest'
import fixture_adapter from '@opentrons/shared-data/labware/definitions/2/opentrons_96_pcr_adapter/1.json'
import fixture_96_wellplate from '@opentrons/shared-data/labware/definitions/2/opentrons_96_wellplate_200ul_pcr_full_skirt/1.json'

import { opentrons96PcrAdapterV1, fixture96Plate } from '@opentrons/shared-data'

import { i18n } from '../../../i18n'
import { renderWithProviders } from '../../../__testing-utils__'
import { getIsLabwareOffsetCodeSnippetsOn } from '../../../redux/config'
import { getLabwareDefinitionsFromCommands } from '../../LabwarePositionCheck/utils/labware'
import { ApplyHistoricOffsets } from '..'

import type { LabwareDefinition2 } from '@opentrons/shared-data'
import type { OffsetCandidate } from '../hooks/useOffsetCandidatesForAnalysis'

vi.mock('../../../redux/config')
vi.mock('../../LabwarePositionCheck/utils/labware')

const mockLabwareDef = fixture_96_wellplate as LabwareDefinition2
const mockAdapterDef = fixture_adapter as LabwareDefinition2
const mockLabwareDef = fixture96Plate as LabwareDefinition2
const mockAdapterDef = opentrons96PcrAdapterV1 as LabwareDefinition2

const mockFirstCandidate: OffsetCandidate = {
id: 'first_offset_id',
Expand Down Expand Up @@ -59,7 +61,7 @@ const mockFourthCandidate: OffsetCandidate = {
}

describe('ApplyHistoricOffsets', () => {
const mockSetShouldApplyOffsets = jest.fn()
const mockSetShouldApplyOffsets = vi.fn()
const render = (
props?: Partial<React.ComponentProps<typeof ApplyHistoricOffsets>>
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import { mockDefinition } from '../../../../../redux/custom-labware/__fixtures__'
import { getNestedLabwareInfo } from '../getNestedLabwareInfo'
import type { RunTimeCommand } from '@opentrons/shared-data'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import { getLatestCurrentOffsets } from '../utils'
import type { LabwareOffset } from '@opentrons/api-client'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as React from 'react'
import { fireEvent } from '@testing-library/react'
import { fireEvent, screen } from '@testing-library/react'
import { describe, it, beforeEach, vi, expect } from 'vitest'

import { SPACING, COLORS } from '@opentrons/components'

import {
nestedTextMatcher,
renderWithProviders,
SPACING,
COLORS,
} from '@opentrons/components'
} from '../../../../../__testing-utils__'
import { i18n } from '../../../../../i18n'
import {
useTrackEvent,
Expand All @@ -14,29 +16,25 @@ import {
import { getIsOnDevice } from '../../../../../redux/config'
import { LiquidDetailCard } from '../LiquidDetailCard'

jest.mock('../../../../../redux/analytics')
jest.mock('../../../../../redux/config')
import { Mock } from 'vitest'

vi.mock('../../../../../redux/analytics')
vi.mock('../../../../../redux/config')

const mockUseTrackEvent = useTrackEvent as jest.MockedFunction<
typeof useTrackEvent
>
const mockGetIsOnDevice = getIsOnDevice as jest.MockedFunction<
typeof getIsOnDevice
>
const render = (props: React.ComponentProps<typeof LiquidDetailCard>) => {
return renderWithProviders(<LiquidDetailCard {...props} />, {
i18nInstance: i18n,
})[0]
}
let mockTrackEvent: jest.Mock
let mockTrackEvent: Mock

describe('LiquidDetailCard', () => {
let props: React.ComponentProps<typeof LiquidDetailCard>

beforeEach(() => {
mockTrackEvent = jest.fn()
mockUseTrackEvent.mockReturnValue(mockTrackEvent)
mockGetIsOnDevice.mockReturnValue(false)
mockTrackEvent = vi.fn()
vi.mocked(useTrackEvent).mockReturnValue(mockTrackEvent)
vi.mocked(getIsOnDevice).mockReturnValue(false)
props = {
liquidId: '0',
displayName: 'Mock Liquid',
Expand All @@ -48,51 +46,53 @@ describe('LiquidDetailCard', () => {
['A2', 'B2', 'C2', 'D2'],
['A3', 'B3', 'C3', 'D3'],
],
setSelectedValue: jest.fn(),
setSelectedValue: vi.fn(),
selectedValue: '2',
}
})

it('renders liquid name, description, total volume', () => {
const { getByText, getAllByText } = render(props)
getByText('Mock Liquid')
getByText('Mock Description')
getAllByText(nestedTextMatcher('100 µL'))
render(props)
screen.getByText('Mock Liquid')
screen.getByText('Mock Description')
screen.getAllByText(nestedTextMatcher('100 µL'))
})

it('renders clickable box, clicking on it calls track event', () => {
const { getByTestId } = render(props)
fireEvent.click(getByTestId('LiquidDetailCard_box'))
render(props)
fireEvent.click(screen.getByTestId('LiquidDetailCard_box'))
expect(mockTrackEvent).toHaveBeenCalledWith({
name: ANALYTICS_HIGHLIGHT_LIQUID_IN_DETAIL_MODAL,
properties: {},
})
})

it('renders well volume information if selected', () => {
const { getByText, getAllByText } = render({
render({
...props,
selectedValue: '0',
})
getByText('A1')
getByText('B1')
getAllByText(nestedTextMatcher('50 µL'))
screen.getByText('A1')
screen.getByText('B1')
screen.getAllByText(nestedTextMatcher('50 µL'))
})
it('renders well range for volume info if selected', () => {
const { getByText } = render({
render({
...props,
selectedValue: '0',
volumeByWell: { A1: 50, B1: 50, C1: 50, D1: 50 },
})
getByText('A1: D1')
getByText(nestedTextMatcher('50 µL'))
screen.getByText('A1: D1')
screen.getByText(nestedTextMatcher('50 µL'))
})
it('renders liquid name, description, total volume for odd, and clicking item selects the box', () => {
mockGetIsOnDevice.mockReturnValue(true)
const { getByText, getAllByText, getByLabelText } = render(props)
getByText('Mock Liquid')
getByText('Mock Description')
getAllByText(nestedTextMatcher('100 µL'))
getAllByText(nestedTextMatcher('total volume'))
expect(getByLabelText('liquidBox_odd')).toHaveStyle(
vi.mocked(getIsOnDevice).mockReturnValue(true)
render(props)
screen.getByText('Mock Liquid')
screen.getByText('Mock Description')
screen.getAllByText(nestedTextMatcher('100 µL'))
screen.getAllByText(nestedTextMatcher('total volume'))
expect(screen.getByLabelText('liquidBox_odd')).toHaveStyle(
`border: ${SPACING.spacing4} solid ${COLORS.grey30}`
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { when } from 'vitest-when'
import { describe, it, beforeEach, vi, afterEach, expect } from 'vitest'
import { screen } from '@testing-library/react'

import {
LabwareRender,
} from '@opentrons/components'
import { LabwareRender } from '@opentrons/components'
import { parseLiquidsInLoadOrder } from '@opentrons/api-client'

import { nestedTextMatcher, partialComponentPropsMatcher, renderWithProviders } from '../../../../../__testing-utils__'}
import {
nestedTextMatcher,
partialComponentPropsMatcher,
renderWithProviders,
} from '../../../../../__testing-utils__'
import { i18n } from '../../../../../i18n'
import { getIsOnDevice } from '../../../../../redux/config'
import { useMostRecentCompletedAnalysis } from '../../../../LabwarePositionCheck/useMostRecentCompletedAnalysis'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
import * as React from 'react'
import { when, resetAllWhenMocks } from 'jest-when'
import { when } from 'vitest-when'
import { describe, it, beforeEach, vi, afterEach, expect } from 'vitest'

import {
getLabwareDisplayName,
LabwareDefinition2,
ProtocolFile,
LoadedLabware,
fixtureTiprack300ul,
} from '@opentrons/shared-data'
import fixture_tiprack_300_ul from '@opentrons/shared-data/labware/fixtures/2/fixture_tiprack_300_ul.json'
import { nestedTextMatcher, renderWithProviders } from '@opentrons/components'

import {
nestedTextMatcher,
renderWithProviders,
} from '../../../../__testing-utils__'
import { i18n } from '../../../../i18n'
import { useCurrentRun } from '../../../ProtocolUpload/hooks'
import { getLabwareLocation } from '../utils/getLabwareLocation'
import { LabwareInfoOverlay } from '../LabwareInfoOverlay'
import { getLabwareDefinitionUri } from '../utils/getLabwareDefinitionUri'
import { useLabwareOffsetForLabware } from '../useLabwareOffsetForLabware'

jest.mock('../../../ProtocolUpload/hooks')
jest.mock('../utils/getLabwareLocation')
jest.mock('../../hooks')
jest.mock('../utils/getLabwareDefinitionUri')
jest.mock('../useLabwareOffsetForLabware')
vi.mock('../../../ProtocolUpload/hooks')
vi.mock('../utils/getLabwareLocation')
vi.mock('../../hooks')
vi.mock('../utils/getLabwareDefinitionUri')
vi.mock('../useLabwareOffsetForLabware')

// jest.mock('@opentrons/shared-data', () => {
// const actualSharedData = jest.requireActual('@opentrons/shared-data')
// return {
// ...actualSharedData,
// getLabwareDisplayName: jest.fn(),
// }
// })

jest.mock('@opentrons/shared-data', () => {
const actualSharedData = jest.requireActual('@opentrons/shared-data')
vi.mock('@opentrons/shared-data', async importOriginal => {
const actual = await importOriginal<typeof getLabwareDisplayName>()
return {
...actualSharedData,
getLabwareDisplayName: jest.fn(),
...actual,
getLabwareDisplayName: vi.fn(),
}
})

Expand All @@ -40,21 +54,6 @@ const render = (props: React.ComponentProps<typeof LabwareInfoOverlay>) => {
)[0]
}

const mockGetLabwareDisplayName = getLabwareDisplayName as jest.MockedFunction<
typeof getLabwareDisplayName
>
const mockUseCurrentRun = useCurrentRun as jest.MockedFunction<
typeof useCurrentRun
>
const mockUseLabwareOffsetForLabware = useLabwareOffsetForLabware as jest.MockedFunction<
typeof useLabwareOffsetForLabware
>
const mockGetLabwareLocation = getLabwareLocation as jest.MockedFunction<
typeof getLabwareLocation
>
const mockGetLabwareDefinitionUri = getLabwareDefinitionUri as jest.MockedFunction<
typeof getLabwareDefinitionUri
>
const MOCK_LABWARE_ID = 'some_labware_id'
const MOCK_LABWARE_DEFINITION_URI = 'some_labware_definition_uri'
const MOCK_SLOT_NAME = '4'
Expand All @@ -67,7 +66,7 @@ describe('LabwareInfoOverlay', () => {
let labwareDefinitions: ProtocolFile<{}>['labwareDefinitions']
beforeEach(() => {
props = {
definition: fixture_tiprack_300_ul as LabwareDefinition2,
definition: fixtureTiprack300ul as LabwareDefinition2,
displayName: 'fresh tips',
labwareId: MOCK_LABWARE_ID,
runId: MOCK_RUN_ID,
Expand All @@ -79,37 +78,36 @@ describe('LabwareInfoOverlay', () => {
} as LoadedLabware,
]
labwareDefinitions = {
[MOCK_LABWARE_DEFINITION_URI]: fixture_tiprack_300_ul as LabwareDefinition2,
[MOCK_LABWARE_DEFINITION_URI]: fixtureTiprack300ul as LabwareDefinition2,
}
when(mockGetLabwareDisplayName)
when(vi.mocked(getLabwareDisplayName))
.calledWith(props.definition)
.mockReturnValue('mock definition display name')
.thenReturn('mock definition display name')

when(mockUseLabwareOffsetForLabware)
when(vi.mocked(useLabwareOffsetForLabware))
.calledWith(MOCK_RUN_ID, MOCK_LABWARE_ID)
.mockReturnValue({
.thenReturn({
id: 'fake_offset_id',
createdAt: 'fake_timestamp',
definitionUri: 'fake_def_uri',
location: { slotName: MOCK_SLOT_NAME },
vector: MOCK_LABWARE_VECTOR,
})

when(mockUseCurrentRun)
when(vi.mocked(useCurrentRun))
.calledWith()
.mockReturnValue({} as any)
.thenReturn({} as any)

when(mockGetLabwareLocation)
when(vi.mocked(getLabwareLocation))
.calledWith(MOCK_LABWARE_ID, [])
.mockReturnValue({ slotName: MOCK_SLOT_NAME })
.thenReturn({ slotName: MOCK_SLOT_NAME })

when(mockGetLabwareDefinitionUri)
when(vi.mocked(getLabwareDefinitionUri))
.calledWith(MOCK_LABWARE_ID, labware, labwareDefinitions)
.mockReturnValue(MOCK_LABWARE_DEFINITION_URI)
.thenReturn(MOCK_LABWARE_DEFINITION_URI)
})
afterEach(() => {
resetAllWhenMocks()
jest.restoreAllMocks()
vi.restoreAllMocks()
})

it('should render the labware display name if present', () => {
Expand All @@ -131,9 +129,9 @@ describe('LabwareInfoOverlay', () => {
})

it('should render the offset data when offset data exists', () => {
when(mockUseCurrentRun)
when(vi.mocked(useCurrentRun))
.calledWith()
.mockReturnValue({
.thenReturn({
data: {
labwareOffsets: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import { getModuleTypesThatRequireExtraAttention } from '../getModuleTypesThatRequireExtraAttention'

describe('getModuleTypesThatRequireExtraAttention', () => {
Expand Down
Loading

0 comments on commit 26f7624

Please sign in to comment.