Skip to content

Commit bf3ddfe

Browse files
authored
RI-7068: replace EuiSwitch with SwitchInput (#4622)
* RI-7068: replace EuiSwitch for AutoRefresh * update AutoRefresh * SwitchInput for WorkbenchSettings * SwitchInput for ConsentOptions * SwitchInput for Monitor * SwitchInput for Graph * SwitchInput for MessageClaimPopover * SwitchInput for db analytics * remove euiSwitch styles * SwitchInput for redistimeseries-app * fix failing unit tests * refactor userEvent imports * update SwitchInput props signature * RI-7068: update SwitchInput props * update SwitchInput import path
1 parent 662ee58 commit bf3ddfe

File tree

39 files changed

+217
-463
lines changed

39 files changed

+217
-463
lines changed

redisinsight/ui/src/components/auto-refresh/AutoRefresh.spec.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import React from 'react'
22
import { instance, mock } from 'ts-mockito'
3-
import { fireEvent, screen, render, act } from 'uiSrc/utils/test-utils'
3+
import {
4+
userEvent,
5+
fireEvent,
6+
screen,
7+
render,
8+
act,
9+
} from 'uiSrc/utils/test-utils'
410
import { localStorageService } from 'uiSrc/services'
511
import AutoRefresh, { Props } from './AutoRefresh'
612
import { DEFAULT_REFRESH_RATE } from './utils'
@@ -72,7 +78,7 @@ describe('AutoRefresh', () => {
7278
render(<AutoRefresh {...instance(mockedProps)} displayText />)
7379

7480
fireEvent.click(screen.getByTestId('auto-refresh-config-btn'))
75-
fireEvent.click(screen.getByTestId('auto-refresh-switch'))
81+
await userEvent.click(screen.getByTestId('auto-refresh-switch'))
7682

7783
expect(screen.getByTestId('refresh-message-label')).toHaveTextContent(
7884
/Auto refresh:/i,
@@ -153,7 +159,7 @@ describe('AutoRefresh', () => {
153159
render(<AutoRefresh {...instance(mockedProps)} onRefresh={onRefresh} />)
154160

155161
fireEvent.click(screen.getByTestId('auto-refresh-config-btn'))
156-
fireEvent.click(screen.getByTestId('auto-refresh-switch'))
162+
await userEvent.click(screen.getByTestId('auto-refresh-switch'))
157163
fireEvent.click(screen.getByTestId('refresh-rate'))
158164

159165
fireEvent.change(screen.getByTestId(INLINE_ITEM_EDITOR), {
@@ -259,7 +265,7 @@ describe('AutoRefresh', () => {
259265
)
260266

261267
fireEvent.click(screen.getByTestId('auto-refresh-config-btn'))
262-
fireEvent.click(screen.getByTestId('auto-refresh-switch'))
268+
await userEvent.click(screen.getByTestId('auto-refresh-switch'))
263269
fireEvent.click(screen.getByTestId('refresh-rate'))
264270
fireEvent.change(screen.getByTestId(INLINE_ITEM_EDITOR), {
265271
target: { value: '1' },

redisinsight/ui/src/components/auto-refresh/AutoRefresh.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useState } from 'react'
2-
import { EuiIcon, EuiPopover, EuiSwitch, EuiToolTip } from '@elastic/eui'
2+
import { EuiIcon, EuiPopover, EuiToolTip } from '@elastic/eui'
33
import cx from 'classnames'
44
import { ChevronDownIcon, RefreshIcon } from 'uiSrc/components/base/icons'
55
import {
@@ -13,6 +13,7 @@ import { localStorageService } from 'uiSrc/services'
1313
import { BrowserStorageItem } from 'uiSrc/constants'
1414
import { IconButton } from 'uiSrc/components/base/forms/buttons'
1515
import { ColorText } from 'uiSrc/components/base/text'
16+
import { SwitchInput } from 'uiSrc/components/base/inputs'
1617
import {
1718
DEFAULT_REFRESH_RATE,
1819
DURATION_FIRST_REFRESH_TIME,
@@ -264,16 +265,13 @@ const AutoRefresh = ({
264265
/>
265266
}
266267
>
267-
<div className={styles.switch}>
268-
<EuiSwitch
269-
compressed
270-
label="Auto Refresh"
271-
checked={enableAutoRefresh}
272-
onChange={(e) => onChangeEnableAutoRefresh(e.target.checked)}
273-
className={styles.switchOption}
274-
data-testid={getDataTestid('auto-refresh-switch')}
275-
/>
276-
</div>
268+
<SwitchInput
269+
title="Auto Refresh"
270+
checked={enableAutoRefresh}
271+
onCheckedChange={onChangeEnableAutoRefresh}
272+
className={styles.switchOption}
273+
data-testid={getDataTestid('auto-refresh-switch')}
274+
/>
277275
<div className={styles.inputContainer}>
278276
<div className={styles.inputLabel}>Refresh rate:</div>
279277
{!editingRate && (

redisinsight/ui/src/components/auto-refresh/styles.module.scss

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,7 @@
102102
}
103103

104104
.switchOption {
105-
:global(.euiSwitch__label) {
106-
color: var(--euiTextSubduedColor) !important;
107-
font-size: 13px !important;
108-
}
109-
110-
:global(.euiSwitch__button) {
111-
width: 28px !important;
112-
margin-right: 4px;
113-
}
105+
padding-bottom: 16px;
114106
}
115107

116108
.enable {
@@ -149,4 +141,4 @@
149141
display: inline-block !important;
150142
}
151143
}
152-
}
144+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react'
2+
import userEvent from '@testing-library/user-event'
3+
import { render } from '@testing-library/react'
4+
5+
import SwitchInput from './SwitchInput'
6+
7+
describe('SwitchInput', () => {
8+
it('should render with default props', () => {
9+
const { container } = render(<SwitchInput title="On" />)
10+
11+
expect(container.firstChild).toHaveTextContent('On')
12+
})
13+
14+
it('should render with titleOff when provided', () => {
15+
const { container } = render(
16+
<SwitchInput title="On" titleOff="Off" checked={false} />,
17+
)
18+
19+
expect(container.firstChild).toHaveTextContent('Off')
20+
})
21+
22+
it('should fall back to title when titleOff is not provided', () => {
23+
const { container } = render(<SwitchInput title="On" checked={false} />)
24+
25+
expect(container.firstChild).toHaveTextContent('On')
26+
})
27+
28+
it('should call onCheckedChange when toggled', async () => {
29+
const onCheckedChange = jest.fn()
30+
const { getByRole, container } = render(
31+
<SwitchInput title="On" onCheckedChange={onCheckedChange} />,
32+
)
33+
34+
expect(container.firstChild).toHaveTextContent('On')
35+
36+
const switchElement = getByRole('switch')
37+
await userEvent.click(switchElement)
38+
39+
expect(onCheckedChange).toHaveBeenCalledWith(true)
40+
expect(container.firstChild).toHaveTextContent('On')
41+
})
42+
43+
it('should apply custom styles', () => {
44+
const { container } = render(
45+
<SwitchInput title="On" style={{ backgroundColor: 'red' }} />,
46+
)
47+
expect(container.firstChild).toHaveStyle('background-color: red')
48+
})
49+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react'
2+
3+
import { Switch } from '@redis-ui/components'
4+
5+
type SwitchInputProps = Omit<React.ComponentProps<typeof Switch>, 'titleOn'>
6+
7+
const SwitchInput = ({
8+
style,
9+
title,
10+
titleOff,
11+
...props
12+
}: SwitchInputProps) => (
13+
<Switch
14+
{...props}
15+
titleOn={title}
16+
titleOff={titleOff !== undefined ? titleOff : title}
17+
style={{
18+
alignItems: 'center',
19+
...style,
20+
}}
21+
/>
22+
)
23+
24+
export default SwitchInput
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { default as PasswordInput } from './PasswordInput'
22
export { default as SearchInput } from './SearchInput'
33
export { default as NumericInput } from './NumericInput'
4+
export { default as SwitchInput } from './SwitchInput'
45
export { default as TextArea } from './TextArea'

redisinsight/ui/src/components/consents-settings/ConsentOption/ConsentOption.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react'
2-
import { EuiSwitch } from '@elastic/eui'
32
import parse from 'html-react-parser'
43

54
import { FlexItem, Row } from 'uiSrc/components/base/layout/flex'
65
import { Spacer } from 'uiSrc/components/base/layout/spacer'
76
import { Text } from 'uiSrc/components/base/text'
7+
import { SwitchInput } from 'uiSrc/components/base/inputs'
88
import { IConsent } from '../ConsentsSettings'
99

1010
import styles from '../styles.module.scss'
@@ -42,14 +42,11 @@ const ConsentOption = (props: Props) => {
4242
)}
4343
<Row gap="m">
4444
<FlexItem>
45-
<EuiSwitch
46-
showLabel={false}
47-
label=""
45+
<SwitchInput
4846
checked={checked}
49-
onChange={(e) =>
50-
onChangeAgreement(e.target.checked, consent.agreementName)
47+
onCheckedChange={(checked) =>
48+
onChangeAgreement(checked, consent.agreementName)
5149
}
52-
className={styles.switchOption}
5350
data-testid={`switch-option-${consent.agreementName}`}
5451
disabled={consent?.disabled}
5552
/>

redisinsight/ui/src/components/consents-settings/ConsentsNotifications/ConsentsNotifications.spec.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import React from 'react'
22
import { cloneDeep } from 'lodash'
33
import {
44
render,
5+
userEvent,
56
screen,
6-
fireEvent,
77
mockedStore,
88
cleanup,
99
clearStoreActions,
10-
act,
1110
} from 'uiSrc/utils/test-utils'
1211
import { updateUserConfigSettings } from 'uiSrc/slices/user/user-settings'
1312
import ConsentsNotifications from './ConsentsNotifications'
@@ -85,11 +84,8 @@ describe('ConsentsNotifications', () => {
8584
it('option change should call "updateUserConfigSettingsAction"', async () => {
8685
render(<ConsentsNotifications />)
8786

88-
await act(() => {
89-
screen.getAllByTestId(/switch-option/).forEach(async (el) => {
90-
fireEvent.click(el)
91-
})
92-
})
87+
const elements = screen.getAllByTestId(/switch-option/)
88+
await Promise.all(elements.map((el) => userEvent.click(el)))
9389

9490
const expectedActions = [{}].fill(updateUserConfigSettings(), 0)
9591
expect(

redisinsight/ui/src/components/consents-settings/ConsentsPrivacy/ConsentsPrivacy.spec.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import React from 'react'
22
import { cloneDeep } from 'lodash'
33
import {
44
render,
5+
userEvent,
56
screen,
6-
fireEvent,
77
mockedStore,
88
cleanup,
99
clearStoreActions,
10-
act,
1110
} from 'uiSrc/utils/test-utils'
1211
import { updateUserConfigSettings } from 'uiSrc/slices/user/user-settings'
1312
import ConsentsPrivacy from './ConsentsPrivacy'
@@ -85,11 +84,8 @@ describe('ConsentsPrivacy', () => {
8584
it('option change should call "updateUserConfigSettingsAction"', async () => {
8685
render(<ConsentsPrivacy />)
8786

88-
await act(() => {
89-
screen.getAllByTestId(/switch-option/).forEach(async (el) => {
90-
fireEvent.click(el)
91-
})
92-
})
87+
const elements = screen.getAllByTestId(/switch-option/)
88+
await Promise.all(elements.map((el) => userEvent.click(el)))
9389

9490
const expectedActions = [updateUserConfigSettings()]
9591
expect(

redisinsight/ui/src/components/consents-settings/ConsentsSettings.spec.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react'
22
import { cloneDeep } from 'lodash'
33
import {
4+
userEvent,
45
render,
56
screen,
6-
fireEvent,
77
mockedStore,
88
cleanup,
99
} from 'uiSrc/utils/test-utils'
@@ -85,11 +85,10 @@ describe('ConsentsSettings', () => {
8585
expect(screen.getByTestId(BTN_SUBMIT)).toBeDisabled()
8686
})
8787

88-
it('should be able to submit with required options with true value', () => {
88+
it('should be able to submit with required options with true value', async () => {
8989
render(<ConsentsSettings />)
90-
screen.getAllByTestId(/switch-option/).forEach((el) => {
91-
fireEvent.click(el)
92-
})
90+
const elements = screen.getAllByTestId(/switch-option/)
91+
await Promise.all(elements.map((el) => userEvent.click(el)))
9392
expect(screen.getByTestId(BTN_SUBMIT)).not.toBeDisabled()
9493
})
9594
})

0 commit comments

Comments
 (0)