-
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.
refactor(app): refactor PreventRobotCaching section (#14335)
* refactor(app): refactor PreventRobotCaching section
- Loading branch information
Showing
5 changed files
with
141 additions
and
44 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
app/src/organisms/AdvancedSettings/PreventRobotCaching.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,60 @@ | ||
import * as React from 'react' | ||
import { Trans, useTranslation } from 'react-i18next' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
|
||
import { | ||
ALIGN_CENTER, | ||
Box, | ||
Flex, | ||
JUSTIFY_SPACE_BETWEEN, | ||
SPACING, | ||
TYPOGRAPHY, | ||
} from '@opentrons/components' | ||
|
||
import { StyledText } from '../../atoms/text' | ||
import { ToggleButton } from '../../atoms/buttons' | ||
import { getConfig, toggleConfigValue } from '../../redux/config' | ||
|
||
import type { Dispatch, State } from '../../redux/types' | ||
|
||
export function PreventRobotCaching(): JSX.Element { | ||
const { t } = useTranslation('app_settings') | ||
const displayUnavailRobots = useSelector((state: State) => { | ||
return getConfig(state)?.discovery.disableCache ?? false | ||
}) | ||
const dispatch = useDispatch<Dispatch>() | ||
|
||
return ( | ||
<Flex alignItems={ALIGN_CENTER} justifyContent={JUSTIFY_SPACE_BETWEEN}> | ||
<Box width="70%"> | ||
<StyledText | ||
css={TYPOGRAPHY.h3SemiBold} | ||
paddingBottom={SPACING.spacing8} | ||
id="AdvancedSettings_unavailableRobots" | ||
> | ||
{t('prevent_robot_caching')} | ||
</StyledText> | ||
<StyledText as="p"> | ||
<Trans | ||
t={t} | ||
i18nKey="prevent_robot_caching_description" | ||
components={{ | ||
strong: ( | ||
<StyledText | ||
as="span" | ||
fontWeight={TYPOGRAPHY.fontWeightSemiBold} | ||
/> | ||
), | ||
}} | ||
/> | ||
</StyledText> | ||
</Box> | ||
<ToggleButton | ||
label="display_unavailable_robots" | ||
toggledOn={!displayUnavailRobots} | ||
onClick={() => dispatch(toggleConfigValue('discovery.disableCache'))} | ||
id="AdvancedSettings_unavailableRobotsToggleButton" | ||
/> | ||
</Flex> | ||
) | ||
} |
63 changes: 63 additions & 0 deletions
63
app/src/organisms/AdvancedSettings/__tests__/PreventRobotCaching.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,63 @@ | ||
import * as React from 'react' | ||
import { when, resetAllWhenMocks } from 'jest-when' | ||
import { screen, fireEvent } from '@testing-library/react' | ||
|
||
import { renderWithProviders } from '@opentrons/components' | ||
import { i18n } from '../../../i18n' | ||
|
||
import { getConfig, toggleConfigValue } from '../../../redux/config' | ||
import { PreventRobotCaching } from '../PreventRobotCaching' | ||
|
||
import type { State } from '../../../redux/types' | ||
|
||
jest.mock('../../../redux/config') | ||
|
||
const mockGetConfig = getConfig as jest.MockedFunction<typeof getConfig> | ||
const mockToggleConfigValue = toggleConfigValue as jest.MockedFunction< | ||
typeof toggleConfigValue | ||
> | ||
|
||
const MOCK_STATE: State = { | ||
config: { | ||
discovery: { | ||
disableCache: false, | ||
}, | ||
}, | ||
} as any | ||
|
||
const render = () => { | ||
return renderWithProviders(<PreventRobotCaching />, { | ||
i18nInstance: i18n, | ||
}) | ||
} | ||
|
||
describe('PreventRobotCaching', () => { | ||
beforeEach(() => { | ||
when(mockGetConfig) | ||
.calledWith(MOCK_STATE) | ||
.mockReturnValue(MOCK_STATE.config) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
resetAllWhenMocks() | ||
}) | ||
|
||
it('should render text and toggle button', () => { | ||
render() | ||
screen.getByText('Prevent Robot Caching') | ||
screen.queryByText( | ||
'The app will immediately clear unavailable robots and will not remember unavailable robots while this is enabled. On networks with many robots, preventing caching may improve network performance at the expense of slower and less reliable robot discovery on app launch.' | ||
) | ||
screen.getByRole('switch', { name: 'display_unavailable_robots' }) | ||
}) | ||
|
||
it('should call mock toggleConfigValue when clicking the toggle button', () => { | ||
render() | ||
const toggleButton = screen.getByRole('switch', { | ||
name: 'display_unavailable_robots', | ||
}) | ||
fireEvent.click(toggleButton) | ||
expect(mockToggleConfigValue).toHaveBeenCalledWith('discovery.disableCache') | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './EnableDevTools' | ||
export * from './OT2AdvancedSettings' | ||
export * from './PreventRobotCaching' |
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
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