Skip to content

Commit

Permalink
update P* tests for vitest migration
Browse files Browse the repository at this point in the history
  • Loading branch information
koji committed Feb 28, 2024
1 parent 9a80146 commit 6a74fcb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as React from 'react'
import { fireEvent } from '@testing-library/react'
import { fireEvent, screen } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom'
import { renderWithProviders } from '@opentrons/components'
import { describe, it, vi, beforeEach, afterEach, expect } from 'vitest'

import { renderWithProviders } from '../../../__testing-utils__'
import { i18n } from '../../../i18n'
import {
useTrackEvent,
Expand All @@ -17,21 +18,11 @@ import {

import { ProtocolOverflowMenu } from '../ProtocolOverflowMenu'

jest.mock('../../../redux/analytics')
jest.mock('../../../redux/protocol-storage')
vi.mock('../../../redux/analytics')
vi.mock('../../../redux/protocol-storage')

const mockHandleRunProtocol = jest.fn()
const mockHandleSendProtocolToFlex = jest.fn()

const mockViewProtocolSourceFolder = viewProtocolSourceFolder as jest.MockedFunction<
typeof viewProtocolSourceFolder
>
const mockRemoveProtocol = removeProtocol as jest.MockedFunction<
typeof removeProtocol
>
const mockUseTrackEvent = useTrackEvent as jest.MockedFunction<
typeof useTrackEvent
>
const mockHandleRunProtocol = vi.fn()
const mockHandleSendProtocolToFlex = vi.fn()

const render = () => {
return renderWithProviders(
Expand All @@ -46,32 +37,32 @@ const render = () => {
)
}

let mockTrackEvent: jest.Mock
let mockTrackEvent: vi.Mock

describe('ProtocolOverflowMenu', () => {
beforeEach(() => {
mockTrackEvent = jest.fn()
mockUseTrackEvent.mockReturnValue(mockTrackEvent)
mockTrackEvent = vi.fn()
vi.mocked(useTrackEvent).mockReturnValue(mockTrackEvent)
})

afterEach(() => {
jest.resetAllMocks()
vi.resetAllMocks()
})

it('should render label when clicking overflowMenu', () => {
const [{ getByTestId, getByText }] = render()
const button = getByTestId('ProtocolOverflowMenu_overflowBtn')
render()
const button = screen.getByTestId('ProtocolOverflowMenu_overflowBtn')
fireEvent.click(button)
getByText('Show in folder')
getByText('Start setup')
getByText('Delete')
screen.getByText('Show in folder')
screen.getByText('Start setup')
screen.getByText('Delete')
})

it('should call run protocol when clicking Start setup button', () => {
const [{ getByTestId, getByText }] = render()
const button = getByTestId('ProtocolOverflowMenu_overflowBtn')
render()
const button = screen.getByTestId('ProtocolOverflowMenu_overflowBtn')
fireEvent.click(button)
const runButton = getByText('Start setup')
const runButton = screen.getByText('Start setup')
fireEvent.click(runButton)
expect(mockTrackEvent).toHaveBeenCalledWith({
name: ANALYTICS_PROTOCOL_PROCEED_TO_RUN,
Expand All @@ -93,47 +84,49 @@ describe('ProtocolOverflowMenu', () => {
})

it('should call folder open function when clicking show in folder', () => {
const [{ getByTestId, getByText }] = render()
const button = getByTestId('ProtocolOverflowMenu_overflowBtn')
render()
const button = screen.getByTestId('ProtocolOverflowMenu_overflowBtn')
fireEvent.click(button)
const folderButton = getByText('Show in folder')
const folderButton = screen.getByText('Show in folder')
fireEvent.click(folderButton)
expect(mockViewProtocolSourceFolder).toHaveBeenCalled()
expect(vi.mocked(viewProtocolSourceFolder)).toHaveBeenCalled()
})

it('should render modal when clicking delete button', () => {
const [{ getByTestId, getByText, getByRole }] = render()
const button = getByTestId('ProtocolOverflowMenu_overflowBtn')
render()
const button = screen.getByTestId('ProtocolOverflowMenu_overflowBtn')
fireEvent.click(button)
const deleteButton = getByText('Delete')
const deleteButton = screen.getByText('Delete')
fireEvent.click(deleteButton)
getByText('Delete this protocol?')
getByText(
screen.getByText('Delete this protocol?')
screen.getByText(
'This protocol will be moved to this computer’s trash and may be unrecoverable.'
)
getByRole('button', { name: 'Yes, delete protocol' })
getByRole('button', { name: 'cancel' })
screen.getByRole('button', { name: 'Yes, delete protocol' })
screen.getByRole('button', { name: 'cancel' })
})

it('should call delete function when clicking yes button', () => {
const [{ getByTestId, getByText, getByRole }] = render()
const button = getByTestId('ProtocolOverflowMenu_overflowBtn')
render()
const button = screen.getByTestId('ProtocolOverflowMenu_overflowBtn')
fireEvent.click(button)
const deleteButton = getByText('Delete')
const deleteButton = screen.getByText('Delete')
fireEvent.click(deleteButton)
const yesButton = getByRole('button', { name: 'Yes, delete protocol' })
const yesButton = screen.getByRole('button', {
name: 'Yes, delete protocol',
})
fireEvent.click(yesButton)
expect(mockRemoveProtocol).toHaveBeenCalled()
expect(vi.mocked(removeProtocol)).toHaveBeenCalled()
})

it('should close modal when clicking cancel button', () => {
const [{ getByTestId, getByText, getByRole, queryByText }] = render()
const button = getByTestId('ProtocolOverflowMenu_overflowBtn')
render()
const button = screen.getByTestId('ProtocolOverflowMenu_overflowBtn')
fireEvent.click(button)
const deleteButton = getByText('Delete')
const deleteButton = screen.getByText('Delete')
fireEvent.click(deleteButton)
const cancelButton = getByRole('button', { name: 'cancel' })
const cancelButton = screen.getByRole('button', { name: 'cancel' })
fireEvent.click(cancelButton)
expect(queryByText('Delete this protocol?')).not.toBeInTheDocument()
expect(screen.queryByText('Delete this protocol?')).not.toBeInTheDocument()
})
})
19 changes: 10 additions & 9 deletions app/src/organisms/ProtocolsLanding/__tests__/UploadInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import * as React from 'react'
import { fireEvent, screen } from '@testing-library/react'
import { BrowserRouter } from 'react-router-dom'
import { renderWithProviders } from '@opentrons/components'
import { describe, it, vi, beforeEach, afterEach, expect } from 'vitest'
import { renderWithProviders } from '../../../__testing-utils__'
import { i18n } from '../../../i18n'
import {
useTrackEvent,
ANALYTICS_IMPORT_PROTOCOL_TO_APP,
} from '../../../redux/analytics'
import { ProtocolUploadInput } from '../ProtocolUploadInput'

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

const mockUseTrackEvent = useTrackEvent as jest.Mock<typeof useTrackEvent>
const mockUseTrackEvent = useTrackEvent as vi.Mock<typeof useTrackEvent>

describe('ProtocolUploadInput', () => {
let onUpload: jest.MockedFunction<() => {}>
let trackEvent: jest.MockedFunction<any>
let onUpload: vi.MockedFunction<() => {}>
let trackEvent: vi.MockedFunction<any>
const render = () => {
return renderWithProviders(
<BrowserRouter>
Expand All @@ -28,12 +29,12 @@ describe('ProtocolUploadInput', () => {
}

beforeEach(() => {
onUpload = jest.fn()
trackEvent = jest.fn()
onUpload = vi.fn()
trackEvent = vi.fn()
mockUseTrackEvent.mockReturnValue(trackEvent)
})
afterEach(() => {
jest.resetAllMocks()
vi.resetAllMocks()
})

it('renders correct contents for empty state', () => {
Expand All @@ -52,7 +53,7 @@ describe('ProtocolUploadInput', () => {
render()
const button = screen.getByRole('button', { name: 'Upload' })
const input = screen.getByTestId('file_input')
input.click = jest.fn()
input.click = vi.fn()
fireEvent.click(button)
expect(input.click).toHaveBeenCalled()
})
Expand Down
8 changes: 5 additions & 3 deletions app/src/organisms/ProtocolsLanding/__tests__/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as React from 'react'
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import { renderHook } from '@testing-library/react'
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'

import { FLEX_ROBOT_TYPE, OT2_ROBOT_TYPE } from '@opentrons/shared-data'

import { useSortedProtocols } from '../hooks'
Expand Down Expand Up @@ -277,12 +279,12 @@ const mockStoredProtocolData = [
] as StoredProtocolData[]

describe('useSortedProtocols', () => {
const store: Store<State> = createStore(jest.fn(), {})
const store: Store<State> = createStore(vi.fn(), {})
beforeEach(() => {
store.dispatch = jest.fn()
store.dispatch = vi.fn()
})
afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
})

it('should return an object with protocols sorted alphabetically', () => {
Expand Down
1 change: 1 addition & 0 deletions app/src/organisms/ProtocolsLanding/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest'
import { getisFlexProtocol, getRobotTypeDisplayName } from '../utils'
import type { ProtocolAnalysisOutput } from '@opentrons/shared-data'

Expand Down

0 comments on commit 6a74fcb

Please sign in to comment.