Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions apps/settings/src/components/PersonalInfo/BirthdaySection.spec.js
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'

Copy link
Copy Markdown
Contributor

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?

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')
})
})
48 changes: 37 additions & 11 deletions apps/settings/src/components/PersonalInfo/BirthdaySection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
:id="inputId"
type="date"
label=""
:model-value="value"
:model-value="timezoneAdjustedValue"
@input="onInput" />

<p class="property__helper-text-message">
Expand All @@ -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',

Expand All @@ -44,7 +54,7 @@ export default {
data() {
let initialValue = null
if (birthdate.value) {
initialValue = new Date(birthdate.value)
initialValue = birthdateValueToDate(birthdate.value)
}

return {
Expand All @@ -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

@susnux susnux Jun 25, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not writable computed, no need for the getter...

Suggested change
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
},
timezoneAdjustedValue() {
// 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
},

},
},

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)
},

Expand All @@ -91,7 +117,7 @@ export default {
try {
const responseData = await savePrimaryAccountProperty(
this.birthdate.name,
value,
value.toISOString(),
)
this.handleResponse({
value,
Expand Down
4 changes: 2 additions & 2 deletions dist/settings-vue-settings-personal-info.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/settings-vue-settings-personal-info.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"msw": "^2.14.6",
"sass": "^1.100.0",
"stylelint": "^17.12.0",
"timezone-mock": "^1.4.3",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used by any migrated frontend, so this belongs into build/frontend-legacy/package.json

"vite": "^7.3.5",
"vitest": "^4.1.4"
},
Expand Down
Loading