cascader-panel.test.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. import { nextTick, ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { beforeEach, describe, expect, test, vi } from 'vitest'
  4. import { Check, Loading } from '@element-plus/icons-vue'
  5. import CascaderPanel from '../src/index.vue'
  6. import type {
  7. CascaderOption,
  8. CascaderProps,
  9. CascaderValue,
  10. LazyLoad,
  11. } from '../src/node'
  12. const NORMAL_OPTIONS = [
  13. {
  14. value: 'beijing',
  15. label: 'Beijing',
  16. children: [],
  17. leaf: true,
  18. },
  19. {
  20. value: 'zhejiang',
  21. label: 'Zhejiang',
  22. children: [
  23. {
  24. value: 'hangzhou',
  25. label: 'Hangzhou',
  26. },
  27. {
  28. value: 'ningbo',
  29. label: 'Ningbo',
  30. },
  31. ],
  32. },
  33. {
  34. value: 'shanghai',
  35. label: 'Shanghai',
  36. children: [
  37. // for test nodes in different levels have same value
  38. {
  39. value: 'shanghai',
  40. label: 'Shanghai',
  41. },
  42. ],
  43. },
  44. {
  45. value: 'guangdong',
  46. label: 'Guangdong',
  47. children: [],
  48. },
  49. ]
  50. const DISABLED_OPTIONS = [
  51. {
  52. value: 'beijing',
  53. label: 'beijing',
  54. disabled: true,
  55. },
  56. {
  57. value: 'zhejiang',
  58. label: 'Zhejiang',
  59. children: [
  60. {
  61. value: 'hangzhou',
  62. label: 'Hangzhou',
  63. },
  64. ],
  65. },
  66. {
  67. value: 'jiangsu',
  68. label: 'Jiangsu',
  69. disabled: true,
  70. children: [
  71. {
  72. value: 'nanjing',
  73. label: 'Nanjing',
  74. },
  75. ],
  76. },
  77. ]
  78. const CUSTOM_PROPS_OPTIONS = [
  79. {
  80. id: 'beijing',
  81. name: 'Beijing',
  82. areas: [],
  83. },
  84. {
  85. id: 'zhejiang',
  86. name: 'Zhejiang',
  87. areas: [
  88. {
  89. id: 'hangzhou',
  90. name: 'Hangzhou',
  91. },
  92. {
  93. id: 'ningbo',
  94. name: 'Ningbo',
  95. invalid: true,
  96. },
  97. ],
  98. },
  99. ]
  100. const MENU = '.el-cascader-menu'
  101. const NODE = '.el-cascader-node'
  102. const VALID_NODE = '.el-cascader-node:not(.is-disabled)'
  103. const EXPAND_ARROW = '.arrow-right.el-cascader-node__postfix'
  104. const CHECKBOX = '.el-checkbox__input'
  105. const RADIO = '.el-radio__input'
  106. let id = 0
  107. const lazyLoad: LazyLoad = (node, resolve) => {
  108. const { level } = node
  109. setTimeout(() => {
  110. const nodes = Array.from({ length: level + 1 }).map(() => ({
  111. value: ++id,
  112. label: `option${id}`,
  113. leaf: level >= 1,
  114. }))
  115. resolve(nodes)
  116. }, 1000)
  117. }
  118. describe('CascaderPanel.vue', () => {
  119. beforeEach(() => {
  120. id = 0
  121. })
  122. test('expand and check', async () => {
  123. const handleChange = vi.fn()
  124. const handleExpandChange = vi.fn()
  125. const value = ref([])
  126. const wrapper = mount(() => (
  127. <CascaderPanel
  128. v-model={value.value}
  129. options={NORMAL_OPTIONS}
  130. onChange={handleChange}
  131. onExpandChange={handleExpandChange}
  132. />
  133. ))
  134. const options = wrapper.findAll(NODE)
  135. const [bjNode, zjNode, , gdNode] = options
  136. expect(wrapper.findAll(MENU).length).toBe(1)
  137. expect(options.length).toBe(4)
  138. expect(bjNode.text()).toBe('Beijing')
  139. expect(bjNode.find(EXPAND_ARROW).exists()).toBe(false)
  140. expect(zjNode.find(EXPAND_ARROW).exists()).toBe(true)
  141. expect(gdNode.find(EXPAND_ARROW).exists()).toBe(false)
  142. await zjNode.trigger('click')
  143. const menus = wrapper.findAll(MENU)
  144. const hzNode = menus[1].find(NODE)
  145. expect(menus.length).toBe(2)
  146. expect(handleExpandChange).toBeCalledTimes(1)
  147. await hzNode.trigger('click')
  148. // won't trigger when expanding node not change
  149. expect(handleExpandChange).toBeCalledTimes(1)
  150. expect(handleChange).toBeCalledTimes(1)
  151. expect(value.value).toEqual(['zhejiang', 'hangzhou'])
  152. await bjNode.trigger('click')
  153. expect(wrapper.findAll(MENU).length).toBe(1)
  154. expect(handleExpandChange).toBeCalledTimes(2)
  155. expect(handleChange).toBeCalledTimes(2)
  156. expect(value.value).toEqual(['beijing'])
  157. await gdNode.trigger('click')
  158. expect(wrapper.findAll(MENU).length).toBe(1)
  159. expect(handleExpandChange).toBeCalledTimes(3)
  160. expect(handleChange).toBeCalledTimes(3)
  161. expect(value.value).toEqual(['guangdong'])
  162. })
  163. test('with default value', async () => {
  164. const value = ref<null | CascaderValue>(['zhejiang', 'hangzhou'])
  165. const wrapper = mount(() => (
  166. <CascaderPanel v-model={value.value} options={NORMAL_OPTIONS} />
  167. ))
  168. await nextTick()
  169. const menus = wrapper.findAll(MENU)
  170. const [, zjNode, shNode] = menus[0].findAll(NODE)
  171. const hzNode = menus[1].find(NODE)
  172. expect(menus.length).toBe(2)
  173. expect(zjNode.classes('in-active-path')).toBe(true)
  174. expect(hzNode.classes('is-active')).toBe(true)
  175. expect(hzNode.findComponent(Check).exists()).toBe(true)
  176. value.value = ['beijing']
  177. await nextTick()
  178. expect(wrapper.findAll(MENU).length).toBe(1)
  179. expect(wrapper.find(NODE).classes('is-active')).toBe(true)
  180. // leaf node should be checked
  181. value.value = ['shanghai', 'shanghai']
  182. await nextTick()
  183. const secondMenu = wrapper.findAll(MENU)[1]
  184. expect(shNode.classes('is-active')).toBe(false)
  185. expect(secondMenu.find(NODE).classes('is-active')).toBe(true)
  186. // leaf node should be checked
  187. value.value = null
  188. await nextTick()
  189. expect(wrapper.find('is-active').exists()).toBe(false)
  190. })
  191. test('disabled options', async () => {
  192. const handleChange = vi.fn()
  193. const handleExpandChange = vi.fn()
  194. const value = ref([])
  195. const wrapper = mount(() => (
  196. <CascaderPanel
  197. v-model={value.value}
  198. options={DISABLED_OPTIONS}
  199. onChange={handleChange}
  200. onExpandChange={handleExpandChange}
  201. />
  202. ))
  203. const [bjNode, zjNode, jsNode] = wrapper.findAll(NODE)
  204. expect(wrapper.findAll(VALID_NODE).length).toBe(1)
  205. await bjNode.trigger('click')
  206. expect(handleChange).not.toBeCalled()
  207. expect(handleExpandChange).not.toBeCalled()
  208. await jsNode.trigger('click')
  209. expect(handleExpandChange).not.toBeCalled()
  210. await zjNode.trigger('click')
  211. expect(wrapper.findAll(MENU).length).toBe(2)
  212. expect(handleExpandChange).toBeCalledTimes(1)
  213. })
  214. test('options change', async () => {
  215. const options = ref(NORMAL_OPTIONS)
  216. const wrapper = mount(() => <CascaderPanel options={options.value} />)
  217. expect(wrapper.find(NODE).exists()).toBe(true)
  218. options.value = []
  219. await nextTick()
  220. expect(wrapper.find(NODE).exists()).toBe(false)
  221. })
  222. test('expand by hover', async () => {
  223. const props: CascaderProps = { expandTrigger: 'hover' }
  224. const wrapper = mount(() => (
  225. <CascaderPanel options={DISABLED_OPTIONS} props={props} />
  226. ))
  227. const [zjNode, jsNode] = wrapper.findAll(NODE).slice(1)
  228. await jsNode.trigger('mouseenter')
  229. expect(wrapper.findAll(MENU).length).toBe(1)
  230. await zjNode.trigger('mouseenter')
  231. expect(wrapper.findAll(MENU).length).toBe(2)
  232. })
  233. test('emit value only', async () => {
  234. const value = ref('shanghai')
  235. const props = { emitPath: false }
  236. const wrapper = mount(() => (
  237. <CascaderPanel
  238. v-model={value.value}
  239. options={NORMAL_OPTIONS}
  240. props={props}
  241. />
  242. ))
  243. await nextTick()
  244. const shNode = wrapper.findAll(MENU)[1].find(NODE)
  245. expect(shNode.classes('is-active')).toBe(true)
  246. await wrapper.find(NODE).trigger('click')
  247. expect(value.value).toBe('beijing')
  248. })
  249. test('emit value only, issue 1531', async () => {
  250. const value = ref(null)
  251. const options = [
  252. {
  253. value: 0,
  254. label: 'label one',
  255. },
  256. {
  257. value: 1,
  258. label: 'label two',
  259. },
  260. ]
  261. const props = { emitPath: false }
  262. const wrapper = mount(() => (
  263. <CascaderPanel v-model={value.value} options={options} props={props} />
  264. ))
  265. await nextTick()
  266. const shNode = wrapper.findAll(MENU)[0].find(NODE)
  267. expect(shNode.classes('is-active')).toBe(false)
  268. await wrapper.findAll(NODE)[0].trigger('click')
  269. expect(value.value).toBe(0)
  270. await wrapper.findAll(NODE)[1].trigger('click')
  271. expect(value.value).toBe(1)
  272. })
  273. test('multiple mode', async () => {
  274. const value = ref([])
  275. const props = {
  276. multiple: true,
  277. }
  278. const wrapper = mount(() => (
  279. <CascaderPanel
  280. v-model={value.value}
  281. options={NORMAL_OPTIONS}
  282. props={props}
  283. />
  284. ))
  285. const zjNode = wrapper.findAll(NODE)[1]
  286. const zjCheckbox = zjNode.find(CHECKBOX)
  287. expect(zjCheckbox.exists()).toBe(true)
  288. await zjNode.trigger('click')
  289. const secondMenu = wrapper.findAll(MENU)[1]
  290. const [hzCheckbox, nbCheckbox] = secondMenu.findAll(CHECKBOX)
  291. await hzCheckbox.find('input').trigger('click')
  292. expect(hzCheckbox.classes('is-checked')).toBe(true)
  293. expect(zjCheckbox.classes('is-indeterminate')).toBe(true)
  294. expect(value.value).toEqual([['zhejiang', 'hangzhou']])
  295. await nbCheckbox.find('input').trigger('click')
  296. expect(zjCheckbox.classes('is-checked')).toBe(true)
  297. expect(value.value).toEqual([
  298. ['zhejiang', 'hangzhou'],
  299. ['zhejiang', 'ningbo'],
  300. ])
  301. await zjCheckbox.find('input').trigger('click')
  302. expect(zjCheckbox.classes('is-checked')).toBe(false)
  303. expect(nbCheckbox.classes('is-checked')).toBe(false)
  304. expect(nbCheckbox.classes('is-checked')).toBe(false)
  305. expect(value.value).toEqual([])
  306. })
  307. test('multiple mode with disabled default value', async () => {
  308. const props = {
  309. multiple: true,
  310. }
  311. const wrapper = mount(() => (
  312. <CascaderPanel
  313. modelValue={[['beijing']]}
  314. options={DISABLED_OPTIONS}
  315. props={props}
  316. />
  317. ))
  318. await nextTick()
  319. const bjNode = wrapper.find(NODE)
  320. const bjCheckbox = wrapper.find(CHECKBOX)
  321. expect(bjNode.classes('is-disabled')).toBe(true)
  322. expect(bjCheckbox.classes('is-disabled')).toBe(true)
  323. expect(bjCheckbox.classes('is-checked')).toBe(true)
  324. })
  325. test('check strictly in single mode', async () => {
  326. const value = ref([])
  327. const props = {
  328. checkStrictly: true,
  329. }
  330. const wrapper = mount(() => (
  331. <CascaderPanel
  332. v-model={value.value}
  333. options={NORMAL_OPTIONS}
  334. props={props}
  335. />
  336. ))
  337. const zjRadio = wrapper.findAll(RADIO)[1]
  338. expect(zjRadio.exists()).toBe(true)
  339. await zjRadio.find('input').trigger('click')
  340. expect(value.value).toEqual(['zhejiang'])
  341. })
  342. test('check strictly in single mode with disabled options', async () => {
  343. const props = {
  344. checkStrictly: true,
  345. }
  346. const wrapper = mount(() => (
  347. <CascaderPanel options={DISABLED_OPTIONS} props={props} />
  348. ))
  349. const [bjNode, , jsNode] = wrapper.findAll(NODE)
  350. const bjRadio = bjNode.find(RADIO)
  351. // leaf nodes should add a disabled style for that they are not expandable
  352. // but non-leaf nodes are not
  353. expect(bjNode.classes('is-disabled')).toBe(true)
  354. expect(jsNode.classes('is-disabled')).toBe(false)
  355. expect(bjRadio.classes('is-disabled')).toBe(true)
  356. expect(bjRadio.find('input[disabled]').exists()).toBe(true)
  357. await jsNode.trigger('click')
  358. expect(wrapper.findAll(MENU).length).toEqual(2)
  359. })
  360. test('check strictly in multiple mode', async () => {
  361. const value = ref([['shanghai']])
  362. const props = {
  363. checkStrictly: true,
  364. multiple: true,
  365. }
  366. const wrapper = mount(() => (
  367. <CascaderPanel
  368. v-model={value.value}
  369. options={NORMAL_OPTIONS}
  370. props={props}
  371. />
  372. ))
  373. const shNode = wrapper.findAll(NODE)[2]
  374. const [, zjCheckbox, shCheckbox] = wrapper.findAll(CHECKBOX)
  375. await nextTick()
  376. await shNode.trigger('click')
  377. const shCheckbox2 = wrapper.findAll(MENU)[1].find(CHECKBOX)
  378. expect(shCheckbox.classes('is-checked')).toBe(true)
  379. expect(shCheckbox2.classes('is-checked')).toBe(false)
  380. await zjCheckbox.find('input').trigger('click')
  381. expect(value.value).toEqual([['shanghai'], ['zhejiang']])
  382. })
  383. test('custom props', async () => {
  384. const value = ref([])
  385. const props = {
  386. value: 'id',
  387. label: 'name',
  388. children: 'areas',
  389. disabled: 'invalid',
  390. leaf: (data: typeof CUSTOM_PROPS_OPTIONS[0]) => !data.areas?.length,
  391. }
  392. const wrapper = mount(() => (
  393. <CascaderPanel
  394. v-model={value.value}
  395. options={CUSTOM_PROPS_OPTIONS}
  396. props={props as any}
  397. />
  398. ))
  399. const [bjNode, zjNode] = wrapper.findAll(NODE)
  400. expect(bjNode.text()).toBe('Beijing')
  401. expect(bjNode.find(EXPAND_ARROW).exists()).toBe(false)
  402. await zjNode.trigger('click')
  403. const [hzNode, nbNode] = wrapper.findAll(MENU)[1].findAll(NODE)
  404. expect(hzNode.exists()).toBe(true)
  405. expect(nbNode.classes('is-disabled')).toBe(true)
  406. await hzNode.trigger('click')
  407. expect(value.value).toEqual(['zhejiang', 'hangzhou'])
  408. })
  409. test('lazy load', async () => {
  410. vi.useFakeTimers()
  411. const value = ref([])
  412. const props = {
  413. lazy: true,
  414. lazyLoad,
  415. }
  416. const wrapper = mount(() => (
  417. <CascaderPanel v-model={value.value} props={props} />
  418. ))
  419. vi.runAllTimers()
  420. await nextTick()
  421. const firstOption = wrapper.find(NODE)
  422. expect(firstOption.exists()).toBe(true)
  423. await firstOption.trigger('click')
  424. expect(firstOption.findComponent(Loading).exists()).toBe(true)
  425. vi.runAllTimers()
  426. await nextTick()
  427. expect(firstOption.findComponent(Loading).exists()).toBe(false)
  428. const secondMenu = wrapper.findAll(MENU)[1]
  429. expect(secondMenu.exists()).toBe(true)
  430. await secondMenu.find(NODE).trigger('click')
  431. expect(value.value).toEqual([1, 2])
  432. vi.useRealTimers()
  433. })
  434. test('lazy load with default primitive value', async () => {
  435. vi.useFakeTimers()
  436. const props = {
  437. lazy: true,
  438. lazyLoad,
  439. }
  440. const wrapper = mount(() => (
  441. <CascaderPanel modelValue={[1, 2]} props={props} />
  442. ))
  443. vi.runAllTimers()
  444. await nextTick()
  445. expect(wrapper.findAll(MENU).length).toBe(2)
  446. expect(wrapper.find(`.is-active`).text()).toBe('option2')
  447. vi.useRealTimers()
  448. })
  449. test('lazy load with default object value', async () => {
  450. vi.useFakeTimers()
  451. const value = [{ id: 1 }, { id: 2 }] as any
  452. const props: CascaderProps = {
  453. lazy: true,
  454. lazyLoad(node, resolve) {
  455. const { level } = node
  456. setTimeout(() => {
  457. const nodes = Array.from({ length: level + 1 }).map(() => ({
  458. value: { id: ++id },
  459. label: `option${id}`,
  460. leaf: level >= 1,
  461. }))
  462. resolve(nodes as any)
  463. }, 1000)
  464. },
  465. }
  466. const wrapper = mount(() => (
  467. <CascaderPanel modelValue={value} props={props} />
  468. ))
  469. vi.runAllTimers()
  470. await nextTick()
  471. expect(wrapper.findAll(MENU).length).toBe(2)
  472. expect(wrapper.find(`.is-active`).text()).toBe('option2')
  473. vi.useRealTimers()
  474. })
  475. test('no loaded nodes should not be checked', async () => {
  476. vi.useFakeTimers()
  477. const props: CascaderProps = {
  478. multiple: true,
  479. lazy: true,
  480. lazyLoad(node, resolve) {
  481. const { level } = node
  482. setTimeout(() => {
  483. const nodes = Array.from({ length: level + 1 }).map(() => {
  484. ++id
  485. return {
  486. value: id,
  487. label: `option${id}`,
  488. leaf: id === 3,
  489. }
  490. })
  491. resolve(nodes)
  492. }, 1000)
  493. },
  494. }
  495. const wrapper = mount(() => <CascaderPanel props={props} />)
  496. vi.runAllTimers()
  497. await nextTick()
  498. const firstMenu = wrapper.findAll(MENU)[0]
  499. const firstOption = wrapper.find(NODE)
  500. await firstOption.trigger('click')
  501. vi.runAllTimers()
  502. await nextTick()
  503. await firstMenu.find(CHECKBOX).find('input').trigger('click')
  504. const secondMenu = wrapper.findAll(MENU)[1]
  505. expect(secondMenu.exists()).toBe(true)
  506. expect(firstMenu.find(CHECKBOX).classes('is-checked')).toBe(false)
  507. expect(firstMenu.find(CHECKBOX).classes('is-indeterminate')).toBe(true)
  508. expect(secondMenu.findAll(CHECKBOX)[0].classes('is-checked')).toBe(false)
  509. expect(secondMenu.findAll(CHECKBOX)[0].classes('is-indeterminate')).toBe(
  510. false
  511. )
  512. expect(secondMenu.findAll(CHECKBOX)[1].classes('is-checked')).toBe(true)
  513. expect(secondMenu.findAll(CHECKBOX)[1].classes('is-indeterminate')).toBe(
  514. false
  515. )
  516. vi.useRealTimers()
  517. })
  518. test('no loaded nodes with checkStrictly should be selectable', async () => {
  519. vi.useFakeTimers()
  520. const props: CascaderProps = {
  521. checkStrictly: true,
  522. lazy: true,
  523. lazyLoad(node, resolve) {
  524. const { level } = node
  525. setTimeout(() => {
  526. const nodes = Array.from({ length: level + 1 }).map(() => ({
  527. value: ++id,
  528. label: `Option - ${id}`,
  529. leaf: level >= 2,
  530. }))
  531. resolve(nodes)
  532. }, 1000)
  533. },
  534. }
  535. const wrapper = mount(() => <CascaderPanel props={props} />)
  536. vi.runAllTimers()
  537. await nextTick()
  538. const firstMenu = wrapper.findAll(MENU)[0]
  539. await firstMenu.find(RADIO).trigger('click')
  540. expect(firstMenu.find(RADIO).classes('is-checked')).toBe(true)
  541. expect(firstMenu.find(RADIO).classes('is-indeterminate')).toBe(false)
  542. vi.runAllTimers()
  543. await nextTick()
  544. let secondMenu = wrapper.findAll(MENU)[1]
  545. expect(secondMenu).toBeUndefined()
  546. await firstMenu.find(NODE).trigger('click')
  547. vi.runAllTimers()
  548. await nextTick()
  549. secondMenu = wrapper.findAll(MENU)[1]
  550. await secondMenu.find(NODE).trigger('click')
  551. expect(secondMenu.find(RADIO).classes('is-checked')).toBe(false)
  552. expect(secondMenu.find(RADIO).classes('is-indeterminate')).toBe(false)
  553. vi.useRealTimers()
  554. })
  555. test('getCheckedNodes and clearCheckedNodes', () => {
  556. const props = { multiple: true }
  557. const wrapper = mount(() => (
  558. <CascaderPanel
  559. options={NORMAL_OPTIONS}
  560. props={props}
  561. modelValue={[['shanghai', 'shanghai']]}
  562. />
  563. ))
  564. const vm = wrapper.findComponent(CascaderPanel).vm
  565. expect(vm.getCheckedNodes(false)?.length).toBe(2)
  566. expect(vm.getCheckedNodes(true)?.length).toBe(1)
  567. vm.clearCheckedNodes()
  568. expect(vm.getCheckedNodes(false)?.length).toBe(0)
  569. })
  570. test('options async load with default value', async () => {
  571. const options = ref<CascaderOption[]>([])
  572. const wrapper = mount(() => (
  573. <CascaderPanel
  574. options={options.value}
  575. modelValue={['shanghai', 'shanghai']}
  576. />
  577. ))
  578. const vm = wrapper.findComponent(CascaderPanel).vm
  579. expect(vm.getCheckedNodes(false)?.length).toBe(0)
  580. options.value = NORMAL_OPTIONS
  581. await nextTick()
  582. expect(vm.getCheckedNodes(true)?.length).toBe(1)
  583. })
  584. })