date-time-picker.test.tsx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. import { nextTick, ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, describe, expect, it } from 'vitest'
  4. import dayjs from 'dayjs'
  5. import triggerEvent from '@element-plus/test-utils/trigger-event'
  6. import { ElFormItem } from '@element-plus/components/form'
  7. import DatePicker from '../src/date-picker'
  8. import type { VNode } from 'vue'
  9. const formatStr = 'YYYY-MM-DD HH:mm:ss'
  10. const makeRange = (start: number, end: number) => {
  11. const result = []
  12. for (let i = start; i <= end; i++) {
  13. result.push(i)
  14. }
  15. return result
  16. }
  17. const _mount = (render: () => VNode) => {
  18. return mount(render, { attachTo: document.body })
  19. }
  20. afterEach(() => {
  21. document.documentElement.innerHTML = ''
  22. })
  23. describe('Datetime Picker', () => {
  24. it('both picker show correct formated value (extract date-format and time-format from format property', async () => {
  25. const value = ref(new Date(2018, 2, 5, 10, 15, 24))
  26. const format = ref('YYYY/MM/DD HH:mm A')
  27. const wrapper = _mount(() => (
  28. <DatePicker v-model={value.value} type="datetime" format={format.value} />
  29. ))
  30. const input = wrapper.find('input')
  31. input.trigger('blur')
  32. input.trigger('focus')
  33. await nextTick()
  34. const dateInput: HTMLInputElement = document.querySelector(
  35. '.el-date-picker__time-header > span:nth-child(1) input'
  36. )!
  37. const timeInput: HTMLInputElement = document.querySelector(
  38. '.el-date-picker__time-header > span:nth-child(2) input'
  39. )!
  40. timeInput.focus()
  41. await nextTick()
  42. // both input shows correct value
  43. expect(dateInput.value).toBe('2018/03/05')
  44. expect(timeInput.value).toBe('10:15 AM')
  45. format.value = 'MM-DD-YYYY HH a'
  46. await nextTick()
  47. expect(dateInput.value).toBe('03-05-2018')
  48. expect(timeInput.value).toBe('10 am')
  49. })
  50. it('both picker show correct value', async () => {
  51. const value = ref(new Date(2000, 9, 1, 10, 0, 1))
  52. const wrapper = _mount(() => (
  53. <DatePicker v-model={value.value} type="datetime" />
  54. ))
  55. const input = wrapper.find('input')
  56. input.trigger('blur')
  57. input.trigger('focus')
  58. await nextTick()
  59. const dateInput: HTMLInputElement = document.querySelector(
  60. '.el-date-picker__time-header > span:nth-child(1) input'
  61. )!
  62. const timeInput: HTMLInputElement = document.querySelector(
  63. '.el-date-picker__time-header > span:nth-child(2) input'
  64. )!
  65. timeInput.focus()
  66. await nextTick()
  67. // both input shows correct value
  68. expect(dateInput.value).toBe('2000-10-01')
  69. expect(timeInput.value).toBe('10:00:01')
  70. // time spinner highlight is correct
  71. let spinners = document.querySelectorAll(
  72. '.el-time-spinner ul li.is-active'
  73. ) as NodeListOf<HTMLElement>
  74. expect(spinners[0].textContent).toBe('10')
  75. expect(spinners[1].textContent).toBe('00')
  76. expect(spinners[2].textContent).toBe('01')
  77. value.value = new Date(2001, 10, 2, 11, 1, 2)
  78. await nextTick()
  79. spinners = document.querySelectorAll(
  80. '.el-time-spinner ul li.is-active'
  81. ) as NodeListOf<HTMLElement>
  82. expect(dateInput.value).toBe('2001-11-02')
  83. expect(timeInput.value).toBe('11:01:02')
  84. expect(spinners[0].textContent).toBe('11')
  85. expect(spinners[1].textContent).toBe('01')
  86. expect(spinners[2].textContent).toBe('02')
  87. })
  88. it('click now button', async () => {
  89. const value = ref('')
  90. const wrapper = _mount(() => (
  91. <DatePicker v-model={value.value} type="datetime" />
  92. ))
  93. const input = wrapper.find('input')
  94. input.trigger('blur')
  95. input.trigger('focus')
  96. await nextTick()
  97. ;(
  98. document.querySelector('.el-picker-panel__link-btn') as HTMLElement
  99. ).click()
  100. await nextTick()
  101. // test if is current time (deviation 10 seconds)
  102. expect(dayjs(value.value).diff(dayjs()) < 10).toBeTruthy()
  103. })
  104. it('time-picker select && input time && input date', async () => {
  105. const value = ref('')
  106. const wrapper = _mount(() => (
  107. <DatePicker v-model={value.value} type="datetime" />
  108. ))
  109. expect(value.value).toBe('')
  110. const input = wrapper.find('input')
  111. input.trigger('blur')
  112. input.trigger('focus')
  113. await nextTick()
  114. const input_ = document.querySelectorAll(
  115. '.el-date-picker__editor-wrap input'
  116. )[1] as HTMLElement
  117. input_.focus()
  118. await nextTick()
  119. const timePanel = document.querySelector('.el-time-panel')
  120. expect(
  121. timePanel!.querySelector('.el-time-spinner')!.innerHTML
  122. ).not.toBeNull()
  123. const button: HTMLElement = document.querySelector(
  124. '.el-time-panel .confirm'
  125. )!
  126. button.click()
  127. await nextTick()
  128. expect(value.value).not.toBe('')
  129. const timeInput = document.querySelectorAll(
  130. '.el-date-picker__editor-wrap input'
  131. )[1] as HTMLInputElement
  132. timeInput.value = '20:30:33'
  133. timeInput.dispatchEvent(new Event('change'))
  134. await nextTick()
  135. const valueResult = dayjs(value.value)
  136. expect(valueResult.hour()).toBe(20)
  137. expect(valueResult.minute()).toBe(30)
  138. expect(valueResult.second()).toBe(33)
  139. const dateInput: HTMLInputElement = document.querySelector(
  140. '.el-date-picker__editor-wrap input'
  141. )!
  142. dateInput.value = '2017-02-02'
  143. dateInput.dispatchEvent(new Event('change'))
  144. await nextTick()
  145. const valueResult2 = dayjs(value.value)
  146. expect(valueResult2.year()).toBe(2017)
  147. expect(valueResult2.month()).toBe(1)
  148. expect(valueResult2.date()).toBe(2)
  149. })
  150. it('now button: can not choose disabled date', async () => {
  151. let isDisable = true
  152. const value = ref('')
  153. const disabledDate = () => isDisable
  154. const wrapper = _mount(() => (
  155. <DatePicker
  156. v-model={value.value}
  157. type="datetime"
  158. disabledDate={disabledDate}
  159. />
  160. ))
  161. const input = wrapper.find('input')
  162. input.trigger('blur')
  163. input.trigger('focus')
  164. await nextTick()
  165. // click now button
  166. const btn: HTMLElement = document.querySelector(
  167. '.el-picker-panel__footer .is-text'
  168. )!
  169. btn.click()
  170. await nextTick()
  171. expect(value.value).toBe('')
  172. isDisable = false
  173. await nextTick()
  174. btn.click()
  175. await nextTick()
  176. expect(value.value).not.toBe('')
  177. })
  178. it('confirm button honors picked date', async () => {
  179. const value = ref(new Date(2000, 9, 1, 12, 0, 0))
  180. const wrapper = _mount(() => (
  181. <DatePicker v-model={value.value} type="datetime" />
  182. ))
  183. const input = wrapper.find('input')
  184. input.trigger('blur')
  185. input.trigger('focus')
  186. await nextTick()
  187. // changed month / year should not effect picked time
  188. ;(
  189. document.querySelector(
  190. '.el-date-picker__header .arrow-right'
  191. ) as HTMLElement
  192. ).click()
  193. ;(
  194. document.querySelector(
  195. '.el-date-picker__header .d-arrow-right'
  196. ) as HTMLElement
  197. ).click()
  198. // click confirm button
  199. ;(
  200. document.querySelectorAll(
  201. '.el-picker-panel__footer .el-button'
  202. )[1] as HTMLElement
  203. ).click()
  204. expect(dayjs(value.value).format(formatStr)).toBe('2000-10-01 12:00:00')
  205. })
  206. it('selectableRange', async () => {
  207. const disabledHoursArr = [
  208. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 23,
  209. ]
  210. const disabledHoursData = () => {
  211. return disabledHoursArr
  212. }
  213. const disabledMinutesData = (hour: number) => {
  214. // ['17:30:00 - 18:30:00', '18:50:00 - 20:30:00', '21:00:00 - 22:00:00']
  215. if (hour === 17) {
  216. return makeRange(0, 29)
  217. }
  218. if (hour === 18) {
  219. return makeRange(31, 49)
  220. }
  221. if (hour === 20) {
  222. return makeRange(31, 59)
  223. }
  224. if (hour === 22) {
  225. return makeRange(1, 59)
  226. }
  227. return []
  228. }
  229. const disabledSecondsData = (hour: number, minute: number) => {
  230. if (hour === 18 && minute === 30) {
  231. return makeRange(1, 59)
  232. }
  233. if (hour === 20 && minute === 30) {
  234. return makeRange(1, 59)
  235. }
  236. if (hour === 22 && minute === 0) {
  237. return makeRange(1, 59)
  238. }
  239. return []
  240. }
  241. const value = ref(new Date(2019, 0, 1, 18, 50))
  242. const wrapper = _mount(() => (
  243. <DatePicker
  244. v-model={value.value}
  245. type="datetime"
  246. disabledHours={disabledHoursData}
  247. disabledMinutes={disabledMinutesData}
  248. disabledSeconds={disabledSecondsData}
  249. />
  250. ))
  251. const input = wrapper.find('input')
  252. input.trigger('blur')
  253. input.trigger('focus')
  254. await nextTick()
  255. const input1 = document.querySelectorAll(
  256. '.el-date-picker__editor-wrap input'
  257. )[1] as HTMLInputElement
  258. input1.blur()
  259. input1.focus()
  260. await nextTick()
  261. const list = document.querySelectorAll('.el-time-spinner__list')
  262. const hoursEl = list[0]
  263. const disabledHours = Array.from(
  264. hoursEl.querySelectorAll('.is-disabled')
  265. ).map((node) => Number(node.textContent))
  266. expect(disabledHours).toStrictEqual(disabledHoursArr)
  267. const minutesEl = list[1]
  268. const disabledMinutes = Array.from(
  269. minutesEl.querySelectorAll('.is-disabled')
  270. ).map((node) => Number(node.textContent))
  271. expect(disabledMinutes.length).toBe(19)
  272. })
  273. it('defaultTime takes effect when the type is datetime', async () => {
  274. const value = ref('')
  275. const defaultTime = ref(new Date(2000, 1, 1, 12, 24, 48))
  276. const wrapper = _mount(() => (
  277. <DatePicker
  278. v-model={value.value}
  279. type="datetime"
  280. default-time={defaultTime.value}
  281. />
  282. ))
  283. const input = wrapper.find('input')
  284. input.trigger('blur')
  285. input.trigger('focus')
  286. await nextTick()
  287. const someDateTd: HTMLElement = document.querySelector(
  288. '.el-picker-panel__content tr:nth-child(3) td:nth-child(4)'
  289. )!
  290. const timeInput: HTMLInputElement = document.querySelector(
  291. '.el-date-picker__time-header > span:nth-child(2) input'
  292. )!
  293. someDateTd.click()
  294. timeInput.focus()
  295. await nextTick()
  296. expect(timeInput.value).toBe('12:24:48')
  297. // time spinner highlight is correct
  298. const spinners = document.querySelectorAll(
  299. '.el-time-spinner ul li.is-active'
  300. ) as NodeListOf<HTMLElement>
  301. expect(spinners[0].textContent).toBe('12')
  302. expect(spinners[1].textContent).toBe('24')
  303. expect(spinners[2].textContent).toBe('48')
  304. })
  305. it('defaultTime only takes effect when time is not selected', async () => {
  306. const value = ref('')
  307. const defaultTime = ref(new Date(2000, 1, 1, 12, 0, 0))
  308. const wrapper = _mount(() => (
  309. <DatePicker
  310. v-model={value.value}
  311. type="datetime"
  312. default-time={defaultTime.value}
  313. />
  314. ))
  315. const input = wrapper.find('input')
  316. input.trigger('blur')
  317. input.trigger('focus')
  318. await nextTick()
  319. const cells = document.querySelectorAll('.available .el-date-table-cell')
  320. ;(cells[0] as HTMLElement).click()
  321. await nextTick()
  322. const timeInput: HTMLInputElement = document.querySelector(
  323. '.el-date-picker__time-header > span:nth-child(2) input'
  324. )!
  325. expect(timeInput.value).toBe('12:00:00')
  326. timeInput.focus()
  327. await nextTick()
  328. const spinner: HTMLElement = document.querySelector(
  329. '.el-time-spinner ul li.is-active'
  330. )!
  331. ;(spinner.nextSibling as HTMLElement).click()
  332. await nextTick()
  333. expect(timeInput.value).toBe('13:00:00')
  334. })
  335. })
  336. describe('Datetimerange', () => {
  337. it('select daterange and default Time and input format', async () => {
  338. const value = ref([
  339. new Date(2000, 10, 8, 10, 10),
  340. new Date(2000, 10, 11, 10, 10),
  341. ])
  342. const wrapper = _mount(() => (
  343. <DatePicker
  344. v-model={value.value}
  345. type="datetimerange"
  346. default-time={new Date(2020, 1, 1, 1, 1, 1)}
  347. format="YYYY/MM/DD HH:mm A"
  348. />
  349. ))
  350. const input = wrapper.find('input')
  351. input.trigger('blur')
  352. input.trigger('focus')
  353. await nextTick()
  354. const pickers = document.querySelectorAll('.el-date-range-picker__content')
  355. const leftCell = pickers[0].querySelector('td.available')!
  356. const rightCell = pickers[1].querySelector('td.available')!
  357. triggerEvent(leftCell, 'mousemove', true)
  358. triggerEvent(leftCell, 'click', true)
  359. await nextTick()
  360. triggerEvent(rightCell, 'mousemove', true)
  361. triggerEvent(rightCell, 'click', true)
  362. await nextTick()
  363. ;(
  364. document.querySelectorAll(
  365. '.el-picker-panel__footer .el-button'
  366. )[1] as HTMLElement
  367. ).click()
  368. await nextTick()
  369. expect(value.value.map((_) => dayjs(_).format(formatStr))).toStrictEqual([
  370. '2000-11-01 01:01:01',
  371. '2000-12-01 01:01:01',
  372. ])
  373. const pickerss = document.querySelectorAll(
  374. '.el-date-range-picker__time-header .el-date-range-picker__editors-wrap'
  375. )
  376. const left = {
  377. dateInput: pickerss[0].querySelector(
  378. '.el-date-range-picker__time-picker-wrap:nth-child(1) input'
  379. ),
  380. timeInput: pickerss[0].querySelector(
  381. '.el-date-range-picker__time-picker-wrap:nth-child(2) input'
  382. ),
  383. }
  384. const right = {
  385. dateInput: pickerss[1].querySelector(
  386. '.el-date-range-picker__time-picker-wrap:nth-child(1) input'
  387. ),
  388. timeInput: pickerss[1].querySelector(
  389. '.el-date-range-picker__time-picker-wrap:nth-child(2) input'
  390. ),
  391. }
  392. await nextTick()
  393. // both input shows correct value
  394. expect((left.dateInput as HTMLInputElement).value).toBe('2000/11/01')
  395. expect((left.timeInput as HTMLInputElement).value).toBe('01:01 AM')
  396. expect((right.dateInput as HTMLInputElement).value).toBe('2000/12/01')
  397. expect((right.timeInput as HTMLInputElement).value).toBe('01:01 AM')
  398. })
  399. it('input date', async () => {
  400. const value = ref<string[]>([])
  401. const wrapper = _mount(() => (
  402. <DatePicker v-model={value.value} type="datetimerange" />
  403. ))
  404. const input = wrapper.find('input')
  405. input.trigger('blur')
  406. input.trigger('focus')
  407. await nextTick()
  408. const pickerss = document.querySelectorAll(
  409. '.el-date-range-picker__time-header .el-date-range-picker__editors-wrap'
  410. )
  411. const leftDateInput = pickerss[0].querySelector(
  412. '.el-date-range-picker__time-picker-wrap:nth-child(1) input'
  413. ) as HTMLInputElement
  414. const rightDateInput = pickerss[0].querySelector(
  415. '.el-date-range-picker__time-picker-wrap:nth-child(1) input'
  416. ) as HTMLInputElement
  417. leftDateInput.value = '1999-03-04'
  418. triggerEvent(leftDateInput, 'input', true)
  419. triggerEvent(leftDateInput, 'change', true)
  420. await nextTick()
  421. const pickers = document.querySelectorAll('.el-date-range-picker__content')
  422. const leftCell = pickers[0].querySelector('td.available')!
  423. const rightCell = pickers[1].querySelector('td.available')!
  424. triggerEvent(leftCell, 'mousemove', true)
  425. triggerEvent(leftCell, 'click', true)
  426. await nextTick()
  427. triggerEvent(rightCell, 'mousemove', true)
  428. triggerEvent(rightCell, 'click', true)
  429. await nextTick()
  430. const btn = document.querySelectorAll(
  431. '.el-picker-panel__footer .el-button'
  432. )[1] as HTMLElement
  433. btn.click()
  434. await nextTick()
  435. expect(value.value.map((_) => dayjs(_).format(formatStr))).toStrictEqual([
  436. '1999-03-01 00:00:00',
  437. '1999-04-01 00:00:00',
  438. ])
  439. // input date when minDate > maxDate
  440. rightDateInput.value = '1998-01-01'
  441. triggerEvent(rightDateInput, 'input', true)
  442. triggerEvent(rightDateInput, 'change', true)
  443. await nextTick()
  444. btn.click()
  445. await nextTick()
  446. expect(dayjs(value.value[0]).isBefore(value.value[1])).toBeTruthy()
  447. })
  448. it('select time', async () => {
  449. const value = ref('')
  450. const wrapper = _mount(() => (
  451. <DatePicker v-model={value.value} type="datetimerange" />
  452. ))
  453. expect(value.value).toBe('')
  454. const input = wrapper.find('input')
  455. input.trigger('blur')
  456. input.trigger('focus')
  457. await nextTick()
  458. const timeInput = document.querySelectorAll(
  459. '.el-date-range-picker__editors-wrap input'
  460. )[1] as HTMLInputElement
  461. timeInput.blur()
  462. timeInput.focus()
  463. timeInput.blur()
  464. await nextTick()
  465. const button: HTMLElement = document.querySelector(
  466. '.el-date-range-picker__time-picker-wrap .el-time-panel .confirm'
  467. )!
  468. button.click()
  469. await nextTick()
  470. const btn = document.querySelectorAll(
  471. '.el-picker-panel__footer .el-button'
  472. )[1] as HTMLElement
  473. btn.click()
  474. await nextTick()
  475. expect(value.value).not.toBe('')
  476. })
  477. it('confirm honors disabledDate', async () => {
  478. const value = ref('')
  479. const disabledDate = (date: Date) => {
  480. return date.getTime() < new Date(2000, 9, 1).getTime() // 2000-10-01
  481. }
  482. const wrapper = _mount(() => (
  483. <DatePicker
  484. v-model={value.value}
  485. type="datetimerange"
  486. disabledDate={disabledDate}
  487. />
  488. ))
  489. const input = wrapper.find('input')
  490. input.trigger('blur')
  491. input.trigger('focus')
  492. await nextTick()
  493. // simulate user input of invalid date
  494. const pickerss = document.querySelectorAll(
  495. '.el-date-range-picker__time-header .el-date-range-picker__editors-wrap'
  496. )
  497. const leftDateInput = pickerss[0].querySelector(
  498. '.el-date-range-picker__time-picker-wrap:nth-child(1) input'
  499. ) as HTMLInputElement
  500. leftDateInput.value = '2000-09-01'
  501. triggerEvent(leftDateInput, 'input', true)
  502. triggerEvent(leftDateInput, 'change', true)
  503. await nextTick()
  504. const btn = document.querySelectorAll(
  505. '.el-picker-panel__footer .el-button'
  506. )[1] as HTMLElement
  507. expect(btn.getAttribute('disabled')).not.toBeUndefined() // invalid input disables button
  508. btn.click()
  509. await nextTick()
  510. const rangePanel = document.querySelector('.el-date-range-picker')!
  511. expect(rangePanel.getAttribute('visible')).toBe('true') // popper still open
  512. expect(value.value).toBe('')
  513. leftDateInput.value = '2001-09-01'
  514. triggerEvent(leftDateInput, 'input', true)
  515. triggerEvent(leftDateInput, 'change', true)
  516. await nextTick()
  517. expect(btn.getAttribute('disabled')).not.toBeUndefined()
  518. btn.click()
  519. await nextTick()
  520. expect(rangePanel.getAttribute('visible')).toBe('false') // popper dismiss
  521. expect(value.value).not.toBe('')
  522. })
  523. it('selectableRange', async () => {
  524. const disabledHoursArr = [
  525. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 23,
  526. ]
  527. const disabledHoursRightArr = [0, 1, 2]
  528. const value = ref('')
  529. const disabledHoursData = (role: string) => {
  530. if (role === 'end') {
  531. return disabledHoursRightArr
  532. }
  533. return disabledHoursArr
  534. }
  535. const wrapper = _mount(() => (
  536. <DatePicker
  537. v-model={value.value}
  538. type="datetimerange"
  539. disabledHours={disabledHoursData}
  540. />
  541. ))
  542. const input = wrapper.find('input')
  543. input.trigger('blur')
  544. input.trigger('focus')
  545. await nextTick()
  546. const pickerss = document.querySelectorAll(
  547. '.el-date-range-picker__time-header .el-date-range-picker__editors-wrap'
  548. )
  549. const leftDateInput = pickerss[0].querySelector(
  550. '.el-date-range-picker__time-picker-wrap:nth-child(2) input'
  551. ) as HTMLInputElement
  552. const rightDateInput = pickerss[1].querySelector(
  553. '.el-date-range-picker__time-picker-wrap:nth-child(2) input'
  554. ) as HTMLInputElement
  555. leftDateInput.blur()
  556. leftDateInput.focus()
  557. await nextTick()
  558. const listLeft = document.querySelectorAll(
  559. '.el-date-range-picker__editors-wrap .el-time-spinner__list'
  560. )
  561. const hoursEl = listLeft[0]
  562. const disabledHours = Array.from(
  563. hoursEl.querySelectorAll('.is-disabled')
  564. ).map((node) => Number(node.textContent))
  565. expect(disabledHours).toStrictEqual(disabledHoursArr)
  566. const button = document.querySelector(
  567. '.el-date-range-picker__time-picker-wrap .el-time-panel .confirm'
  568. ) as HTMLElement
  569. button.click()
  570. await nextTick()
  571. rightDateInput.blur()
  572. rightDateInput.focus()
  573. await nextTick()
  574. const listRight = document.querySelectorAll(
  575. '.el-date-range-picker__editors-wrap.is-right .el-time-spinner__list'
  576. )
  577. const hoursEl2 = listRight[0]
  578. const disabledHours2 = Array.from(
  579. hoursEl2.querySelectorAll('.is-disabled')
  580. ).map((node) => Number(node.textContent))
  581. expect(disabledHours2).toStrictEqual(disabledHoursRightArr)
  582. })
  583. it('select same date, different time', async () => {
  584. const leftSelect = ['10', '59', '59']
  585. const value = ref<string[]>([])
  586. const wrapper = _mount(() => (
  587. <DatePicker v-model={value.value} type="datetimerange" />
  588. ))
  589. const input = wrapper.find('input')
  590. input.trigger('blur')
  591. input.trigger('focus')
  592. await nextTick()
  593. const pickers = document.querySelectorAll('.el-date-range-picker__content')!
  594. const leftCell = pickers[0].querySelector('td.available')!
  595. triggerEvent(leftCell, 'mousemove', true)
  596. triggerEvent(leftCell, 'click', true)
  597. await nextTick()
  598. triggerEvent(leftCell, 'mousemove', true)
  599. triggerEvent(leftCell, 'click', true)
  600. await nextTick()
  601. const leftTimeInput = document.querySelectorAll(
  602. '.el-date-range-picker__editors-wrap input'
  603. )[1] as HTMLInputElement
  604. leftTimeInput.blur()
  605. leftTimeInput.focus()
  606. await nextTick()
  607. const leftList = document.querySelectorAll('.el-time-spinner__list')
  608. triggerEvent(leftList[0].children[+leftSelect[0]], 'click', true)
  609. await nextTick()
  610. triggerEvent(leftList[1].children[+leftSelect[1]], 'click', true)
  611. await nextTick()
  612. triggerEvent(leftList[2].children[+leftSelect[2]], 'click', true)
  613. await nextTick()
  614. ;(
  615. document.querySelector('.el-time-panel__btn.confirm') as HTMLElement
  616. ).click()
  617. await nextTick()
  618. const rightTimeInput = document.querySelectorAll(
  619. '.el-date-range-picker__editors-wrap input'
  620. )[3] as HTMLInputElement
  621. rightTimeInput.blur()
  622. rightTimeInput.focus()
  623. await nextTick()
  624. const rightList = document.querySelectorAll(
  625. '.is-right .el-time-spinner__list'
  626. ) as any
  627. // auto set left time to right time
  628. expect(
  629. rightList[0]!.querySelector('.el-time-spinner__item.is-active').innerHTML
  630. ).toBe(leftSelect[0])
  631. expect(
  632. rightList[1].querySelector('.el-time-spinner__item.is-active').innerHTML
  633. ).toBe(leftSelect[1])
  634. expect(
  635. rightList[2].querySelector('.el-time-spinner__item.is-active').innerHTML
  636. ).toBe(leftSelect[2])
  637. triggerEvent(rightList[0].children[12], 'click', true)
  638. await nextTick()
  639. triggerEvent(rightList[1].children[12], 'click', true)
  640. await nextTick()
  641. triggerEvent(rightList[2].children[12], 'click', true)
  642. await nextTick()
  643. ;(
  644. document.querySelector(
  645. '.is-right .el-time-panel__btn.confirm'
  646. ) as HTMLElement
  647. ).click()
  648. await nextTick()
  649. ;(
  650. document.querySelectorAll(
  651. '.el-picker-panel__footer .el-button'
  652. )[1] as HTMLElement
  653. ).click()
  654. await nextTick()
  655. expect(value.value.map((_) => dayjs(_).format('HH:mm:ss'))).toStrictEqual([
  656. '10:59:59',
  657. '12:12:12',
  658. ])
  659. })
  660. describe('form item accessibility integration', () => {
  661. it('automatic id attachment', async () => {
  662. const wrapper = _mount(() => (
  663. <ElFormItem label="Foobar" data-test-ref="item">
  664. <DatePicker type="datetime" />
  665. </ElFormItem>
  666. ))
  667. await nextTick()
  668. const formItem = wrapper.find('[data-test-ref="item"]')
  669. const formItemLabel = formItem.find('.el-form-item__label')
  670. const datePickerInput = wrapper.find('.el-input__inner')
  671. expect(formItem.attributes().role).toBeFalsy()
  672. expect(formItemLabel.attributes().for).toBe(
  673. datePickerInput.attributes().id
  674. )
  675. })
  676. it('specified id attachment', async () => {
  677. const wrapper = _mount(() => (
  678. <ElFormItem label="Foobar" data-test-ref="item">
  679. <DatePicker type="datetime" id="foobar" />
  680. </ElFormItem>
  681. ))
  682. await nextTick()
  683. const formItem = wrapper.find('[data-test-ref="item"]')
  684. const formItemLabel = formItem.find('.el-form-item__label')
  685. const datePickerInput = wrapper.find('.el-input__inner')
  686. expect(formItem.attributes().role).toBeFalsy()
  687. expect(datePickerInput.attributes().id).toBe('foobar')
  688. expect(formItemLabel.attributes().for).toBe(
  689. datePickerInput.attributes().id
  690. )
  691. })
  692. it('form item role is group when multiple inputs', async () => {
  693. const wrapper = _mount(() => (
  694. <ElFormItem label="Foobar" data-test-ref="item">
  695. <DatePicker type="datetime" />
  696. <DatePicker type="datetime" />
  697. </ElFormItem>
  698. ))
  699. await nextTick()
  700. const formItem = wrapper.find('[data-test-ref="item"]')
  701. expect(formItem.attributes().role).toBe('group')
  702. })
  703. })
  704. it('display value', async () => {
  705. const value = ref([undefined, undefined])
  706. const wrapper = _mount(() => (
  707. <DatePicker v-model={value.value} type="datetimerange" />
  708. ))
  709. await nextTick()
  710. const [startInput, endInput] = wrapper.findAll('input')
  711. expect(startInput.element.value).toBe('')
  712. expect(endInput.element.value).toBe('')
  713. })
  714. })