|
13 | 13 | jest.mock('@react-aria/live-announcer'); |
14 | 14 | import {act, pointerMap, render} from '@react-spectrum/test-utils-internal'; |
15 | 15 | import {announce} from '@react-aria/live-announcer'; |
16 | | -import {Button, FieldError, Group, Input, Label, NumberField, NumberFieldContext, Text} from '../'; |
| 16 | +import {Button, FieldError, Form, Group, Input, Label, NumberField, NumberFieldContext, Text} from '../'; |
17 | 17 | import React from 'react'; |
18 | 18 | import userEvent from '@testing-library/user-event'; |
19 | 19 |
|
@@ -250,4 +250,40 @@ describe('NumberField', () => { |
250 | 250 | await user.keyboard('{Enter}'); |
251 | 251 | expect(input).toHaveValue('200'); |
252 | 252 | }); |
| 253 | + |
| 254 | + it('should not reset validation errors on blur when value has not changed', async () => { |
| 255 | + let {getByRole} = render( |
| 256 | + <Form validationErrors={{testNumber: 'This field has an error.'}}> |
| 257 | + <NumberField name="testNumber" defaultValue={5}> |
| 258 | + <Label>Test Number</Label> |
| 259 | + <Group> |
| 260 | + <Button slot="decrement">-</Button> |
| 261 | + <Input /> |
| 262 | + <Button slot="increment">+</Button> |
| 263 | + </Group> |
| 264 | + <FieldError /> |
| 265 | + </NumberField> |
| 266 | + </Form> |
| 267 | + ); |
| 268 | + |
| 269 | + let input = getByRole('textbox'); |
| 270 | + let numberfield = input.closest('.react-aria-NumberField'); |
| 271 | + |
| 272 | + // Validation error should be displayed |
| 273 | + expect(numberfield).toHaveAttribute('data-invalid'); |
| 274 | + expect(input).toHaveAttribute('aria-describedby'); |
| 275 | + expect(document.getElementById(input.getAttribute('aria-describedby').split(' ')[0])).toHaveTextContent('This field has an error.'); |
| 276 | + |
| 277 | + // Focus the field without changing the value |
| 278 | + act(() => { input.focus(); }); |
| 279 | + expect(numberfield).toHaveAttribute('data-invalid'); |
| 280 | + |
| 281 | + // Blur the field without changing the value |
| 282 | + act(() => { input.blur(); }); |
| 283 | + |
| 284 | + // Validation error should still be displayed because the value didn't change |
| 285 | + expect(numberfield).toHaveAttribute('data-invalid'); |
| 286 | + expect(input).toHaveAttribute('aria-describedby'); |
| 287 | + expect(document.getElementById(input.getAttribute('aria-describedby').split(' ')[0])).toHaveTextContent('This field has an error.'); |
| 288 | + }); |
253 | 289 | }); |
0 commit comments