-
Notifications
You must be signed in to change notification settings - Fork 4.9k
DataForm: Fix the datetime control sending two updates per calendar interaction #81440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { useRef } from '@wordpress/element'; | ||
| import { ValidatedSelectControl } from '../select-control'; | ||
|
|
||
| describe( 'ControlWithError', () => { | ||
| describe( 'Reveal during pending validation', () => { | ||
| it( 'should keep the pending indicator instead of a native error on a synthetic `invalid` event', async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| function PendingValidatedSelectControl() { | ||
| const ref = useRef< HTMLSelectElement >( null ); | ||
| return ( | ||
| <> | ||
| <ValidatedSelectControl | ||
| ref={ ref } | ||
| label="Color" | ||
| required | ||
| value="" | ||
| options={ [ | ||
| { label: 'Select a color...', value: '' }, | ||
| { label: 'Red', value: 'red' }, | ||
| ] } | ||
| onChange={ () => {} } | ||
| customValidity={ { | ||
| type: 'validating', | ||
| message: 'Validating...', | ||
| } } | ||
| /> | ||
| <button | ||
| type="button" | ||
| onClick={ () => | ||
| ref.current?.dispatchEvent( | ||
| new Event( 'invalid', { | ||
| cancelable: true, | ||
| } ) | ||
| ) | ||
| } | ||
| > | ||
| Show errors | ||
| </button> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| render( <PendingValidatedSelectControl /> ); | ||
|
|
||
| await user.click( | ||
| screen.getByRole( 'button', { name: 'Show errors' } ) | ||
| ); | ||
|
|
||
| await waitFor( () => { | ||
| expect( screen.getByText( 'Validating...' ) ).toBeVisible(); | ||
| } ); | ||
| expect( | ||
| screen.queryByText( 'Constraints not satisfied' ) | ||
| ).not.toBeInTheDocument(); | ||
| } ); | ||
| } ); | ||
| } ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1000,4 +1000,123 @@ describe( 'DataForm component', () => { | |
| expect( speak ).not.toHaveBeenCalled(); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'datetime fields', () => { | ||
| const datetimeFields = [ | ||
| { | ||
| id: 'date', | ||
| label: 'Date', | ||
| type: 'datetime' as const, | ||
| isValid: { required: true }, | ||
| }, | ||
| ]; | ||
|
|
||
| const datetimeForm = { | ||
| fields: [ 'date' ], | ||
| }; | ||
|
|
||
| const dayButton = ( date: Date ) => | ||
| screen.getByRole( 'button', { | ||
| name: new RegExp( | ||
| new Intl.DateTimeFormat( 'en-US', { | ||
| weekday: 'long', | ||
| year: 'numeric', | ||
| month: 'long', | ||
| day: 'numeric', | ||
| } ).format( date ) | ||
| ), | ||
| } ); | ||
|
|
||
| // Waits out any timeouts scheduled by the control, so that a | ||
| // duplicate update scheduled for a later tick is not missed. | ||
| const flushTimeouts = () => | ||
| act( | ||
| () => new Promise( ( resolve ) => setTimeout( resolve, 10 ) ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: the control only schedules 0 ms timeouts, so I guess the 10 ms sleep is arbitrary. Could we use 0 ms here (or fake timers) so the test does not rely on a magic number? Harmless as-is.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think 10 is fine (upper bound) because while we could use 0 ms to catch duplicates at the same tick, the slightly bigger timeout could catch cases with a slightly later timeout. I agree 10 is still arbitrary and could update if you want. I don't have strong opinions on this 😄 |
||
| ); | ||
|
|
||
| function ControlledForm( { | ||
| onChange: onChangeProp = noop, | ||
| }: { | ||
| onChange?: ( edits: { date?: string } ) => void; | ||
| } ) { | ||
| const [ item, setItem ] = useState< { | ||
| date: string | undefined; | ||
| } >( { date: '2026-01-10T10:00:00.000Z' } ); | ||
| return ( | ||
| <Dataform | ||
| onChange={ ( edits ) => { | ||
| onChangeProp( edits ); | ||
| setItem( ( prev ) => ( { ...prev, ...edits } ) ); | ||
| } } | ||
| fields={ datetimeFields } | ||
| form={ datetimeForm } | ||
| data={ item } | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| it( 'should call onChange once when a date is selected in the calendar', async () => { | ||
| const onChange = jest.fn(); | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <Dataform | ||
| onChange={ onChange } | ||
| fields={ datetimeFields } | ||
| form={ datetimeForm } | ||
| data={ { date: '2026-01-10T10:00:00.000Z' } } | ||
| /> | ||
| ); | ||
|
|
||
| await user.click( dayButton( new Date( 2026, 0, 15 ) ) ); | ||
| await flushTimeouts(); | ||
|
|
||
| expect( onChange ).toHaveBeenCalledTimes( 1 ); | ||
| // The time is preserved from the previous value. | ||
| expect( onChange ).toHaveBeenCalledWith( { | ||
| date: '2026-01-15T10:00:00.000Z', | ||
| } ); | ||
| expect( speak ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'should call onChange once and show the required error when the date is cleared, keeping focus on the day button', async () => { | ||
| const onChange = jest.fn(); | ||
| const user = userEvent.setup(); | ||
|
|
||
| render( <ControlledForm onChange={ onChange } /> ); | ||
|
|
||
| // Clicking the selected day deselects it. | ||
| await user.click( dayButton( new Date( 2026, 0, 10 ) ) ); | ||
| await flushTimeouts(); | ||
|
|
||
| expect( onChange ).toHaveBeenCalledTimes( 1 ); | ||
| expect( onChange ).toHaveBeenCalledWith( { date: undefined } ); | ||
| expect( | ||
| await screen.findByText( 'Constraints not satisfied' ) | ||
| ).toBeVisible(); | ||
| // Focus does not move, so the error is announced instead. | ||
| expect( speak ).toHaveBeenCalledWith( 'Constraints not satisfied' ); | ||
| expect( dayButton( new Date( 2026, 0, 10 ) ) ).toHaveFocus(); | ||
| } ); | ||
|
|
||
| it( 'should clear the revealed error when a valid date is selected in the calendar', async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render( <ControlledForm /> ); | ||
|
|
||
| // Clicking the selected day deselects it, making the field invalid. | ||
| await user.click( dayButton( new Date( 2026, 0, 10 ) ) ); | ||
| await flushTimeouts(); | ||
|
|
||
| expect( | ||
| await screen.findByText( 'Constraints not satisfied' ) | ||
| ).toBeVisible(); | ||
|
|
||
| await user.click( dayButton( new Date( 2026, 0, 15 ) ) ); | ||
| await flushTimeouts(); | ||
|
|
||
| expect( | ||
| screen.queryByText( 'Constraints not satisfied' ) | ||
| ).not.toBeInTheDocument(); | ||
| } ); | ||
| } ); | ||
| } ); | ||
Uh oh!
There was an error while loading. Please reload this page.