2020-09-16 14:49:21 +08:00
|
|
|
import { nextTick } from 'vue'
|
2021-08-24 13:36:48 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2022-06-04 21:40:09 +08:00
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
2021-09-17 15:27:31 +08:00
|
|
|
import dayjs from 'dayjs'
|
2022-04-19 12:46:57 +08:00
|
|
|
import triggerEvent from '@element-plus/test-utils/trigger-event'
|
2022-01-04 09:15:15 +08:00
|
|
|
import { rAF } from '@element-plus/test-utils/tick'
|
2022-05-05 22:04:32 +08:00
|
|
|
import { ElFormItem } from '@element-plus/components/form'
|
2020-09-16 14:49:21 +08:00
|
|
|
import TimePicker from '../src/time-picker'
|
2021-05-10 22:52:23 +08:00
|
|
|
import Picker from '../src/common/picker.vue'
|
2020-09-16 14:49:21 +08:00
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const _mount = (template: string, data, otherObj?) =>
|
|
|
|
mount(
|
|
|
|
{
|
|
|
|
components: {
|
|
|
|
'el-time-picker': TimePicker,
|
2022-05-05 22:04:32 +08:00
|
|
|
'el-form-item': ElFormItem,
|
2021-09-04 19:29:28 +08:00
|
|
|
},
|
|
|
|
template,
|
|
|
|
data,
|
|
|
|
...otherObj,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
attachTo: 'body',
|
|
|
|
}
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
|
|
|
|
const makeRange = (start, end) => {
|
|
|
|
const result = []
|
|
|
|
for (let i = start; i <= end; i++) {
|
|
|
|
result.push(i)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
const getSpinnerTextAsArray = (dom, selector) => {
|
2022-03-08 14:03:32 +08:00
|
|
|
return Array.prototype.slice
|
2020-09-16 14:49:21 +08:00
|
|
|
.call(dom.querySelectorAll(selector))
|
2021-09-04 19:29:28 +08:00
|
|
|
.map((node) => Number(node.textContent))
|
2020-09-16 14:49:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-06-04 21:40:09 +08:00
|
|
|
document.body.innerHTML = ''
|
2020-09-16 14:49:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('TimePicker', () => {
|
2020-12-16 22:10:35 +08:00
|
|
|
it('create & custom class & style', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
:placeholder="placeholder"
|
|
|
|
:readonly="readonly"
|
2020-12-16 22:10:35 +08:00
|
|
|
:style="{color:'red'}"
|
|
|
|
class="customClass"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ placeholder: 'test_', readonly: true })
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
const input = wrapper.find('input')
|
|
|
|
expect(input.attributes('placeholder')).toBe('test_')
|
|
|
|
expect(input.attributes('readonly')).not.toBeUndefined()
|
2020-12-16 22:10:35 +08:00
|
|
|
const outterInput = wrapper.find('.el-input')
|
|
|
|
expect(outterInput.classes()).toContain('customClass')
|
|
|
|
expect(outterInput.attributes().style).toBeDefined()
|
2020-09-16 14:49:21 +08:00
|
|
|
})
|
|
|
|
|
2021-01-15 18:08:09 +08:00
|
|
|
it('set format && default value && set AM/PM spinner && no $attr to panel', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
:format="format"
|
|
|
|
v-model="value"
|
2021-01-15 18:08:09 +08:00
|
|
|
class="customClass"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ format: 'hh-mm:ss A', value: new Date(2016, 9, 10, 18, 40) })
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
await nextTick()
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
expect(input.element.value).toBe('06-40:00 PM') // format
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
|
|
|
const list = document.querySelectorAll('.el-time-spinner__list')
|
|
|
|
const hoursEl = list[0]
|
|
|
|
const items = hoursEl.querySelectorAll('.el-time-spinner__item')
|
|
|
|
expect(items[0].textContent).toBe('12 AM') // am pm
|
|
|
|
expect(items[1].textContent).toBe('01 AM')
|
|
|
|
expect(items[12].textContent).toBe('12 PM')
|
|
|
|
expect(items[15].textContent).toBe('03 PM')
|
2022-03-23 22:58:43 +08:00
|
|
|
const times = document.querySelectorAll('.el-time-spinner__list .is-active')
|
2020-09-16 14:49:21 +08:00
|
|
|
expect(times[0].textContent).toBe('06 PM')
|
|
|
|
expect(times[1].textContent).toBe('40') // default value
|
|
|
|
expect(times[2].textContent).toBe('00')
|
2021-01-15 18:08:09 +08:00
|
|
|
const panel = document.querySelector('.el-time-panel') as any
|
2022-04-19 12:46:57 +08:00
|
|
|
expect(panel.classList.contains('customClass')).toBeFalsy()
|
2020-09-16 14:49:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('select time', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
v-model="value"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ value: '' })
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
|
|
|
const list = document.querySelectorAll('.el-time-spinner__list')
|
|
|
|
const hoursEl = list[0]
|
|
|
|
const minutesEl = list[1]
|
|
|
|
const secondsEl = list[2]
|
|
|
|
const hourEl = hoursEl.querySelectorAll('.el-time-spinner__item')[4] as any
|
2021-09-04 19:29:28 +08:00
|
|
|
const minuteEl = minutesEl.querySelectorAll(
|
|
|
|
'.el-time-spinner__item'
|
|
|
|
)[36] as any
|
|
|
|
const secondEl = secondsEl.querySelectorAll(
|
|
|
|
'.el-time-spinner__item'
|
|
|
|
)[20] as any
|
2020-09-16 14:49:21 +08:00
|
|
|
// click hour, minute, second one at a time.
|
|
|
|
hourEl.click()
|
|
|
|
await nextTick()
|
|
|
|
minuteEl.click()
|
|
|
|
await nextTick()
|
|
|
|
secondEl.click()
|
|
|
|
await nextTick()
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const date = vm.value
|
2022-03-23 22:58:43 +08:00
|
|
|
expect(hourEl.classList.contains('is-active')).toBeTruthy()
|
|
|
|
expect(minuteEl.classList.contains('is-active')).toBeTruthy()
|
|
|
|
expect(secondEl.classList.contains('is-active')).toBeTruthy()
|
2020-09-16 14:49:21 +08:00
|
|
|
expect(date.getHours()).toBe(4)
|
|
|
|
expect(date.getMinutes()).toBe(36)
|
|
|
|
expect(date.getSeconds()).toBe(20)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('click confirm / cancel button', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
v-model="value"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ value: '' })
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
2021-09-04 19:29:28 +08:00
|
|
|
await nextTick()
|
|
|
|
;(document.querySelector('.el-time-panel__btn.cancel') as any).click()
|
2020-09-16 14:49:21 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
expect(vm.value).toBe('')
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
2021-09-04 19:29:28 +08:00
|
|
|
await nextTick()
|
|
|
|
;(document.querySelector('.el-time-panel__btn.confirm') as any).click()
|
2020-09-16 14:49:21 +08:00
|
|
|
expect(vm.value instanceof Date).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
2021-03-31 16:14:45 +08:00
|
|
|
it('should update oldValue when visible change', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2021-03-31 16:14:45 +08:00
|
|
|
v-model="value"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ value: new Date(2016, 9, 10, 18, 40) })
|
|
|
|
)
|
2021-03-31 16:14:45 +08:00
|
|
|
|
|
|
|
// show picker panel
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
// select time
|
|
|
|
const list = document.querySelectorAll('.el-time-spinner__list')
|
|
|
|
const hoursEl = list[0]
|
|
|
|
const minutesEl = list[1]
|
|
|
|
const secondsEl = list[2]
|
|
|
|
const hourEl = hoursEl.querySelectorAll('.el-time-spinner__item')[4] as any
|
2021-09-04 19:29:28 +08:00
|
|
|
const minuteEl = minutesEl.querySelectorAll(
|
|
|
|
'.el-time-spinner__item'
|
|
|
|
)[36] as any
|
|
|
|
const secondEl = secondsEl.querySelectorAll(
|
|
|
|
'.el-time-spinner__item'
|
|
|
|
)[20] as any
|
2021-03-31 16:14:45 +08:00
|
|
|
hourEl.click()
|
|
|
|
await nextTick()
|
|
|
|
minuteEl.click()
|
|
|
|
await nextTick()
|
|
|
|
secondEl.click()
|
2021-09-04 19:29:28 +08:00
|
|
|
await nextTick()
|
2021-03-31 16:14:45 +08:00
|
|
|
|
|
|
|
// click confirm button
|
2021-09-04 19:29:28 +08:00
|
|
|
;(document.querySelector('.el-time-panel__btn.confirm') as any).click()
|
2021-03-31 16:14:45 +08:00
|
|
|
const date = (wrapper.vm as any).value
|
|
|
|
expect(date.getHours()).toBe(4)
|
|
|
|
expect(date.getMinutes()).toBe(36)
|
|
|
|
expect(date.getSeconds()).toBe(20)
|
|
|
|
|
|
|
|
// show picker panel and click cancel button
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
2021-09-04 19:29:28 +08:00
|
|
|
await nextTick()
|
|
|
|
;(document.querySelector('.el-time-panel__btn.cancel') as any).click()
|
2021-03-31 16:14:45 +08:00
|
|
|
expect(date.getHours()).toBe(4)
|
|
|
|
expect(date.getMinutes()).toBe(36)
|
|
|
|
expect(date.getSeconds()).toBe(20)
|
|
|
|
})
|
|
|
|
|
2020-09-16 14:49:21 +08:00
|
|
|
it('set format', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
v-model="value"
|
|
|
|
format='HH:mm'
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ value: '' })
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
|
|
|
const spinnerDom = document.querySelectorAll('.el-time-spinner__wrapper')
|
|
|
|
const minutesDom = spinnerDom[1]
|
|
|
|
const secondsDom = spinnerDom[2]
|
|
|
|
expect(minutesDom).not.toBeUndefined()
|
|
|
|
expect(secondsDom).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
2022-05-16 21:10:06 +08:00
|
|
|
it('event change, focus, blur, keydown', async () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
const changeHandler = vi.fn()
|
|
|
|
const focusHandler = vi.fn()
|
|
|
|
const blurHandler = vi.fn()
|
2022-05-16 21:10:06 +08:00
|
|
|
const keydownHandler = vi.fn()
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
v-model="value"
|
|
|
|
@change="onChange"
|
|
|
|
@focus="onFocus"
|
|
|
|
@blur="onBlur"
|
2022-05-16 21:10:06 +08:00
|
|
|
@keydown="onKeydown"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ value: new Date(2016, 9, 10, 18, 40) }),
|
|
|
|
{
|
|
|
|
methods: {
|
|
|
|
onChange(e) {
|
|
|
|
return changeHandler(e)
|
|
|
|
},
|
|
|
|
onFocus(e) {
|
|
|
|
return focusHandler(e)
|
|
|
|
},
|
|
|
|
onBlur(e) {
|
|
|
|
return blurHandler(e)
|
|
|
|
},
|
2022-05-16 21:10:06 +08:00
|
|
|
onKeydown(e) {
|
|
|
|
return keydownHandler(e)
|
|
|
|
},
|
2020-09-16 14:49:21 +08:00
|
|
|
},
|
2021-09-04 19:29:28 +08:00
|
|
|
}
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
2022-05-10 21:51:17 +08:00
|
|
|
await rAF() // Set selection range causes focus to be retained
|
|
|
|
input.element.blur()
|
|
|
|
input.trigger('blur')
|
|
|
|
await nextTick()
|
|
|
|
await rAF() // Blur is delayed to ensure focus was not moved to popper
|
2022-05-16 21:10:06 +08:00
|
|
|
input.trigger('keydown')
|
|
|
|
await nextTick()
|
|
|
|
await rAF()
|
2020-09-16 14:49:21 +08:00
|
|
|
expect(focusHandler).toHaveBeenCalledTimes(1)
|
2022-05-10 21:51:17 +08:00
|
|
|
expect(blurHandler).toHaveBeenCalledTimes(1)
|
2022-05-16 21:10:06 +08:00
|
|
|
expect(keydownHandler).toHaveBeenCalledTimes(1)
|
2022-05-10 21:51:17 +08:00
|
|
|
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
|
|
|
await rAF()
|
2020-09-16 14:49:21 +08:00
|
|
|
const list = document.querySelectorAll('.el-time-spinner__list')
|
|
|
|
const hoursEl = list[0]
|
|
|
|
const hourEl = hoursEl.querySelectorAll('.el-time-spinner__item')[4] as any
|
|
|
|
hourEl.click()
|
|
|
|
await nextTick()
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(changeHandler).toHaveBeenCalledTimes(0)
|
|
|
|
;(document.querySelector('.el-time-panel__btn.confirm') as any).click()
|
2020-09-16 14:49:21 +08:00
|
|
|
await nextTick()
|
2021-04-14 10:16:36 +08:00
|
|
|
await nextTick() // onchange is triggered by props.modelValue update
|
|
|
|
expect(changeHandler).toHaveBeenCalledTimes(1)
|
2020-09-16 14:49:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('selectableRange ', async () => {
|
|
|
|
// ['17:30:00 - 18:30:00', '18:50:00 - 20:30:00', '21:00:00 - 22:00:00']
|
2021-09-04 19:29:28 +08:00
|
|
|
const disabledHoursArr = [
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 23,
|
|
|
|
]
|
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
v-model="value"
|
|
|
|
:disabled-hours="disabledHours"
|
|
|
|
:disabled-minutes="disabledMinutes"
|
|
|
|
:disabled-seconds="disabledSeconds"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ value: '' }),
|
|
|
|
{
|
|
|
|
methods: {
|
|
|
|
disabledHours() {
|
|
|
|
return disabledHoursArr
|
|
|
|
},
|
|
|
|
disabledMinutes(hour) {
|
|
|
|
// ['17:30:00 - 18:30:00', '18:50:00 - 20:30:00', '21:00:00 - 22:00:00']
|
|
|
|
if (hour === 17) {
|
|
|
|
return makeRange(0, 29)
|
|
|
|
}
|
|
|
|
if (hour === 18) {
|
|
|
|
return makeRange(31, 49)
|
|
|
|
}
|
|
|
|
if (hour === 20) {
|
|
|
|
return makeRange(31, 59)
|
|
|
|
}
|
|
|
|
if (hour === 22) {
|
|
|
|
return makeRange(1, 59)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
disabledSeconds(hour, minute) {
|
|
|
|
if (hour === 18 && minute === 30) {
|
|
|
|
return makeRange(1, 59)
|
|
|
|
}
|
|
|
|
if (hour === 20 && minute === 30) {
|
|
|
|
return makeRange(1, 59)
|
|
|
|
}
|
|
|
|
if (hour === 22 && minute === 0) {
|
|
|
|
return makeRange(1, 59)
|
|
|
|
}
|
|
|
|
},
|
2020-09-16 14:49:21 +08:00
|
|
|
},
|
2021-09-04 19:29:28 +08:00
|
|
|
}
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
const list = document.querySelectorAll('.el-time-spinner__list')
|
|
|
|
const hoursEl = list[0]
|
|
|
|
const minutesEl = list[1]
|
|
|
|
const secondsEl = list[2]
|
2022-03-23 22:58:43 +08:00
|
|
|
const disabledHours = getSpinnerTextAsArray(hoursEl, '.is-disabled')
|
2020-09-16 14:49:21 +08:00
|
|
|
expect(disabledHours).toEqual(disabledHoursArr)
|
2021-09-04 19:29:28 +08:00
|
|
|
const hourSpinners = hoursEl.querySelectorAll('.el-time-spinner__item')
|
|
|
|
;(hourSpinners[18] as any).click()
|
2020-09-16 14:49:21 +08:00
|
|
|
await nextTick()
|
2022-03-23 22:58:43 +08:00
|
|
|
const disabledMinutes = getSpinnerTextAsArray(minutesEl, '.is-disabled')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(disabledMinutes.every((t) => t > 30 && t < 50)).toBeTruthy()
|
|
|
|
expect(disabledMinutes.length).toEqual(19)
|
|
|
|
;(hourSpinners[22] as any).click()
|
2020-09-16 14:49:21 +08:00
|
|
|
await nextTick()
|
2022-03-23 22:58:43 +08:00
|
|
|
const enabledMinutes = getSpinnerTextAsArray(
|
|
|
|
minutesEl,
|
|
|
|
':not(.is-disabled)'
|
|
|
|
)
|
|
|
|
const enabledSeconds = getSpinnerTextAsArray(
|
|
|
|
secondsEl,
|
|
|
|
':not(.is-disabled)'
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
expect(enabledMinutes).toEqual([0])
|
|
|
|
expect(enabledSeconds).toEqual([0])
|
|
|
|
})
|
2021-04-06 13:28:57 +08:00
|
|
|
|
|
|
|
it('ref focus', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
_mount(
|
|
|
|
`<el-time-picker
|
2021-04-06 13:28:57 +08:00
|
|
|
v-model="value"
|
|
|
|
ref="input"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ value: new Date(2016, 9, 10, 18, 40) }),
|
|
|
|
{
|
|
|
|
mounted() {
|
|
|
|
this.$refs.input.focus()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2022-01-04 09:15:15 +08:00
|
|
|
// This one allows mounted to take effect
|
2021-04-06 13:28:57 +08:00
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
// These following two allows popper to gets rendered.
|
|
|
|
await rAF()
|
2021-04-06 13:28:57 +08:00
|
|
|
const popperEl = document.querySelector('.el-picker__popper')
|
|
|
|
const attr = popperEl.getAttribute('aria-hidden')
|
|
|
|
expect(attr).toEqual('false')
|
|
|
|
})
|
2021-07-12 11:07:23 +08:00
|
|
|
|
|
|
|
it('ref blur', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
_mount(
|
|
|
|
`<el-time-picker
|
2021-07-12 11:07:23 +08:00
|
|
|
v-model="value"
|
|
|
|
ref="input"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ value: new Date(2016, 9, 10, 18, 40) }),
|
|
|
|
{
|
|
|
|
mounted() {
|
|
|
|
this.$refs.input.focus()
|
|
|
|
this.$refs.input.blur()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2021-07-12 11:07:23 +08:00
|
|
|
await nextTick()
|
|
|
|
const popperEl = document.querySelector('.el-picker__popper')
|
|
|
|
const attr = popperEl.getAttribute('aria-hidden')
|
2022-01-11 10:24:48 +08:00
|
|
|
expect(attr).toEqual('false')
|
2021-07-12 11:07:23 +08:00
|
|
|
})
|
2021-07-13 09:25:04 +08:00
|
|
|
|
|
|
|
it('model value should sync when disabled-hours was updated', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`
|
2021-07-13 09:25:04 +08:00
|
|
|
<el-time-picker
|
|
|
|
v-model="value"
|
|
|
|
:disabled-hours="disabledHours"
|
|
|
|
value-format="YYYY-MM-DD HH:mm:ss"
|
|
|
|
/>
|
2021-09-04 19:29:28 +08:00
|
|
|
`,
|
|
|
|
() => ({
|
|
|
|
value: '2000-01-01 00:00:00',
|
|
|
|
minHour: '8',
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
computed: {
|
|
|
|
disabledHours() {
|
|
|
|
return () => {
|
2022-03-08 14:03:32 +08:00
|
|
|
return Array.from({ length: 24 })
|
2021-09-04 19:29:28 +08:00
|
|
|
.fill(null)
|
|
|
|
.map((_, i) => i)
|
2022-03-08 14:03:32 +08:00
|
|
|
.filter((h) => h < Number.parseInt(this.minHour, 10))
|
2021-09-04 19:29:28 +08:00
|
|
|
}
|
|
|
|
},
|
2021-07-13 09:25:04 +08:00
|
|
|
},
|
2021-09-04 19:29:28 +08:00
|
|
|
}
|
|
|
|
)
|
2021-07-13 09:25:04 +08:00
|
|
|
await nextTick()
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
expect(vm.value).toEqual('2000-01-01 08:00:00')
|
|
|
|
vm.minHour = '9'
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value).toEqual('2000-01-01 09:00:00')
|
|
|
|
vm.minHour = '8'
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value).toEqual('2000-01-01 09:00:00')
|
|
|
|
})
|
2022-06-08 16:13:45 +08:00
|
|
|
|
|
|
|
it('picker-panel should not pop up when readonly', async () => {
|
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
|
|
|
readonly
|
|
|
|
/>`,
|
|
|
|
() => ({})
|
|
|
|
)
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
await input.trigger('mousedown')
|
|
|
|
await nextTick()
|
|
|
|
expect((wrapper.findComponent(Picker).vm as any).pickerVisible).toEqual(
|
|
|
|
false
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('picker-panel should not pop up when disabled', async () => {
|
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
|
|
|
disabled
|
|
|
|
/>`,
|
|
|
|
() => ({})
|
|
|
|
)
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
await input.trigger('mousedown')
|
|
|
|
await nextTick()
|
|
|
|
expect((wrapper.findComponent(Picker).vm as any).pickerVisible).toEqual(
|
|
|
|
false
|
|
|
|
)
|
|
|
|
})
|
2020-09-16 14:49:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('TimePicker(range)', () => {
|
|
|
|
it('create', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
v-model="value"
|
2021-12-12 17:54:21 +08:00
|
|
|
size="small"
|
2020-09-16 14:49:21 +08:00
|
|
|
:is-range="true"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({
|
|
|
|
value: [new Date(2016, 9, 10, 18, 40), new Date(2016, 9, 10, 19, 40)],
|
|
|
|
})
|
|
|
|
)
|
2021-12-12 17:54:21 +08:00
|
|
|
expect(wrapper.find('.el-range-editor--small').exists()).toBeTruthy()
|
2020-09-16 14:49:21 +08:00
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
// For skipping Transition animation
|
|
|
|
await rAF()
|
2021-09-04 19:29:28 +08:00
|
|
|
const list = document.querySelectorAll(
|
2022-03-23 22:58:43 +08:00
|
|
|
'.el-time-spinner__list .el-time-spinner__item.is-active'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
|
|
|
;['18', '40', '00', '19', '40', '00'].forEach((_, i) => {
|
2020-09-16 14:49:21 +08:00
|
|
|
expect(list[i].textContent).toBe(_)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('default value', async () => {
|
|
|
|
const defaultValue = [
|
|
|
|
new Date(2000, 9, 1, 10, 20, 0),
|
|
|
|
new Date(2000, 9, 1, 11, 10, 0),
|
|
|
|
]
|
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
v-model="value"
|
|
|
|
:default-value="defaultValue"
|
|
|
|
:is-range="true"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ value: '', defaultValue })
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
// For skipping Transition animation
|
|
|
|
await rAF()
|
2021-09-04 19:29:28 +08:00
|
|
|
const list = document.querySelectorAll(
|
2022-03-23 22:58:43 +08:00
|
|
|
'.el-time-spinner__list .el-time-spinner__item.is-active'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
;['10', '20', '00', '11', '10', '00'].forEach((_, i) => {
|
2020-09-16 14:49:21 +08:00
|
|
|
expect(list[i].textContent).toBe(_)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('cancel button', async () => {
|
2021-12-23 13:32:30 +08:00
|
|
|
const cancelDates = [
|
|
|
|
new Date(2016, 9, 10, 9, 40),
|
|
|
|
new Date(2016, 9, 10, 15, 40),
|
|
|
|
]
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
v-model="value"
|
|
|
|
is-range
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
2021-12-23 13:32:30 +08:00
|
|
|
() => ({
|
|
|
|
value: cancelDates,
|
|
|
|
})
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('blur')
|
2021-12-23 13:32:30 +08:00
|
|
|
await nextTick()
|
2020-09-16 14:49:21 +08:00
|
|
|
input.trigger('focus')
|
2021-09-04 19:29:28 +08:00
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
// For skipping Transition animation
|
|
|
|
await rAF()
|
2021-09-04 19:29:28 +08:00
|
|
|
;(document.querySelector('.el-time-panel__btn.cancel') as any).click()
|
2022-03-22 17:03:39 +08:00
|
|
|
await rAF()
|
2020-09-16 14:49:21 +08:00
|
|
|
const vm = wrapper.vm as any
|
2021-12-23 13:32:30 +08:00
|
|
|
expect(vm.value).toEqual(cancelDates)
|
2022-03-22 17:03:39 +08:00
|
|
|
expect((wrapper.findComponent(Picker).vm as any).pickerVisible).toEqual(
|
|
|
|
false
|
|
|
|
)
|
|
|
|
expect(document.querySelector('.el-picker-panel')).toBeNull()
|
2020-09-16 14:49:21 +08:00
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
2021-09-04 19:29:28 +08:00
|
|
|
await nextTick()
|
|
|
|
;(document.querySelector('.el-time-panel__btn.confirm') as any).click()
|
2022-03-08 14:03:32 +08:00
|
|
|
expect(Array.isArray(vm.value)).toBeTruthy()
|
2021-09-04 19:29:28 +08:00
|
|
|
vm.value.forEach((_) => {
|
2020-09-16 14:49:21 +08:00
|
|
|
expect(_ instanceof Date).toBeTruthy()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('selectableRange ', async () => {
|
|
|
|
// left ['08:00:00 - 12:59:59'] right ['11:00:00 - 16:59:59']
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-09-16 14:49:21 +08:00
|
|
|
v-model="value"
|
|
|
|
is-range
|
|
|
|
:disabled-hours="disabledHours"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({
|
|
|
|
value: [new Date(2016, 9, 10, 9, 40), new Date(2016, 9, 10, 15, 40)],
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
methods: {
|
|
|
|
disabledHours(role) {
|
|
|
|
if (role === 'start') {
|
|
|
|
return makeRange(0, 7).concat(makeRange(13, 23))
|
|
|
|
}
|
|
|
|
return makeRange(0, 10).concat(makeRange(17, 23))
|
|
|
|
},
|
2020-09-16 14:49:21 +08:00
|
|
|
},
|
2021-09-04 19:29:28 +08:00
|
|
|
}
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
// For skipping Transition animation
|
|
|
|
await rAF()
|
2020-09-16 14:49:21 +08:00
|
|
|
|
|
|
|
const list = document.querySelectorAll('.el-time-spinner__list')
|
|
|
|
const leftHoursEl = list[0]
|
2021-09-04 19:29:28 +08:00
|
|
|
const leftEndbledHours = getSpinnerTextAsArray(
|
|
|
|
leftHoursEl,
|
2022-03-23 22:58:43 +08:00
|
|
|
':not(.is-disabled)'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2021-08-27 19:13:37 +08:00
|
|
|
expect(leftEndbledHours).toEqual([8, 9, 10, 11, 12])
|
2020-09-16 14:49:21 +08:00
|
|
|
const rightHoursEl = list[3]
|
2021-09-04 19:29:28 +08:00
|
|
|
const rightEndbledHours = getSpinnerTextAsArray(
|
|
|
|
rightHoursEl,
|
2022-03-23 22:58:43 +08:00
|
|
|
':not(.is-disabled)'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
|
|
|
expect(rightEndbledHours).toEqual([11, 12, 13, 14, 15, 16])
|
|
|
|
;(leftHoursEl.querySelectorAll('.el-time-spinner__item')[12] as any).click()
|
2020-09-16 14:49:21 +08:00
|
|
|
await nextTick()
|
2021-09-04 19:29:28 +08:00
|
|
|
const NextRightEndbledHours = getSpinnerTextAsArray(
|
|
|
|
rightHoursEl,
|
2022-03-23 22:58:43 +08:00
|
|
|
':not(.is-disabled)'
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2021-08-27 19:13:37 +08:00
|
|
|
expect(NextRightEndbledHours).toEqual([12, 13, 14, 15, 16])
|
2020-09-16 14:49:21 +08:00
|
|
|
})
|
2020-12-31 11:12:52 +08:00
|
|
|
|
|
|
|
it('arrow key', async () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
2020-12-31 11:12:52 +08:00
|
|
|
v-model="value"
|
|
|
|
format="YYYY-MM-DD HH:mm:ss"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({ value: new Date(2016, 9, 10, 18, 40) })
|
|
|
|
)
|
2020-12-31 11:12:52 +08:00
|
|
|
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
|
|
|
const initValue = input.element.value
|
|
|
|
triggerEvent(input.element, 'keydown', 'ArrowDown')
|
|
|
|
await nextTick()
|
|
|
|
const addOneHour = input.element.value
|
|
|
|
triggerEvent(input.element, 'keydown', 'ArrowRight')
|
|
|
|
await nextTick()
|
|
|
|
triggerEvent(input.element, 'keydown', 'ArrowDown')
|
|
|
|
await nextTick()
|
|
|
|
const addOneHourOneMinute = input.element.value
|
|
|
|
expect(dayjs(initValue).diff(addOneHour, 'minute')).toEqual(-60)
|
|
|
|
expect(dayjs(initValue).diff(addOneHourOneMinute, 'minute')).toEqual(-61)
|
|
|
|
})
|
2021-05-10 22:52:23 +08:00
|
|
|
|
|
|
|
it('should be able to inherit options from parent injection', async () => {
|
|
|
|
const ElPopperOptions = {
|
|
|
|
strategy: 'fixed',
|
|
|
|
}
|
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
|
|
|
v-model="value"
|
|
|
|
format="YYYY-MM-DD HH:mm:ss"
|
|
|
|
:popper-options="options"
|
2021-09-04 19:29:28 +08:00
|
|
|
/>`,
|
|
|
|
() => ({
|
|
|
|
value: new Date(2016, 9, 10, 18, 40),
|
|
|
|
options: ElPopperOptions,
|
|
|
|
}),
|
2021-05-10 22:52:23 +08:00
|
|
|
{
|
|
|
|
provide() {
|
|
|
|
return {
|
|
|
|
ElPopperOptions,
|
|
|
|
}
|
|
|
|
},
|
2021-09-04 19:29:28 +08:00
|
|
|
}
|
2021-05-10 22:52:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
expect((wrapper.findComponent(Picker).vm as any).elPopperOptions).toEqual(
|
|
|
|
ElPopperOptions
|
|
|
|
)
|
2021-05-10 22:52:23 +08:00
|
|
|
})
|
2021-11-02 23:09:36 +08:00
|
|
|
|
|
|
|
it('am/pm mode avoid render redundant content', async () => {
|
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
|
|
|
v-model="timeRange"
|
|
|
|
is-range
|
|
|
|
range-separator="To"
|
|
|
|
start-placeholder="Start time"
|
|
|
|
end-placeholder="End time"
|
|
|
|
arrow-control
|
|
|
|
format="hh:mm:ss a"
|
|
|
|
>
|
|
|
|
</el-time-picker>
|
|
|
|
`,
|
|
|
|
() => ({
|
|
|
|
timeRange: [],
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.trigger('blur')
|
|
|
|
input.trigger('focus')
|
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
// For skipping Transition animation
|
|
|
|
await rAF()
|
|
|
|
|
2021-11-02 23:09:36 +08:00
|
|
|
const list = document.querySelectorAll('.el-time-spinner__list')
|
|
|
|
expect(
|
|
|
|
list[0]
|
2022-03-23 22:58:43 +08:00
|
|
|
.querySelector('.el-time-spinner__item.is-active')
|
2021-11-02 23:09:36 +08:00
|
|
|
.innerHTML.split(' ').length
|
|
|
|
).toBe(2)
|
|
|
|
expect(
|
|
|
|
list[1]
|
2022-03-23 22:58:43 +08:00
|
|
|
.querySelector('.el-time-spinner__item.is-active')
|
2021-11-02 23:09:36 +08:00
|
|
|
.innerHTML.split(' ').length
|
|
|
|
).toBe(1)
|
|
|
|
expect(
|
|
|
|
list[2]
|
2022-03-23 22:58:43 +08:00
|
|
|
.querySelector('.el-time-spinner__item.is-active')
|
2021-11-02 23:09:36 +08:00
|
|
|
.innerHTML.split(' ').length
|
|
|
|
).toBe(1)
|
|
|
|
})
|
2022-05-05 22:04:32 +08:00
|
|
|
|
|
|
|
describe('form item accessibility integration', () => {
|
|
|
|
it('automatic id attachment', async () => {
|
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-form-item label="Foobar" data-test-ref="item">
|
|
|
|
<el-time-picker />
|
|
|
|
</el-form-item>`,
|
|
|
|
() => ({})
|
|
|
|
)
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
const formItem = wrapper.find('[data-test-ref="item"]')
|
|
|
|
const formItemLabel = formItem.find('.el-form-item__label')
|
|
|
|
const timePickerInput = wrapper.find('.el-input__inner')
|
|
|
|
expect(formItem.attributes().role).toBeFalsy()
|
|
|
|
expect(formItemLabel.attributes().for).toBe(
|
|
|
|
timePickerInput.attributes().id
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('specified id attachment', async () => {
|
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-form-item label="Foobar" data-test-ref="item">
|
|
|
|
<el-time-picker id="foobar" />
|
|
|
|
</el-form-item>`,
|
|
|
|
() => ({})
|
|
|
|
)
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
const formItem = wrapper.find('[data-test-ref="item"]')
|
|
|
|
const formItemLabel = formItem.find('.el-form-item__label')
|
|
|
|
const timePickerInput = wrapper.find('.el-input__inner')
|
|
|
|
expect(formItem.attributes().role).toBeFalsy()
|
|
|
|
expect(timePickerInput.attributes().id).toBe('foobar')
|
|
|
|
expect(formItemLabel.attributes().for).toBe(
|
|
|
|
timePickerInput.attributes().id
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('form item role is group when multiple inputs', async () => {
|
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-form-item label="Foobar" data-test-ref="item">
|
|
|
|
<el-time-picker />
|
|
|
|
<el-time-picker />
|
|
|
|
</el-form-item>`,
|
|
|
|
() => ({})
|
|
|
|
)
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
const formItem = wrapper.find('[data-test-ref="item"]')
|
|
|
|
expect(formItem.attributes().role).toBe('group')
|
|
|
|
})
|
|
|
|
})
|
2022-06-04 21:40:09 +08:00
|
|
|
|
|
|
|
describe('dismiss events restore picker', () => {
|
|
|
|
let wrapper: ReturnType<typeof _mount>
|
|
|
|
|
|
|
|
const findInput = () =>
|
|
|
|
wrapper.findComponent({
|
|
|
|
name: 'ElInput',
|
|
|
|
})
|
|
|
|
const findClear = () => wrapper.find('.clear-icon')
|
|
|
|
const findPicker = () =>
|
|
|
|
wrapper.findComponent({
|
|
|
|
name: 'Picker',
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = _mount(
|
|
|
|
`<el-time-picker v-model="value" ref="input" />`,
|
|
|
|
() => ({ value: new Date(2016, 9, 10, 18, 40) })
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.unmount()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to focus back and callout picker after clear', async () => {
|
|
|
|
await nextTick()
|
|
|
|
const input = findInput()
|
|
|
|
await input.trigger('mouseenter')
|
|
|
|
await rAF()
|
|
|
|
const clearIcon = findClear()
|
|
|
|
await clearIcon.trigger('click')
|
|
|
|
await rAF()
|
|
|
|
expect(document.activeElement).toBe(wrapper.find('input').element)
|
|
|
|
expect(document.querySelector('.el-time-panel')).toBeFalsy()
|
|
|
|
await input.vm.$emit('input', 'a')
|
|
|
|
await rAF()
|
|
|
|
expect(document.querySelector('.el-time-panel')).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to focus back and callout picker after pick', async () => {
|
|
|
|
await nextTick()
|
|
|
|
const picker = findPicker()
|
|
|
|
const input = findInput()
|
|
|
|
picker.vm.onPick('', false)
|
|
|
|
await rAF()
|
|
|
|
expect(document.activeElement).toBe(wrapper.find('input').element)
|
|
|
|
expect(document.querySelector('.el-time-panel')).toBeFalsy()
|
|
|
|
input.vm.$emit('input', 'a')
|
|
|
|
await rAF()
|
|
|
|
expect(document.querySelector('.el-time-panel')).toBeTruthy()
|
|
|
|
})
|
|
|
|
})
|
2022-06-08 11:16:44 +08:00
|
|
|
|
|
|
|
it('display value', async () => {
|
|
|
|
const wrapper = _mount(
|
|
|
|
`<el-time-picker
|
|
|
|
v-model="value"
|
|
|
|
:is-range="true"
|
|
|
|
/>`,
|
|
|
|
() => ({ value: [undefined, undefined] })
|
|
|
|
)
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
const [startInput, endInput] = wrapper.findAll('input')
|
|
|
|
expect(startInput.element.value).toBe('')
|
|
|
|
expect(endInput.element.value).toBe('')
|
|
|
|
})
|
2020-09-16 14:49:21 +08:00
|
|
|
})
|