Skip to content

Commit

Permalink
migration react-api-client
Browse files Browse the repository at this point in the history
  • Loading branch information
jerader committed Mar 1, 2024
1 parent aee12fe commit 20504ec
Show file tree
Hide file tree
Showing 42 changed files with 421 additions and 816 deletions.
1 change: 1 addition & 0 deletions react-api-client/src/api/__tests__/useHost.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// tests for the HostConfig context and hook
import * as React from 'react'
import { describe, it, expect } from 'vitest'
import { renderHook } from '@testing-library/react'

import { ApiHostProvider, useHost } from '..'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { when, resetAllWhenMocks } from 'jest-when'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from 'react-query'
import { act, renderHook, waitFor } from '@testing-library/react'
import {
Expand All @@ -10,13 +10,8 @@ import { useHost } from '../../api'
import { useDeleteCalibrationMutation } from '..'
import type { HostConfig, Response, EmptyResponse } from '@opentrons/api-client'

jest.mock('@opentrons/api-client')
jest.mock('../../api/useHost')

const mockDeleteCalibration = deleteCalibration as jest.MockedFunction<
typeof deleteCalibration
>
const mockUseHost = useHost as jest.MockedFunction<typeof useHost>
vi.mock('@opentrons/api-client')
vi.mock('../../api/useHost')

const HOST_CONFIG: HostConfig = { hostname: 'localhost' }
const DELETE_CAL_DATA_RESPONSE = {
Expand All @@ -41,15 +36,10 @@ describe('useDeleteCalibrationMutation hook', () => {

wrapper = clientProvider
})
afterEach(() => {
resetAllWhenMocks()
})

it('should return no data when calling deleteProtocol if the request fails', async () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockDeleteCalibration)
.calledWith(HOST_CONFIG, requestParams)
.mockRejectedValue('oh no')
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(deleteCalibration).mockRejectedValue('oh no')

const { result } = renderHook(() => useDeleteCalibrationMutation(), {
wrapper,
Expand All @@ -64,12 +54,11 @@ describe('useDeleteCalibrationMutation hook', () => {
})

it('should delete calibration data when calling the deleteCalibration callback', async () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockDeleteCalibration)
.calledWith(HOST_CONFIG, requestParams)
.mockResolvedValue({
data: DELETE_CAL_DATA_RESPONSE,
} as Response<EmptyResponse>)
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(deleteCalibration)
.mockResolvedValue({
data: DELETE_CAL_DATA_RESPONSE,
} as Response<EmptyResponse>)

const { result } = renderHook(() => useDeleteCalibrationMutation(), {
wrapper,
Expand Down
31 changes: 12 additions & 19 deletions react-api-client/src/health/__tests__/useHealth.test.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
// tests for the useHealth hooks
import * as React from 'react'
import { when } from 'jest-when'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from 'react-query'
import { renderHook, waitFor } from '@testing-library/react'

import { getHealth as mockGetHealth } from '@opentrons/api-client'
import { useHost as mockUseHost } from '../../api'
import { getHealth } from '@opentrons/api-client'
import { useHost } from '../../api'
import { useHealth } from '..'

import type { HostConfig, Response, Health } from '@opentrons/api-client'

jest.mock('@opentrons/api-client')
jest.mock('../../api/useHost')

const getHealth = mockGetHealth as jest.MockedFunction<typeof mockGetHealth>
const useHost = mockUseHost as jest.MockedFunction<typeof mockUseHost>
vi.mock('@opentrons/api-client')
vi.mock('../../api/useHost')

const HOST_CONFIG: HostConfig = { hostname: 'localhost' }
const HEALTH_RESPONSE: Health = { name: 'robot-name' } as Health
Expand All @@ -33,32 +30,28 @@ describe('useHealth hook', () => {
wrapper = clientProvider
})

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

it('should return no data if no host', () => {
when(useHost).calledWith().mockReturnValue(null)
vi.mocked(useHost).mockReturnValue(null)

const { result } = renderHook(useHealth, { wrapper })

expect(result.current).toBeUndefined()
})

it('should return no data if health request fails', () => {
when(useHost).calledWith().mockReturnValue(HOST_CONFIG)
when(getHealth).calledWith(HOST_CONFIG).mockRejectedValue('oh no')
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(getHealth).mockRejectedValue('oh no')

const { result } = renderHook(useHealth, { wrapper })

expect(result.current).toBeUndefined()
})

it('should return health response data', async () => {
when(useHost).calledWith().mockReturnValue(HOST_CONFIG)
when(getHealth)
.calledWith(HOST_CONFIG)
.mockResolvedValue({ data: HEALTH_RESPONSE } as Response<Health>)
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(getHealth).mockResolvedValue({
data: HEALTH_RESPONSE,
} as Response<Health>)

const { result } = renderHook(() => useHealth(), { wrapper })

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { when, resetAllWhenMocks } from 'jest-when'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from 'react-query'
import { act, renderHook, waitFor } from '@testing-library/react'
import { createMaintenanceCommand } from '@opentrons/api-client'
Expand All @@ -10,13 +10,8 @@ import { MAINTENANCE_RUN_ID, mockAnonLoadCommand } from '../__fixtures__'

import type { HostConfig } from '@opentrons/api-client'

jest.mock('@opentrons/api-client')
jest.mock('../../api/useHost')

const mockCreateMaintenanceCommand = createMaintenanceCommand as jest.MockedFunction<
typeof createMaintenanceCommand
>
const mockUseHost = useHost as jest.MockedFunction<typeof useHost>
vi.mock('@opentrons/api-client')
vi.mock('../../api/useHost')

const HOST_CONFIG: HostConfig = { hostname: 'localhost' }

Expand All @@ -32,15 +27,12 @@ describe('useCreateMaintenanceCommandMutation hook', () => {
)
wrapper = clientProvider
})
afterEach(() => {
resetAllWhenMocks()
})

it('should issue the given command to the given run when callback is called', async () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockCreateMaintenanceCommand)
.calledWith(HOST_CONFIG, MAINTENANCE_RUN_ID, mockAnonLoadCommand, {})
.mockResolvedValue({ data: 'something' } as any)
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(createMaintenanceCommand).mockResolvedValue({
data: 'something',
} as any)

const { result } = renderHook(() => useCreateMaintenanceCommandMutation(), {
wrapper,
Expand All @@ -60,13 +52,10 @@ describe('useCreateMaintenanceCommandMutation hook', () => {
it('should pass waitUntilComplete and timeout through if given command', async () => {
const waitUntilComplete = true
const timeout = 2000
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockCreateMaintenanceCommand)
.calledWith(HOST_CONFIG, MAINTENANCE_RUN_ID, mockAnonLoadCommand, {
waitUntilComplete,
timeout,
})
.mockResolvedValue({ data: 'something' } as any)
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(createMaintenanceCommand).mockResolvedValue({
data: 'something',
} as any)

const { result } = renderHook(() => useCreateMaintenanceCommandMutation(), {
wrapper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { when, resetAllWhenMocks } from 'jest-when'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from 'react-query'
import { act, renderHook, waitFor } from '@testing-library/react'
import { createMaintenanceRun } from '@opentrons/api-client'
Expand All @@ -13,13 +13,8 @@ import type {
MaintenanceRun,
} from '@opentrons/api-client'

jest.mock('@opentrons/api-client')
jest.mock('../../api/useHost')

const mockCreateMaintenanceRun = createMaintenanceRun as jest.MockedFunction<
typeof createMaintenanceRun
>
const mockUseHost = useHost as jest.MockedFunction<typeof useHost>
vi.mock('@opentrons/api-client')
vi.mock('../../api/useHost')

const HOST_CONFIG: HostConfig = { hostname: 'localhost' }

Expand All @@ -36,15 +31,11 @@ describe('useCreateMaintenanceRunMutation hook', () => {

wrapper = clientProvider
})
afterEach(() => {
resetAllWhenMocks()
})

it('should return no data when calling createMaintenanceRun if the request fails', async () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockCreateMaintenanceRun)
.calledWith(HOST_CONFIG, {})
.mockRejectedValue('oh no')
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(createMaintenanceRun)
.mockRejectedValue('oh no')

const { result } = renderHook(() => useCreateMaintenanceRunMutation(), {
wrapper,
Expand All @@ -64,12 +55,10 @@ describe('useCreateMaintenanceRunMutation hook', () => {
location: { slotName: '1' },
vector: { x: 1, y: 2, z: 3 },
}
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockCreateMaintenanceRun)
.calledWith(HOST_CONFIG, { labwareOffsets: [mockOffset] })
.mockResolvedValue({
data: mockMaintenanceRunResponse,
} as Response<MaintenanceRun>)
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(createMaintenanceRun).mockResolvedValue({
data: mockMaintenanceRunResponse,
} as Response<MaintenanceRun>)

const { result } = renderHook(() => useCreateMaintenanceRunMutation(), {
wrapper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { when, resetAllWhenMocks } from 'jest-when'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from 'react-query'
import { act, renderHook, waitFor } from '@testing-library/react'
import { deleteMaintenanceRun } from '@opentrons/api-client'
Expand All @@ -9,13 +9,8 @@ import { useDeleteMaintenanceRunMutation } from '..'

import type { HostConfig, EmptyResponse, Response } from '@opentrons/api-client'

jest.mock('@opentrons/api-client')
jest.mock('../../api/useHost')

const mockDeleteMaintenanceRun = deleteMaintenanceRun as jest.MockedFunction<
typeof deleteMaintenanceRun
>
const mockUseHost = useHost as jest.MockedFunction<typeof useHost>
vi.mock('@opentrons/api-client')
vi.mock('../../api/useHost')

const HOST_CONFIG: HostConfig = { hostname: 'localhost' }

Expand All @@ -32,15 +27,11 @@ describe('useDeleteMaintenanceRunMutation hook', () => {

wrapper = clientProvider
})
afterEach(() => {
resetAllWhenMocks()
})

it('should return no data when calling DeleteMaintenanceRun if the request fails', async () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockDeleteMaintenanceRun)
.calledWith(HOST_CONFIG, MAINTENANCE_RUN_ID)
.mockRejectedValue('oh no')
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(deleteMaintenanceRun)
.mockRejectedValue('oh no')

const { result } = renderHook(() => useDeleteMaintenanceRunMutation(), {
wrapper,
Expand All @@ -54,10 +45,9 @@ describe('useDeleteMaintenanceRunMutation hook', () => {
})

it('should delete a maintenance run when calling the deleteMaintenanceRun callback with basic run args', async () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockDeleteMaintenanceRun)
.calledWith(HOST_CONFIG, MAINTENANCE_RUN_ID)
.mockResolvedValue({ data: { data: null } } as Response<EmptyResponse>)
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(deleteMaintenanceRun)
.mockResolvedValue({ data: { data: null } } as Response<EmptyResponse>)

const { result } = renderHook(() => useDeleteMaintenanceRunMutation(), {
wrapper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { when, resetAllWhenMocks } from 'jest-when'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from 'react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { getMaintenanceRun } from '@opentrons/api-client'
Expand All @@ -13,13 +13,8 @@ import type {
MaintenanceRun,
} from '@opentrons/api-client'

jest.mock('@opentrons/api-client')
jest.mock('../../api/useHost')

const mockGetMaintenanceRun = getMaintenanceRun as jest.MockedFunction<
typeof getMaintenanceRun
>
const mockUseHost = useHost as jest.MockedFunction<typeof useHost>
vi.mock('@opentrons/api-client')
vi.mock('../../api/useHost')

const HOST_CONFIG: HostConfig = { hostname: 'localhost' }
const MAINTENANCE_RUN_RESPONSE = {
Expand All @@ -39,12 +34,9 @@ describe('useMaintenanceRunQuery hook', () => {

wrapper = clientProvider
})
afterEach(() => {
resetAllWhenMocks()
})

it('should return no data if no host', () => {
when(mockUseHost).calledWith().mockReturnValue(null)
vi.mocked(useHost).mockReturnValue(null)

const { result } = renderHook(
() => useMaintenanceRunQuery(MAINTENANCE_RUN_ID),
Expand All @@ -57,10 +49,8 @@ describe('useMaintenanceRunQuery hook', () => {
})

it('should return no data if the get maintenance run request fails', () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockGetMaintenanceRun)
.calledWith(HOST_CONFIG, MAINTENANCE_RUN_ID)
.mockRejectedValue('oh no')
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(getMaintenanceRun).mockRejectedValue('oh no')

const { result } = renderHook(
() => useMaintenanceRunQuery(MAINTENANCE_RUN_ID),
Expand All @@ -72,12 +62,10 @@ describe('useMaintenanceRunQuery hook', () => {
})

it('should return a maintenance run', async () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockGetMaintenanceRun)
.calledWith(HOST_CONFIG, MAINTENANCE_RUN_ID)
.mockResolvedValue({
data: MAINTENANCE_RUN_RESPONSE,
} as Response<MaintenanceRun>)
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(getMaintenanceRun).mockResolvedValue({
data: MAINTENANCE_RUN_RESPONSE,
} as Response<MaintenanceRun>)

const { result } = renderHook(
() => useMaintenanceRunQuery(MAINTENANCE_RUN_ID),
Expand Down
Loading

0 comments on commit 20504ec

Please sign in to comment.