2021-07-23 17:06:01 +08:00
|
|
|
import { nextTick } from 'vue'
|
|
|
|
import { NOOP } from '@vue/shared'
|
2021-07-28 20:15:22 +08:00
|
|
|
import { EVENT_CODE } from '@element-plus/utils/aria'
|
2021-08-24 13:36:48 +08:00
|
|
|
import { makeMountFunc } from '@element-plus/test-utils/make-mount'
|
2022-01-04 09:15:15 +08:00
|
|
|
import { rAF } from '@element-plus/test-utils/tick'
|
2021-12-04 11:20:06 +08:00
|
|
|
import { CircleClose } from '@element-plus/icons-vue'
|
2021-11-04 14:37:40 +08:00
|
|
|
import { hasClass } from '@element-plus/utils/dom'
|
2021-07-28 20:15:22 +08:00
|
|
|
import Select from '../src/select.vue'
|
2021-07-23 17:06:01 +08:00
|
|
|
|
|
|
|
jest.useFakeTimers()
|
|
|
|
|
|
|
|
const _mount = makeMountFunc({
|
|
|
|
components: {
|
|
|
|
'el-select': Select,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const createData = (count = 1000) => {
|
|
|
|
const initials = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
|
|
|
|
return Array.from({ length: count }).map((_, idx) => ({
|
|
|
|
value: `option_${idx + 1}`,
|
|
|
|
label: `${initials[idx % 10]}${idx}`,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const clickClearButton = async (wrapper) => {
|
2021-08-25 17:23:13 +08:00
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
const selectVm = select.vm as any
|
|
|
|
selectVm.states.comboBoxHovering = true
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-10-27 23:17:13 +08:00
|
|
|
const clearBtn = wrapper.findComponent(CircleClose)
|
2021-08-25 17:23:13 +08:00
|
|
|
expect(clearBtn.exists()).toBeTruthy()
|
|
|
|
await clearBtn.trigger('click')
|
|
|
|
}
|
|
|
|
|
2021-07-23 17:06:01 +08:00
|
|
|
interface SelectProps {
|
|
|
|
popperClass?: string
|
2021-07-28 20:15:22 +08:00
|
|
|
value?: string | string[] | number | number[]
|
2021-07-23 17:06:01 +08:00
|
|
|
options?: any[]
|
|
|
|
disabled?: boolean
|
|
|
|
clearable?: boolean
|
|
|
|
multiple?: boolean
|
2021-07-24 23:51:53 +08:00
|
|
|
filterable?: boolean
|
2021-08-29 09:46:38 +08:00
|
|
|
remote?: boolean
|
2021-07-23 17:06:01 +08:00
|
|
|
multipleLimit?: number
|
2021-08-25 17:23:13 +08:00
|
|
|
allowCreate?: boolean
|
2021-07-28 20:15:22 +08:00
|
|
|
popperAppendToBody?: boolean
|
|
|
|
placeholder?: string
|
2021-07-23 17:06:01 +08:00
|
|
|
[key: string]: any
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SelectEvents {
|
|
|
|
onChange?: (value?: string) => void
|
|
|
|
onVisibleChange?: (visible?: boolean) => void
|
|
|
|
onRemoveTag?: (tag?: string) => void
|
2021-07-24 23:51:53 +08:00
|
|
|
onFocus?: (event?: FocusEvent) => void
|
|
|
|
onBlur?: (event?) => void
|
2021-08-29 09:46:38 +08:00
|
|
|
filterMethod?: (query: string) => void | undefined
|
|
|
|
remoteMethod?: (query: string) => void | undefined
|
2021-07-23 17:06:01 +08:00
|
|
|
[key: string]: (...args) => any
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const createSelect = (
|
|
|
|
options: {
|
|
|
|
data?: () => SelectProps
|
|
|
|
methods?: SelectEvents
|
|
|
|
slots?: {
|
|
|
|
empty?: string
|
|
|
|
default?: string
|
|
|
|
}
|
|
|
|
} = {}
|
|
|
|
) => {
|
|
|
|
const emptySlot =
|
|
|
|
(options.slots &&
|
|
|
|
options.slots.empty &&
|
|
|
|
`<template #empty>${options.slots.empty}</template>`) ||
|
|
|
|
''
|
|
|
|
const defaultSlot =
|
|
|
|
(options.slots &&
|
|
|
|
options.slots.default &&
|
|
|
|
`<template #default="{item}">${options.slots.default}</template>`) ||
|
|
|
|
''
|
|
|
|
return _mount(
|
|
|
|
`
|
2021-07-23 17:06:01 +08:00
|
|
|
<el-select
|
|
|
|
:options="options"
|
|
|
|
:popper-class="popperClass"
|
2021-10-20 11:24:09 +08:00
|
|
|
:value-key="valueKey"
|
2021-07-23 17:06:01 +08:00
|
|
|
:disabled="disabled"
|
|
|
|
:clearable="clearable"
|
|
|
|
:multiple="multiple"
|
2021-07-24 23:51:53 +08:00
|
|
|
:filterable="filterable"
|
2021-07-23 17:06:01 +08:00
|
|
|
:multiple-limit="multipleLimit"
|
2021-07-28 20:15:22 +08:00
|
|
|
:popper-append-to-body="popperAppendToBody"
|
|
|
|
:placeholder="placeholder"
|
2021-08-25 17:23:13 +08:00
|
|
|
:allow-create="allowCreate"
|
2021-08-29 09:46:38 +08:00
|
|
|
:remote="remote"
|
2022-01-18 11:24:54 +08:00
|
|
|
:reserve-keyword="reserveKeyword"
|
2021-11-04 14:37:40 +08:00
|
|
|
:scrollbar-always-on="scrollbarAlwaysOn"
|
2021-09-04 19:29:28 +08:00
|
|
|
${
|
|
|
|
options.methods && options.methods.filterMethod
|
|
|
|
? `:filter-method="filterMethod"`
|
|
|
|
: ''
|
|
|
|
}
|
|
|
|
${
|
|
|
|
options.methods && options.methods.remoteMethod
|
|
|
|
? `:remote-method="remoteMethod"`
|
|
|
|
: ''
|
|
|
|
}
|
2021-07-23 17:06:01 +08:00
|
|
|
@change="onChange"
|
|
|
|
@visible-change="onVisibleChange"
|
|
|
|
@remove-tah="onRemoveTag"
|
2021-07-24 23:51:53 +08:00
|
|
|
@focus="onFocus"
|
|
|
|
@blur="onBlur"
|
2021-07-28 20:15:22 +08:00
|
|
|
v-model="value">
|
|
|
|
${defaultSlot}
|
|
|
|
${emptySlot}
|
|
|
|
</el-select>
|
2021-09-04 19:29:28 +08:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
options: createData(),
|
|
|
|
value: '',
|
|
|
|
popperClass: '',
|
|
|
|
allowCreate: false,
|
2021-10-20 11:24:09 +08:00
|
|
|
valueKey: 'value',
|
2021-09-04 19:29:28 +08:00
|
|
|
disabled: false,
|
|
|
|
clearable: false,
|
|
|
|
multiple: false,
|
|
|
|
remote: false,
|
|
|
|
filterable: false,
|
2022-01-18 11:24:54 +08:00
|
|
|
reserveKeyword: false,
|
2021-09-04 19:29:28 +08:00
|
|
|
multipleLimit: 0,
|
|
|
|
popperAppendToBody: true,
|
|
|
|
placeholder: DEFAULT_PLACEHOLDER,
|
2021-11-04 14:37:40 +08:00
|
|
|
scrollbarAlwaysOn: false,
|
2021-09-04 19:29:28 +08:00
|
|
|
...(options.data && options.data()),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onChange: NOOP,
|
|
|
|
onVisibleChange: NOOP,
|
|
|
|
onRemoveTag: NOOP,
|
|
|
|
onFocus: NOOP,
|
|
|
|
onBlur: NOOP,
|
|
|
|
...options.methods,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2021-07-23 17:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function getOptions(): HTMLElement[] {
|
2021-09-04 19:29:28 +08:00
|
|
|
return Array.from(
|
|
|
|
document.querySelectorAll<HTMLElement>(`.${OPTION_ITEM_CLASS_NAME}`)
|
|
|
|
)
|
2021-07-23 17:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const CLASS_NAME = 'el-select-v2'
|
|
|
|
const WRAPPER_CLASS_NAME = 'el-select-v2__wrapper'
|
|
|
|
const OPTION_ITEM_CLASS_NAME = 'el-select-dropdown__option-item'
|
|
|
|
const PLACEHOLDER_CLASS_NAME = 'el-select-v2__placeholder'
|
|
|
|
const DEFAULT_PLACEHOLDER = 'Select'
|
|
|
|
|
|
|
|
describe('Select', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
document.body.innerHTML = ''
|
|
|
|
})
|
|
|
|
|
|
|
|
it('create', async () => {
|
|
|
|
const wrapper = createSelect()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(wrapper.classes()).toContain(CLASS_NAME)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toContain(
|
|
|
|
DEFAULT_PLACEHOLDER
|
|
|
|
)
|
2021-07-23 17:06:01 +08:00
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
await wrapper.trigger('click')
|
|
|
|
expect((select.vm as any).expanded).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('options rendered correctly', async () => {
|
2021-07-23 17:06:01 +08:00
|
|
|
const wrapper = createSelect()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const options = document.getElementsByClassName(OPTION_ITEM_CLASS_NAME)
|
|
|
|
const result = [].every.call(options, (option, index) => {
|
|
|
|
const text = option.textContent
|
|
|
|
return text === vm.options[index].label
|
|
|
|
})
|
|
|
|
expect(result).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it('custom dropdown class', async () => {
|
2021-07-23 17:06:01 +08:00
|
|
|
createSelect({
|
|
|
|
data: () => ({
|
|
|
|
popperClass: 'custom-dropdown',
|
|
|
|
}),
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(document.querySelector('.el-popper').classList).toContain(
|
|
|
|
'custom-dropdown'
|
|
|
|
)
|
2021-07-23 17:06:01 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('default value', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => ({
|
|
|
|
value: '2',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: '1',
|
|
|
|
label: 'option_a',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '2',
|
|
|
|
label: 'option_b',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '3',
|
|
|
|
label: 'option_c',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
const vm = wrapper.vm as any
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe(
|
|
|
|
vm.options[1].label
|
|
|
|
)
|
2021-07-23 17:06:01 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('default value is null or undefined', async () => {
|
|
|
|
const wrapper = createSelect()
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
|
|
|
|
vm.value = vm.options[2].value
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(placeholder.text()).toBe(vm.options[2].label)
|
|
|
|
vm.value = null
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('sync set value and options', async () => {
|
|
|
|
const wrapper = createSelect()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
vm.value = vm.options[1].value
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(placeholder.text()).toBe(vm.options[1].label)
|
|
|
|
vm.options[1].label = 'option bb aa'
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(placeholder.text()).toBe('option bb aa')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('single select', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
count: 0,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onChange() {
|
|
|
|
this.count++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
const options = getOptions()
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
expect(vm.value).toBe('')
|
|
|
|
expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
|
|
|
|
options[2].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(vm.value).toBe(vm.options[2].value)
|
|
|
|
expect(placeholder.text()).toBe(vm.options[2].label)
|
|
|
|
options[4].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(vm.value).toBe(vm.options[4].value)
|
|
|
|
expect(placeholder.text()).toBe(vm.options[4].label)
|
|
|
|
expect(vm.count).toBe(2)
|
|
|
|
})
|
|
|
|
|
2021-10-20 11:24:09 +08:00
|
|
|
it('value-key option', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
id: 'id 1',
|
|
|
|
value: 'value 1',
|
|
|
|
label: 'option 1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'id 2',
|
|
|
|
value: 'value 2',
|
|
|
|
label: 'option 2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'id 3',
|
|
|
|
value: 'value 3',
|
|
|
|
label: 'option 3',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
value: '',
|
|
|
|
valueKey: 'id',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const options = getOptions()
|
|
|
|
options[1].click()
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value).toBe(vm.options[1].id)
|
|
|
|
vm.valueKey = 'value'
|
|
|
|
await nextTick()
|
|
|
|
options[2].click()
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value).toBe(vm.options[2].value)
|
|
|
|
})
|
|
|
|
|
2021-07-23 17:06:01 +08:00
|
|
|
it('disabled option', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: '1',
|
|
|
|
label: 'option 1',
|
|
|
|
disabled: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '2',
|
|
|
|
label: 'option 2',
|
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '3',
|
|
|
|
label: 'option 3',
|
|
|
|
disabled: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
2021-09-04 19:29:28 +08:00
|
|
|
const option = document.querySelector<HTMLElement>(
|
|
|
|
`.el-select-dropdown__option-item.is-disabled`
|
|
|
|
)
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(option.textContent).toBe(vm.options[1].label)
|
|
|
|
option.click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(vm.value).toBe('')
|
|
|
|
expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
|
|
|
|
vm.options[2].disabled = true
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-09-04 19:29:28 +08:00
|
|
|
const options = document.querySelectorAll<HTMLElement>(
|
|
|
|
`.el-select-dropdown__option-item.is-disabled`
|
|
|
|
)
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(options.length).toBe(2)
|
|
|
|
expect(options.item(1).textContent).toBe(vm.options[2].label)
|
|
|
|
options.item(1).click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(vm.value).toBe('')
|
|
|
|
expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('disabled select', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
disabled: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find(`.${WRAPPER_CLASS_NAME}`).classes()).toContain(
|
|
|
|
'is-disabled'
|
|
|
|
)
|
2021-07-23 17:06:01 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('visible event', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
visible: false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onVisibleChange(visible) {
|
|
|
|
this.visible = visible
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
await wrapper.trigger('click')
|
2021-08-31 14:17:43 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(vm.visible).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('clearable', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => ({ clearable: true }),
|
|
|
|
})
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
vm.value = vm.options[1].value
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-08-25 17:23:13 +08:00
|
|
|
await clickClearButton(wrapper)
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(vm.value).toBe('')
|
|
|
|
const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('multiple', () => {
|
|
|
|
it('multiple select', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
multiple: true,
|
|
|
|
value: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const options = getOptions()
|
|
|
|
options[1].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(vm.value.length).toBe(1)
|
|
|
|
expect(vm.value[0]).toBe(vm.options[1].value)
|
|
|
|
options[3].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(vm.value.length).toBe(2)
|
|
|
|
expect(vm.value[1]).toBe(vm.options[3].value)
|
|
|
|
const tagIcon = wrapper.find('.el-tag__close')
|
|
|
|
await tagIcon.trigger('click')
|
|
|
|
expect(vm.value.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('remove-tag', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
removeTag: '',
|
|
|
|
multiple: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onRemoveTag(tag) {
|
|
|
|
this.removeTag = tag
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const options = getOptions()
|
|
|
|
options[0].click()
|
|
|
|
await nextTick()
|
|
|
|
options[1].click()
|
|
|
|
await nextTick()
|
|
|
|
options[2].click()
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value.length).toBe(3)
|
|
|
|
const tagCloseIcons = wrapper.findAll('.el-tag__close')
|
|
|
|
await tagCloseIcons[1].trigger('click')
|
|
|
|
expect(vm.value.length).toBe(2)
|
|
|
|
await tagCloseIcons[0].trigger('click')
|
|
|
|
expect(vm.value.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('limit', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
multiple: true,
|
|
|
|
multipleLimit: 2,
|
|
|
|
value: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const options = getOptions()
|
|
|
|
options[1].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
options[2].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(vm.value.length).toBe(2)
|
|
|
|
options[3].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-23 17:06:01 +08:00
|
|
|
expect(vm.value.length).toBe(2)
|
|
|
|
})
|
2021-10-20 11:24:09 +08:00
|
|
|
|
|
|
|
it('value-key option', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
id: 'id 1',
|
|
|
|
value: 'value 1',
|
|
|
|
label: 'option 1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'id 2',
|
|
|
|
value: 'value 2',
|
|
|
|
label: 'option 2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'id 3',
|
|
|
|
value: 'value 3',
|
|
|
|
label: 'option 3',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
multiple: true,
|
|
|
|
value: [],
|
|
|
|
valueKey: 'id',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const options = getOptions()
|
|
|
|
options[1].click()
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value.length).toBe(1)
|
|
|
|
expect(vm.value[0]).toBe(vm.options[1].id)
|
|
|
|
vm.valueKey = 'value'
|
|
|
|
await nextTick()
|
|
|
|
options[2].click()
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value.length).toBe(2)
|
|
|
|
expect(vm.value[1]).toBe(vm.options[2].value)
|
|
|
|
})
|
2021-07-23 17:06:01 +08:00
|
|
|
})
|
2021-07-24 23:51:53 +08:00
|
|
|
|
2022-01-22 10:43:46 +08:00
|
|
|
describe('manually set modelValue', () => {
|
|
|
|
it('set modelValue in single select', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
value: '',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
const options = getOptions()
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
|
|
|
|
expect(vm.value).toBe('')
|
|
|
|
expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
|
|
|
|
|
|
|
|
options[0].click()
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value).toBe(vm.options[0].value)
|
|
|
|
expect(placeholder.text()).toBe(vm.options[0].label)
|
|
|
|
const option = vm.options[0].value
|
|
|
|
console.log(option)
|
|
|
|
|
|
|
|
vm.value = ''
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value).toBe('')
|
|
|
|
expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
|
|
|
|
|
|
|
|
vm.value = option
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value).toBe('option_1')
|
|
|
|
expect(placeholder.text()).toBe('a0')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('set modelValue in multiple select', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
multiple: true,
|
|
|
|
value: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
let placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
expect(placeholder.exists()).toBeTruthy()
|
|
|
|
|
|
|
|
vm.value = ['option_1']
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.find('.el-select-v2__tags-text').text()).toBe('a0')
|
|
|
|
placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
expect(placeholder.exists()).toBeFalsy()
|
|
|
|
|
|
|
|
vm.value = []
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.find('.el-select-v2__tags-text').exists()).toBeFalsy()
|
|
|
|
placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
expect(placeholder.exists()).toBeTruthy()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-07-24 23:51:53 +08:00
|
|
|
describe('event', () => {
|
|
|
|
it('focus & blur', async () => {
|
|
|
|
const onFocus = jest.fn()
|
|
|
|
const onBlur = jest.fn()
|
|
|
|
const wrapper = createSelect({
|
|
|
|
methods: {
|
|
|
|
onFocus,
|
|
|
|
onBlur,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
await input.trigger('focus')
|
2021-07-28 20:15:22 +08:00
|
|
|
const selectVm = select.vm as any
|
2021-07-24 23:51:53 +08:00
|
|
|
// Simulate focus state to trigger menu multiple times
|
2021-07-28 20:15:22 +08:00
|
|
|
selectVm.toggleMenu()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
selectVm.toggleMenu()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-24 23:51:53 +08:00
|
|
|
// Simulate click the outside
|
2021-07-28 20:15:22 +08:00
|
|
|
selectVm.handleClickOutside()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-24 23:51:53 +08:00
|
|
|
expect(onFocus).toHaveBeenCalledTimes(1)
|
|
|
|
expect(onBlur).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('focus & blur for multiple & filterable select', async () => {
|
|
|
|
const onFocus = jest.fn()
|
|
|
|
const onBlur = jest.fn()
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
multiple: true,
|
|
|
|
filterable: true,
|
|
|
|
value: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onFocus,
|
|
|
|
onBlur,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
await input.trigger('focus')
|
2021-07-28 20:15:22 +08:00
|
|
|
const selectVm = select.vm as any
|
2021-07-24 23:51:53 +08:00
|
|
|
// Simulate focus state to trigger menu multiple times
|
2021-07-28 20:15:22 +08:00
|
|
|
selectVm.toggleMenu()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
selectVm.toggleMenu()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-24 23:51:53 +08:00
|
|
|
// Select multiple items in multiple mode without triggering focus
|
|
|
|
const options = getOptions()
|
|
|
|
options[1].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-24 23:51:53 +08:00
|
|
|
options[2].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-24 23:51:53 +08:00
|
|
|
expect(onFocus).toHaveBeenCalledTimes(1)
|
|
|
|
// Simulate click the outside
|
2021-07-28 20:15:22 +08:00
|
|
|
selectVm.handleClickOutside()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
|
|
|
await nextTick()
|
2021-07-24 23:51:53 +08:00
|
|
|
expect(onBlur).toHaveBeenCalled()
|
|
|
|
})
|
2021-07-28 20:15:22 +08:00
|
|
|
|
|
|
|
it('only emit change on user input', async () => {
|
|
|
|
const handleChanged = jest.fn()
|
|
|
|
const wrapper = createSelect({
|
|
|
|
methods: {
|
|
|
|
onChange: handleChanged,
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
vm.value = 'option_2'
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
expect(handleChanged).toHaveBeenCalledTimes(0)
|
|
|
|
const options = getOptions()
|
|
|
|
options[4].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
expect(handleChanged).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-08-25 17:23:13 +08:00
|
|
|
describe('allow-create', () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
it('single select', async () => {
|
2021-08-25 17:23:13 +08:00
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
2021-08-29 09:46:38 +08:00
|
|
|
popperAppendToBody: false,
|
2021-08-25 17:23:13 +08:00
|
|
|
allowCreate: true,
|
|
|
|
filterable: true,
|
|
|
|
clearable: true,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: '1',
|
|
|
|
label: 'option 1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '2',
|
|
|
|
label: 'option 2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '3',
|
|
|
|
label: 'option 3',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
const selectVm = select.vm as any
|
|
|
|
selectVm.expanded = true
|
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
await rAF()
|
2021-08-25 17:23:13 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
// create a new option
|
2021-08-29 09:46:38 +08:00
|
|
|
input.element.value = '1111'
|
|
|
|
await input.trigger('input')
|
|
|
|
await nextTick()
|
2021-08-25 17:23:13 +08:00
|
|
|
expect(selectVm.filteredOptions.length).toBe(1)
|
|
|
|
// selected the new option
|
2021-08-29 09:46:38 +08:00
|
|
|
selectVm.onSelect(selectVm.filteredOptions[0])
|
2021-08-25 17:23:13 +08:00
|
|
|
expect(vm.value).toBe('1111')
|
2021-08-29 09:46:38 +08:00
|
|
|
selectVm.expanded = false
|
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
await rAF()
|
2021-08-29 09:46:38 +08:00
|
|
|
selectVm.expanded = true
|
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
await rAF()
|
2021-08-25 17:23:13 +08:00
|
|
|
expect(selectVm.filteredOptions.length).toBe(4)
|
|
|
|
selectVm.handleClear()
|
|
|
|
expect(selectVm.filteredOptions.length).toBe(3)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('multiple', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
allowCreate: true,
|
|
|
|
filterable: true,
|
|
|
|
clearable: true,
|
|
|
|
multiple: true,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: '1',
|
|
|
|
label: 'option 1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '2',
|
|
|
|
label: 'option 2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '3',
|
|
|
|
label: 'option 3',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-08-25 17:23:13 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
await wrapper.trigger('click')
|
2021-08-29 09:46:38 +08:00
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.element.value = '1111'
|
|
|
|
await input.trigger('input')
|
|
|
|
await nextTick()
|
2021-08-25 17:23:13 +08:00
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
const selectVm = select.vm as any
|
|
|
|
expect(selectVm.filteredOptions.length).toBe(1)
|
|
|
|
// selected the new option
|
2021-08-29 09:46:38 +08:00
|
|
|
selectVm.onSelect(selectVm.filteredOptions[0])
|
2021-08-25 17:23:13 +08:00
|
|
|
// closed the menu
|
|
|
|
await wrapper.trigger('click')
|
2021-08-29 09:46:38 +08:00
|
|
|
input.element.value = '2222'
|
|
|
|
await input.trigger('input')
|
|
|
|
await nextTick()
|
|
|
|
selectVm.onSelect(selectVm.filteredOptions[0])
|
2021-08-25 17:23:13 +08:00
|
|
|
expect(JSON.stringify(vm.value)).toBe(JSON.stringify(['1111', '2222']))
|
|
|
|
await wrapper.trigger('click')
|
|
|
|
expect(selectVm.filteredOptions.length).toBe(5)
|
|
|
|
// remove tag
|
|
|
|
const tagCloseIcons = wrapper.findAll('.el-tag__close')
|
|
|
|
await tagCloseIcons[1].trigger('click')
|
|
|
|
expect(selectVm.filteredOptions.length).toBe(4)
|
|
|
|
// simulate backspace
|
|
|
|
await wrapper.find('input').trigger('keydown', {
|
|
|
|
key: EVENT_CODE.backspace,
|
|
|
|
})
|
|
|
|
expect(selectVm.filteredOptions.length).toBe(3)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-18 11:24:54 +08:00
|
|
|
it('reserve-keyword', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
filterable: true,
|
|
|
|
clearable: true,
|
|
|
|
multiple: true,
|
|
|
|
reserveKeyword: true,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: 'a1',
|
|
|
|
label: 'a1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'b1',
|
|
|
|
label: 'b1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'a2',
|
|
|
|
label: 'a2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'b2',
|
|
|
|
label: 'b2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
await nextTick()
|
|
|
|
await wrapper.trigger('click')
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
|
|
|
|
input.element.value = 'a'
|
|
|
|
await input.trigger('input')
|
|
|
|
await nextTick()
|
|
|
|
let options = getOptions()
|
|
|
|
expect(options.length).toBe(2)
|
|
|
|
options[0].click()
|
|
|
|
await nextTick()
|
|
|
|
options = getOptions()
|
|
|
|
expect(options.length).toBe(2)
|
|
|
|
|
|
|
|
input.element.value = ''
|
|
|
|
await input.trigger('input')
|
|
|
|
await nextTick()
|
|
|
|
options = getOptions()
|
|
|
|
expect(options.length).toBe(4)
|
|
|
|
|
|
|
|
vm.reserveKeyword = false
|
|
|
|
await nextTick()
|
|
|
|
input.element.value = 'a'
|
|
|
|
await input.trigger('input')
|
|
|
|
await nextTick()
|
|
|
|
options = getOptions()
|
|
|
|
expect(options.length).toBe(2)
|
|
|
|
options[0].click()
|
|
|
|
await nextTick()
|
|
|
|
options = getOptions()
|
|
|
|
expect(options.length).toBe(4)
|
|
|
|
})
|
|
|
|
|
2021-07-28 20:15:22 +08:00
|
|
|
it('render empty slot', async () => {
|
|
|
|
const wrapper = createSelect({
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2021-07-28 20:15:22 +08:00
|
|
|
return {
|
|
|
|
options: [],
|
|
|
|
popperAppendToBody: false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
slots: {
|
|
|
|
empty: '<div class="empty-slot">EmptySlot</div>',
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.findComponent({
|
|
|
|
name: 'ElPopperContent',
|
|
|
|
})
|
|
|
|
.find('.empty-slot')
|
|
|
|
.exists()
|
|
|
|
).toBeTruthy()
|
2021-07-28 20:15:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should set placeholder to label of selected option when filterable is true and multiple is false', async () => {
|
|
|
|
const wrapper = createSelect({
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2021-07-28 20:15:22 +08:00
|
|
|
return {
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: '1',
|
|
|
|
label: 'option 1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '2',
|
|
|
|
label: 'option 2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '3',
|
|
|
|
label: 'option 3',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
filterable: true,
|
|
|
|
multiple: false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
vm.value = '2'
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
const selectVm = select.vm as any
|
|
|
|
selectVm.toggleMenu()
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
await input.trigger('focus')
|
|
|
|
expect(placeholder.text()).toBe('option 2')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('default value is null or undefined', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
value: null,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
|
|
|
|
vm.value = undefined
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
|
|
|
|
})
|
|
|
|
|
2021-11-21 20:23:24 +08:00
|
|
|
it('default value is 0', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data: () => ({
|
|
|
|
value: 0,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: 0,
|
|
|
|
label: 'option_a',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 1,
|
|
|
|
label: 'option_b',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 2,
|
|
|
|
label: 'option_c',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
expect(placeholder.text()).toBe('option_a')
|
|
|
|
})
|
|
|
|
|
2021-07-28 20:15:22 +08:00
|
|
|
it('emptyText error show', async () => {
|
|
|
|
const wrapper = createSelect({
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2021-07-28 20:15:22 +08:00
|
|
|
return {
|
|
|
|
value: `${Math.random()}`,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
const vm = wrapper.vm as any
|
|
|
|
const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
|
|
|
|
expect(placeholder.text()).toBe(vm.value)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('customized option renderer', async () => {
|
|
|
|
const wrapper = createSelect({
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2021-07-28 20:15:22 +08:00
|
|
|
return {
|
|
|
|
popperAppendToBody: false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
slots: {
|
|
|
|
default: `
|
|
|
|
<div class="custom-renderer">
|
|
|
|
<span style="margin-right: 8px;">{{ item.label }}</span>
|
2021-08-26 09:57:56 +08:00
|
|
|
<span style="color: var(--el-text-color-secondary); font-size: 13px">
|
2021-07-28 20:15:22 +08:00
|
|
|
{{ item.value }}
|
2021-08-24 13:36:48 +08:00
|
|
|
</span>
|
|
|
|
</div>
|
2021-07-28 20:15:22 +08:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.findComponent({
|
|
|
|
name: 'ElPopperContent',
|
|
|
|
})
|
|
|
|
.findAll('.custom-renderer').length
|
|
|
|
).toBeGreaterThan(0)
|
2021-07-28 20:15:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('tag of disabled option is not closable', async () => {
|
|
|
|
const wrapper = createSelect({
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2021-07-28 20:15:22 +08:00
|
|
|
return {
|
|
|
|
multiple: true,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: 1,
|
2021-08-29 09:46:38 +08:00
|
|
|
label: 'option 1',
|
2021-07-28 20:15:22 +08:00
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 2,
|
2021-08-29 09:46:38 +08:00
|
|
|
label: 'option 2',
|
2021-07-28 20:15:22 +08:00
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 3,
|
2021-08-29 09:46:38 +08:00
|
|
|
label: 'option 3',
|
2021-07-28 20:15:22 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
value: [2, 3],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
expect(wrapper.findAll('.el-tag').length).toBe(2)
|
|
|
|
const tagCloseIcons = wrapper.findAll('.el-tag__close')
|
|
|
|
expect(tagCloseIcons.length).toBe(1)
|
|
|
|
await tagCloseIcons[0].trigger('click')
|
|
|
|
expect(wrapper.findAll('.el-tag__close').length).toBe(0)
|
|
|
|
expect(wrapper.findAll('.el-tag').length).toBe(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('modelValue should be deep reactive in multiple mode', async () => {
|
|
|
|
const wrapper = createSelect({
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2021-07-28 20:15:22 +08:00
|
|
|
return {
|
|
|
|
multiple: true,
|
|
|
|
value: ['option_1', 'option_2', 'option_3'],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
expect(wrapper.findAll('.el-tag').length).toBe(3)
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
vm.value.splice(0, 1)
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
expect(wrapper.findAll('.el-tag').length).toBe(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should reset placeholder after clear when both multiple and filterable are true', async () => {
|
|
|
|
const wrapper = createSelect({
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2021-07-28 20:15:22 +08:00
|
|
|
return {
|
|
|
|
value: ['option_1'],
|
|
|
|
clearable: true,
|
|
|
|
filterable: true,
|
|
|
|
multiple: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).exists()).toBeFalsy()
|
|
|
|
// When all tags are removed, the placeholder should be displayed
|
|
|
|
const tagCloseIcon = wrapper.find('.el-tag__close')
|
|
|
|
await tagCloseIcon.trigger('click')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe(
|
|
|
|
DEFAULT_PLACEHOLDER
|
|
|
|
)
|
2021-07-28 20:15:22 +08:00
|
|
|
// The placeholder should disappear after it is selected again
|
|
|
|
const options = getOptions()
|
|
|
|
options[0].click()
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-07-28 20:15:22 +08:00
|
|
|
expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).exists()).toBeFalsy()
|
|
|
|
// Simulate keyboard events
|
|
|
|
const selectInput = wrapper.find('input')
|
2021-08-31 14:17:43 +08:00
|
|
|
await selectInput.trigger('keydown', {
|
2021-07-28 20:15:22 +08:00
|
|
|
key: EVENT_CODE.backspace,
|
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
await nextTick()
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe(
|
|
|
|
DEFAULT_PLACEHOLDER
|
|
|
|
)
|
2021-07-24 23:51:53 +08:00
|
|
|
})
|
2021-08-29 09:46:38 +08:00
|
|
|
|
|
|
|
describe('filter method', () => {
|
|
|
|
async function testFilterMethod({ multiple = false }) {
|
|
|
|
const filterMethod = jest.fn()
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
filterable: true,
|
|
|
|
multiple,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
filterMethod,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.element.value = 'query'
|
|
|
|
await input.trigger('input')
|
|
|
|
expect(filterMethod).toHaveBeenCalled()
|
|
|
|
}
|
|
|
|
it('should call filter method', async () => {
|
|
|
|
await testFilterMethod({ multiple: false })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call filter method in multiple mode', async () => {
|
|
|
|
await testFilterMethod({ multiple: true })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should re-render', async () => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
multiple: true,
|
|
|
|
filterable: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2021-09-04 19:29:28 +08:00
|
|
|
filterMethod() {
|
2021-08-29 09:46:38 +08:00
|
|
|
this.options = [
|
|
|
|
{
|
|
|
|
value: 1,
|
|
|
|
label: 'option 1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 2,
|
|
|
|
label: 'option 2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 3,
|
|
|
|
label: 'option 3',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.element.value = 'query'
|
|
|
|
await input.trigger('input')
|
|
|
|
await nextTick()
|
|
|
|
input.element.value = ''
|
|
|
|
await input.trigger('input')
|
|
|
|
await nextTick()
|
|
|
|
const options = getOptions()
|
|
|
|
expect(options.length).toBe(3)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('remote search', () => {
|
|
|
|
async function testRemoteSearch({ multiple = false }) {
|
|
|
|
const remoteMethod = jest.fn()
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
filterable: true,
|
|
|
|
remote: true,
|
|
|
|
multiple,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
remoteMethod,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const input = wrapper.find('input')
|
|
|
|
input.element.value = 'query'
|
|
|
|
await input.trigger('input')
|
|
|
|
expect(remoteMethod).toHaveBeenCalled()
|
|
|
|
}
|
|
|
|
it('should call remote method', async () => {
|
|
|
|
await testRemoteSearch({ multiple: false })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call remote method in multiple mode', async () => {
|
|
|
|
await testRemoteSearch({ multiple: true })
|
|
|
|
})
|
|
|
|
})
|
2021-08-31 14:17:43 +08:00
|
|
|
|
|
|
|
it('keyboard operations', async () => {
|
|
|
|
const wrapper = createSelect({
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2021-08-31 14:17:43 +08:00
|
|
|
return {
|
|
|
|
multiple: true,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: 1,
|
|
|
|
label: 'option 1',
|
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 2,
|
|
|
|
label: 'option 2',
|
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 3,
|
|
|
|
label: 'option 3',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 4,
|
|
|
|
label: 'option 4',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 5,
|
|
|
|
label: 'option 5',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: 51,
|
|
|
|
label: 'option 5-1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 52,
|
|
|
|
label: 'option 5-2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 53,
|
|
|
|
label: 'option 5-3',
|
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 6,
|
|
|
|
label: 'option 6',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
value: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
const selectVm = select.vm as any
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
await wrapper.trigger('click')
|
|
|
|
await nextTick()
|
|
|
|
expect(selectVm.states.hoveringIndex).toBe(-1)
|
|
|
|
// should skip the disabled option
|
|
|
|
selectVm.onKeyboardNavigate('forward')
|
|
|
|
selectVm.onKeyboardNavigate('forward')
|
|
|
|
await nextTick()
|
|
|
|
expect(selectVm.states.hoveringIndex).toBe(3)
|
|
|
|
// should skip the group option
|
|
|
|
selectVm.onKeyboardNavigate('backward')
|
|
|
|
selectVm.onKeyboardNavigate('backward')
|
|
|
|
selectVm.onKeyboardNavigate('backward')
|
|
|
|
selectVm.onKeyboardNavigate('backward')
|
|
|
|
await nextTick()
|
|
|
|
expect(selectVm.states.hoveringIndex).toBe(5)
|
|
|
|
selectVm.onKeyboardNavigate('backward')
|
|
|
|
selectVm.onKeyboardNavigate('backward')
|
|
|
|
selectVm.onKeyboardNavigate('backward')
|
|
|
|
await nextTick()
|
|
|
|
// navigate to the last one
|
|
|
|
expect(selectVm.states.hoveringIndex).toBe(9)
|
|
|
|
selectVm.onKeyboardSelect()
|
|
|
|
await nextTick()
|
|
|
|
expect(vm.value).toEqual([6])
|
|
|
|
})
|
2021-09-01 13:14:17 +08:00
|
|
|
|
|
|
|
it('multiple select when content overflow', async () => {
|
|
|
|
const wrapper = createSelect({
|
2021-09-04 19:29:28 +08:00
|
|
|
data() {
|
2021-09-01 13:14:17 +08:00
|
|
|
return {
|
2021-09-04 19:29:28 +08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: '选项1',
|
|
|
|
label:
|
|
|
|
'黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '选项2',
|
|
|
|
label:
|
|
|
|
'双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '选项3',
|
|
|
|
label: '蚵仔煎蚵仔煎蚵仔煎蚵仔煎蚵仔煎蚵仔煎',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '选项4',
|
|
|
|
label: '龙须面',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '选项5',
|
|
|
|
label: '北京烤鸭',
|
|
|
|
},
|
|
|
|
],
|
2021-09-01 13:14:17 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
const selectVm = select.vm as any
|
|
|
|
const selectDom = wrapper.find('.el-select-v2__wrapper').element
|
|
|
|
const selectRect = {
|
|
|
|
height: 40,
|
|
|
|
width: 221,
|
2021-09-04 19:29:28 +08:00
|
|
|
x: 44,
|
|
|
|
y: 8,
|
|
|
|
top: 8,
|
2021-09-01 13:14:17 +08:00
|
|
|
}
|
2021-09-04 19:29:28 +08:00
|
|
|
const mockSelectWidth = jest
|
|
|
|
.spyOn(selectDom, 'getBoundingClientRect')
|
|
|
|
.mockReturnValue(selectRect as DOMRect)
|
2021-09-01 13:14:17 +08:00
|
|
|
selectVm.handleResize()
|
|
|
|
const options = getOptions()
|
|
|
|
options[0].click()
|
|
|
|
await nextTick()
|
|
|
|
options[1].click()
|
|
|
|
await nextTick()
|
|
|
|
options[2].click()
|
|
|
|
await nextTick()
|
|
|
|
const tagWrappers = wrapper.findAll('.el-select-v2__tags-text')
|
2021-09-04 19:29:28 +08:00
|
|
|
for (let i = 0; i < tagWrappers.length; i++) {
|
2021-09-01 13:14:17 +08:00
|
|
|
const tagWrapperDom = tagWrappers[i].element
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
parseInt(tagWrapperDom.style.maxWidth) === selectRect.width - 42
|
|
|
|
).toBe(true)
|
2021-09-01 13:14:17 +08:00
|
|
|
}
|
|
|
|
mockSelectWidth.mockRestore()
|
|
|
|
})
|
2021-11-04 14:37:40 +08:00
|
|
|
|
|
|
|
describe('scrollbarAlwaysOn flag control the scrollbar whether always displayed', () => {
|
|
|
|
it('The default scrollbar is not always displayed', async (done) => {
|
|
|
|
const wrapper = createSelect()
|
|
|
|
await nextTick()
|
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
await wrapper.trigger('click')
|
|
|
|
expect((select.vm as any).expanded).toBeTruthy()
|
|
|
|
const box = document.querySelector<HTMLElement>('.el-vl__wrapper')
|
|
|
|
expect(hasClass(box, 'always-on')).toBe(false)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('set the scrollbar-always-on value to true, keep the scroll bar displayed', async (done) => {
|
|
|
|
const wrapper = createSelect({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
scrollbarAlwaysOn: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
const select = wrapper.findComponent(Select)
|
|
|
|
await wrapper.trigger('click')
|
|
|
|
expect((select.vm as any).expanded).toBeTruthy()
|
|
|
|
const box = document.querySelector<HTMLElement>('.el-vl__wrapper')
|
|
|
|
expect(hasClass(box, 'always-on')).toBe(true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2021-07-23 17:06:01 +08:00
|
|
|
})
|