element-plus/packages/dropdown/__tests__/dropdown.spec.tsx

287 lines
9.4 KiB
TypeScript
Raw Normal View History

2020-08-28 11:44:04 +08:00
import { mount } from '@vue/test-utils'
import { EVENT_CODE } from '@element-plus/utils/aria'
2020-09-02 17:44:14 +08:00
import { sleep } from '@element-plus/test-utils'
2020-08-28 11:44:04 +08:00
import Dropdown from '../src/dropdown.vue'
import DropdownItem from '../src/dropdown-item.vue'
import DropdownMenu from '../src/dropdown-menu.vue'
2020-09-03 13:12:44 +08:00
const TIMEOUT = 250
2020-08-28 11:44:04 +08:00
const MOUSE_ENTER_EVENT = 'mouseenter'
const MOUSE_LEAVE_EVENT = 'mouseleave'
const CLICK = 'click'
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 () => {
2020-09-21 15:39:53 +08:00
const wrapper = mount({
setup() {
return () => (
<Dropdown
ref="b"
placement="right"
v-slots = {
{
dropdown: () => (
<DropdownMenu>
<DropdownItem>Apple</DropdownItem>
<DropdownItem>Orange</DropdownItem>
<DropdownItem>Cherry</DropdownItem>
<DropdownItem disabled>Peach</DropdownItem>
<DropdownItem divided>Pear</DropdownItem>
</DropdownMenu>
)
}
}
>
<span class="el-dropdown-link" ref="a">
dropdown<i class="el-icon-arrow-down el-icon--right"></i>
</span>
</Dropdown>
)
}
})
2020-11-04 11:24:36 +08:00
const content = wrapper.findComponent({ ref: 'b' }).vm as any
2020-08-28 11:44:04 +08:00
const triggerElm = wrapper.find('.el-dropdown-link')
expect(content.visible).toBe(false)
2020-11-04 11:24:36 +08:00
await triggerElm.trigger('keydown')
await triggerElm.trigger('focus')
2020-08-28 11:44:04 +08:00
await triggerElm.trigger(MOUSE_ENTER_EVENT)
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
expect(content.visible).toBe(true)
2020-08-28 11:44:04 +08:00
await triggerElm.trigger(MOUSE_LEAVE_EVENT)
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
expect(content.visible).toBe(false)
2020-08-28 11:44:04 +08:00
})
test('menu click', async () => {
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(command) {
this.name = command.name
},
},
},
)
2020-11-04 11:24:36 +08:00
// const content = wrapper.findComponent({ ref: 'b' }).vm as any
2020-08-28 11:44:04 +08:00
const triggerElm = wrapper.find('.el-dropdown-link')
await triggerElm.trigger(MOUSE_ENTER_EVENT)
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
2020-08-28 11:44:04 +08:00
await wrapper.findComponent({ ref: 'c' }).trigger('click')
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
2020-08-28 11:44:04 +08:00
expect((wrapper.vm as any).name).toBe('CommandC')
})
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: '',
}),
)
2020-11-04 11:24:36 +08:00
const content = wrapper.findComponent({ ref: 'b' }).vm as any
2020-08-28 11:44:04 +08:00
const triggerElm = wrapper.find('.el-dropdown-link')
expect(content.visible).toBe(false)
2020-08-28 11:44:04 +08:00
await triggerElm.trigger(MOUSE_ENTER_EVENT)
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
expect(content.visible).toBe(false)
2020-08-28 11:44:04 +08:00
await triggerElm.trigger(CLICK)
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
expect(content.visible).toBe(true)
2020-08-28 11:44:04 +08:00
})
test('split button', async () => {
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() {
this.name = 'click'
},
},
},
)
2020-11-04 11:24:36 +08:00
const content = wrapper.findComponent({ ref: 'b' }).vm as any
2020-08-28 11:44:04 +08:00
const triggerElm = wrapper.find('.el-dropdown__caret-button')
const button = wrapper.find('.el-button')
expect(content.visible).toBe(false)
2020-08-28 11:44:04 +08:00
await button.trigger('click')
expect((wrapper.vm as any).name).toBe('click')
2020-11-04 11:24:36 +08:00
await triggerElm.trigger('keydown')
await triggerElm.trigger('focus')
2020-08-28 11:44:04 +08:00
await triggerElm.trigger(MOUSE_ENTER_EVENT)
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
expect(content.visible).toBe(true)
2020-08-28 11:44:04 +08:00
})
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>
`,
() => ({}),
)
2020-11-04 11:24:36 +08:00
const content = wrapper.findComponent({ ref: 'b' }).vm as any
2020-08-28 11:44:04 +08:00
const triggerElm = wrapper.find('.el-dropdown-link')
2020-11-04 11:24:36 +08:00
await triggerElm.trigger('keydown')
await triggerElm.trigger('focus')
2020-08-28 11:44:04 +08:00
await triggerElm.trigger(MOUSE_ENTER_EVENT)
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
2020-08-28 11:44:04 +08:00
await wrapper.findComponent({ ref: 'c' }).trigger('click')
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
expect(content.visible).toBe(true)
2020-08-28 11:44:04 +08:00
})
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>
`,
() => ({}),
)
2020-11-04 11:24:36 +08:00
const content = wrapper.findComponent({ ref: 'b' }).vm as any
2020-08-28 11:44:04 +08:00
const triggerElm = wrapper.find('.el-dropdown-link')
2020-11-04 11:24:36 +08:00
await triggerElm.trigger('keydown')
await triggerElm.trigger('focus')
2020-08-28 11:44:04 +08:00
await triggerElm.trigger(MOUSE_ENTER_EVENT)
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
2020-08-28 11:44:04 +08:00
await triggerElm.trigger('keydown', {
code: EVENT_CODE.enter,
2020-08-28 11:44:04 +08:00
})
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
expect(content.visible).toBe(false)
2020-08-28 11:44:04 +08:00
await triggerElm.trigger(MOUSE_ENTER_EVENT)
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
2020-08-28 11:44:04 +08:00
await triggerElm.trigger('keydown', {
code: EVENT_CODE.tab,
2020-08-28 11:44:04 +08:00
})
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
expect(content.visible).toBe(false)
2020-08-28 11:44:04 +08:00
})
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>
`,
() => ({}),
)
const content = wrapper.findComponent({ ref: 'a' })
const triggerElm = wrapper.find('.el-dropdown-link')
await triggerElm.trigger(MOUSE_ENTER_EVENT)
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
2020-08-28 11:44:04 +08:00
await content.trigger('keydown', {
code: EVENT_CODE.down,
2020-08-28 11:44:04 +08:00
})
2020-09-03 13:12:44 +08:00
await sleep(TIMEOUT)
2020-08-28 11:44:04 +08:00
expect(wrapper.findComponent({ ref: 'd' }).attributes('tabindex')).toBe('0')
})
})