|
1 | 1 | import { expect, vi } from 'vitest'; |
| 2 | +import * as React from 'react'; |
2 | 3 | import { createRenderer, fireEvent, screen } from '@mui/internal-test-utils'; |
3 | 4 | import { Field } from '@base-ui/react/field'; |
4 | 5 | import { Form } from '@base-ui/react/form'; |
@@ -98,6 +99,171 @@ describe('<Field.Control />', () => { |
98 | 99 | expect(screen.getByText('Required')).toBeInTheDocument(); |
99 | 100 | }); |
100 | 101 |
|
| 102 | + describe('filled state ownership', () => { |
| 103 | + it('publishes an empty state when a filled control is replaced by a fresh one', async () => { |
| 104 | + function TestCase() { |
| 105 | + const [instance, setInstance] = React.useState(0); |
| 106 | + |
| 107 | + return ( |
| 108 | + <Field.Root data-testid="root"> |
| 109 | + <Field.Control key={instance} /> |
| 110 | + <button type="button" onClick={() => setInstance(1)}> |
| 111 | + Replace |
| 112 | + </button> |
| 113 | + </Field.Root> |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + await render(<TestCase />); |
| 118 | + |
| 119 | + fireEvent.change(screen.getByRole('textbox'), { target: { value: 'a' } }); |
| 120 | + expect(screen.getByTestId('root')).toHaveAttribute('data-filled', ''); |
| 121 | + |
| 122 | + fireEvent.click(screen.getByRole('button', { name: 'Replace' })); |
| 123 | + |
| 124 | + expect(screen.getByRole('textbox')).toHaveValue(''); |
| 125 | + expect(screen.getByTestId('root')).not.toHaveAttribute('data-filled'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('does not let a superseded control clear the active control state', async () => { |
| 129 | + function TestCase() { |
| 130 | + const [value, setValue] = React.useState('a'); |
| 131 | + |
| 132 | + return ( |
| 133 | + <Field.Root data-testid="root"> |
| 134 | + <Field.Control value={value} onValueChange={setValue} /> |
| 135 | + <Field.Control defaultValue="filled" /> |
| 136 | + <button type="button" onClick={() => setValue('')}> |
| 137 | + Clear first |
| 138 | + </button> |
| 139 | + </Field.Root> |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + await render(<TestCase />); |
| 144 | + |
| 145 | + expect(screen.getByTestId('root')).toHaveAttribute('data-filled', ''); |
| 146 | + |
| 147 | + fireEvent.click(screen.getByRole('button', { name: 'Clear first' })); |
| 148 | + |
| 149 | + expect(screen.getByTestId('root')).toHaveAttribute('data-filled', ''); |
| 150 | + }); |
| 151 | + |
| 152 | + it('keeps the active control readable after a superseded control unmounts', async () => { |
| 153 | + const validate = vi.fn<(value: unknown) => string | null>(() => null); |
| 154 | + |
| 155 | + function TestCase() { |
| 156 | + const actionsRef = React.useRef<Field.Root.Actions>(null); |
| 157 | + const [oldMounted, setOldMounted] = React.useState(true); |
| 158 | + |
| 159 | + return ( |
| 160 | + <div> |
| 161 | + <Field.Root validate={validate} actionsRef={actionsRef}> |
| 162 | + {oldMounted && <Field.Control key="old" defaultValue="old" />} |
| 163 | + <Field.Control key="new" defaultValue="new" /> |
| 164 | + </Field.Root> |
| 165 | + <button type="button" onClick={() => setOldMounted(false)}> |
| 166 | + Unmount old |
| 167 | + </button> |
| 168 | + <button type="button" onClick={() => actionsRef.current?.validate()}> |
| 169 | + Validate |
| 170 | + </button> |
| 171 | + </div> |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + await render(<TestCase />); |
| 176 | + |
| 177 | + fireEvent.click(screen.getByRole('button', { name: 'Unmount old' })); |
| 178 | + fireEvent.click(screen.getByRole('button', { name: 'Validate' })); |
| 179 | + |
| 180 | + expect(validate).toHaveBeenCalledTimes(1); |
| 181 | + expect(validate.mock.lastCall?.[0]).toBe('new'); |
| 182 | + }); |
| 183 | + |
| 184 | + it('lets a remaining control publish after the owning control unmounts', async () => { |
| 185 | + function TestCase() { |
| 186 | + const [value, setValue] = React.useState('a'); |
| 187 | + const [mounted, setMounted] = React.useState(true); |
| 188 | + |
| 189 | + return ( |
| 190 | + <Field.Root data-testid="root"> |
| 191 | + <Field.Control value={value} onValueChange={setValue} /> |
| 192 | + {mounted && <Field.Control defaultValue="filled" />} |
| 193 | + <button type="button" onClick={() => setMounted(false)}> |
| 194 | + Unmount second |
| 195 | + </button> |
| 196 | + <button type="button" onClick={() => setValue('')}> |
| 197 | + Clear first |
| 198 | + </button> |
| 199 | + </Field.Root> |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + await render(<TestCase />); |
| 204 | + |
| 205 | + expect(screen.getByTestId('root')).toHaveAttribute('data-filled', ''); |
| 206 | + |
| 207 | + fireEvent.click(screen.getByRole('button', { name: 'Unmount second' })); |
| 208 | + fireEvent.click(screen.getByRole('button', { name: 'Clear first' })); |
| 209 | + |
| 210 | + expect(screen.getByTestId('root')).not.toHaveAttribute('data-filled'); |
| 211 | + }); |
| 212 | + }); |
| 213 | + |
| 214 | + describe('focused state ownership', () => { |
| 215 | + it('releases the focused state when the focused control unmounts', async () => { |
| 216 | + function TestCase() { |
| 217 | + const [mounted, setMounted] = React.useState(true); |
| 218 | + |
| 219 | + return ( |
| 220 | + <Field.Root data-testid="root"> |
| 221 | + {mounted && <Field.Control />} |
| 222 | + <button type="button" onClick={() => setMounted(false)}> |
| 223 | + Remove |
| 224 | + </button> |
| 225 | + </Field.Root> |
| 226 | + ); |
| 227 | + } |
| 228 | + |
| 229 | + await render(<TestCase />); |
| 230 | + |
| 231 | + fireEvent.focus(screen.getByRole('textbox')); |
| 232 | + expect(screen.getByTestId('root')).toHaveAttribute('data-focused', ''); |
| 233 | + |
| 234 | + fireEvent.click(screen.getByRole('button', { name: 'Remove' })); |
| 235 | + |
| 236 | + expect(screen.getByTestId('root')).not.toHaveAttribute('data-focused'); |
| 237 | + }); |
| 238 | + |
| 239 | + it('does not let a blurred control release the focused state of another control', async () => { |
| 240 | + function TestCase() { |
| 241 | + const [mounted, setMounted] = React.useState(true); |
| 242 | + |
| 243 | + return ( |
| 244 | + <Field.Root data-testid="root"> |
| 245 | + {mounted && <Field.Control data-testid="first" />} |
| 246 | + <Field.Control data-testid="second" /> |
| 247 | + <button type="button" onClick={() => setMounted(false)}> |
| 248 | + Remove first |
| 249 | + </button> |
| 250 | + </Field.Root> |
| 251 | + ); |
| 252 | + } |
| 253 | + |
| 254 | + await render(<TestCase />); |
| 255 | + |
| 256 | + fireEvent.focus(screen.getByTestId('first')); |
| 257 | + fireEvent.blur(screen.getByTestId('first')); |
| 258 | + fireEvent.focus(screen.getByTestId('second')); |
| 259 | + expect(screen.getByTestId('root')).toHaveAttribute('data-focused', ''); |
| 260 | + |
| 261 | + fireEvent.click(screen.getByRole('button', { name: 'Remove first' })); |
| 262 | + |
| 263 | + expect(screen.getByTestId('root')).toHaveAttribute('data-focused', ''); |
| 264 | + }); |
| 265 | + }); |
| 266 | + |
101 | 267 | it.skipIf(isJSDOM)('should sync focused state when autoFocus is used with SSR', async () => { |
102 | 268 | vi.spyOn(console, 'error') |
103 | 269 | .mockName('console.error') |
|
0 commit comments