menu.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // @ts-nocheck
  2. import { nextTick } from 'vue'
  3. import { mount } from '@vue/test-utils'
  4. import { afterEach, describe, expect, test, vi } from 'vitest'
  5. import Menu from '../src/menu'
  6. import MenuGroup from '../src/menu-item-group.vue'
  7. import MenuItem from '../src/menu-item.vue'
  8. import SubMenu from '../src/sub-menu'
  9. const _mount = (template: string, options = {}) =>
  10. mount({
  11. components: {
  12. 'el-menu': Menu,
  13. 'el-menu-item-group': MenuGroup,
  14. 'el-menu-item': MenuItem,
  15. 'el-sub-menu': SubMenu,
  16. },
  17. template,
  18. ...options,
  19. })
  20. describe('menu', () => {
  21. afterEach(() => {
  22. document.body.innerHTML = ''
  23. })
  24. test('create', async () => {
  25. const wrapper = _mount(
  26. `<el-menu>
  27. <el-menu-item index="1" ref="item1">处理中心</el-menu-item>
  28. <el-menu-item index="2" ref="item2">订单管理</el-menu-item>
  29. </el-menu>`
  30. )
  31. const item1 = await wrapper.findComponent({ ref: 'item1' })
  32. const item2 = await wrapper.findComponent({ ref: 'item2' })
  33. await item1.trigger('click')
  34. await nextTick()
  35. expect(item1.classes()).toContain('is-active')
  36. await item2.trigger('click')
  37. await nextTick()
  38. expect(item2.classes()).toContain('is-active')
  39. })
  40. test('background-color', async () => {
  41. const backgroundColor = '#f00'
  42. const textColor = '#000'
  43. const activeTextColor = '#0f0'
  44. const wrapper = _mount(
  45. `<el-menu default-active="2"
  46. background-color="${backgroundColor}"
  47. text-color="${textColor}"
  48. active-text-color="${activeTextColor}">
  49. <el-menu-item index="1" ref="item1">处理中心</el-menu-item>
  50. <el-menu-item index="2" ref="item2">订单管理</el-menu-item>
  51. </el-menu>`
  52. )
  53. const instance = wrapper.vm.$el
  54. const item1 = await wrapper.findComponent({ ref: 'item1' })
  55. // const item2 = await wrapper.findComponent({ ref: 'item2' })
  56. expect(
  57. window.getComputedStyle(instance)._values['--el-menu-bg-color']
  58. ).toEqual(backgroundColor)
  59. // We can not test final style, so comment it out for now.
  60. // expect(instance.style.backgroundColor).toEqual(backgroundColor)
  61. // expect(item1.vm.$el.style.backgroundColor).toEqual(backgroundColor)
  62. // expect(item1.vm.$el.style.color).toEqual(textColor)
  63. // expect(item2.vm.$el.style.color).toEqual(activeTextColor)
  64. await item1.trigger('mouseenter')
  65. await nextTick()
  66. // expect(item1.vm.$el.style.backgroundColor).toEqual('rgb(204, 0, 0)')
  67. })
  68. test('menu-item click', async () => {
  69. const wrapper = _mount(
  70. `<el-menu>
  71. <el-menu-item @click="onMenuItemClick" index="1" ref="item1">处理中心</el-menu-item>
  72. <el-menu-item index="2" ref="item2">订单管理</el-menu-item>
  73. </el-menu>`,
  74. {
  75. data() {
  76. return {
  77. clicksCount: 0,
  78. }
  79. },
  80. methods: {
  81. onMenuItemClick(item) {
  82. expect(item).toMatchObject({
  83. index: '1',
  84. indexPath: ['1'],
  85. })
  86. this.clicksCount = this.clicksCount + 1
  87. },
  88. },
  89. }
  90. )
  91. const item1 = await wrapper.findComponent({ ref: 'item1' })
  92. await item1.trigger('click')
  93. await nextTick()
  94. expect((wrapper.vm as any).clicksCount).toEqual(1)
  95. })
  96. test('menu-item disabled', async () => {
  97. const wrapper = _mount(
  98. `<el-menu default-active="2">
  99. <el-menu-item index="1" ref="item1" disabled>处理中心</el-menu-item>
  100. <el-menu-item index="2" ref="item2">订单管理</el-menu-item>
  101. </el-menu>`
  102. )
  103. const item1 = await wrapper.findComponent({ ref: 'item1' })
  104. const item2 = await wrapper.findComponent({ ref: 'item2' })
  105. expect(item2.classes()).toContain('is-active')
  106. await item1.trigger('click')
  107. await nextTick()
  108. expect(item1.classes().includes('is-active')).toBeFalsy()
  109. })
  110. test('open method', async () => {
  111. const wrapper = _mount(
  112. `<div>
  113. <el-menu
  114. ref="menu"
  115. default-active="2"
  116. class="el-menu-vertical-demo"
  117. >
  118. <el-sub-menu index="1" ref="subMenu">
  119. <template #title>
  120. <span>导航一</span>
  121. </template>
  122. <el-menu-item-group>
  123. <template #title>分组一</template>
  124. <el-menu-item index="1-1">选项1</el-menu-item>
  125. <el-menu-item index="1-2">选项2</el-menu-item>
  126. </el-menu-item-group>
  127. <el-menu-item-group title="分组2">
  128. <el-menu-item index="1-3">选项3</el-menu-item>
  129. </el-menu-item-group>
  130. <el-sub-menu index="1-4">
  131. <template #title>选项4</template>
  132. <el-menu-item index="1-4-1">选项1</el-menu-item>
  133. </el-sub-menu>
  134. </el-sub-menu>
  135. <el-menu-item index="2">
  136. <template #title>导航二</template>
  137. </el-menu-item>
  138. </el-menu>
  139. <button @click="open"></button>
  140. </div>
  141. `,
  142. {
  143. methods: {
  144. open() {
  145. this.$refs.menu.open('1')
  146. },
  147. },
  148. }
  149. )
  150. const button = wrapper.find('button')
  151. button.trigger('click')
  152. await nextTick()
  153. const elSubMenu = wrapper.findComponent({ ref: 'subMenu' })
  154. expect(elSubMenu.vm.$.exposed.opened).toBeTruthy()
  155. })
  156. test('hover-bg-color', async () => {
  157. const wrapper = _mount(
  158. `<el-menu ref="menu" default-active="2"
  159. :background-color="background"
  160. text-color="#000"
  161. active-text-color="#0f0">
  162. <el-menu-item index="1" ref="item1">处理中心</el-menu-item>
  163. <el-menu-item index="2" ref="item2">订单管理</el-menu-item>
  164. </el-menu>`,
  165. {
  166. data() {
  167. return {
  168. background: '#008C74',
  169. }
  170. },
  171. }
  172. )
  173. await nextTick()
  174. const vm = wrapper.vm as any
  175. // expect(vm.$refs.menu.hoverBackground).toEqual('rgb(0, 112, 93)')
  176. vm.background = '#F00'
  177. await nextTick()
  178. // expect(vm.$refs.menu.hoverBackground).toEqual('rgb(204, 0, 0)')
  179. })
  180. test('menu-overflow', async () => {
  181. // TODO: jsdom not support `offsetWidth` and `ResizeObserver`.
  182. })
  183. })
  184. describe('default active', () => {
  185. test('normal active', async () => {
  186. const wrapper = _mount(
  187. `<el-menu default-active="2">
  188. <el-menu-item index="1" ref="item1">处理中心</el-menu-item>
  189. <el-menu-item index="2" ref="item2">订单管理</el-menu-item>
  190. </el-menu>`
  191. )
  192. const item1 = await wrapper.findComponent({ ref: 'item1' })
  193. const item2 = await wrapper.findComponent({ ref: 'item2' })
  194. expect(item2.classes()).toContain('is-active')
  195. await item1.trigger('click')
  196. await nextTick()
  197. expect(item1.classes()).toContain('is-active')
  198. })
  199. test('dynamic active', async () => {
  200. const wrapper = _mount(
  201. `<el-menu :default-active="active">
  202. <el-menu-item index="1" ref="item1">active watch处理中心</el-menu-item>
  203. <el-menu-item index="2" ref="item2">active watch订单管理</el-menu-item>
  204. </el-menu>`,
  205. {
  206. data() {
  207. return {
  208. active: '2',
  209. }
  210. },
  211. }
  212. )
  213. const instance = wrapper.vm as any
  214. instance.active = '1'
  215. await nextTick()
  216. const item1 = await wrapper.findComponent({ ref: 'item1' })
  217. expect(item1.classes()).toContain('is-active')
  218. })
  219. test('vertical submenu item active', async () => {
  220. const wrapper = _mount(
  221. `<div>
  222. <el-menu default-active="2-2" mode="vertical">
  223. <el-menu-item index="1" ref="item1">处理中心</el-menu-item>
  224. <el-sub-menu index="2" ref="submenu">
  225. <template slot="title">我的工作台</template>
  226. <el-menu-item index="2-1">选项1</el-menu-item>
  227. <el-menu-item index="2-2" ref="submenuItem2">选项2</el-menu-item>
  228. <el-menu-item index="2-3">选项3</el-menu-item>
  229. </el-sub-menu>
  230. <el-menu-item index="3">订单管理</el-menu-item>
  231. </el-menu>
  232. </div>`
  233. )
  234. const submenu = await wrapper.findComponent({ ref: 'submenu' })
  235. const submenuItem2 = await wrapper.findComponent({ ref: 'submenuItem2' })
  236. expect(submenuItem2.classes()).toContain('is-active')
  237. await nextTick()
  238. expect(submenu.classes()).toContain('is-opened')
  239. expect(submenu.classes()).toContain('is-active')
  240. })
  241. test('horizontal submenu item active', async () => {
  242. const wrapper = _mount(
  243. `<div>
  244. <el-menu default-active="2-2">
  245. <el-menu-item index="1" ref="item1">处理中心</el-menu-item>
  246. <el-sub-menu index="2" ref="submenu">
  247. <template slot="title">我的工作台</template>
  248. <el-menu-item index="2-1">选项1</el-menu-item>
  249. <el-menu-item index="2-2" ref="submenuItem2">选项2</el-menu-item>
  250. <el-menu-item index="2-3">选项3</el-menu-item>
  251. </el-sub-menu>
  252. <el-menu-item index="3">订单管理</el-menu-item>
  253. </el-menu>
  254. </div>`
  255. )
  256. const submenu = await wrapper.findComponent({ ref: 'submenu' })
  257. const submenuItem2 = await wrapper.findComponent({ ref: 'submenuItem2' })
  258. expect(submenuItem2.classes()).toContain('is-active')
  259. await nextTick()
  260. expect(submenu.classes()).toContain('is-opened')
  261. expect(submenu.classes()).toContain('is-active')
  262. })
  263. })
  264. describe('submenu', () => {
  265. test('toggle', async () => {
  266. const wrapper = _mount(
  267. `<el-menu>
  268. <el-menu-item index="1" ref="item1">处理中心</el-menu-item>
  269. <el-sub-menu index="2" ref="submenu">
  270. <template slot="title">我的工作台</template>
  271. <el-menu-item index="2-1">选项1</el-menu-item>
  272. <el-menu-item index="2-2" ref="submenuItem2">选项2</el-menu-item>
  273. <el-menu-item index="2-3">选项3</el-menu-item>
  274. </el-sub-menu>
  275. <el-menu-item index="3">订单管理</el-menu-item>
  276. </el-menu>`
  277. )
  278. const submenu = await wrapper.findComponent({ ref: 'submenu' })
  279. const submenuItem2 = await wrapper.findComponent({ ref: 'submenuItem2' })
  280. submenu.vm.$el.querySelector('.el-sub-menu__title').click()
  281. await nextTick()
  282. expect(submenu.classes()).toContain('is-opened')
  283. submenuItem2.trigger('click')
  284. await nextTick()
  285. expect(submenuItem2.classes()).toContain('is-active')
  286. submenu.trigger('click')
  287. await nextTick()
  288. expect(submenu.classes()).toContain('is-opened')
  289. })
  290. test('default opened', async () => {
  291. const wrapper = _mount(
  292. `<el-menu :default-openeds="defaultOpeneds">
  293. <el-menu-item index="1">default opened处理中心</el-menu-item>
  294. <el-sub-menu index="2" ref="submenu1">
  295. <template slot="title">default opened我的工作台</template>
  296. <el-menu-item index="2-1">选项1</el-menu-item>
  297. <el-menu-item index="2-2" ref="submenu1Item2">选项2</el-menu-item>
  298. <el-menu-item index="2-3">选项3</el-menu-item>
  299. </el-sub-menu>
  300. <el-sub-menu index="3" ref="submenu2">
  301. <template slot="title">default opened订单管理</template>
  302. <el-menu-item index="3-1">选项1</el-menu-item>
  303. <el-menu-item index="3-2" ref="submenu2Item2">选项2</el-menu-item>
  304. <el-menu-item index="3-3">选项3</el-menu-item>
  305. </el-sub-menu>
  306. </el-menu>`,
  307. {
  308. data() {
  309. return {
  310. defaultOpeneds: ['2', '3'],
  311. }
  312. },
  313. }
  314. )
  315. const submenu1 = await wrapper.findComponent({ ref: 'submenu1' })
  316. const submenu2 = await wrapper.findComponent({ ref: 'submenu2' })
  317. await nextTick()
  318. expect(submenu1.classes()).toContain('is-opened')
  319. expect(submenu2.classes()).toContain('is-opened')
  320. const instance = wrapper.vm as any
  321. instance.defaultOpeneds = ['2']
  322. await nextTick()
  323. expect(submenu1.classes()).toContain('is-opened')
  324. expect(submenu2.classes()).toContain('is-opened')
  325. })
  326. test('disabled', async () => {
  327. const wrapper = _mount(
  328. `<el-menu>
  329. <el-menu-item index="1" ref="item1">处理中心</el-menu-item>
  330. <el-sub-menu index="2" ref="submenu" disabled>
  331. <template slot="title">我的工作台</template>
  332. <el-menu-item index="2-1">选项1</el-menu-item>
  333. <el-menu-item index="2-2" ref="submenuItem2">选项2</el-menu-item>
  334. <el-menu-item index="2-3">选项3</el-menu-item>
  335. </el-sub-menu>
  336. <el-menu-item index="3">订单管理</el-menu-item>
  337. </el-menu>`,
  338. {
  339. data() {
  340. return {
  341. defaultOpeneds: ['2', '3'],
  342. }
  343. },
  344. }
  345. )
  346. const submenu = await wrapper.findComponent({ ref: 'submenu' })
  347. await submenu.trigger('click')
  348. await nextTick()
  349. expect(submenu.classes().includes('is-opened')).toBeFalsy()
  350. })
  351. })
  352. describe('other', () => {
  353. test('disabled', async () => {
  354. const wrapper = _mount(
  355. `<el-menu unique-opened default-active="2-2">
  356. <el-menu-item index="1">处理中心</el-menu-item>
  357. <el-sub-menu index="2" ref="submenu1">
  358. <template slot="title">我的工作台</template>
  359. <el-menu-item index="2-1">选项1</el-menu-item>
  360. <el-menu-item index="2-2" ref="submenu1Item2">选项2</el-menu-item>
  361. <el-menu-item index="2-3">选项3</el-menu-item>
  362. </el-sub-menu>
  363. <el-sub-menu index="3" ref="submenu2">
  364. <template slot="title">订单管理</template>
  365. <el-menu-item index="3-1">选项1</el-menu-item>
  366. <el-menu-item index="3-2" ref="submenu2Item2">选项2</el-menu-item>
  367. <el-menu-item index="3-3">选项3</el-menu-item>
  368. </el-sub-menu>
  369. </el-menu>`
  370. )
  371. const submenu2 = await wrapper.findComponent({ ref: 'submenu2' })
  372. submenu2.vm.$el.querySelector('.el-sub-menu__title').click()
  373. await nextTick()
  374. const submenu1 = await wrapper.findComponent({ ref: 'submenu1' })
  375. expect(submenu1.classes().includes('is-opened')).toBeFalsy()
  376. })
  377. test('horizontal mode', async () => {
  378. const onOpen = vi.fn()
  379. const wrapper = _mount(
  380. `<el-menu mode="horizontal" @open="onOpen">
  381. <el-menu-item index="1">处理中心</el-menu-item>
  382. <el-sub-menu index="2" ref="submenu">
  383. <template slot="title">我的工作台</template>
  384. <el-menu-item index="2-1">选项1</el-menu-item>
  385. <el-menu-item index="2-2" ref="submenuItem2">选项2</el-menu-item>
  386. <el-menu-item index="2-3">选项3</el-menu-item>
  387. </el-sub-menu>
  388. <el-menu-item index="3">订单管理</el-menu-item>
  389. </el-menu>`,
  390. {
  391. methods: {
  392. onOpen,
  393. },
  394. }
  395. )
  396. await nextTick()
  397. expect(wrapper.classes()).toContain('el-menu--horizontal')
  398. const submenu = wrapper.findComponent({ ref: 'submenu' })
  399. vi.useFakeTimers()
  400. await submenu.trigger('mouseenter')
  401. vi.runAllTimers()
  402. vi.useRealTimers()
  403. expect(onOpen).toHaveBeenCalled()
  404. })
  405. test('menu group', async () => {
  406. const wrapper = _mount(
  407. `<el-menu mode="vertical" default-active="1">
  408. <el-menu-item-group title="分组一" ref="group1">
  409. <el-menu-item index="1">导航一</el-menu-item>
  410. <el-menu-item index="2">导航二</el-menu-item>
  411. </el-menu-item-group>
  412. <el-sub-menu index="5">
  413. <template slot="title">导航五</template>
  414. <el-menu-item-group title="分组二">
  415. <el-menu-item index="5-1">选项1</el-menu-item>
  416. <el-menu-item index="5-2">选项2</el-menu-item>
  417. </el-menu-item-group>
  418. </el-sub-menu>
  419. </el-menu>`
  420. )
  421. const group1 = await wrapper.findComponent({ ref: 'group1' })
  422. expect(
  423. group1.vm.$el.querySelector('.el-menu-item-group__title').innerHTML
  424. ).toEqual('分组一')
  425. })
  426. test('dynamic menus, issue 9092', async () => {
  427. const wrapper = _mount(
  428. `<el-menu :default-active="active">
  429. <el-menu-item
  430. v-for="menu in menus"
  431. :index="menu.name"
  432. :key="menu.name">
  433. {{menu.description}}
  434. </el-menu-item>
  435. </el-menu>`,
  436. {
  437. data() {
  438. return {
  439. active: '',
  440. menus: [],
  441. }
  442. },
  443. }
  444. )
  445. const instance = wrapper.vm as any
  446. instance.menus = [
  447. { name: '1', description: 'happy' },
  448. { name: '2', description: 'new' },
  449. { name: '3', description: 'year' },
  450. ]
  451. await nextTick()
  452. instance.active = '2'
  453. await nextTick()
  454. expect(
  455. instance.$el.querySelector('.el-menu-item.is-active').innerHTML
  456. ).toEqual('new')
  457. })
  458. })