@registrucentras/rc-ses-react-components 1.8.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/icons/PencilSimpleLineIcon.d.ts +1 -1
- package/dist/components/common/SegmentedControl/components/SegmentButton/index.d.ts +19 -0
- package/dist/components/common/SegmentedControl/index.d.ts +14 -0
- package/dist/components/form/inputs/SearchInput/index.d.ts +10 -0
- package/dist/components/loaders/FullPageLoader/index.d.ts +9 -0
- package/dist/components/loaders/Loader/index.d.ts +26 -0
- package/dist/library/index.d.ts +6 -2
- package/dist/library/types.d.ts +6 -2
- package/dist/registrucentras-rc-ses-react-components.cjs +10 -10
- package/dist/registrucentras-rc-ses-react-components.js +2569 -2277
- package/dist/stories/components/display/SegmentedControl.stories.d.ts +202 -0
- package/dist/stories/components/loaders/FullPageLoader.stories.d.ts +37 -0
- package/dist/stories/components/loaders/Loader.stories.d.ts +36 -0
- package/dist/stories/components/{spinner → loaders}/LoadingSpinner.stories.d.ts +4 -4
- package/package.json +1 -1
- package/src/assets/icons/PencilSimpleLineIcon.tsx +8 -2
- package/src/components/common/Button/index.tsx +1 -3
- package/src/components/common/SegmentedControl/components/SegmentButton/index.test.tsx +85 -0
- package/src/components/common/SegmentedControl/components/SegmentButton/index.tsx +110 -0
- package/src/components/common/SegmentedControl/index.test.tsx +164 -0
- package/src/components/common/SegmentedControl/index.tsx +188 -0
- package/src/components/common/SelectableCardList/components/SelectableCardListItem.test.tsx +74 -1
- package/src/components/common/SelectableCardList/components/SelectableCardListItem.tsx +16 -1
- package/src/components/common/Snackbar/index.test.tsx +10 -4
- package/src/components/form/inputs/CheckboxFormControl.test.tsx +11 -7
- package/src/components/form/inputs/SearchInput/index.test.tsx +68 -2
- package/src/components/form/inputs/SearchInput/index.tsx +24 -13
- package/src/components/loaders/FullPageLoader/index.test.tsx +80 -0
- package/src/components/loaders/FullPageLoader/index.tsx +68 -0
- package/src/components/loaders/Loader/index.test.tsx +58 -0
- package/src/components/loaders/Loader/index.tsx +65 -0
- package/src/components/{LoadingSpinner/LoadingSpinner.test.tsx → loaders/LoadingSpinner/index.test.tsx} +1 -1
- package/src/components/{LoadingSpinner/LoadingSpinner.tsx → loaders/LoadingSpinner/index.tsx} +1 -1
- package/src/components/overlays/Dialog/index.test.tsx +1 -1
- package/src/i18n/namespaces/common/en.json +7 -0
- package/src/i18n/namespaces/common/lt.json +7 -0
- package/src/library/index.ts +7 -1
- package/src/library/types.ts +21 -4
- package/src/setupTests.ts +13 -0
- package/src/stories/components/display/SegmentedControl.stories.tsx +311 -0
- package/src/stories/components/loaders/FullPageLoader.stories.tsx +126 -0
- package/src/stories/components/loaders/Loader.stories.tsx +76 -0
- package/src/stories/components/{spinner → loaders}/LoadingSpinner.stories.tsx +2 -2
- /package/dist/components/{LoadingSpinner/LoadingSpinner.d.ts → loaders/LoadingSpinner/index.d.ts} +0 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
2
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import RcSesSegmentedControl, { type RcSesSegmentOption } from './index'
|
|
5
|
+
|
|
6
|
+
describe('RcSesSegmentedControl', () => {
|
|
7
|
+
const options: RcSesSegmentOption[] = [
|
|
8
|
+
{ id: 'option-1', label: 'Option 1', disabled: false },
|
|
9
|
+
{ id: 'option-2', label: 'Option 2', disabled: false },
|
|
10
|
+
{ id: 'option-3', label: 'Option 3', disabled: false },
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
const defaultProps = {
|
|
14
|
+
options,
|
|
15
|
+
value: 'option-1',
|
|
16
|
+
onChange: vi.fn(),
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test('renders all options', () => {
|
|
20
|
+
render(<RcSesSegmentedControl {...defaultProps} />)
|
|
21
|
+
|
|
22
|
+
expect(screen.getByRole('radio', { name: 'Option 1' })).toBeInTheDocument()
|
|
23
|
+
expect(screen.getByRole('radio', { name: 'Option 2' })).toBeInTheDocument()
|
|
24
|
+
expect(screen.getByRole('radio', { name: 'Option 3' })).toBeInTheDocument()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('has correct container role (radiogroup by default)', () => {
|
|
28
|
+
render(<RcSesSegmentedControl {...defaultProps} />)
|
|
29
|
+
|
|
30
|
+
expect(screen.getByRole('radiogroup')).toBeInTheDocument()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('marks selected option with aria-checked=true', () => {
|
|
34
|
+
render(<RcSesSegmentedControl {...defaultProps} value='option-2' />)
|
|
35
|
+
|
|
36
|
+
expect(screen.getByRole('radio', { name: 'Option 2' })).toHaveAttribute(
|
|
37
|
+
'aria-checked',
|
|
38
|
+
'true',
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('calls onChange when option clicked', () => {
|
|
43
|
+
const onChange = vi.fn()
|
|
44
|
+
render(<RcSesSegmentedControl {...defaultProps} onChange={onChange} />)
|
|
45
|
+
|
|
46
|
+
fireEvent.click(screen.getByRole('radio', { name: 'Option 2' }))
|
|
47
|
+
expect(onChange).toHaveBeenCalledWith('option-2')
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('navigates with ArrowRight key', () => {
|
|
51
|
+
const onChange = vi.fn()
|
|
52
|
+
render(<RcSesSegmentedControl {...defaultProps} onChange={onChange} />)
|
|
53
|
+
|
|
54
|
+
const option1 = screen.getByRole('radio', { name: 'Option 1' })
|
|
55
|
+
fireEvent.keyDown(option1, { key: 'ArrowRight' })
|
|
56
|
+
|
|
57
|
+
expect(onChange).toHaveBeenCalledWith('option-2')
|
|
58
|
+
expect(screen.getByRole('radio', { name: 'Option 2' })).toHaveFocus()
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('jumps to first/last option with Home/End keys', () => {
|
|
62
|
+
const onChange = vi.fn()
|
|
63
|
+
render(
|
|
64
|
+
<RcSesSegmentedControl {...defaultProps} onChange={onChange} value='option-2' />,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const option2 = screen.getByRole('radio', { name: 'Option 2' })
|
|
68
|
+
fireEvent.keyDown(option2, { key: 'Home' })
|
|
69
|
+
|
|
70
|
+
expect(onChange).toHaveBeenCalledWith('option-1')
|
|
71
|
+
expect(screen.getByRole('radio', { name: 'Option 1' })).toHaveFocus()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('selects option with Enter key', () => {
|
|
75
|
+
const onChange = vi.fn()
|
|
76
|
+
render(
|
|
77
|
+
<RcSesSegmentedControl {...defaultProps} onChange={onChange} value='option-1' />,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
const option2 = screen.getByRole('radio', { name: 'Option 2' })
|
|
81
|
+
fireEvent.focus(option2)
|
|
82
|
+
fireEvent.keyDown(option2, { key: 'Enter' })
|
|
83
|
+
|
|
84
|
+
expect(onChange).toHaveBeenCalledWith('option-2')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('selects option with Space key', () => {
|
|
88
|
+
const onChange = vi.fn()
|
|
89
|
+
render(
|
|
90
|
+
<RcSesSegmentedControl {...defaultProps} onChange={onChange} value='option-1' />,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
const option2 = screen.getByRole('radio', { name: 'Option 2' })
|
|
94
|
+
fireEvent.keyDown(option2, { key: ' ' })
|
|
95
|
+
|
|
96
|
+
expect(onChange).toHaveBeenCalledWith('option-2')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('skips disabled options in navigation', () => {
|
|
100
|
+
const onChange = vi.fn()
|
|
101
|
+
const optionsWithDisabled: RcSesSegmentOption[] = [
|
|
102
|
+
{ id: 'option-1', label: 'Option 1', disabled: false },
|
|
103
|
+
{ id: 'option-2', label: 'Option 2', disabled: true },
|
|
104
|
+
{ id: 'option-3', label: 'Option 3', disabled: false },
|
|
105
|
+
]
|
|
106
|
+
|
|
107
|
+
render(
|
|
108
|
+
<RcSesSegmentedControl
|
|
109
|
+
options={optionsWithDisabled}
|
|
110
|
+
value='option-1'
|
|
111
|
+
onChange={onChange}
|
|
112
|
+
/>,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
const option1 = screen.getByRole('radio', { name: 'Option 1' })
|
|
116
|
+
fireEvent.keyDown(option1, { key: 'ArrowRight' })
|
|
117
|
+
|
|
118
|
+
expect(onChange).toHaveBeenCalledWith('option-3')
|
|
119
|
+
expect(screen.getByRole('radio', { name: 'Option 3' })).toHaveFocus()
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('disables disabled options', () => {
|
|
123
|
+
const optionsWithDisabled: RcSesSegmentOption[] = [
|
|
124
|
+
{ id: 'option-1', label: 'Option 1', disabled: false },
|
|
125
|
+
{ id: 'option-2', label: 'Option 2', disabled: true },
|
|
126
|
+
]
|
|
127
|
+
|
|
128
|
+
render(
|
|
129
|
+
<RcSesSegmentedControl
|
|
130
|
+
options={optionsWithDisabled}
|
|
131
|
+
value='option-1'
|
|
132
|
+
onChange={vi.fn()}
|
|
133
|
+
/>,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
expect(screen.getByRole('radio', { name: 'Option 2' })).toBeDisabled()
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
test('supports tab role with tab buttons', () => {
|
|
140
|
+
render(<RcSesSegmentedControl {...defaultProps} role='tablist' />)
|
|
141
|
+
|
|
142
|
+
expect(screen.getByRole('tab', { name: 'Option 1' })).toBeInTheDocument()
|
|
143
|
+
expect(screen.getByRole('tab', { name: 'Option 2' })).toBeInTheDocument()
|
|
144
|
+
expect(screen.getByRole('tab', { name: 'Option 3' })).toBeInTheDocument()
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('updates selected option when value prop changes', () => {
|
|
148
|
+
const { rerender } = render(
|
|
149
|
+
<RcSesSegmentedControl {...defaultProps} value='option-1' />,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
expect(screen.getByRole('radio', { name: 'Option 1' })).toHaveAttribute(
|
|
153
|
+
'aria-checked',
|
|
154
|
+
'true',
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
rerender(<RcSesSegmentedControl {...defaultProps} value='option-2' />)
|
|
158
|
+
|
|
159
|
+
expect(screen.getByRole('radio', { name: 'Option 2' })).toHaveAttribute(
|
|
160
|
+
'aria-checked',
|
|
161
|
+
'true',
|
|
162
|
+
)
|
|
163
|
+
})
|
|
164
|
+
})
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import React, { useCallback, useImperativeHandle, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import { useTranslation } from 'react-i18next'
|
|
3
|
+
|
|
4
|
+
import { grey } from '@/theme/palette'
|
|
5
|
+
|
|
6
|
+
import RcSesSegmentButton, { type RcSesSegmentOption } from './components/SegmentButton'
|
|
7
|
+
|
|
8
|
+
export type { RcSesSegmentOption }
|
|
9
|
+
|
|
10
|
+
export interface RcSesSegmentedControlProps {
|
|
11
|
+
options: RcSesSegmentOption[]
|
|
12
|
+
value: string
|
|
13
|
+
onChange: (value: string) => void
|
|
14
|
+
size?: 'small' | 'regular'
|
|
15
|
+
role?: 'radiogroup' | 'tablist'
|
|
16
|
+
disableBg?: boolean
|
|
17
|
+
ariaLabel?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const RcSesSegmentedControl = React.forwardRef<
|
|
21
|
+
HTMLDivElement,
|
|
22
|
+
RcSesSegmentedControlProps
|
|
23
|
+
>(
|
|
24
|
+
(
|
|
25
|
+
{
|
|
26
|
+
options,
|
|
27
|
+
value,
|
|
28
|
+
onChange,
|
|
29
|
+
size = 'regular',
|
|
30
|
+
role = 'radiogroup',
|
|
31
|
+
disableBg = false,
|
|
32
|
+
ariaLabel,
|
|
33
|
+
},
|
|
34
|
+
ref,
|
|
35
|
+
) => {
|
|
36
|
+
const { t } = useTranslation('common')
|
|
37
|
+
const [focusIndex, setFocusIndex] = useState(() => {
|
|
38
|
+
const selectedIndex = options.findIndex((opt) => opt.id === value && !opt.disabled)
|
|
39
|
+
if (selectedIndex !== -1) return selectedIndex
|
|
40
|
+
return Math.max(
|
|
41
|
+
0,
|
|
42
|
+
options.findIndex((opt) => !opt.disabled),
|
|
43
|
+
)
|
|
44
|
+
})
|
|
45
|
+
const containerRef = useRef<HTMLDivElement>(null)
|
|
46
|
+
const buttonRefs = useRef<(HTMLButtonElement | null)[]>([])
|
|
47
|
+
|
|
48
|
+
useImperativeHandle(ref, () => containerRef.current as HTMLDivElement)
|
|
49
|
+
|
|
50
|
+
React.useEffect(() => {
|
|
51
|
+
const selectedIndex = options.findIndex((opt) => opt.id === value && !opt.disabled)
|
|
52
|
+
if (selectedIndex !== -1) setFocusIndex(selectedIndex)
|
|
53
|
+
}, [value, options])
|
|
54
|
+
|
|
55
|
+
const getEnabledIndices = useCallback(
|
|
56
|
+
() => options.map((opt, i) => (!opt.disabled ? i : -1)).filter((i) => i !== -1),
|
|
57
|
+
[options],
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const focusSegment = useCallback((index: number) => {
|
|
61
|
+
setFocusIndex(index)
|
|
62
|
+
buttonRefs.current[index]?.focus()
|
|
63
|
+
}, [])
|
|
64
|
+
|
|
65
|
+
const handleKeyDown = useCallback(
|
|
66
|
+
(e: React.KeyboardEvent<HTMLButtonElement>, index: number) => {
|
|
67
|
+
const enabledIndices = getEnabledIndices()
|
|
68
|
+
const currentPos = enabledIndices.indexOf(index)
|
|
69
|
+
|
|
70
|
+
switch (e.key) {
|
|
71
|
+
case 'ArrowLeft':
|
|
72
|
+
case 'ArrowUp': {
|
|
73
|
+
e.preventDefault()
|
|
74
|
+
const prevIndex =
|
|
75
|
+
currentPos > 0
|
|
76
|
+
? enabledIndices[currentPos - 1]
|
|
77
|
+
: enabledIndices[enabledIndices.length - 1]
|
|
78
|
+
focusSegment(prevIndex)
|
|
79
|
+
onChange(options[prevIndex].id)
|
|
80
|
+
break
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
case 'ArrowRight':
|
|
84
|
+
case 'ArrowDown': {
|
|
85
|
+
e.preventDefault()
|
|
86
|
+
const nextIndex =
|
|
87
|
+
currentPos < enabledIndices.length - 1
|
|
88
|
+
? enabledIndices[currentPos + 1]
|
|
89
|
+
: enabledIndices[0]
|
|
90
|
+
focusSegment(nextIndex)
|
|
91
|
+
onChange(options[nextIndex].id)
|
|
92
|
+
break
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
case 'Home': {
|
|
96
|
+
e.preventDefault()
|
|
97
|
+
const firstIndex = enabledIndices[0]
|
|
98
|
+
focusSegment(firstIndex)
|
|
99
|
+
onChange(options[firstIndex].id)
|
|
100
|
+
break
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
case 'End': {
|
|
104
|
+
e.preventDefault()
|
|
105
|
+
const lastIndex = enabledIndices[enabledIndices.length - 1]
|
|
106
|
+
focusSegment(lastIndex)
|
|
107
|
+
onChange(options[lastIndex].id)
|
|
108
|
+
break
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
case 'Enter':
|
|
112
|
+
case ' ':
|
|
113
|
+
e.preventDefault()
|
|
114
|
+
onChange(options[index].id)
|
|
115
|
+
break
|
|
116
|
+
|
|
117
|
+
default:
|
|
118
|
+
break
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
[focusSegment, getEnabledIndices, onChange, options],
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
const handleSegmentClick = useCallback(
|
|
125
|
+
(optionId: string, index: number) => {
|
|
126
|
+
onChange(optionId)
|
|
127
|
+
setFocusIndex(index)
|
|
128
|
+
},
|
|
129
|
+
[onChange],
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
const handleFocus = useCallback((index: number) => {
|
|
133
|
+
setFocusIndex(index)
|
|
134
|
+
}, [])
|
|
135
|
+
|
|
136
|
+
const isSelected = useCallback((optionId: string) => value === optionId, [value])
|
|
137
|
+
const ariaRole = role === 'tablist' ? 'tab' : 'radio'
|
|
138
|
+
const defaultAriaLabel =
|
|
139
|
+
ariaLabel ||
|
|
140
|
+
t(
|
|
141
|
+
role === 'tablist'
|
|
142
|
+
? 'components.SegmentedControl.tabs'
|
|
143
|
+
: 'components.SegmentedControl.options',
|
|
144
|
+
)
|
|
145
|
+
const finalAriaLabel = defaultAriaLabel
|
|
146
|
+
|
|
147
|
+
const containerStyles = useMemo<React.CSSProperties>(
|
|
148
|
+
() => ({
|
|
149
|
+
display: 'flex',
|
|
150
|
+
flexWrap: 'wrap',
|
|
151
|
+
gap: '2px 8px',
|
|
152
|
+
padding: '4px',
|
|
153
|
+
backgroundColor: disableBg ? 'transparent' : grey['50'],
|
|
154
|
+
width: 'fit-content',
|
|
155
|
+
borderRadius: '8px',
|
|
156
|
+
}),
|
|
157
|
+
[disableBg],
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<div
|
|
162
|
+
ref={containerRef}
|
|
163
|
+
role={role}
|
|
164
|
+
aria-label={finalAriaLabel}
|
|
165
|
+
style={containerStyles}
|
|
166
|
+
>
|
|
167
|
+
{options.map((option, index) => (
|
|
168
|
+
<RcSesSegmentButton
|
|
169
|
+
key={option.id}
|
|
170
|
+
ref={(el) => {
|
|
171
|
+
buttonRefs.current[index] = el
|
|
172
|
+
}}
|
|
173
|
+
option={option}
|
|
174
|
+
isSelected={isSelected(option.id)}
|
|
175
|
+
isFocused={focusIndex === index && !option.disabled}
|
|
176
|
+
size={size}
|
|
177
|
+
ariaRole={ariaRole}
|
|
178
|
+
onClick={(optionId) => handleSegmentClick(optionId, index)}
|
|
179
|
+
onKeyDown={(e) => handleKeyDown(e, index)}
|
|
180
|
+
onFocus={() => handleFocus(index)}
|
|
181
|
+
/>
|
|
182
|
+
))}
|
|
183
|
+
</div>
|
|
184
|
+
)
|
|
185
|
+
},
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
export default RcSesSegmentedControl
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
-
import { render, screen } from '@testing-library/react'
|
|
2
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
3
3
|
import { type ReactElement } from 'react'
|
|
4
4
|
import { describe, expect, test, vi } from 'vitest'
|
|
5
5
|
|
|
@@ -94,4 +94,77 @@ describe('SelectableCardListItem', () => {
|
|
|
94
94
|
expect(screen.getByText('List entry A')).toBeInTheDocument()
|
|
95
95
|
expect(screen.getByText('List entry B')).toBeInTheDocument()
|
|
96
96
|
})
|
|
97
|
+
|
|
98
|
+
test('calls onSelect once when Enter key is pressed on radio', () => {
|
|
99
|
+
const onSelect = vi.fn()
|
|
100
|
+
renderItem(
|
|
101
|
+
<SelectableCardListItem
|
|
102
|
+
title='Test'
|
|
103
|
+
selected={false}
|
|
104
|
+
onSelect={onSelect}
|
|
105
|
+
listItems={[]}
|
|
106
|
+
/>,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
const radio = screen.getByRole('radio')
|
|
110
|
+
radio.focus()
|
|
111
|
+
fireEvent.keyDown(radio, { key: 'Enter', code: 'Enter' })
|
|
112
|
+
|
|
113
|
+
expect(onSelect).toHaveBeenCalledTimes(1)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('calls onSelect once when radio is clicked (no double-call)', () => {
|
|
117
|
+
const onSelect = vi.fn()
|
|
118
|
+
renderItem(
|
|
119
|
+
<SelectableCardListItem
|
|
120
|
+
title='Test'
|
|
121
|
+
selected={false}
|
|
122
|
+
onSelect={onSelect}
|
|
123
|
+
listItems={[]}
|
|
124
|
+
/>,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
const radio = screen.getByRole('radio')
|
|
128
|
+
fireEvent.click(radio)
|
|
129
|
+
|
|
130
|
+
expect(onSelect).toHaveBeenCalledTimes(1)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('calls onSelect once when card is clicked outside radio', () => {
|
|
134
|
+
const onSelect = vi.fn()
|
|
135
|
+
renderItem(
|
|
136
|
+
<SelectableCardListItem
|
|
137
|
+
title='Test title'
|
|
138
|
+
selected={false}
|
|
139
|
+
onSelect={onSelect}
|
|
140
|
+
listItems={[]}
|
|
141
|
+
/>,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
// click on the title area (outside the radio)
|
|
145
|
+
const titleElement = screen.getByRole('heading', { name: 'Test title' })
|
|
146
|
+
fireEvent.click(titleElement)
|
|
147
|
+
|
|
148
|
+
expect(onSelect).toHaveBeenCalledTimes(1)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
test('loading card is not selectable', () => {
|
|
152
|
+
const onSelect = vi.fn()
|
|
153
|
+
const { container } = renderItem(
|
|
154
|
+
<SelectableCardListItem
|
|
155
|
+
title='Loading'
|
|
156
|
+
selected={false}
|
|
157
|
+
onSelect={onSelect}
|
|
158
|
+
isLoading
|
|
159
|
+
listItems={[]}
|
|
160
|
+
/>,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
// no radio is rendered while loading, so there is no keyboard tab stop
|
|
164
|
+
expect(screen.queryByRole('radio')).toBeNull()
|
|
165
|
+
|
|
166
|
+
// mouse clicks are blocked by pointer-events on the card root
|
|
167
|
+
const card = container.querySelector('.MuiCard-root')
|
|
168
|
+
expect(card).toHaveStyle('pointer-events: none')
|
|
169
|
+
})
|
|
97
170
|
})
|
|
@@ -33,6 +33,18 @@ const SelectableCardListItem = ({
|
|
|
33
33
|
<Radio
|
|
34
34
|
aria-label={`Select ${title}`}
|
|
35
35
|
checked={selected}
|
|
36
|
+
onChange={(e) => {
|
|
37
|
+
e.stopPropagation()
|
|
38
|
+
onSelect()
|
|
39
|
+
}}
|
|
40
|
+
onClick={(e) => e.stopPropagation()}
|
|
41
|
+
onKeyDown={(e) => {
|
|
42
|
+
if (e.key === 'Enter') {
|
|
43
|
+
e.preventDefault()
|
|
44
|
+
e.stopPropagation()
|
|
45
|
+
onSelect()
|
|
46
|
+
}
|
|
47
|
+
}}
|
|
36
48
|
sx={{
|
|
37
49
|
p: 0,
|
|
38
50
|
height: '1.25rem',
|
|
@@ -78,8 +90,11 @@ const SelectableCardListItem = ({
|
|
|
78
90
|
borderRadius: '0.75rem',
|
|
79
91
|
padding: { xs: '1rem' },
|
|
80
92
|
borderColor: selected ? palette.primary.main : palette.grey[300],
|
|
81
|
-
transition: '
|
|
93
|
+
transition: 'border-color 0.2s ease',
|
|
82
94
|
gap: { xs: '0.5rem', md: '0.25rem' },
|
|
95
|
+
'&:has(input:focus-visible)': {
|
|
96
|
+
borderColor: palette.grey[900],
|
|
97
|
+
},
|
|
83
98
|
}}
|
|
84
99
|
title={titleTemplate}
|
|
85
100
|
slotProps={{
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
-
import { fireEvent, render, screen } from '@testing-library/react'
|
|
2
|
+
import { act, fireEvent, render, screen } from '@testing-library/react'
|
|
3
3
|
import { ReactNode } from 'react'
|
|
4
4
|
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
|
|
5
5
|
|
|
@@ -20,7 +20,9 @@ describe('RcSesSnackbar', () => {
|
|
|
20
20
|
})
|
|
21
21
|
|
|
22
22
|
afterEach(() => {
|
|
23
|
-
|
|
23
|
+
act(() => {
|
|
24
|
+
vi.runOnlyPendingTimers()
|
|
25
|
+
})
|
|
24
26
|
vi.useRealTimers()
|
|
25
27
|
})
|
|
26
28
|
|
|
@@ -67,7 +69,9 @@ describe('RcSesSnackbar', () => {
|
|
|
67
69
|
const statusElement = screen.getByText('Test')
|
|
68
70
|
expect(statusElement).toBeInTheDocument()
|
|
69
71
|
|
|
70
|
-
|
|
72
|
+
act(() => {
|
|
73
|
+
vi.advanceTimersByTime(1000)
|
|
74
|
+
})
|
|
71
75
|
|
|
72
76
|
expect(handleClose).toHaveBeenCalled()
|
|
73
77
|
})
|
|
@@ -78,7 +82,9 @@ describe('RcSesSnackbar', () => {
|
|
|
78
82
|
<RcSesSnackbar {...DEFAULT_PROPS} onClose={handleClose} persist duration={1000} />,
|
|
79
83
|
)
|
|
80
84
|
|
|
81
|
-
|
|
85
|
+
act(() => {
|
|
86
|
+
vi.advanceTimersByTime(2000)
|
|
87
|
+
})
|
|
82
88
|
|
|
83
89
|
expect(handleClose).not.toHaveBeenCalled()
|
|
84
90
|
})
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
-
import { render } from '@testing-library/react'
|
|
2
|
+
import { fireEvent, render, waitFor } from '@testing-library/react'
|
|
3
3
|
import { useForm } from 'react-hook-form'
|
|
4
4
|
import { describe, expect, test, vi } from 'vitest'
|
|
5
5
|
|
|
@@ -40,7 +40,7 @@ describe('RcSesCheckboxFormControl - Indeterminate State', () => {
|
|
|
40
40
|
expect(checkbox.checked).toBe(false)
|
|
41
41
|
})
|
|
42
42
|
|
|
43
|
-
test('should call onChildValuesChange with [true, true, true] when clicking parent in indeterminate state', () => {
|
|
43
|
+
test('should call onChildValuesChange with [true, true, true] when clicking parent in indeterminate state', async () => {
|
|
44
44
|
const onChildValuesChange = vi.fn()
|
|
45
45
|
const { container } = render(
|
|
46
46
|
<TestWrapper
|
|
@@ -49,11 +49,13 @@ describe('RcSesCheckboxFormControl - Indeterminate State', () => {
|
|
|
49
49
|
/>,
|
|
50
50
|
)
|
|
51
51
|
const checkbox = container.querySelector('input#checkbox') as HTMLInputElement
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
fireEvent.click(checkbox)
|
|
53
|
+
await waitFor(() => {
|
|
54
|
+
expect(onChildValuesChange).toHaveBeenCalledWith([true, true, true])
|
|
55
|
+
})
|
|
54
56
|
})
|
|
55
57
|
|
|
56
|
-
test('should call onChildValuesChange with [false, false, false] when clicking checked parent', () => {
|
|
58
|
+
test('should call onChildValuesChange with [false, false, false] when clicking checked parent', async () => {
|
|
57
59
|
const onChildValuesChange = vi.fn()
|
|
58
60
|
const { container } = render(
|
|
59
61
|
<TestWrapper
|
|
@@ -62,7 +64,9 @@ describe('RcSesCheckboxFormControl - Indeterminate State', () => {
|
|
|
62
64
|
/>,
|
|
63
65
|
)
|
|
64
66
|
const checkbox = container.querySelector('input#checkbox') as HTMLInputElement
|
|
65
|
-
|
|
66
|
-
|
|
67
|
+
fireEvent.click(checkbox)
|
|
68
|
+
await waitFor(() => {
|
|
69
|
+
expect(onChildValuesChange).toHaveBeenCalledWith([false, false, false])
|
|
70
|
+
})
|
|
67
71
|
})
|
|
68
72
|
})
|
|
@@ -135,7 +135,7 @@ describe('RcSesSearchInput', () => {
|
|
|
135
135
|
expect(onSearch).not.toHaveBeenCalled()
|
|
136
136
|
})
|
|
137
137
|
|
|
138
|
-
test('
|
|
138
|
+
test('calls onSearch on Enter when search button is enabled', () => {
|
|
139
139
|
const onSearch = vi.fn()
|
|
140
140
|
render(<TestWrapper onSearch={onSearch} defaultValue='hello' />)
|
|
141
141
|
|
|
@@ -143,7 +143,7 @@ describe('RcSesSearchInput', () => {
|
|
|
143
143
|
fireEvent.click(input)
|
|
144
144
|
fireEvent.keyDown(input, { key: 'Enter', code: 'Enter', charCode: 13 })
|
|
145
145
|
|
|
146
|
-
expect(onSearch).
|
|
146
|
+
expect(onSearch).toHaveBeenCalledWith('hello')
|
|
147
147
|
})
|
|
148
148
|
|
|
149
149
|
test('does not call onSearch when slotProps.field.onKeyDown calls preventDefault', () => {
|
|
@@ -200,4 +200,70 @@ describe('RcSesSearchInput', () => {
|
|
|
200
200
|
expect(fieldOnChange).toHaveBeenCalled()
|
|
201
201
|
})
|
|
202
202
|
})
|
|
203
|
+
|
|
204
|
+
describe('onSearch callback', () => {
|
|
205
|
+
test('calls onSearch when Enter is pressed', () => {
|
|
206
|
+
const onSearch = vi.fn()
|
|
207
|
+
render(
|
|
208
|
+
<TestWrapper onSearch={onSearch} defaultValue='query' showSearchButton={false} />,
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
const input = screen.getByRole('textbox')
|
|
212
|
+
fireEvent.keyDown(input, { key: 'Enter', code: 'Enter', charCode: 13 })
|
|
213
|
+
|
|
214
|
+
expect(onSearch).toHaveBeenCalledWith('query')
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
test('calls onSearch when search button is clicked', () => {
|
|
218
|
+
const onSearch = vi.fn()
|
|
219
|
+
render(<TestWrapper onSearch={onSearch} defaultValue='query' />)
|
|
220
|
+
|
|
221
|
+
const button = screen.getByRole('button', { name: 'searchButtonLabel' })
|
|
222
|
+
fireEvent.click(button)
|
|
223
|
+
|
|
224
|
+
expect(onSearch).toHaveBeenCalledWith('query')
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
test('does not call onSearch when value is empty', () => {
|
|
228
|
+
const onSearch = vi.fn()
|
|
229
|
+
render(<TestWrapper onSearch={onSearch} showSearchButton={false} />)
|
|
230
|
+
|
|
231
|
+
const input = screen.getByRole('textbox')
|
|
232
|
+
fireEvent.keyDown(input, { key: 'Enter', code: 'Enter', charCode: 13 })
|
|
233
|
+
|
|
234
|
+
expect(onSearch).not.toHaveBeenCalled()
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test('does not call parent form onSubmit when nested in a form', () => {
|
|
238
|
+
const parentFormOnSubmit = vi.fn((e) => e.preventDefault())
|
|
239
|
+
const searchOnSearch = vi.fn()
|
|
240
|
+
|
|
241
|
+
const NestedFormWrapper = () => {
|
|
242
|
+
const { control } = useForm({ defaultValues: { search: 'query' } })
|
|
243
|
+
return (
|
|
244
|
+
<ThemeProvider theme={theme}>
|
|
245
|
+
<form onSubmit={parentFormOnSubmit}>
|
|
246
|
+
<RcSesSearchInput
|
|
247
|
+
id='search'
|
|
248
|
+
name='search'
|
|
249
|
+
control={control}
|
|
250
|
+
label='Search'
|
|
251
|
+
errors={undefined}
|
|
252
|
+
onSearch={searchOnSearch}
|
|
253
|
+
showSearchButton={false}
|
|
254
|
+
/>
|
|
255
|
+
</form>
|
|
256
|
+
</ThemeProvider>
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
render(<NestedFormWrapper />)
|
|
261
|
+
|
|
262
|
+
const input = screen.getByRole('textbox')
|
|
263
|
+
fireEvent.keyDown(input, { key: 'Enter', code: 'Enter', charCode: 13 })
|
|
264
|
+
|
|
265
|
+
expect(parentFormOnSubmit).not.toHaveBeenCalled()
|
|
266
|
+
expect(searchOnSearch).toHaveBeenCalledWith('query')
|
|
267
|
+
})
|
|
268
|
+
})
|
|
203
269
|
})
|