Skip to content

Commit

Permalink
feat(app): this PR is for robot rename function
Browse files Browse the repository at this point in the history
This PR is for robot rename function and this PR will be used by #10723 to retrieve the new robot
name

partially fix #10709
  • Loading branch information
koji committed Jun 22, 2022
1 parent 78a578d commit 5015c92
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
56 changes: 56 additions & 0 deletions app/src/organisms/Devices/hooks/useRobotAnalyticsData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as React from 'react'
import { useSelector, useDispatch } from 'react-redux'

import { useRobot } from './'
import { getAttachedPipettes } from '../../../redux/pipettes'
import { getRobotSettings, fetchSettings } from '../../../redux/robot-settings'
import { FF_PREFIX } from '../../../redux/analytics'
import {
getRobotApiVersion,
getRobotFirmwareVersion,
} from '../../../redux/discovery'

import type { State, Dispatch } from '../../../redux/types'
import type { RobotAnalyticsData } from '../../../redux/analytics/types'

/**
*
* @param {string} robotName
* @returns {RobotAnalyticsData}
* for use in trackEvent
*/
export function useRobotAnalyticsData(
robotName: string
): RobotAnalyticsData | null {
const robot = useRobot(robotName)
const pipettes = useSelector((state: State) =>
getAttachedPipettes(state, robotName)
)
const settings = useSelector((state: State) =>
getRobotSettings(state, robotName)
)
const dispatch = useDispatch<Dispatch>()

React.useEffect(() => {
dispatch(fetchSettings(robotName))
}, [dispatch, robotName])

if (robot != null) {
// @ts-expect-error RobotAnalyticsData type needs boolean values should it be boolean | string
return settings.reduce<RobotAnalyticsData>(
(result, setting) => ({
...result,
[`${FF_PREFIX}${setting.id}`]: !!(setting.value ?? false),
}),
// @ts-expect-error RobotAnalyticsData type needs boolean values should it be boolean | string
{
robotApiServerVersion: getRobotApiVersion(robot) || '',
robotSmoothieVersion: getRobotFirmwareVersion(robot) || '',
robotLeftPipette: pipettes.left?.model || '',
robotRightPipette: pipettes.right?.model || '',
}
)
}

return null
}
Empty file.
70 changes: 70 additions & 0 deletions react-api-client/src/server/__tests__/useRobotName.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as React from 'react'
import { when, resetAllWhenMocks } from 'jest-when'
import { QueryClient, QueryClientProvider } from 'react-query'
import { renderHook } from '@testing-library/react-hooks'
import { getRobotName } from '@opentrons/api-client'
import { useHost } from '../../api'
import { useRobotName } from '..'

import type {
HostConfig,
Response,
CurrentRobotName,
} from '@opentrons/api-client'
import { act } from 'react-test-renderer'

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

const mockGetRobotName = getRobotName as jest.MockedFunction<
typeof getRobotName
>
const mockUseHost = useHost as jest.MockedFunction<typeof useHost>

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

const ROBOT_NAME = { name: 'otie' }

describe('useRobotName hook', () => {
let wrapper: React.FunctionComponent<{}>

beforeEach(() => {
const queryClient = new QueryClient()
const clientProvider: React.FunctionComponent<{}> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)

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

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

const { result } = renderHook(useRobotName, { wrapper })
expect(result.current.data).toBeUndefined()
})

it('should return no data if the getRobotName request fails', () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockGetRobotName).calledWith(HOST_CONFIG).mockRejectedValue('fail')

const { result } = renderHook(useRobotName, { wrapper })
expect(result.current.data).toBeUndefined()
})

it('should return robot name', async () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockGetRobotName)
.calledWith(HOST_CONFIG)
.mockResolvedValue({
data: ROBOT_NAME,
} as Response<CurrentRobotName>)

const { result, waitFor } = renderHook(useRobotName, { wrapper })
await waitFor(() => result.current.data != null)
expect(result.current.data).toEqual(ROBOT_NAME)
})
})

0 comments on commit 5015c92

Please sign in to comment.