Skip to content

Commit

Permalink
refactor(app): refactor show labware offset snippets (#14367)
Browse files Browse the repository at this point in the history
* refactor(app): refactor ShowLabwareOffsetSnippets
  • Loading branch information
koji authored Jan 30, 2024
1 parent 404f66b commit 0c2fbf5
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 53 deletions.
60 changes: 60 additions & 0 deletions app/src/organisms/AdvancedSettings/ShowLabwareOffsetSnippets.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as React from 'react'
import { useTranslation } from 'react-i18next'
import { useSelector, useDispatch } from 'react-redux'

import {
Flex,
Box,
ALIGN_CENTER,
JUSTIFY_SPACE_BETWEEN,
TYPOGRAPHY,
SPACING,
} from '@opentrons/components'

import { StyledText } from '../../atoms/text'
import { ToggleButton } from '../../atoms/buttons'
import {
getIsLabwareOffsetCodeSnippetsOn,
updateConfigValue,
} from '../../redux/config'
import { Dispatch } from '../../redux/types'

export function ShowLabwareOffsetSnippets(): JSX.Element {
const { t } = useTranslation(['app_settings', 'shared'])
const dispatch = useDispatch<Dispatch>()
const isLabwareOffsetCodeSnippetsOn = useSelector(
getIsLabwareOffsetCodeSnippetsOn
)

const toggleLabwareOffsetData = (): void => {
dispatch(
updateConfigValue(
'labware.showLabwareOffsetCodeSnippets',
Boolean(!isLabwareOffsetCodeSnippetsOn)
)
)
}

return (
<Flex alignItems={ALIGN_CENTER} justifyContent={JUSTIFY_SPACE_BETWEEN}>
<Box width="70%">
<StyledText
css={TYPOGRAPHY.h3SemiBold}
paddingBottom={SPACING.spacing8}
id="AdvancedSettings_showLink"
>
{t('show_labware_offset_snippets')}
</StyledText>
<StyledText as="p">
{t('show_labware_offset_snippets_description')}
</StyledText>
</Box>
<ToggleButton
label="show_link_to_get_labware_offset_data"
toggledOn={isLabwareOffsetCodeSnippetsOn}
onClick={toggleLabwareOffsetData}
id="AdvancedSettings_showLinkToggleButton"
/>
</Flex>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as React from 'react'
import { fireEvent, screen } from '@testing-library/react'

import { i18n } from '../../../i18n'
import { renderWithProviders } from '@opentrons/components'
import {
getIsLabwareOffsetCodeSnippetsOn,
updateConfigValue,
} from '../../../redux/config'
import { ShowLabwareOffsetSnippets } from '../ShowLabwareOffsetSnippets'

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

const mockGetIsLabwareOffsetCodeSnippetsOn = getIsLabwareOffsetCodeSnippetsOn as jest.MockedFunction<
typeof getIsLabwareOffsetCodeSnippetsOn
>
const mockUpdateConfigValue = updateConfigValue as jest.MockedFunction<
typeof updateConfigValue
>

const render = () => {
return (
renderWithProviders(<ShowLabwareOffsetSnippets />),
{
i18nInstance: i18n,
}
)
}

describe('ShowLabwareOffsetSnippets', () => {
beforeEach(() => {
mockGetIsLabwareOffsetCodeSnippetsOn.mockReturnValue(true)
})
it('renders the display show link to get labware offset data section', () => {
render()
screen.getByText('Show Labware Offset data code snippets')
screen.getByText(
'Only for users who need to apply Labware Offset data outside of the Opentrons App. When enabled, code snippets for Jupyter Notebook and SSH are available during protocol setup.'
)
screen.getByRole('switch', { name: 'show_link_to_get_labware_offset_data' })
})

it('should call a mock function when clicking toggle button', () => {
render()
const toggleButton = screen.getByRole('switch', {
name: 'show_link_to_get_labware_offset_data',
})
fireEvent.click(toggleButton)
expect(mockUpdateConfigValue).toHaveBeenCalledWith(
'labware.showLabwareOffsetCodeSnippets',
false
)
})
})
1 change: 1 addition & 0 deletions app/src/organisms/AdvancedSettings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from './OT2AdvancedSettings'
export * from './OverridePathToPython'
export * from './PreventRobotCaching'
export * from './ShowHeaterShakerAttachmentModal'
export * from './ShowLabwareOffsetSnippets'
export * from './U2EInformation'
36 changes: 3 additions & 33 deletions app/src/pages/AppSettings/AdvancedSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ANALYTICS_CHANGE_CUSTOM_LABWARE_SOURCE_FOLDER,
} from '../../redux/analytics'
import { Divider } from '../../atoms/structure'
import { TertiaryButton, ToggleButton } from '../../atoms/buttons'
import { TertiaryButton } from '../../atoms/buttons'
import { StyledText } from '../../atoms/text'
import {
ClearUnavailableRobots,
Expand All @@ -35,6 +35,7 @@ import {
PreventRobotCaching,
ShowHeaterShakerAttachmentModal,
U2EInformation,
ShowLabwareOffsetSnippets,
} from '../../organisms/AdvancedSettings'

import type { Dispatch } from '../../redux/types'
Expand All @@ -47,19 +48,7 @@ export function AdvancedSettings(): JSX.Element {
Config.getUpdateChannelOptions
)
const labwarePath = useSelector(CustomLabware.getCustomLabwareDirectory)
const isLabwareOffsetCodeSnippetsOn = useSelector(
Config.getIsLabwareOffsetCodeSnippetsOn
)
const dispatch = useDispatch<Dispatch>()
const toggleLabwareOffsetData = (): void => {
dispatch(
Config.updateConfigValue(
'labware.showLabwareOffsetCodeSnippets',
Boolean(!isLabwareOffsetCodeSnippetsOn)
)
)
}

const handleChannel = (_: string, value: string): void => {
dispatch(Config.updateConfigValue('update.channel', value))
}
Expand Down Expand Up @@ -186,26 +175,7 @@ export function AdvancedSettings(): JSX.Element {
<Divider marginY={SPACING.spacing24} />
<ShowHeaterShakerAttachmentModal />
<Divider marginY={SPACING.spacing24} />
<Flex alignItems={ALIGN_CENTER} justifyContent={JUSTIFY_SPACE_BETWEEN}>
<Box width="70%">
<StyledText
css={TYPOGRAPHY.h3SemiBold}
paddingBottom={SPACING.spacing8}
id="AdvancedSettings_showLink"
>
{t('show_labware_offset_snippets')}
</StyledText>
<StyledText as="p">
{t('show_labware_offset_snippets_description')}
</StyledText>
</Box>
<ToggleButton
label="show_link_to_get_labware_offset_data"
toggledOn={isLabwareOffsetCodeSnippetsOn}
onClick={toggleLabwareOffsetData}
id="AdvancedSettings_showLinkToggleButton"
/>
</Flex>
<ShowLabwareOffsetSnippets />
<Divider marginY={SPACING.spacing24} />
<OverridePathToPython />
<Divider marginY={SPACING.spacing24} />
Expand Down
30 changes: 10 additions & 20 deletions app/src/pages/AppSettings/__test__/AdvancedSettings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
OT2AdvancedSettings,
OverridePathToPython,
PreventRobotCaching,
ShowLabwareOffsetSnippets,
U2EInformation,
ShowHeaterShakerAttachmentModal,
} from '../../../organisms/AdvancedSettings'
Expand Down Expand Up @@ -52,10 +53,6 @@ const getChannelOptions = Config.getUpdateChannelOptions as jest.MockedFunction<
typeof Config.getUpdateChannelOptions
>

const mockGetIsLabwareOffsetCodeSnippetsOn = Config.getIsLabwareOffsetCodeSnippetsOn as jest.MockedFunction<
typeof Config.getIsLabwareOffsetCodeSnippetsOn
>

const mockUseTrackEvent = useTrackEvent as jest.MockedFunction<
typeof useTrackEvent
>
Expand All @@ -72,6 +69,9 @@ const mockEnableDevTools = EnableDevTools as jest.MockedFunction<
const mockU2EInformation = U2EInformation as jest.MockedFunction<
typeof U2EInformation
>
const mockShowLabwareOffsetSnippets = ShowLabwareOffsetSnippets as jest.MockedFunction<
typeof ShowLabwareOffsetSnippets
>
const mockClearUnavailableRobots = ClearUnavailableRobots as jest.MockedFunction<
typeof ClearUnavailableRobots
>
Expand Down Expand Up @@ -101,6 +101,9 @@ describe('AdvancedSettings', () => {
mockOT2AdvancedSettings.mockReturnValue(<div>mock OT2AdvancedSettings</div>)
mockEnableDevTools.mockReturnValue(<div>mock EnableDevTools</div>)
mockU2EInformation.mockReturnValue(<div>mock U2EInformation</div>)
mockShowLabwareOffsetSnippets.mockReturnValue(
<div>mock ShowLabwareOffsetSnippets</div>
)
mockClearUnavailableRobots.mockReturnValue(
<div>mock ClearUnavailableRobots</div>
)
Expand Down Expand Up @@ -166,22 +169,9 @@ describe('AdvancedSettings', () => {
expect(screen.getByText('mock U2EInformation'))
})

it('renders the display show link to get labware offset data section', () => {
const [{ getByText, getByRole }] = render()
getByText('Show Labware Offset data code snippets')
getByText(
'Only for users who need to apply Labware Offset data outside of the Opentrons App. When enabled, code snippets for Jupyter Notebook and SSH are available during protocol setup.'
)
getByRole('switch', { name: 'show_link_to_get_labware_offset_data' })
})

it('renders the toggle button on when show link to labware offset data setting is true', () => {
mockGetIsLabwareOffsetCodeSnippetsOn.mockReturnValue(true)
const [{ getByRole }] = render()
const toggleButton = getByRole('switch', {
name: 'show_link_to_get_labware_offset_data',
})
expect(toggleButton.getAttribute('aria-checked')).toBe('true')
it('should render mock show link to get labware offset data section', () => {
render()
screen.getByText('mock ShowLabwareOffsetSnippets')
})

it('should render mock ShowHeaterShakerAttachmentModal section', () => {
Expand Down

0 comments on commit 0c2fbf5

Please sign in to comment.