-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): this PR is for robot rename function
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
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
70
react-api-client/src/server/__tests__/useRobotName.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |