element-plus/packages/components/dropdown/__tests__/dropdown.spec.ts
jeremywu dd19cae2bc
refactor(components): popper composables (#5035)
* refactor(components): popper composables

- Refactor popper composables

* updates

* updates for tooltip

* Updates for popper. TODO: fix controlled tooltip animation

* Fix controlled mode popper animation issue

* Add new feature for customizing tooltip theme

* Fix popover and popconfirm error

* - Add Collection component for wrapping a collection of component
- Add FocusTrap component for trap focus for popups
- Add RovingFocus component for roving focus component type
- Adjust dropdown component based on these newly added components
- Add popper-trigger component for placing the trigger
- TODO: Finish current dropdown component, and all component's tests plus documents

* Refactor popper

* Complete organizing popper

* Almost finish dropdown

* Update popper tests

* update only-child test

* Finish focus trap component test

* Finish tooltip content test

* Finish tooltip trigger tests

* Finish tooltip tests

* finish tests for Collection and RovingFocusGroup

* Fix test cases for timeselect & select & popover

* Fix popover, popconfirm, menu bug and test cases

* Fix select-v2 test error caused by updating popper

* Fix date-picker test issue for updating popper

* fix test cases

* Fix eslint

* Rebase dev & fix tests

* Remove unused code
2022-01-04 09:15:15 +08:00

452 lines
14 KiB
TypeScript

import { nextTick } from 'vue'
import { mount } from '@vue/test-utils'
import { rAF } from '@element-plus/test-utils/tick'
import { EVENT_CODE } from '@element-plus/utils/aria'
import { ElTooltip } from '@element-plus/components/tooltip'
import Dropdown from '../src/dropdown.vue'
import DropdownItem from '../src/dropdown-item.vue'
import DropdownMenu from '../src/dropdown-menu.vue'
const MOUSE_ENTER_EVENT = 'mouseenter'
const MOUSE_LEAVE_EVENT = 'mouseleave'
const MOUSE_DOWN = 'mousedown'
const CONTEXTMENU = 'contextmenu'
jest.useFakeTimers()
const _mount = (template: string, data, otherObj?) =>
mount({
components: {
[Dropdown.name]: Dropdown,
[DropdownItem.name]: DropdownItem,
[DropdownMenu.name]: DropdownMenu,
},
template,
data,
...otherObj,
})
describe('Dropdown', () => {
test('create', async () => {
const wrapper = _mount(
`
<el-dropdown ref="b" placement="right">
<span class="el-dropdown-link" ref="a">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item>Apple</el-dropdown-item>
<el-dropdown-item>Orange</el-dropdown-item>
<el-dropdown-item>Cherry</el-dropdown-item>
<el-dropdown-item disabled>Peach</el-dropdown-item>
<el-dropdown-item divided>Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({})
)
await nextTick()
const content = wrapper.findComponent(ElTooltip).vm as InstanceType<
typeof ElTooltip
>
const triggerElm = wrapper.find('.el-tooltip__trigger')
expect(content.open).toBe(false)
await triggerElm.trigger(MOUSE_ENTER_EVENT)
jest.runAllTimers()
await rAF()
expect(content.open).toBe(true)
await triggerElm.trigger(MOUSE_LEAVE_EVENT)
jest.runAllTimers()
await rAF()
expect(content.open).toBe(false)
})
test('menu click', async () => {
const commandHandler = jest.fn()
const wrapper = _mount(
`
<el-dropdown ref="b" @command="commandHandler" placement="right">
<span class="el-dropdown-link" ref="a">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item command="a">Apple</el-dropdown-item>
<el-dropdown-item command="b">Orange</el-dropdown-item>
<el-dropdown-item ref="c" :command="myCommandObject">Cherry</el-dropdown-item>
<el-dropdown-item command="d">Peach</el-dropdown-item>
<el-dropdown-item command="e">Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({
myCommandObject: { name: 'CommandC' },
name: '',
}),
{
methods: {
commandHandler,
},
}
)
await nextTick()
// const content = wrapper.findComponent({ ref: 'b' }).vm as any
const triggerElm = wrapper.find('.el-tooltip__trigger')
await triggerElm.trigger(MOUSE_ENTER_EVENT)
await nextTick()
jest.runAllTimers()
await rAF()
await wrapper
.findComponent({ ref: 'c' })
.findComponent({
name: 'DropdownItemImpl',
})
.trigger('click')
await nextTick()
jest.runAllTimers()
await rAF()
expect(commandHandler).toHaveBeenCalled()
})
test('trigger', async () => {
const wrapper = _mount(
`
<el-dropdown trigger="click" ref="b" placement="right">
<span class="el-dropdown-link" ref="a">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item command="a">Apple</el-dropdown-item>
<el-dropdown-item command="b">Orange</el-dropdown-item>
<el-dropdown-item ref="c" :command="myCommandObject">Cherry</el-dropdown-item>
<el-dropdown-item command="d">Peach</el-dropdown-item>
<el-dropdown-item command="e">Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({
myCommandObject: { name: 'CommandC' },
name: '',
})
)
await nextTick()
const content = wrapper.findComponent(ElTooltip).vm as InstanceType<
typeof ElTooltip
>
const triggerElm = wrapper.find('.el-dropdown-link')
expect(content.open).toBe(false)
await triggerElm.trigger(MOUSE_ENTER_EVENT)
jest.runAllTimers()
await rAF()
expect(content.open).toBe(false)
await triggerElm.trigger(MOUSE_DOWN, {
button: 0,
})
jest.runAllTimers()
await rAF()
expect(content.open).toBe(true)
})
test('trigger contextmenu', async () => {
const wrapper = _mount(
`
<el-dropdown trigger="contextmenu" ref="b" placement="right">
<span class="el-dropdown-link" ref="a">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item command="a">Apple</el-dropdown-item>
<el-dropdown-item command="b">Orange</el-dropdown-item>
<el-dropdown-item ref="c" :command="myCommandObject">Cherry</el-dropdown-item>
<el-dropdown-item command="d">Peach</el-dropdown-item>
<el-dropdown-item command="e">Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({
myCommandObject: { name: 'CommandC' },
name: '',
})
)
await nextTick()
const content = wrapper.findComponent(ElTooltip).vm as InstanceType<
typeof ElTooltip
>
const triggerElm = wrapper.find('.el-dropdown-link')
expect(content.open).toBe(false)
await triggerElm.trigger(CONTEXTMENU)
jest.runAllTimers()
await rAF()
expect(content.open).toBe(true)
})
test('split button', async () => {
const handleClick = jest.fn()
const wrapper = _mount(
`
<el-dropdown @click="handleClick" split-button type="primary" ref="b" placement="right">
dropdown
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item command="a">Apple</el-dropdown-item>
<el-dropdown-item command="b">Orange</el-dropdown-item>
<el-dropdown-item ref="c" :command="myCommandObject">Cherry</el-dropdown-item>
<el-dropdown-item command="d">Peach</el-dropdown-item>
<el-dropdown-item command="e">Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({
myCommandObject: { name: 'CommandC' },
name: '',
}),
{
methods: {
handleClick,
},
}
)
await nextTick()
const content = wrapper.findComponent(ElTooltip).vm as InstanceType<
typeof ElTooltip
>
const triggerElm = wrapper.find('.el-dropdown__caret-button')
const button = wrapper.find('.el-button')
expect(content.open).toBe(false)
await button.trigger('click')
expect(handleClick).toHaveBeenCalled()
await triggerElm.trigger(MOUSE_ENTER_EVENT)
jest.runAllTimers()
await rAF()
expect(content.open).toBe(true)
})
test('hide on click', async () => {
const wrapper = _mount(
`
<el-dropdown ref="b" placement="right" :hide-on-click="false">
<span class="el-dropdown-link" ref="a">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item>Apple</el-dropdown-item>
<el-dropdown-item>Orange</el-dropdown-item>
<el-dropdown-item ref="c">Cherry</el-dropdown-item>
<el-dropdown-item disabled>Peach</el-dropdown-item>
<el-dropdown-item divided>Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({})
)
await nextTick()
const content = wrapper.findComponent(ElTooltip).vm as InstanceType<
typeof ElTooltip
>
expect(content.open).toBe(false)
const triggerElm = wrapper.find('.el-tooltip__trigger')
await triggerElm.trigger(MOUSE_ENTER_EVENT)
jest.runAllTimers()
await rAF()
expect(content.open).toBe(true)
await wrapper
.findComponent({ ref: 'c' })
.findComponent({
name: 'DropdownItemImpl',
})
.trigger('click')
jest.runAllTimers()
await rAF()
expect(content.open).toBe(true)
})
test('triggerElm keydown', async () => {
const wrapper = _mount(
`
<el-dropdown ref="b" placement="right" :hide-on-click="false">
<span class="el-dropdown-link" ref="a">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item>Apple</el-dropdown-item>
<el-dropdown-item>Orange</el-dropdown-item>
<el-dropdown-item ref="c">Cherry</el-dropdown-item>
<el-dropdown-item disabled>Peach</el-dropdown-item>
<el-dropdown-item divided>Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({})
)
await nextTick()
const content = wrapper.findComponent(ElTooltip).vm as InstanceType<
typeof ElTooltip
>
const triggerElm = wrapper.find('.el-tooltip__trigger')
await triggerElm.trigger(MOUSE_ENTER_EVENT)
jest.runAllTimers()
await rAF()
await triggerElm.trigger('keydown', {
code: EVENT_CODE.enter,
})
jest.runAllTimers()
await rAF()
expect(content.open).toBe(false)
await triggerElm.trigger(MOUSE_ENTER_EVENT)
jest.runAllTimers()
await rAF()
await triggerElm.trigger('keydown', {
code: EVENT_CODE.tab,
})
jest.runAllTimers()
await rAF()
expect(content.open).toBe(true)
})
test('dropdown menu keydown', async () => {
const wrapper = _mount(
`
<el-dropdown ref="b" placement="right" :hide-on-click="false">
<span class="el-dropdown-link" ref="a">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu ref="a">
<el-dropdown-item ref="d">Apple</el-dropdown-item>
<el-dropdown-item>Orange</el-dropdown-item>
<el-dropdown-item ref="c">Cherry</el-dropdown-item>
<el-dropdown-item disabled>Peach</el-dropdown-item>
<el-dropdown-item divided>Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({})
)
await nextTick()
const content = wrapper.findComponent({ ref: 'a' })
const triggerElm = wrapper.find('.el-tooltip__trigger')
await triggerElm.trigger(MOUSE_ENTER_EVENT)
jest.runAllTimers()
await rAF()
await content.trigger('keydown', {
code: EVENT_CODE.down,
})
jest.runAllTimers()
await rAF()
expect(
wrapper
.findComponent({ ref: 'd' })
.findComponent({
name: 'DropdownItemImpl',
})
.element.getAttribute('tabindex')
).toBe('0')
})
test('max height', async () => {
const wrapper = _mount(
`
<el-dropdown ref="b" max-height="60px">
<span class="el-dropdown-link" ref="a">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item>Apple</el-dropdown-item>
<el-dropdown-item>Orange</el-dropdown-item>
<el-dropdown-item>Cherry</el-dropdown-item>
<el-dropdown-item disabled>Peach</el-dropdown-item>
<el-dropdown-item divided>Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({})
)
await nextTick()
const scrollbar = wrapper
.findComponent({
ref: 'b',
})
.findComponent({ ref: 'scrollbar' })
expect(scrollbar.find('.el-scrollbar__wrap').attributes('style')).toContain(
'max-height: 60px;'
)
})
test('tooltip debounce', async () => {
const wrapper = _mount(
`
<el-dropdown ref="b">
<span class="el-dropdown-link">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item>Apple</el-dropdown-item>
<el-dropdown-item>Orange</el-dropdown-item>
<el-dropdown-item>Cherry</el-dropdown-item>
<el-dropdown-item>Peach</el-dropdown-item>
<el-dropdown-item>Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({})
)
const content = wrapper.findComponent(ElTooltip).vm as InstanceType<
typeof ElTooltip
>
const triggerElm = wrapper.find('.el-tooltip__trigger')
expect(content.open).toBe(false)
await triggerElm.trigger(MOUSE_ENTER_EVENT)
await triggerElm.trigger(MOUSE_LEAVE_EVENT)
await triggerElm.trigger(MOUSE_ENTER_EVENT)
jest.runAllTimers()
await rAF()
expect(content.open).toBe(true)
})
test('popperClass', async () => {
const wrapper = await _mount(
`
<el-dropdown ref="b" max-height="60px" popper-class="custom-popper-class">
<span class="el-dropdown-link" ref="a">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item>Apple</el-dropdown-item>
<el-dropdown-item>Orange</el-dropdown-item>
<el-dropdown-item>Cherry</el-dropdown-item>
<el-dropdown-item disabled>Peach</el-dropdown-item>
<el-dropdown-item divided>Pear</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({})
)
const popperElement = wrapper.findComponent({
name: 'ElPopperContent',
}).element
expect(popperElement.classList.contains('custom-popper-class')).toBe(true)
})
})