-
-
Notifications
You must be signed in to change notification settings - Fork 5k
fix(settings): display birthday correctly regardless of browsers timezone #61586
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: master
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,106 @@ | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import { mount } from '@vue/test-utils' | ||
| import timezoneMock from 'timezone-mock' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| let personalInfoParameters | ||
| vi.mock('@nextcloud/initial-state', () => ({ | ||
| loadState(app, key, fallback) { | ||
| if (app === 'settings' && key === 'personalInfoParameters' && personalInfoParameters !== undefined) { | ||
| return personalInfoParameters | ||
| } | ||
| if (fallback !== undefined) { | ||
| return fallback | ||
| } | ||
|
|
||
| console.error('Unexpected loadState call without fallback', { app, key }) | ||
| throw new Error() | ||
| }, | ||
| })) | ||
|
|
||
| const savePrimaryAccountProperty = vi.hoisted(() => vi.fn()) | ||
| vi.mock('../../service/PersonalInfo/PersonalInfoService.js', () => ({ | ||
| savePrimaryAccountProperty, | ||
| })) | ||
|
|
||
| async function mountBirthdaySection() { | ||
| const BirthdaySection = await import('./BirthdaySection.vue') | ||
| return mount(BirthdaySection.default, { | ||
| mocks: { | ||
| t: (_app, text) => text, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| timezoneMock.unregister() | ||
| personalInfoParameters = undefined | ||
| vi.resetModules() | ||
| }) | ||
|
|
||
| describe('BirthdaySection', () => { | ||
| it('saves value', async () => { | ||
| personalInfoParameters = { | ||
| birthdate: { | ||
| name: 'birthdate', | ||
| value: null, | ||
| }, | ||
| } | ||
| savePrimaryAccountProperty.mockReturnValue(Promise.resolve({ | ||
| ocs: { meta: { status: 'ok' } }, | ||
| })) | ||
| const wrapper = await mountBirthdaySection() | ||
|
|
||
| const input = wrapper.find('input') | ||
| await input.setValue('1987-12-01') | ||
|
|
||
| await expect.poll(() => savePrimaryAccountProperty.mock.calls.length).toBe(1) | ||
| expect(savePrimaryAccountProperty).toHaveBeenCalledWith( | ||
| 'birthdate', | ||
| '1987-12-01T00:00:00.000Z', | ||
| ) | ||
| expect(input.element.value).toBe('1987-12-01') | ||
| }) | ||
|
|
||
| it('displays value when browser timezone is set', async () => { | ||
| timezoneMock.register('US/Pacific') | ||
| personalInfoParameters = { | ||
| birthdate: { | ||
| name: 'birthdate', | ||
| value: '1987-12-15T00:00:00.000Z', | ||
| }, | ||
| } | ||
|
|
||
| const wrapper = await mountBirthdaySection() | ||
|
|
||
| expect(wrapper.find('input').element.value).toBe('1987-12-15') | ||
| }) | ||
|
|
||
| it('saves value when browser timezone is set', async () => { | ||
| timezoneMock.register('US/Pacific') | ||
| personalInfoParameters = { | ||
| birthdate: { | ||
| name: 'birthdate', | ||
| value: null, | ||
| }, | ||
| } | ||
| savePrimaryAccountProperty.mockReturnValue(Promise.resolve({ | ||
| ocs: { meta: { status: 'ok' } }, | ||
| })) | ||
| const wrapper = await mountBirthdaySection() | ||
|
|
||
| const input = wrapper.find('input') | ||
| await input.setValue('1987-12-01') | ||
|
|
||
| await expect.poll(() => savePrimaryAccountProperty.mock.calls.length).toBe(1) | ||
| expect(savePrimaryAccountProperty).toHaveBeenCalledWith( | ||
| 'birthdate', | ||
| '1987-12-01T00:00:00.000Z', | ||
| ) | ||
| expect(input.element.value).toBe('1987-12-01') | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,7 +13,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| :id="inputId" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type="date" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label="" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :model-value="value" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :model-value="timezoneAdjustedValue" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @input="onInput" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <p class="property__helper-text-message"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -33,6 +33,16 @@ import { handleError } from '../../utils/handlers.js' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { birthdate } = loadState('settings', 'personalInfoParameters', {}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Convert a birthdate string value into a Date. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param {string } birthdateValue - e.g. "1987-12-01" or "1987-12-01T00:00:00.000Z" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return {Date} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function birthdateValueToDate(birthdateValue) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new Date(birthdateValue) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: 'BirthdaySection', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -44,7 +54,7 @@ export default { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| data() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let initialValue = null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (birthdate.value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| initialValue = new Date(birthdate.value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| initialValue = birthdateValueToDate(birthdate.value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -64,22 +74,38 @@ export default { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new Date(this.birthdate.value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return birthdateValueToDate(this.birthdate.value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
76
to
78
Contributor
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. This is no longer a writable computed. - value: {
+ value() {
- get() {
- return birthdateValueToDate(this.birthdate.value)
+ return birthdateValueToDate(this.birthdate.value)
- }, |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** @param {Date} value The date to set */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set(value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const day = value.getDate().toString().padStart(2, '0') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const month = (value.getMonth() + 1).toString().padStart(2, '0') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const year = value.getFullYear() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.birthdate.value = `${year}-${month}-${day}` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Adjusted value for usage with `NcDateTimePickerNative` (internally `<input="date">`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * The saved value is is UTC and we want to show it the same regardless of the browsers/OSs timezone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * When the adjusted value is displayed and the users timezone is applied, this adjusted value then looks like the UTC value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timezoneAdjustedValue: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // example: this.birthdate.value === '1987-12-01T00:00:00.000Z' or '1987-12-01' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // example: Mon Nov 30 1987 16:00:00 GMT-0800 (Pacific Standard Time) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `NcDateTimePickerNative` would show this as 11/30/1987 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const date = this.value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const timezoneOffsetMilliseconds = date.getTimezoneOffset() * 60 * 1000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const adjustedDate = new Date(date.getTime() + timezoneOffsetMilliseconds) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // example: Tue Dec 01 1987 00:00:00 GMT-0800 (Pacific Standard Time) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `NcDateTimePickerNative` will show this as 12/01/1987 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return adjustedDate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+86
to
99
Contributor
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. This is not writable computed, no need for the getter...
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| methods: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onInput(e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.value = e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const day = e.getDate().toString().padStart(2, '0') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const month = (e.getMonth() + 1).toString().padStart(2, '0') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const year = e.getFullYear() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.birthdate.value = `${year}-${month}-${day}` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.debouncePropertyChange(this.value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -91,7 +117,7 @@ export default { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const responseData = await savePrimaryAccountProperty( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.birthdate.name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value.toISOString(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.handleResponse({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,6 +103,7 @@ | |
| "msw": "^2.14.6", | ||
| "sass": "^1.100.0", | ||
| "stylelint": "^17.12.0", | ||
| "timezone-mock": "^1.4.3", | ||
|
Contributor
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. This is not used by any migrated frontend, so this belongs into |
||
| "vite": "^7.3.5", | ||
| "vitest": "^4.1.4" | ||
| }, | ||
|
|
||
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.
Does
vi.stubEnv("TZ", ...)does not work?