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
184 changes: 184 additions & 0 deletions apps/settings/src/components/PersonalInfo/BirthdaySection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { mount } from '@vue/test-utils'
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { nextTick } from 'vue'

let personalInfoParameters
vi.mock('@nextcloud/initial-state', () => ({
Expand Down Expand Up @@ -43,6 +44,7 @@ beforeAll(async () => {

beforeEach(() => {
vi.resetModules()
savePrimaryAccountProperty.mockClear()
})

afterEach(() => {
Expand Down Expand Up @@ -111,4 +113,186 @@ describe('BirthdaySection', () => {
)
expect(input.element.value).toBe('1987-12-01')
})

it('ignores null value from partially filled picker and does not save', async () => {
personalInfoParameters = {
birthdate: {
name: 'birthdate',
value: '1987-12-15T00:00:00.000Z',
},
}
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
const wrapper = await mountBirthdaySection()

const picker = wrapper.findComponent({ name: 'NcDateTimePickerNative' })
picker.vm.$emit('update:modelValue', null)
await nextTick()

await new Promise((resolve) => setTimeout(resolve, 550))
expect(savePrimaryAccountProperty).not.toHaveBeenCalled()
expect(wrapper.find('input').element.value).toBe('1987-12-15')
expect(consoleError).not.toHaveBeenCalled()
consoleError.mockRestore()
})

it('saves value emitted on update:modelValue', async () => {
personalInfoParameters = {
birthdate: {
name: 'birthdate',
value: null,
},
}
savePrimaryAccountProperty.mockReturnValue(Promise.resolve({
ocs: { meta: { status: 'ok' } },
}))
const wrapper = await mountBirthdaySection()

const picker = wrapper.findComponent({ name: 'NcDateTimePickerNative' })
picker.vm.$emit('update:modelValue', new Date(1988, 2, 20))
await nextTick()

await expect.poll(() => savePrimaryAccountProperty.mock.calls.length).toBe(1)
expect(savePrimaryAccountProperty).toHaveBeenCalledWith(
'birthdate',
'1988-03-20T00:00:00.000Z',
)
expect(wrapper.find('input').element.value).toBe('1988-03-20')
})

it('does not throw or save on partially typed dates', async () => {
personalInfoParameters = {
birthdate: {
name: 'birthdate',
value: null,
},
}
const wrapper = await mountBirthdaySection()

const input = wrapper.find('input')
await input.setValue('')

await new Promise((resolve) => setTimeout(resolve, 550))
expect(savePrimaryAccountProperty).not.toHaveBeenCalled()
expect(wrapper.vm.birthdate.value).toBe(null)
})

it('settles a burst of picker updates into a single save of the final date', async () => {
personalInfoParameters = {
birthdate: {
name: 'birthdate',
value: null,
},
}
savePrimaryAccountProperty.mockReturnValue(Promise.resolve({
ocs: { meta: { status: 'ok' } },
}))
const wrapper = await mountBirthdaySection()

// Chrome completes the date while the year is still being typed
const picker = wrapper.findComponent({ name: 'NcDateTimePickerNative' })
for (const date of [new Date(2000, 11, 31), new Date(2031, 8, 11), new Date(2011, 7, 8), new Date(2008, 6, 7)]) {
picker.vm.$emit('update:modelValue', date)
await new Promise((resolve) => setTimeout(resolve, 50))
}
expect(wrapper.vm.birthdate.value).toBe(null)

await new Promise((resolve) => setTimeout(resolve, 550))
expect(savePrimaryAccountProperty).toHaveBeenCalledTimes(1)
expect(savePrimaryAccountProperty).toHaveBeenCalledWith(
'birthdate',
'2008-07-07T00:00:00.000Z',
)
expect(wrapper.vm.birthdate.value).toBe('2008-07-07')
expect(wrapper.find('input').element.value).toBe('2008-07-07')
})

it('renders an empty picker for a null birth date', async () => {
personalInfoParameters = {
birthdate: {
name: 'birthdate',
value: null,
},
}
const wrapper = await mountBirthdaySection()

const picker = wrapper.findComponent({ name: 'NcDateTimePickerNative' })
expect(picker.props('modelValue')).toBe(null)
expect(wrapper.find('input').element.value).toBe('')
})

it('renders an empty picker for an unparseable birth date', async () => {
personalInfoParameters = {
birthdate: {
name: 'birthdate',
value: 'not-a-date',
},
}
const wrapper = await mountBirthdaySection()

const picker = wrapper.findComponent({ name: 'NcDateTimePickerNative' })
expect(picker.props('modelValue')).toBe(null)
expect(wrapper.find('input').element.value).toBe('')
})

it('ignores dates clamped from overflowing year segments', async () => {
personalInfoParameters = {
birthdate: {
name: 'birthdate',
value: null,
},
}
const wrapper = await mountBirthdaySection()

const picker = wrapper.findComponent({ name: 'NcDateTimePickerNative' })
picker.vm.$emit('update:modelValue', new Date(8640000000000000))
await nextTick()

await new Promise((resolve) => setTimeout(resolve, 550))
expect(savePrimaryAccountProperty).not.toHaveBeenCalled()
expect(wrapper.vm.birthdate.value).toBe(null)
})

it('saves dates at the lower and upper year boundary', async () => {
personalInfoParameters = {
birthdate: {
name: 'birthdate',
value: null,
},
}
savePrimaryAccountProperty.mockReturnValue(Promise.resolve({
ocs: { meta: { status: 'ok' } },
}))
const wrapper = await mountBirthdaySection()

const picker = wrapper.findComponent({ name: 'NcDateTimePickerNative' })
// the Date constructor maps years 0-99 to 19xx, 100 is the lowest testable year
picker.vm.$emit('update:modelValue', new Date(100, 0, 1))
await expect.poll(() => savePrimaryAccountProperty.mock.calls.length).toBe(1)
expect(savePrimaryAccountProperty).toHaveBeenCalledWith(
'birthdate',
'0100-01-01T00:00:00.000Z',
)

picker.vm.$emit('update:modelValue', new Date(9999, 11, 31))
await expect.poll(() => savePrimaryAccountProperty.mock.calls.length).toBe(2)
expect(savePrimaryAccountProperty).toHaveBeenCalledWith(
'birthdate',
'9999-12-31T00:00:00.000Z',
)
expect(wrapper.vm.birthdate.value).toBe('9999-12-31')
})

it('limits the picker to years 1 to 9999', async () => {
personalInfoParameters = {
birthdate: {
name: 'birthdate',
value: null,
},
}
const wrapper = await mountBirthdaySection()

const input = wrapper.find('input')
expect(input.attributes('min')).toBe('0001-01-01')
expect(input.attributes('max')).toBe('9999-12-31')
})
})
48 changes: 38 additions & 10 deletions apps/settings/src/components/PersonalInfo/BirthdaySection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
class="property__field"
type="date"
:label="birthdate.readable"
:min="minDate"
:max="maxDate"
:model-value="timezoneAdjustedValue"
@input="onInput" />
@update:modelValue="onInput" />
<VisibilityScopeControl
class="property__scope"
:readable="birthdate.readable"
Expand Down Expand Up @@ -73,8 +75,23 @@ export default {
return `account-property-${birthdate.name}`
},

minDate() {
// new Date(1, 0, 1) would map the year to 1901
const date = new Date(0)
date.setFullYear(1, 0, 1)
return date
},

maxDate() {
return new Date(9999, 11, 31)
},

value() {
return birthdateValueToDate(this.birthdate.value)
if (!this.birthdate.value) {
return null
}
const date = birthdateValueToDate(this.birthdate.value)
return Number.isNaN(date.getTime()) ? null : date
},

/**
Expand All @@ -85,9 +102,13 @@ export default {
timezoneAdjustedValue() {
// example: this.birthdate.value === '1987-12-01T00:00:00.000Z' or '1987-12-01'

const date = this.value
if (date === null) {
return null
}

// 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)

Expand All @@ -102,16 +123,23 @@ export default {
this.birthdate.scope = scope
},

onInput(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)
/**
* @param {Date|null} value - the picked date or null while the input is only partially filled
*/
onInput(value) {
// the browser clamps overflowing year segments (e.g. 9999999) to 275760-09-13
if (!(value instanceof Date) || value.getFullYear() < 1 || value.getFullYear() > 9999) {
return
}
this.debouncePropertyChange(value)
},

debouncePropertyChange: debounce(async function(value) {
await this.updateProperty(value)
const day = value.getDate().toString().padStart(2, '0')
const month = (value.getMonth() + 1).toString().padStart(2, '0')
const year = value.getFullYear().toString().padStart(4, '0')
this.birthdate.value = `${year}-${month}-${day}`
await this.updateProperty(this.value)
}, 500),

async updateProperty(value) {
Expand Down
Loading