2020-10-30 23:26:33 +08:00
|
|
|
import { nextTick } from 'vue'
|
|
|
|
import { mount } from '@vue/test-utils'
|
2021-10-31 00:25:24 +08:00
|
|
|
import { EVENT_CODE } from '@element-plus/utils/aria'
|
|
|
|
import { triggerEvent } from '@element-plus/test-utils'
|
2021-12-04 11:20:06 +08:00
|
|
|
import { ArrowDown, Check, CircleClose } from '@element-plus/icons-vue'
|
2020-10-30 23:26:33 +08:00
|
|
|
import Cascader from '../src/index.vue'
|
|
|
|
|
|
|
|
const OPTIONS = [
|
|
|
|
{
|
|
|
|
value: 'zhejiang',
|
|
|
|
label: 'Zhejiang',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
value: 'hangzhou',
|
|
|
|
label: 'Hangzhou',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'ningbo',
|
|
|
|
label: 'Ningbo',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
|
|
|
const TRIGGER = '.el-cascader'
|
|
|
|
const NODE = '.el-cascader-node'
|
|
|
|
const TAG = '.el-tag'
|
|
|
|
const SUGGESTION_ITEM = '.el-cascader__suggestion-item'
|
2021-10-31 00:25:24 +08:00
|
|
|
const SUGGESTION_PANEL = '.el-cascader__suggestion-panel'
|
|
|
|
const DROPDOWN = '.el-cascader__dropdown'
|
2020-10-30 23:26:33 +08:00
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const _mount: typeof mount = (options) =>
|
|
|
|
mount(
|
|
|
|
{
|
|
|
|
components: {
|
|
|
|
Cascader,
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
attachTo: 'body',
|
|
|
|
}
|
|
|
|
)
|
2020-10-30 23:26:33 +08:00
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
document.body.innerHTML = ''
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Cascader.vue', () => {
|
|
|
|
test('toggle popper visible', async () => {
|
|
|
|
const handleVisibleChange = jest.fn()
|
|
|
|
const wrapper = _mount({
|
|
|
|
template: `
|
|
|
|
<cascader @visible-change="handleVisibleChange" />
|
|
|
|
`,
|
|
|
|
methods: {
|
|
|
|
handleVisibleChange,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const trigger = wrapper.find(TRIGGER)
|
2021-10-27 23:17:13 +08:00
|
|
|
const dropdown = wrapper.findComponent(ArrowDown).element as HTMLDivElement
|
2020-10-30 23:26:33 +08:00
|
|
|
|
|
|
|
await trigger.trigger('click')
|
|
|
|
expect(dropdown.style.display).not.toBe('none')
|
|
|
|
expect(handleVisibleChange).toBeCalledWith(true)
|
|
|
|
await trigger.trigger('click')
|
|
|
|
expect(handleVisibleChange).toBeCalledWith(false)
|
|
|
|
await trigger.trigger('click')
|
|
|
|
document.body.click()
|
|
|
|
expect(handleVisibleChange).toBeCalledWith(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('expand and check', async () => {
|
|
|
|
const handleChange = jest.fn()
|
|
|
|
const handleExpandChange = jest.fn()
|
|
|
|
const wrapper = _mount({
|
|
|
|
template: `
|
|
|
|
<cascader
|
|
|
|
v-model="value"
|
|
|
|
:options="options"
|
|
|
|
@change="handleChange"
|
|
|
|
@expand-change="handleExpandChange"
|
|
|
|
/>
|
|
|
|
`,
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2020-10-30 23:26:33 +08:00
|
|
|
return {
|
|
|
|
value: [],
|
|
|
|
options: OPTIONS,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
handleChange,
|
|
|
|
handleExpandChange,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const trigger = wrapper.find(TRIGGER)
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
|
|
|
|
await trigger.trigger('click')
|
2021-10-27 23:17:13 +08:00
|
|
|
;(document.querySelector(NODE) as HTMLElement).click()
|
2020-10-30 23:26:33 +08:00
|
|
|
await nextTick()
|
|
|
|
expect(handleExpandChange).toBeCalledWith(['zhejiang'])
|
2021-10-27 23:17:13 +08:00
|
|
|
;(document.querySelectorAll(NODE)[1] as HTMLElement).click()
|
2020-10-30 23:26:33 +08:00
|
|
|
await nextTick()
|
|
|
|
expect(handleChange).toBeCalledWith(['zhejiang', 'hangzhou'])
|
|
|
|
expect(vm.value).toEqual(['zhejiang', 'hangzhou'])
|
|
|
|
expect(wrapper.find('input').element.value).toBe('Zhejiang / Hangzhou')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('with default value', async () => {
|
|
|
|
const wrapper = mount(Cascader, {
|
|
|
|
props: {
|
|
|
|
options: OPTIONS,
|
|
|
|
modelValue: ['zhejiang', 'hangzhou'],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.find('input').element.value).toBe('Zhejiang / Hangzhou')
|
|
|
|
await wrapper.setProps({ modelValue: ['zhejiang', 'ningbo'] })
|
|
|
|
expect(wrapper.find('input').element.value).toBe('Zhejiang / Ningbo')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('options change', async () => {
|
|
|
|
const wrapper = mount(Cascader, {
|
|
|
|
props: {
|
|
|
|
options: OPTIONS,
|
|
|
|
modelValue: ['zhejiang', 'hangzhou'],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await wrapper.setProps({ options: [] })
|
|
|
|
expect(wrapper.find('input').element.value).toBe('')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('disabled', async () => {
|
|
|
|
const handleVisibleChange = jest.fn()
|
|
|
|
const wrapper = _mount({
|
|
|
|
template: `
|
|
|
|
<cascader disabled @visible-change="handleVisibleChange" />
|
|
|
|
`,
|
|
|
|
methods: {
|
|
|
|
handleVisibleChange,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await wrapper.find(TRIGGER).trigger('click')
|
|
|
|
expect(handleVisibleChange).not.toBeCalled()
|
|
|
|
expect(wrapper.find('input[disabled]').exists()).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('custom placeholder', async () => {
|
|
|
|
const wrapper = mount(Cascader, {
|
|
|
|
props: {
|
|
|
|
placeholder: AXIOM,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(wrapper.find('input').attributes().placeholder).toBe(AXIOM)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('clearable', async () => {
|
|
|
|
const wrapper = mount(Cascader, {
|
|
|
|
props: {
|
|
|
|
options: OPTIONS,
|
|
|
|
clearable: true,
|
|
|
|
modelValue: ['zhejiang', 'hangzhou'],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const trigger = wrapper.find(TRIGGER)
|
2021-10-27 23:17:13 +08:00
|
|
|
expect(wrapper.findComponent(ArrowDown).exists()).toBe(true)
|
2020-10-30 23:26:33 +08:00
|
|
|
await trigger.trigger('mouseenter')
|
2021-10-27 23:17:13 +08:00
|
|
|
expect(wrapper.findComponent(ArrowDown).exists()).toBe(false)
|
|
|
|
await wrapper.findComponent(CircleClose).trigger('click')
|
2020-10-30 23:26:33 +08:00
|
|
|
expect(wrapper.find('input').element.value).toBe('')
|
|
|
|
expect((wrapper.vm as any).getCheckedNodes().length).toBe(0)
|
|
|
|
await trigger.trigger('mouseleave')
|
|
|
|
await trigger.trigger('mouseenter')
|
2021-10-27 23:17:13 +08:00
|
|
|
await expect(wrapper.findComponent(CircleClose).exists()).toBe(false)
|
2020-10-30 23:26:33 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('show last level label', async () => {
|
|
|
|
const wrapper = mount(Cascader, {
|
|
|
|
props: {
|
|
|
|
options: OPTIONS,
|
|
|
|
showAllLevels: false,
|
|
|
|
modelValue: ['zhejiang', 'hangzhou'],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.find('input').element.value).toBe('Hangzhou')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('multiple mode', async () => {
|
|
|
|
const wrapper = _mount({
|
|
|
|
template: `
|
|
|
|
<cascader
|
|
|
|
v-model="value"
|
|
|
|
:options="options"
|
|
|
|
:props="props"
|
|
|
|
/>
|
|
|
|
`,
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2020-10-30 23:26:33 +08:00
|
|
|
return {
|
|
|
|
options: OPTIONS,
|
|
|
|
props: { multiple: true },
|
2021-09-04 19:29:28 +08:00
|
|
|
value: [
|
|
|
|
['zhejiang', 'hangzhou'],
|
|
|
|
['zhejiang', 'ningbo'],
|
|
|
|
],
|
2020-10-30 23:26:33 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
const tags = wrapper.findAll(TAG)
|
|
|
|
const [firstTag, secondTag] = tags
|
|
|
|
expect(tags.length).toBe(2)
|
|
|
|
expect(firstTag.text()).toBe('Zhejiang / Hangzhou')
|
|
|
|
expect(secondTag.text()).toBe('Zhejiang / Ningbo')
|
|
|
|
await firstTag.find('.el-tag__close').trigger('click')
|
|
|
|
expect(wrapper.findAll(TAG).length).toBe(1)
|
|
|
|
expect(wrapper.vm.value).toEqual([['zhejiang', 'ningbo']])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('collapse tags', async () => {
|
|
|
|
const wrapper = mount(Cascader, {
|
|
|
|
props: {
|
|
|
|
options: OPTIONS,
|
|
|
|
props: { multiple: true },
|
|
|
|
collapseTags: true,
|
2021-09-04 19:29:28 +08:00
|
|
|
modelValue: [
|
|
|
|
['zhejiang', 'hangzhou'],
|
|
|
|
['zhejiang', 'ningbo'],
|
|
|
|
],
|
2020-10-30 23:26:33 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
const [firstTag, secondTag] = wrapper.findAll(TAG)
|
|
|
|
expect(firstTag.text()).toBe('Zhejiang / Hangzhou')
|
|
|
|
expect(secondTag.text()).toBe('+ 1')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('filterable', async () => {
|
|
|
|
const wrapper = _mount({
|
|
|
|
template: `
|
|
|
|
<cascader
|
|
|
|
v-model="value"
|
|
|
|
:options="options"
|
|
|
|
filterable
|
|
|
|
/>
|
|
|
|
`,
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2020-10-30 23:26:33 +08:00
|
|
|
return {
|
|
|
|
options: OPTIONS,
|
|
|
|
value: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const input = wrapper.find('input')
|
2021-10-01 02:30:31 +08:00
|
|
|
input.element.value = 'Ni'
|
|
|
|
await input.trigger('compositionstart')
|
|
|
|
await input.trigger('input')
|
2020-10-30 23:26:33 +08:00
|
|
|
input.element.value = 'Ha'
|
2021-10-01 02:30:31 +08:00
|
|
|
await input.trigger('compositionupdate')
|
2020-10-30 23:26:33 +08:00
|
|
|
await input.trigger('input')
|
2021-10-01 02:30:31 +08:00
|
|
|
await input.trigger('compositionend')
|
2021-10-27 23:17:13 +08:00
|
|
|
const suggestions = document.querySelectorAll(
|
2021-09-04 19:29:28 +08:00
|
|
|
SUGGESTION_ITEM
|
|
|
|
) as NodeListOf<HTMLElement>
|
2020-10-30 23:26:33 +08:00
|
|
|
const hzSuggestion = suggestions[0]
|
|
|
|
expect(suggestions.length).toBe(1)
|
|
|
|
expect(hzSuggestion.textContent).toBe('Zhejiang / Hangzhou')
|
|
|
|
hzSuggestion.click()
|
|
|
|
await nextTick()
|
2021-10-27 23:17:13 +08:00
|
|
|
expect(wrapper.findComponent(Check).exists()).toBeTruthy()
|
2020-10-30 23:26:33 +08:00
|
|
|
expect(wrapper.vm.value).toEqual(['zhejiang', 'hangzhou'])
|
|
|
|
hzSuggestion.click()
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.vm.value).toEqual(['zhejiang', 'hangzhou'])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('filterable in multiple mode', async () => {
|
|
|
|
const wrapper = _mount({
|
|
|
|
template: `
|
|
|
|
<cascader
|
|
|
|
v-model="value"
|
|
|
|
:options="options"
|
|
|
|
:props="props"
|
|
|
|
filterable
|
|
|
|
/>
|
|
|
|
`,
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2020-10-30 23:26:33 +08:00
|
|
|
return {
|
|
|
|
options: OPTIONS,
|
|
|
|
props: { multiple: true },
|
|
|
|
value: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const input = wrapper.find('.el-cascader__search-input')
|
|
|
|
;(input.element as HTMLInputElement).value = 'Ha'
|
|
|
|
await input.trigger('input')
|
|
|
|
await nextTick()
|
2021-10-27 23:17:13 +08:00
|
|
|
const hzSuggestion = document.querySelector(SUGGESTION_ITEM) as HTMLElement
|
2020-10-30 23:26:33 +08:00
|
|
|
hzSuggestion.click()
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.vm.value).toEqual([['zhejiang', 'hangzhou']])
|
|
|
|
hzSuggestion.click()
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.vm.value).toEqual([])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('filter method', async () => {
|
|
|
|
const filterMethod = jest.fn((node, keyword) => {
|
|
|
|
const { text, value } = node
|
|
|
|
return text.includes(keyword) || value.includes(keyword)
|
|
|
|
})
|
|
|
|
const wrapper = mount(Cascader, {
|
|
|
|
props: {
|
|
|
|
options: OPTIONS,
|
|
|
|
filterable: true,
|
|
|
|
filterMethod,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.element.value = 'ha'
|
|
|
|
await input.trigger('input')
|
2021-10-27 23:17:13 +08:00
|
|
|
const hzSuggestion = document.querySelector(SUGGESTION_ITEM) as HTMLElement
|
2020-10-30 23:26:33 +08:00
|
|
|
expect(filterMethod).toBeCalled()
|
2020-12-05 12:30:15 +08:00
|
|
|
expect(hzSuggestion.textContent).toBe('Zhejiang / Hangzhou')
|
2020-10-30 23:26:33 +08:00
|
|
|
})
|
2021-10-31 00:25:24 +08:00
|
|
|
|
|
|
|
test('filterable keyboard selection', async () => {
|
|
|
|
const wrapper = _mount({
|
|
|
|
template: `
|
|
|
|
<cascader
|
|
|
|
v-model="value"
|
|
|
|
:options="options"
|
|
|
|
filterable
|
|
|
|
/>
|
|
|
|
`,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
options: OPTIONS,
|
|
|
|
value: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
const dropdown = document.querySelector(DROPDOWN)
|
|
|
|
input.element.value = 'h'
|
|
|
|
await input.trigger('input')
|
|
|
|
const suggestionsPanel = document.querySelector(
|
|
|
|
SUGGESTION_PANEL
|
|
|
|
) as HTMLDivElement
|
|
|
|
const suggestions = dropdown.querySelectorAll(
|
|
|
|
SUGGESTION_ITEM
|
|
|
|
) as NodeListOf<HTMLElement>
|
|
|
|
const hzSuggestion = suggestions[0]
|
|
|
|
triggerEvent(suggestionsPanel, 'keydown', EVENT_CODE.down)
|
|
|
|
expect(document.activeElement.textContent).toBe('Zhejiang / Hangzhou')
|
|
|
|
triggerEvent(hzSuggestion, 'keydown', EVENT_CODE.down)
|
|
|
|
expect(document.activeElement.textContent).toBe('Zhejiang / Ningbo')
|
|
|
|
triggerEvent(hzSuggestion, 'keydown', EVENT_CODE.enter)
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.vm.value).toEqual(['zhejiang', 'hangzhou'])
|
|
|
|
})
|
2020-10-30 23:26:33 +08:00
|
|
|
})
|