From 26f7624f9bb3d00927e57a7a71e8c6ab1092a027 Mon Sep 17 00:00:00 2001 From: koji Date: Fri, 1 Mar 2024 17:51:14 -0500 Subject: [PATCH] update test for vitest migration --- .../__tests__/ApplyHistoricOffsets.test.tsx | 12 +-- .../__tests__/getNestedLabwareInfo.test.tsx | 1 + .../__tests__/utils.test.ts | 1 + .../__tests__/LiquidDetailCard.test.tsx | 74 ++++++++-------- .../LiquidsLabwareDetailsModal.test.tsx | 10 ++- .../__tests__/LabwareInfoOverlay.test.tsx | 84 +++++++++---------- ...duleTypesThatRequireExtraAttention.test.ts | 1 + .../__tests__/CheckItem.test.tsx | 33 ++++---- 8 files changed, 113 insertions(+), 103 deletions(-) diff --git a/app/src/organisms/ApplyHistoricOffsets/__tests__/ApplyHistoricOffsets.test.tsx b/app/src/organisms/ApplyHistoricOffsets/__tests__/ApplyHistoricOffsets.test.tsx index 2fcef7e7ea1..e13a8e217bd 100644 --- a/app/src/organisms/ApplyHistoricOffsets/__tests__/ApplyHistoricOffsets.test.tsx +++ b/app/src/organisms/ApplyHistoricOffsets/__tests__/ApplyHistoricOffsets.test.tsx @@ -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', @@ -59,7 +61,7 @@ const mockFourthCandidate: OffsetCandidate = { } describe('ApplyHistoricOffsets', () => { - const mockSetShouldApplyOffsets = jest.fn() + const mockSetShouldApplyOffsets = vi.fn() const render = ( props?: Partial> ) => diff --git a/app/src/organisms/Devices/ProtocolRun/SetupLabware/__tests__/getNestedLabwareInfo.test.tsx b/app/src/organisms/Devices/ProtocolRun/SetupLabware/__tests__/getNestedLabwareInfo.test.tsx index 0ba0fffdcf5..24c50ca3efa 100644 --- a/app/src/organisms/Devices/ProtocolRun/SetupLabware/__tests__/getNestedLabwareInfo.test.tsx +++ b/app/src/organisms/Devices/ProtocolRun/SetupLabware/__tests__/getNestedLabwareInfo.test.tsx @@ -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' diff --git a/app/src/organisms/Devices/ProtocolRun/SetupLabwarePositionCheck/__tests__/utils.test.ts b/app/src/organisms/Devices/ProtocolRun/SetupLabwarePositionCheck/__tests__/utils.test.ts index d22fae15ded..c073e2466f6 100644 --- a/app/src/organisms/Devices/ProtocolRun/SetupLabwarePositionCheck/__tests__/utils.test.ts +++ b/app/src/organisms/Devices/ProtocolRun/SetupLabwarePositionCheck/__tests__/utils.test.ts @@ -1,3 +1,4 @@ +import { describe, it, expect } from 'vitest' import { getLatestCurrentOffsets } from '../utils' import type { LabwareOffset } from '@opentrons/api-client' diff --git a/app/src/organisms/Devices/ProtocolRun/SetupLiquids/__tests__/LiquidDetailCard.test.tsx b/app/src/organisms/Devices/ProtocolRun/SetupLiquids/__tests__/LiquidDetailCard.test.tsx index 8290a9d42a4..7b1489cad25 100644 --- a/app/src/organisms/Devices/ProtocolRun/SetupLiquids/__tests__/LiquidDetailCard.test.tsx +++ b/app/src/organisms/Devices/ProtocolRun/SetupLiquids/__tests__/LiquidDetailCard.test.tsx @@ -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, @@ -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) => { return renderWithProviders(, { i18nInstance: i18n, })[0] } -let mockTrackEvent: jest.Mock +let mockTrackEvent: Mock describe('LiquidDetailCard', () => { let props: React.ComponentProps 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', @@ -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}` ) }) diff --git a/app/src/organisms/Devices/ProtocolRun/SetupLiquids/__tests__/LiquidsLabwareDetailsModal.test.tsx b/app/src/organisms/Devices/ProtocolRun/SetupLiquids/__tests__/LiquidsLabwareDetailsModal.test.tsx index 97e2d71aa64..236f503ece6 100644 --- a/app/src/organisms/Devices/ProtocolRun/SetupLiquids/__tests__/LiquidsLabwareDetailsModal.test.tsx +++ b/app/src/organisms/Devices/ProtocolRun/SetupLiquids/__tests__/LiquidsLabwareDetailsModal.test.tsx @@ -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' diff --git a/app/src/organisms/Devices/ProtocolRun/__tests__/LabwareInfoOverlay.test.tsx b/app/src/organisms/Devices/ProtocolRun/__tests__/LabwareInfoOverlay.test.tsx index 97c89f309a1..47d92466df4 100644 --- a/app/src/organisms/Devices/ProtocolRun/__tests__/LabwareInfoOverlay.test.tsx +++ b/app/src/organisms/Devices/ProtocolRun/__tests__/LabwareInfoOverlay.test.tsx @@ -1,13 +1,19 @@ 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' @@ -15,17 +21,25 @@ 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() return { - ...actualSharedData, - getLabwareDisplayName: jest.fn(), + ...actual, + getLabwareDisplayName: vi.fn(), } }) @@ -40,21 +54,6 @@ const render = (props: React.ComponentProps) => { )[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' @@ -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, @@ -79,15 +78,15 @@ 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', @@ -95,21 +94,20 @@ describe('LabwareInfoOverlay', () => { 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', () => { @@ -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: [ { diff --git a/app/src/organisms/Devices/ProtocolRun/utils/__tests__/getModuleTypesThatRequireExtraAttention.test.ts b/app/src/organisms/Devices/ProtocolRun/utils/__tests__/getModuleTypesThatRequireExtraAttention.test.ts index 279e6f32909..f2058e6900a 100644 --- a/app/src/organisms/Devices/ProtocolRun/utils/__tests__/getModuleTypesThatRequireExtraAttention.test.ts +++ b/app/src/organisms/Devices/ProtocolRun/utils/__tests__/getModuleTypesThatRequireExtraAttention.test.ts @@ -1,3 +1,4 @@ +import { describe, it, expect } from 'vitest' import { getModuleTypesThatRequireExtraAttention } from '../getModuleTypesThatRequireExtraAttention' describe('getModuleTypesThatRequireExtraAttention', () => { diff --git a/app/src/organisms/LabwarePositionCheck/__tests__/CheckItem.test.tsx b/app/src/organisms/LabwarePositionCheck/__tests__/CheckItem.test.tsx index 3d4137f5963..50b3c1ff1a6 100644 --- a/app/src/organisms/LabwarePositionCheck/__tests__/CheckItem.test.tsx +++ b/app/src/organisms/LabwarePositionCheck/__tests__/CheckItem.test.tsx @@ -1,20 +1,28 @@ import * as React from 'react' import { fireEvent, screen } from '@testing-library/react' -import { resetAllWhenMocks, when } from 'jest-when' -import { renderWithProviders, nestedTextMatcher } from '@opentrons/components' +import { when } from 'vitest-when' +import { describe, it, vi, beforeEach, afterEach, expect } from 'vitest' + import { FLEX_ROBOT_TYPE, HEATERSHAKER_MODULE_V1, OT2_ROBOT_TYPE, THERMOCYCLER_MODULE_V2, } from '@opentrons/shared-data' + +import { + nestedTextMatcher, + renderWithProviders, +} from '../../../__testing-utils__' import { i18n } from '../../../i18n' import { CheckItem } from '../CheckItem' import { SECTIONS } from '../constants' import { mockCompletedAnalysis, mockExistingOffsets } from '../__fixtures__' -jest.mock('../../../redux/config') -jest.mock('../../Devices/hooks') +import type { Mock } from 'vitest' + +vi.mock('../../../redux/config') +vi.mock('../../Devices/hooks') const mockStartPosition = { x: 10, y: 20, z: 30 } const mockEndPosition = { x: 9, y: 19, z: 29 } @@ -27,12 +35,10 @@ const render = (props: React.ComponentProps) => { describe('CheckItem', () => { let props: React.ComponentProps - let mockChainRunCommands: jest.Mock + let mockChainRunCommands: Mock beforeEach(() => { - mockChainRunCommands = jest - .fn() - .mockImplementation(() => Promise.resolve([])) + mockChainRunCommands = vi.fn().mockImplementation(() => Promise.resolve([])) props = { section: SECTIONS.CHECK_LABWARE, pipetteId: mockCompletedAnalysis.pipettes[0].id, @@ -40,11 +46,11 @@ describe('CheckItem', () => { definitionUri: mockCompletedAnalysis.labware[0].definitionUri, location: { slotName: 'D1' }, protocolData: mockCompletedAnalysis, - proceed: jest.fn(), + proceed: vi.fn(), chainRunCommands: mockChainRunCommands, - handleJog: jest.fn(), - registerPosition: jest.fn(), - setFatalError: jest.fn(), + handleJog: vi.fn(), + registerPosition: vi.fn(), + setFatalError: vi.fn(), workingOffsets: [], existingOffsets: mockExistingOffsets, isRobotMoving: false, @@ -53,8 +59,7 @@ describe('CheckItem', () => { } }) afterEach(() => { - jest.resetAllMocks() - resetAllWhenMocks() + vi.resetAllMocks() }) it('renders correct copy when preparing space with tip rack', () => { render(props)