Skip to content

Commit

Permalink
refactor(app): migrate redux jest tests to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuff committed Feb 23, 2024
1 parent dab10da commit 3b5247f
Show file tree
Hide file tree
Showing 109 changed files with 694 additions and 728 deletions.
2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"lodash": "4.17.21",
"mixpanel-browser": "2.22.1",
"netmask": "2.0.2",
"node-fetch": "2.6.7",
"path-to-regexp": "3.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down Expand Up @@ -71,6 +72,7 @@
"@types/file-saver": "2.0.1",
"@types/jszip": "3.1.7",
"@types/mixpanel-browser": "^2.35.6",
"@types/node-fetch": "2.6.11",
"@types/styled-components": "^5.1.26",
"axios": "^0.21.1"
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/redux/alerts/__tests__/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import * as Config from '../../config'
import * as Actions from '../actions'

Expand Down
19 changes: 8 additions & 11 deletions app/src/redux/alerts/__tests__/epic.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi, describe, it, expect, beforeEach } from 'vitest'
import { TestScheduler } from 'rxjs/testing'

import * as Cfg from '../../config'
Expand All @@ -7,9 +8,7 @@ import { alertsEpic } from '../epic'
import type { Action, State } from '../../types'
import type { AlertId } from '../types'

jest.mock('../../config/selectors')

const getConfig = Cfg.getConfig as jest.MockedFunction<typeof Cfg.getConfig>
vi.mock('../../config/selectors')

const MOCK_STATE: State = { mockState: true } as any
const MOCK_ALERT_1: AlertId = 'mockAlert1' as any
Expand All @@ -24,15 +23,13 @@ describe('alerts epic', () => {
})
})

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

it('should trigger a config:ADD_UNIQUE_VALUE to save persistent alert ignores', () => {
getConfig.mockImplementation((state: State) => {
expect(state).toEqual(MOCK_STATE)
return { alerts: { ignored: [MOCK_ALERT_1] } } as any
})
vi.mocked(
(Cfg as any).getConfig((state: State) => {
expect(state).toEqual(MOCK_STATE)
return { alerts: { ignored: [MOCK_ALERT_1] } }
})
)

testScheduler.run(({ hot, expectObservable }) => {
const action$ = hot<Action>('-a', {
Expand Down
3 changes: 2 additions & 1 deletion app/src/redux/alerts/__tests__/reducer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Actions from '../actions'
import { describe, it, expect } from 'vitest'
import { alertsReducer } from '../reducer'
import * as Actions from '../actions'

import type { AlertId, AlertsState } from '../types'

Expand Down
12 changes: 4 additions & 8 deletions app/src/redux/alerts/__tests__/selectors.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { vi, describe, it, expect } from 'vitest'

import * as Cfg from '../../config'
import * as Selectors from '../selectors'

import type { State } from '../../types'
import type { Config } from '../../config/types'
import type { AlertId } from '../types'

jest.mock('../../config/selectors')

const getConfig = Cfg.getConfig as jest.MockedFunction<typeof Cfg.getConfig>
vi.mock('../../config/selectors')

const MOCK_ALERT_1: AlertId = 'mockAlert1' as any
const MOCK_ALERT_2: AlertId = 'mockAlert2' as any
Expand All @@ -19,16 +19,12 @@ const MOCK_CONFIG: Config = {

describe('alerts selectors', () => {
const stubGetConfig = (state: State, value = MOCK_CONFIG) => {
getConfig.mockImplementation((s: State) => {
vi.mocked(Cfg.getConfig).mockImplementation((s: State) => {
expect(s).toEqual(state)
return value
})
}

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

it('should be able to get a list of active alerts', () => {
const state: State = {
alerts: { active: [MOCK_ALERT_1, MOCK_ALERT_2], ignored: [] },
Expand Down
2 changes: 2 additions & 0 deletions app/src/redux/analytics/__tests__/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import * as Actions from '../actions'
import * as Constants from '../constants'

Expand Down
2 changes: 2 additions & 0 deletions app/src/redux/analytics/__tests__/alerts-events.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import { makeEvent } from '../make-event'

import * as Alerts from '../../alerts'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import { makeEvent } from '../make-event'

import * as CustomLabware from '../../custom-labware'
Expand Down
16 changes: 7 additions & 9 deletions app/src/redux/analytics/__tests__/epic.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// analytics epics tests
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest'
import { TestScheduler } from 'rxjs/testing'

import * as Cfg from '../../config'
Expand All @@ -12,10 +12,8 @@ import { analyticsEpic } from '../epic'

import type { Action, State } from '../../types'

jest.mock('../make-event')
jest.mock('../mixpanel')

const makeEventMock = makeEvent as jest.MockedFunction<typeof makeEvent>
vi.mock('../make-event')
vi.mock('../mixpanel')

describe('analytics epics', () => {
let testScheduler: TestScheduler
Expand All @@ -26,7 +24,7 @@ describe('analytics epics', () => {
})
})
afterEach(() => {
jest.resetAllMocks()
vi.resetAllMocks()
})

describe('initializeAnalyticsEpic', () => {
Expand Down Expand Up @@ -60,7 +58,7 @@ describe('analytics epics', () => {
const event = { name: 'fooEvent', properties: {} }

testScheduler.run(({ hot, expectObservable, flush }) => {
makeEventMock.mockReturnValueOnce([event] as any)
vi.mocked(makeEvent).mockReturnValueOnce([event] as any)

const action$ = hot<Action>('-a', { a: action } as any)
const state$ = hot<State>('s-', { s: state } as any)
Expand All @@ -77,7 +75,7 @@ describe('analytics epics', () => {
const state = { config: { analytics: { optedIn: true } } }

testScheduler.run(({ hot, expectObservable, flush }) => {
makeEventMock.mockReturnValueOnce([null] as any)
vi.mocked(makeEvent).mockReturnValueOnce([null] as any)

const action$ = hot<Action>('-a', { a: action } as any)
const state$ = hot<State>('s-', { s: state } as any)
Expand All @@ -94,7 +92,7 @@ describe('analytics epics', () => {
const state = { config: null }

testScheduler.run(({ hot, expectObservable, flush }) => {
makeEventMock.mockReturnValueOnce([null] as any)
vi.mocked(makeEvent).mockReturnValueOnce([null] as any)

const action$ = hot<Action>('-a', { a: action } as any)
const state$ = hot<State>('s-', { s: state } as any)
Expand Down
2 changes: 2 additions & 0 deletions app/src/redux/analytics/__tests__/hooks.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it } from 'vitest'

describe('analytics hooks', () => {
it.todo('replace deprecated enzyme test')
})
26 changes: 10 additions & 16 deletions app/src/redux/analytics/__tests__/make-event.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
// events map tests
import { vi, describe, it, expect, beforeEach } from 'vitest'

import { makeEvent } from '../make-event'
import * as selectors from '../selectors'

jest.mock('../selectors')
jest.mock('../../sessions/selectors')
jest.mock('../../discovery/selectors')
jest.mock('../../pipettes/selectors')
jest.mock('../../calibration/selectors')

const getAnalyticsSessionExitDetails = selectors.getAnalyticsSessionExitDetails as jest.MockedFunction<
typeof selectors.getAnalyticsSessionExitDetails
>
const getSessionInstrumentAnalyticsData = selectors.getSessionInstrumentAnalyticsData as jest.MockedFunction<
typeof selectors.getSessionInstrumentAnalyticsData
>
vi.mock('../selectors')
vi.mock('../../sessions/selectors')
vi.mock('../../discovery/selectors')
vi.mock('../../pipettes/selectors')
vi.mock('../../calibration/selectors')

describe('analytics events map', () => {
beforeEach(() => {
jest.resetAllMocks()
vi.resetAllMocks()
})

describe('events with protocol data', () => {
Expand Down Expand Up @@ -85,7 +79,7 @@ describe('analytics events map', () => {
command: { command: 'calibration.exitSession' },
},
} as any
getAnalyticsSessionExitDetails.mockReturnValue({
vi.mocked(selectors.getAnalyticsSessionExitDetails).mockReturnValue({
sessionType: 'my-session-type',
step: 'session-step',
})
Expand Down Expand Up @@ -113,7 +107,7 @@ describe('analytics events map', () => {
},
},
} as any
getSessionInstrumentAnalyticsData.mockReturnValue({
vi.mocked(selectors.getSessionInstrumentAnalyticsData).mockReturnValue({
sessionType: 'my-session-type',
pipetteModel: 'my-pipette-model',
})
Expand Down
4 changes: 3 additions & 1 deletion app/src/redux/analytics/__tests__/selectors.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { vi, describe, it, expect } from 'vitest'

import * as Selectors from '../selectors'
import * as SessionsSelectors from '../../sessions/selectors'

import type { State } from '../../types'
import type { DeckCalibrationSessionDetails } from '../../sessions/deck-calibration/types'

jest.mock('../../sessions/selectors')
vi.mock('../../sessions/selectors')

describe('analytics selectors', () => {
describe('analytics config selectors', () => {
Expand Down
30 changes: 13 additions & 17 deletions app/src/redux/analytics/__tests__/system-info-events.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { vi, describe, it, expect, beforeEach } from 'vitest'

import { makeEvent } from '../make-event'

import * as SystemInfo from '../../system-info'
import * as Fixtures from '../../system-info/__fixtures__'

import type { State } from '../../types'

jest.mock('../../system-info/selectors')

const getU2EDeviceAnalyticsProps = SystemInfo.getU2EDeviceAnalyticsProps as jest.MockedFunction<
typeof SystemInfo.getU2EDeviceAnalyticsProps
>
vi.mock('../../system-info/selectors')

const MOCK_STATE: State = { mockState: true } as any
const MOCK_ANALYTICS_PROPS = {
Expand All @@ -23,14 +21,12 @@ const MOCK_ANALYTICS_PROPS = {

describe('system info analytics events', () => {
beforeEach(() => {
getU2EDeviceAnalyticsProps.mockImplementation(state => {
expect(state).toBe(MOCK_STATE)
return MOCK_ANALYTICS_PROPS
})
})

afterEach(() => {
jest.resetAllMocks()
vi.mocked(SystemInfo.getU2EDeviceAnalyticsProps).mockImplementation(
state => {
expect(state).toBe(MOCK_STATE)
return MOCK_ANALYTICS_PROPS
}
)
})

it('should trigger an event on systemInfo:INITIALIZED', () => {
Expand Down Expand Up @@ -63,7 +59,7 @@ describe('system info analytics events', () => {
})

it('maps no assigned IPv4 address to false', () => {
getU2EDeviceAnalyticsProps.mockReturnValue({
vi.mocked(SystemInfo.getU2EDeviceAnalyticsProps).mockReturnValue({
...MOCK_ANALYTICS_PROPS,
'U2E IPv4 Address': null,
})
Expand All @@ -77,7 +73,7 @@ describe('system info analytics events', () => {
})

it('should not trigger on systemInfo:INITIALIZED if selector returns null', () => {
getU2EDeviceAnalyticsProps.mockReturnValue(null)
vi.mocked(SystemInfo.getU2EDeviceAnalyticsProps).mockReturnValue(null)

const action = SystemInfo.initialized([Fixtures.mockRealtekDevice], [])
const result = makeEvent(action, MOCK_STATE)
Expand All @@ -86,7 +82,7 @@ describe('system info analytics events', () => {
})

it('should not trigger on systemInfo:USB_DEVICE_ADDED if selector returns null', () => {
getU2EDeviceAnalyticsProps.mockReturnValue(null)
vi.mocked(SystemInfo.getU2EDeviceAnalyticsProps).mockReturnValue(null)

const action = SystemInfo.usbDeviceAdded(Fixtures.mockRealtekDevice)
const result = makeEvent(action, MOCK_STATE)
Expand All @@ -95,7 +91,7 @@ describe('system info analytics events', () => {
})

it('should not trigger on systemInfo:NETWORK_INTERFACES_CHANGED if selector returns null', () => {
getU2EDeviceAnalyticsProps.mockReturnValue(null)
vi.mocked(SystemInfo.getU2EDeviceAnalyticsProps).mockReturnValue(null)

const action = SystemInfo.networkInterfacesChanged([
Fixtures.mockNetworkInterface,
Expand Down
2 changes: 2 additions & 0 deletions app/src/redux/calibration/__tests__/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import * as Fixtures from '../__fixtures__'
import * as Actions from '../actions'
import type { CalibrationAction } from '../types'
Expand Down
2 changes: 2 additions & 0 deletions app/src/redux/calibration/__tests__/reducer.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import * as Fixtures from '../__fixtures__'
import * as PipetteOffset from '../pipette-offset'
import * as PipetteOffsetFixtures from '../pipette-offset/__fixtures__'
Expand Down
2 changes: 2 additions & 0 deletions app/src/redux/calibration/__tests__/selectors.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import * as Fixtures from '../__fixtures__'
import * as Selectors from '../selectors'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import { setupEpicTestMocks, runEpicTest } from '../../../robot-api/__utils__'
import * as Fixtures from '../../__fixtures__'
import * as Actions from '../../actions'
Expand All @@ -9,10 +11,6 @@ const makeTriggerAction = (robotName: string) =>
Actions.fetchCalibrationStatus(robotName)

describe('fetch calibration status epic', () => {
afterEach(() => {
jest.resetAllMocks()
})

it('calls GET /calibration/status', () => {
const mocks = setupEpicTestMocks(
makeTriggerAction,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import * as Fixtures from '../__fixtures__'
import * as Actions from '../actions'
import type { PipetteOffsetCalibrationsAction } from '../types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import * as Selectors from '../selectors'
import * as Fixtures from '../__fixtures__'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it, expect } from 'vitest'

import {
setupEpicTestMocks,
runEpicTest,
Expand All @@ -12,10 +14,6 @@ const makeTriggerActionAllCalibrations = (robotName: string) =>
Actions.fetchPipetteOffsetCalibrations(robotName)

describe('fetch pipette offset calibration epics', () => {
afterEach(() => {
jest.resetAllMocks()
})

it('calls GET /calibrations/pipette_offset', () => {
const mocks = setupEpicTestMocks(
makeTriggerActionAllCalibrations,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { describe, it, expect } from 'vitest'

import * as Fixtures from '../__fixtures__'
import * as Actions from '../actions'

import type { TipLengthCalibrationsAction } from '../types'

interface ActionSpec {
Expand Down
Loading

0 comments on commit 3b5247f

Please sign in to comment.