Skip to content

Commit

Permalink
fix(app): allow OT-3 wifi disconnect (#12728)
Browse files Browse the repository at this point in the history
useCanDisconnect does a legacy semver check for robot api versions greater than 3.17.0-alpha.0, but
at this time OT-3 internal releases are versioned 0.x.x. This change allows all robot api versions
on an OT-3 to support disconnect.

closes RQA-817
  • Loading branch information
brenthagen authored May 17, 2023
1 parent 33ebc2e commit f945447
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
29 changes: 24 additions & 5 deletions app/src/resources/networking/__tests__/useCanDisconnect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Provider } from 'react-redux'
import { renderHook } from '@testing-library/react-hooks'
import { getRobotApiVersionByName } from '../../../redux/discovery'

import { useIsOT3 } from '../../../organisms/Devices/hooks'
import { useCanDisconnect } from '../hooks/useCanDisconnect'
import { useWifiList } from '../hooks/useWifiList'

Expand All @@ -13,11 +14,13 @@ import type { State } from '../../../redux/types'
import { SECURITY_WPA_EAP, WifiNetwork } from '@opentrons/api-client'

jest.mock('../hooks/useWifiList')
jest.mock('../../../organisms/Devices/hooks')
jest.mock('../../../redux/discovery')

const mockGetRobotApiVersionByName = getRobotApiVersionByName as jest.MockedFunction<
typeof getRobotApiVersionByName
>
const mockUseIsOT3 = useIsOT3 as jest.MockedFunction<typeof useIsOT3>

const store: Store<State> = createStore(state => state, {})

Expand All @@ -36,10 +39,11 @@ const mockWifiNetwork: WifiNetwork = {
describe('useCanDisconnect', () => {
beforeEach(() => {
when(useWifiList).calledWith('otie').mockReturnValue([])
when(mockUseIsOT3).calledWith('otie').mockReturnValue(false)
})
afterEach(() => resetAllWhenMocks())

it('getCanDisconnect returns true if active network, robot >= 3.17', () => {
it('useCanDisconnect returns true if active network, robot >= 3.17', () => {
when(useWifiList)
.calledWith('otie')
.mockReturnValue([{ ...mockWifiNetwork, active: true }])
Expand All @@ -52,7 +56,7 @@ describe('useCanDisconnect', () => {
expect(result.current).toBe(true)
})

it('getCanDisconnect returns false if no list in state', () => {
it('useCanDisconnect returns false if no list in state', () => {
when(useWifiList).calledWith('otie').mockReturnValue([])

when(mockGetRobotApiVersionByName)
Expand All @@ -63,7 +67,7 @@ describe('useCanDisconnect', () => {
expect(result.current).toBe(false)
})

it('getCanDisconnect returns false if no active network', () => {
it('useCanDisconnect returns false if no active network', () => {
when(useWifiList)
.calledWith('otie')
.mockReturnValue([{ ...mockWifiNetwork, active: false }])
Expand All @@ -76,7 +80,7 @@ describe('useCanDisconnect', () => {
expect(result.current).toBe(false)
})

it('getCanDisconnect returns false if less than 3.17.0', () => {
it('useCanDisconnect returns false if less than 3.17.0', () => {
when(useWifiList)
.calledWith('otie')
.mockReturnValue([{ ...mockWifiNetwork, active: true }])
Expand All @@ -89,7 +93,22 @@ describe('useCanDisconnect', () => {
expect(result.current).toBe(false)
})

it('getCanDisconnect returns false if robot API version not found', () => {
it('useCanDisconnect returns true for an OT-3', () => {
when(useWifiList)
.calledWith('otie')
.mockReturnValue([{ ...mockWifiNetwork, active: true }])

when(mockGetRobotApiVersionByName)
.calledWith({} as any, 'otie')
.mockReturnValue('0.22.999-gamma.1')

when(mockUseIsOT3).calledWith('otie').mockReturnValue(true)

const { result } = renderHook(() => useCanDisconnect('otie'), { wrapper })
expect(result.current).toBe(true)
})

it('useCanDisconnect returns false if robot API version not found', () => {
when(useWifiList)
.calledWith('otie')
.mockReturnValue([{ ...mockWifiNetwork, active: true }])
Expand Down
4 changes: 3 additions & 1 deletion app/src/resources/networking/hooks/useCanDisconnect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useSelector } from 'react-redux'
import Semver from 'semver'
import { useIsOT3 } from '../../../organisms/Devices/hooks'
import { getRobotApiVersionByName } from '../../../redux/discovery'
import { useWifiList } from './useWifiList'

Expand All @@ -8,14 +9,15 @@ import type { State } from '../../../redux/types'
const API_MIN_DISCONNECT_VERSION = '3.17.0-alpha.0'

export const useCanDisconnect = (robotName: string): boolean => {
const isOT3 = useIsOT3(robotName)
const wifiList = useWifiList(robotName)
const apiVersion = useSelector((state: State) => {
return getRobotApiVersionByName(state, robotName)
})

const active = wifiList.some(nw => nw.active)
const supportsDisconnect = Semver.valid(apiVersion)
? Semver.gte(apiVersion as string, API_MIN_DISCONNECT_VERSION)
? isOT3 || Semver.gte(apiVersion as string, API_MIN_DISCONNECT_VERSION)
: false

return Boolean(active && supportsDisconnect)
Expand Down

0 comments on commit f945447

Please sign in to comment.