-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Components: Normalize displayed dates to UTC time for DateTimePicker #73444
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
11b488f
Date: Fix incorrect typing on offset type
aduth 266a936
DateTimePicker: Normalize displayed dates to UTC time
aduth d65bdba
Parse timezoneless input string as UTC
aduth c05bed0
Add link to pull request in CHANGELOG.md
aduth d17a3e4
Add additional combination testing browser at UTC
aduth ac743c6
Use toBeVisible over toBeInTheDocument with positive assertions
aduth 972267f
Add test coverage for inputToDate
aduth 3b3d390
Define timezone-mock as devDependencies of components package
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
packages/components/src/date-time/date-time/test/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import timezoneMock from 'timezone-mock'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { getSettings, setSettings, type DateSettings } from '@wordpress/date'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import DateTimePicker from '..'; | ||
|
|
||
| describe( 'DateTimePicker', () => { | ||
| let originalSettings: DateSettings; | ||
| beforeAll( () => { | ||
| originalSettings = getSettings(); | ||
| setSettings( { | ||
| ...originalSettings, | ||
| timezone: { | ||
| offset: -5, | ||
| offsetFormatted: '-5', | ||
| string: 'America/New_York', | ||
| abbr: 'EST', | ||
| }, | ||
| } ); | ||
| } ); | ||
|
|
||
| afterEach( () => { | ||
| jest.restoreAllMocks(); | ||
| timezoneMock.unregister(); | ||
| } ); | ||
|
|
||
| afterAll( () => { | ||
| setSettings( originalSettings ); | ||
| } ); | ||
|
|
||
| it( 'should display and select dates correctly when timezones match', async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = jest.fn(); | ||
|
|
||
| timezoneMock.register( 'US/Eastern' ); | ||
|
|
||
| const { rerender } = render( | ||
| <DateTimePicker | ||
| currentDate="2025-11-15T00:00:00" | ||
| onChange={ onChange } | ||
| /> | ||
| ); | ||
|
|
||
| expect( | ||
| screen.getByRole( 'button', { | ||
| name: 'November 15, 2025. Selected', | ||
| } ) | ||
| ).toBeVisible(); | ||
|
|
||
| onChange.mockImplementation( ( newDate ) => { | ||
| rerender( | ||
| <DateTimePicker currentDate={ newDate } onChange={ onChange } /> | ||
| ); | ||
| } ); | ||
|
|
||
| await user.click( | ||
| screen.getByRole( 'button', { name: 'November 20, 2025' } ) | ||
| ); | ||
|
|
||
| expect( onChange ).toHaveBeenCalledWith( '2025-11-20T00:00:00' ); | ||
| expect( | ||
| screen.getByRole( 'button', { | ||
| name: 'November 20, 2025. Selected', | ||
| } ) | ||
| ).toBeVisible(); | ||
| } ); | ||
|
|
||
| describe( 'timezone differences between browser and site', () => { | ||
| describe.each( [ | ||
| { | ||
| direction: 'browser behind site', | ||
| timezone: 'US/Pacific' as const, | ||
| time: '21:00:00', // Evening: shifts to next day UTC | ||
| }, | ||
| { | ||
| // Test a scenario where local time is UTC time, to verify that | ||
| // using gmdateI18n (UTC) for formatting works correctly when | ||
| // the browser's timezone already has no offset from UTC. | ||
| direction: 'browser matches UTC (zero offset)', | ||
| timezone: 'UTC' as const, | ||
| time: '00:00:00', | ||
| }, | ||
| { | ||
| direction: 'browser ahead of site', | ||
| timezone: 'Australia/Adelaide' as const, | ||
| time: '00:00:00', // Midnight: shifts to previous day UTC | ||
| }, | ||
| ] )( '$direction', ( { timezone, time } ) => { | ||
| describe.each( [ | ||
| { | ||
| period: 'DST start', | ||
| initialDate: `2025-03-10T${ time }`, | ||
| initialButton: 'March 10, 2025. Selected', | ||
| clickButton: 'March 11, 2025', | ||
| expectedDay: 11, | ||
| expectedDate: `2025-03-11T${ time }`, | ||
| selectedButton: 'March 11, 2025. Selected', | ||
| wrongMonthButton: 'February 28, 2025', | ||
| }, | ||
| { | ||
| period: 'DST end', | ||
| initialDate: `2025-11-01T${ time }`, | ||
| initialButton: 'November 1, 2025. Selected', | ||
| clickButton: 'November 2, 2025', | ||
| expectedDay: 2, | ||
| expectedDate: `2025-11-02T${ time }`, | ||
| selectedButton: 'November 2, 2025. Selected', | ||
| wrongMonthButton: 'October 31, 2025', | ||
| }, | ||
| ] )( | ||
| '$period', | ||
| ( { | ||
| initialDate, | ||
| initialButton, | ||
| clickButton, | ||
| expectedDay, | ||
| expectedDate, | ||
| selectedButton, | ||
| wrongMonthButton, | ||
| } ) => { | ||
| it( 'should display and select dates correctly', async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = jest.fn(); | ||
|
|
||
| timezoneMock.register( timezone ); | ||
|
|
||
| const { rerender } = render( | ||
| <DateTimePicker | ||
| currentDate={ initialDate } | ||
| onChange={ onChange } | ||
| /> | ||
| ); | ||
|
|
||
| // Calendar should not show dates from wrong month | ||
| expect( | ||
| screen.queryByRole( 'button', { | ||
| name: wrongMonthButton, | ||
| } ) | ||
| ).not.toBeInTheDocument(); | ||
|
|
||
| // Should show correct initial date as selected | ||
| expect( | ||
| screen.getByRole( 'button', { | ||
| name: initialButton, | ||
| } ) | ||
| ).toBeVisible(); | ||
|
|
||
| onChange.mockImplementation( ( newDate ) => { | ||
| rerender( | ||
| <DateTimePicker | ||
| currentDate={ newDate } | ||
| onChange={ onChange } | ||
| /> | ||
| ); | ||
| } ); | ||
|
|
||
| await user.click( | ||
| screen.getByRole( 'button', { name: clickButton } ) | ||
| ); | ||
|
|
||
| expect( screen.getByLabelText( 'Day' ) ).toHaveValue( | ||
| expectedDay | ||
| ); | ||
| expect( onChange ).toHaveBeenCalledWith( expectedDate ); | ||
| expect( | ||
| screen.getByRole( 'button', { | ||
| name: selectedButton, | ||
| } ) | ||
| ).toBeVisible(); | ||
| } ); | ||
| } | ||
| ); | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'should preserve time when changing date', async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = jest.fn(); | ||
|
|
||
| timezoneMock.register( 'UTC' ); | ||
|
|
||
| render( | ||
| <DateTimePicker | ||
| currentDate="2025-11-15T14:30:00" | ||
| onChange={ onChange } | ||
| /> | ||
| ); | ||
|
|
||
| await user.click( | ||
| screen.getByRole( 'button', { name: 'November 20, 2025' } ) | ||
| ); | ||
|
|
||
| expect( onChange ).toHaveBeenCalledWith( '2025-11-20T14:30:00' ); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can also be achieved by passing
trueas the third argument ofdateI18n, but it's described as being "effectively deprecated". It's unclear but I don't get the impression that "effectively deprecated" extends togmtdateI18nitself.gutenberg/packages/date/src/index.ts
Lines 493 to 497 in 69251a1