12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634 |
- // @ts-nocheck
- import { nextTick } from 'vue'
- import { NOOP } from '@vue/shared'
- import { afterEach, describe, expect, it, vi } from 'vitest'
- import { hasClass } from '@element-plus/utils'
- import { EVENT_CODE } from '@element-plus/constants'
- import { makeMountFunc } from '@element-plus/test-utils/make-mount'
- import { rAF } from '@element-plus/test-utils/tick'
- import { CircleClose } from '@element-plus/icons-vue'
- import { usePopperContainerId } from '@element-plus/hooks'
- import Select from '../src/select.vue'
- vi.mock('lodash-unified', async () => {
- return {
- ...((await vi.importActual('lodash-unified')) as Record<string, any>),
- debounce: vi.fn((fn) => {
- fn.cancel = vi.fn()
- fn.flush = vi.fn()
- return fn
- }),
- }
- })
- 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}`,
- }))
- }
- const clickClearButton = async (wrapper) => {
- const select = wrapper.findComponent(Select)
- const selectVm = select.vm as any
- selectVm.states.comboBoxHovering = true
- await nextTick()
- const clearBtn = wrapper.findComponent(CircleClose)
- expect(clearBtn.exists()).toBeTruthy()
- await clearBtn.trigger('click')
- }
- interface SelectProps {
- popperClass?: string
- value?: string | string[] | number | number[]
- options?: any[]
- disabled?: boolean
- clearable?: boolean
- multiple?: boolean
- collapseTags?: boolean
- collapseTagsTooltip?: boolean
- filterable?: boolean
- remote?: boolean
- multipleLimit?: number
- allowCreate?: boolean
- popperAppendToBody?: boolean
- placeholder?: string
- [key: string]: any
- }
- interface SelectEvents {
- onChange?: (value?: string) => void
- onVisibleChange?: (visible?: boolean) => void
- onRemoveTag?: (tag?: string) => void
- onFocus?: (event?: FocusEvent) => void
- onBlur?: (event?) => void
- filterMethod?: (query: string) => void | undefined
- remoteMethod?: (query: string) => void | undefined
- [key: string]: (...args) => any
- }
- 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(
- `
- <el-select
- :options="options"
- :popper-class="popperClass"
- :value-key="valueKey"
- :disabled="disabled"
- :clearable="clearable"
- :multiple="multiple"
- :collapseTags="collapseTags"
- :collapseTagsTooltip="collapseTagsTooltip"
- :filterable="filterable"
- :multiple-limit="multipleLimit"
- :placeholder="placeholder"
- :allow-create="allowCreate"
- :remote="remote"
- :reserve-keyword="reserveKeyword"
- :scrollbar-always-on="scrollbarAlwaysOn"
- :teleported="teleported"
- ${
- options.methods && options.methods.filterMethod
- ? `:filter-method="filterMethod"`
- : ''
- }
- ${
- options.methods && options.methods.remoteMethod
- ? `:remote-method="remoteMethod"`
- : ''
- }
- @change="onChange"
- @visible-change="onVisibleChange"
- @remove-tag="onRemoveTag"
- @focus="onFocus"
- @blur="onBlur"
- v-model="value">
- ${defaultSlot}
- ${emptySlot}
- </el-select>
- `,
- {
- data() {
- return {
- options: createData(),
- value: '',
- popperClass: '',
- allowCreate: false,
- valueKey: 'value',
- disabled: false,
- clearable: false,
- multiple: false,
- collapseTags: false,
- collapseTagsTooltip: false,
- remote: false,
- filterable: false,
- reserveKeyword: false,
- multipleLimit: 0,
- placeholder: DEFAULT_PLACEHOLDER,
- scrollbarAlwaysOn: false,
- popperAppendToBody: undefined,
- teleported: undefined,
- ...(options.data && options.data()),
- }
- },
- methods: {
- onChange: NOOP,
- onVisibleChange: NOOP,
- onRemoveTag: NOOP,
- onFocus: NOOP,
- onBlur: NOOP,
- ...options.methods,
- },
- }
- )
- }
- function getOptions(): HTMLElement[] {
- return Array.from(
- document.querySelectorAll<HTMLElement>(`.${OPTION_ITEM_CLASS_NAME}`)
- )
- }
- 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()
- await nextTick()
- expect(wrapper.classes()).toContain(CLASS_NAME)
- expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe('')
- const select = wrapper.findComponent(Select)
- await wrapper.trigger('click')
- expect((select.vm as any).expanded).toBeTruthy()
- })
- it('options rendered correctly', async () => {
- const wrapper = createSelect()
- await nextTick()
- const vm = wrapper.vm as any
- const options = Array.from(
- document.querySelectorAll(`.${OPTION_ITEM_CLASS_NAME}`)
- )
- const result = options.every((option, index) => {
- const text = option.textContent
- return text === vm.options[index].label
- })
- expect(result).toBeTruthy()
- })
- it('custom dropdown class', async () => {
- createSelect({
- data: () => ({
- popperClass: 'custom-dropdown',
- }),
- })
- await nextTick()
- expect([...document.querySelector('.el-popper').classList]).toContain(
- 'custom-dropdown'
- )
- })
- 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
- await nextTick()
- expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe(
- vm.options[1].label
- )
- })
- it('default value is null or undefined', async () => {
- const wrapper = createSelect({
- data: () => ({
- value: undefined,
- }),
- })
- 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
- await nextTick()
- expect(placeholder.text()).toBe(vm.options[2].label)
- vm.value = null
- await nextTick()
- expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
- })
- it('default value is Object', async () => {
- const wrapper = createSelect({
- data: () => ({
- valueKey: 'value',
- value: {
- value: '1',
- label: 'option_a',
- },
- options: [
- {
- value: '1',
- label: 'option_a',
- },
- {
- value: '2',
- label: 'option_b',
- },
- {
- value: '3',
- label: 'option_c',
- },
- ],
- }),
- })
- const vm = wrapper.vm as any
- await nextTick()
- expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe(
- vm.options[0].label
- )
- expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe(
- vm.value.label
- )
- })
- it('sync set value and options', async () => {
- const wrapper = createSelect()
- await nextTick()
- const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
- const vm = wrapper.vm as any
- vm.value = vm.options[1].value
- await nextTick()
- expect(placeholder.text()).toBe(vm.options[1].label)
- vm.options[1].label = 'option bb aa'
- await nextTick()
- expect(placeholder.text()).toBe('option bb aa')
- })
- it('single select', async () => {
- const wrapper = createSelect({
- data() {
- return {
- count: 0,
- }
- },
- methods: {
- onChange() {
- this.count++
- },
- },
- })
- 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('')
- options[2].click()
- await nextTick()
- expect(vm.value).toBe(vm.options[2].value)
- expect(placeholder.text()).toBe(vm.options[2].label)
- options[4].click()
- await nextTick()
- expect(vm.value).toBe(vm.options[4].value)
- expect(placeholder.text()).toBe(vm.options[4].label)
- expect(vm.count).toBe(2)
- })
- 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)
- })
- 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,
- },
- ],
- }
- },
- })
- await nextTick()
- const vm = wrapper.vm as any
- const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
- const option = document.querySelector<HTMLElement>(
- `.el-select-dropdown__option-item.is-disabled`
- )
- expect(option.textContent).toBe(vm.options[1].label)
- option.click()
- await nextTick()
- expect(vm.value).toBe('')
- expect(placeholder.text()).toBe('')
- vm.options[2].disabled = true
- await nextTick()
- const options = document.querySelectorAll<HTMLElement>(
- `.el-select-dropdown__option-item.is-disabled`
- )
- expect(options.length).toBe(2)
- expect(options.item(1).textContent).toBe(vm.options[2].label)
- options.item(1).click()
- await nextTick()
- expect(vm.value).toBe('')
- expect(placeholder.text()).toBe('')
- })
- it('disabled select', async () => {
- const wrapper = createSelect({
- data: () => {
- return {
- disabled: true,
- }
- },
- })
- await nextTick()
- expect(wrapper.find(`.${WRAPPER_CLASS_NAME}`).classes()).toContain(
- 'is-disabled'
- )
- })
- it('visible event', async () => {
- const wrapper = createSelect({
- data: () => {
- return {
- visible: false,
- }
- },
- methods: {
- onVisibleChange(visible) {
- this.visible = visible
- },
- },
- })
- await nextTick()
- const vm = wrapper.vm as any
- await wrapper.trigger('click')
- await nextTick()
- 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
- await nextTick()
- await clickClearButton(wrapper)
- expect(vm.value).toBeUndefined()
- const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
- expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
- })
- describe('initial value', () => {
- it.each([
- [null, DEFAULT_PLACEHOLDER],
- [undefined, DEFAULT_PLACEHOLDER],
- ['', ''],
- [[], DEFAULT_PLACEHOLDER],
- [{}, '[object Object]'],
- ])(
- '[single select] initial value is %s, placeholder is "%s"',
- async (value, placeholder) => {
- const wrapper = createSelect({
- data: () => {
- return {
- value,
- }
- },
- })
- await nextTick()
- expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe(
- placeholder
- )
- }
- )
- it.each([
- [null, DEFAULT_PLACEHOLDER],
- [undefined, DEFAULT_PLACEHOLDER],
- ['', DEFAULT_PLACEHOLDER],
- [[], DEFAULT_PLACEHOLDER],
- [{}, DEFAULT_PLACEHOLDER],
- ])(
- '[multiple select] initial value is %s, placeholder is "%s"',
- async (value, placeholder) => {
- const wrapper = createSelect({
- data: () => {
- return {
- multiple: true,
- value,
- }
- },
- })
- await nextTick()
- expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe(
- placeholder
- )
- }
- )
- })
- describe('multiple', () => {
- it('multiple select', async () => {
- const wrapper = createSelect({
- data: () => {
- return {
- multiple: true,
- value: [],
- }
- },
- })
- 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].value)
- options[3].click()
- await nextTick()
- 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
- },
- },
- })
- await nextTick()
- 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: [],
- }
- },
- })
- await nextTick()
- const vm = wrapper.vm as any
- const options = getOptions()
- options[1].click()
- await nextTick()
- options[2].click()
- await nextTick()
- expect(vm.value.length).toBe(2)
- options[3].click()
- await nextTick()
- expect(vm.value.length).toBe(2)
- })
- 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)
- })
- })
- describe('collapseTags', () => {
- it('use collapseTags', async () => {
- const wrapper = createSelect({
- data: () => {
- return {
- multiple: true,
- collapseTags: true,
- value: [],
- }
- },
- })
- await nextTick()
- const vm = wrapper.vm as any
- const options = getOptions()
- options[0].click()
- await nextTick()
- expect(vm.value.length).toBe(1)
- expect(vm.value[0]).toBe(vm.options[0].value)
- options[1].click()
- await nextTick()
- options[2].click()
- await nextTick()
- expect(vm.value.length).toBe(3)
- const tags = wrapper.findAll('.el-tag').filter((item) => {
- return !hasClass(item.element, 'in-tooltip')
- })
- expect(tags.length).toBe(2)
- })
- it('use collapseTagsTooltip', async () => {
- const wrapper = createSelect({
- data: () => {
- return {
- multiple: true,
- collapseTags: true,
- collapseTagsTooltip: true,
- value: [],
- }
- },
- })
- await nextTick()
- const vm = wrapper.vm as any
- const options = getOptions()
- options[0].click()
- await nextTick()
- expect(vm.value.length).toBe(1)
- expect(vm.value[0]).toBe(vm.options[0].value)
- options[1].click()
- await nextTick()
- options[2].click()
- await nextTick()
- expect(vm.value.length).toBe(3)
- expect(wrapper.findAll('.el-tag')[3].element.textContent).toBe('c2')
- })
- })
- 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('')
- 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
- vm.value = ''
- await nextTick()
- expect(vm.value).toBe('')
- expect(placeholder.text()).toBe('')
- 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()
- })
- })
- describe('event', () => {
- it('focus & blur', async () => {
- const onFocus = vi.fn()
- const onBlur = vi.fn()
- const wrapper = createSelect({
- methods: {
- onFocus,
- onBlur,
- },
- })
- const input = wrapper.find('input')
- const select = wrapper.findComponent(Select)
- await input.trigger('focus')
- const selectVm = select.vm as any
- // Simulate focus state to trigger menu multiple times
- selectVm.toggleMenu()
- await nextTick()
- selectVm.toggleMenu()
- await nextTick()
- // Simulate click the outside
- selectVm.handleClickOutside()
- await nextTick()
- expect(onFocus).toHaveBeenCalledTimes(1)
- expect(onBlur).toHaveBeenCalled()
- })
- it('focus & blur for multiple & filterable select', async () => {
- const onFocus = vi.fn()
- const onBlur = vi.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')
- const selectVm = select.vm as any
- // Simulate focus state to trigger menu multiple times
- selectVm.toggleMenu()
- await nextTick()
- selectVm.toggleMenu()
- await nextTick()
- // Select multiple items in multiple mode without triggering focus
- const options = getOptions()
- options[1].click()
- await nextTick()
- options[2].click()
- await nextTick()
- expect(onFocus).toHaveBeenCalledTimes(1)
- // Simulate click the outside
- selectVm.handleClickOutside()
- await nextTick()
- await nextTick()
- expect(onBlur).toHaveBeenCalled()
- })
- it('only emit change on user input', async () => {
- const handleChanged = vi.fn()
- const wrapper = createSelect({
- methods: {
- onChange: handleChanged,
- },
- })
- await nextTick()
- const vm = wrapper.vm as any
- vm.value = 'option_2'
- await nextTick()
- expect(handleChanged).toHaveBeenCalledTimes(0)
- const options = getOptions()
- options[4].click()
- await nextTick()
- expect(handleChanged).toHaveBeenCalled()
- })
- })
- describe('allow-create', () => {
- it('single select', async () => {
- const wrapper = createSelect({
- data: () => {
- return {
- allowCreate: true,
- filterable: true,
- clearable: true,
- options: [
- {
- value: '1',
- label: 'option 1',
- },
- {
- value: '2',
- label: 'option 2',
- },
- {
- value: '3',
- label: 'option 3',
- },
- ],
- }
- },
- })
- await nextTick()
- const select = wrapper.findComponent(Select)
- const selectVm = select.vm as any
- selectVm.expanded = true
- await nextTick()
- await rAF()
- const vm = wrapper.vm as any
- const input = wrapper.find('input')
- // create a new option
- input.element.value = '1111'
- await input.trigger('input')
- await nextTick()
- expect(selectVm.filteredOptions.length).toBe(1)
- // selected the new option
- selectVm.onSelect(selectVm.filteredOptions[0])
- expect(vm.value).toBe('1111')
- selectVm.expanded = false
- await nextTick()
- await rAF()
- selectVm.expanded = true
- await nextTick()
- await rAF()
- 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',
- },
- ],
- }
- },
- })
- await nextTick()
- const vm = wrapper.vm as any
- await wrapper.trigger('click')
- const input = wrapper.find('input')
- input.element.value = '1111'
- await input.trigger('input')
- await nextTick()
- const select = wrapper.findComponent(Select)
- const selectVm = select.vm as any
- expect(selectVm.filteredOptions.length).toBe(1)
- // selected the new option
- selectVm.onSelect(selectVm.filteredOptions[0])
- // closed the menu
- await wrapper.trigger('click')
- input.element.value = '2222'
- await input.trigger('input')
- await nextTick()
- selectVm.onSelect(selectVm.filteredOptions[0])
- 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)
- })
- })
- 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)
- })
- it('render empty slot', async () => {
- const wrapper = createSelect({
- data() {
- return {
- options: [],
- }
- },
- slots: {
- empty: '<div class="empty-slot">EmptySlot</div>',
- },
- })
- await nextTick()
- expect(
- wrapper
- .findComponent({
- name: 'ElPopperContent',
- })
- .find('.empty-slot')
- .exists()
- ).toBeTruthy()
- })
- it('should set placeholder to label of selected option when filterable is true and multiple is false', async () => {
- const wrapper = createSelect({
- data() {
- return {
- options: [
- {
- value: '1',
- label: 'option 1',
- },
- {
- value: '2',
- label: 'option 2',
- },
- {
- value: '3',
- label: 'option 3',
- },
- ],
- filterable: true,
- multiple: false,
- }
- },
- })
- await nextTick()
- const vm = wrapper.vm as any
- const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
- vm.value = '2'
- await nextTick()
- 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,
- }
- },
- })
- await nextTick()
- const vm = wrapper.vm as any
- const placeholder = wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`)
- expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
- vm.value = undefined
- await nextTick()
- expect(placeholder.text()).toBe(DEFAULT_PLACEHOLDER)
- })
- 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')
- })
- it('emptyText error show', async () => {
- const wrapper = createSelect({
- data() {
- return {
- value: `${Math.random()}`,
- }
- },
- })
- await nextTick()
- 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({
- slots: {
- default: `
- <div class="custom-renderer">
- <span style="margin-right: 8px;">{{ item.label }}</span>
- <span style="color: var(--el-text-color-secondary); font-size: 13px">
- {{ item.value }}
- </span>
- </div>
- `,
- },
- })
- await nextTick()
- expect(
- wrapper
- .findComponent({
- name: 'ElPopperContent',
- })
- .findAll('.custom-renderer').length
- ).toBeGreaterThan(0)
- })
- it('tag of disabled option is not closable', async () => {
- const wrapper = createSelect({
- data() {
- return {
- multiple: true,
- options: [
- {
- value: 1,
- label: 'option 1',
- disabled: true,
- },
- {
- value: 2,
- label: 'option 2',
- disabled: true,
- },
- {
- value: 3,
- label: 'option 3',
- },
- ],
- value: [2, 3],
- }
- },
- })
- await nextTick()
- 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({
- data() {
- return {
- multiple: true,
- value: ['option_1', 'option_2', 'option_3'],
- }
- },
- })
- await nextTick()
- expect(wrapper.findAll('.el-tag').length).toBe(3)
- const vm = wrapper.vm as any
- vm.value.splice(0, 1)
- await nextTick()
- 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({
- data() {
- return {
- value: ['option_1'],
- clearable: true,
- filterable: true,
- multiple: true,
- }
- },
- })
- await nextTick()
- 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')
- expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe(
- DEFAULT_PLACEHOLDER
- )
- // The placeholder should disappear after it is selected again
- const options = getOptions()
- options[0].click()
- await nextTick()
- expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).exists()).toBeFalsy()
- // Simulate keyboard events
- const selectInput = wrapper.find('input')
- await selectInput.trigger('keydown', {
- key: EVENT_CODE.backspace,
- })
- await nextTick()
- expect(wrapper.find(`.${PLACEHOLDER_CLASS_NAME}`).text()).toBe(
- DEFAULT_PLACEHOLDER
- )
- })
- describe('filter method', () => {
- async function testFilterMethod({ multiple = false }) {
- const filterMethod = vi.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: {
- filterMethod() {
- 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 = vi.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 })
- })
- })
- it('keyboard operations', async () => {
- const wrapper = createSelect({
- data() {
- 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])
- })
- it('multiple select when content overflow', async () => {
- const wrapper = createSelect({
- data() {
- return {
- options: [
- {
- value: '选项1',
- label:
- '黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕',
- },
- {
- value: '选项2',
- label:
- '双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶',
- },
- {
- value: '选项3',
- label: '蚵仔煎蚵仔煎蚵仔煎蚵仔煎蚵仔煎蚵仔煎',
- },
- {
- value: '选项4',
- label: '龙须面',
- },
- {
- value: '选项5',
- label: '北京烤鸭',
- },
- ],
- }
- },
- })
- 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,
- x: 44,
- y: 8,
- top: 8,
- }
- const mockSelectWidth = vi
- .spyOn(selectDom, 'getBoundingClientRect')
- .mockReturnValue(selectRect as DOMRect)
- 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')
- for (const tagWrapper of tagWrappers) {
- const tagWrapperDom = tagWrapper.element
- expect(
- Number.parseInt(tagWrapperDom.style.maxWidth) === selectRect.width - 42
- ).toBe(true)
- }
- mockSelectWidth.mockRestore()
- })
- describe('scrollbarAlwaysOn flag control the scrollbar whether always displayed', () => {
- it('The default scrollbar is not always displayed', async () => {
- 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)
- })
- it('set the scrollbar-always-on value to true, keep the scroll bar displayed', async () => {
- 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)
- })
- })
- describe('teleported API', () => {
- it('should mount on popper container', async () => {
- expect(document.body.innerHTML).toBe('')
- createSelect({
- data() {
- return {
- options: [
- {
- value: '选项1',
- label:
- '黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕',
- },
- {
- value: '选项2',
- label:
- '双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶',
- },
- {
- value: '选项3',
- label: '蚵仔煎蚵仔煎蚵仔煎蚵仔煎蚵仔煎蚵仔煎',
- },
- {
- value: '选项4',
- label: '龙须面',
- },
- {
- value: '选项5',
- label: '北京烤鸭',
- },
- ],
- }
- },
- })
- await nextTick()
- const { selector } = usePopperContainerId()
- expect(document.body.querySelector(selector.value)!.innerHTML).not.toBe(
- ''
- )
- })
- it('should not mount on the popper container', async () => {
- expect(document.body.innerHTML).toBe('')
- createSelect({
- data() {
- return {
- teleported: false,
- options: [
- {
- value: '选项1',
- label:
- '黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕黄金糕',
- },
- {
- value: '选项2',
- label:
- '双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶双皮奶',
- },
- {
- value: '选项3',
- label: '蚵仔煎蚵仔煎蚵仔煎蚵仔煎蚵仔煎蚵仔煎',
- },
- {
- value: '选项4',
- label: '龙须面',
- },
- {
- value: '选项5',
- label: '北京烤鸭',
- },
- ],
- }
- },
- })
- await nextTick()
- const { selector } = usePopperContainerId()
- expect(document.body.querySelector(selector.value).innerHTML).toBe('')
- })
- })
- it('filterable case-insensitive', async () => {
- const wrapper = createSelect({
- data: () => {
- return {
- filterable: true,
- options: [
- {
- value: '1',
- label: 'option 1',
- },
- {
- value: '2',
- label: 'option 2',
- },
- {
- value: '3',
- label: 'OPtion 3',
- },
- ],
- }
- },
- })
- await nextTick()
- const select = wrapper.findComponent(Select)
- const selectVm = select.vm as any
- selectVm.expanded = true
- await nextTick()
- await rAF()
- const input = wrapper.find('input')
- input.element.value = 'op'
- await input.trigger('input')
- await nextTick()
- expect(selectVm.filteredOptions.length).toBe(3)
- })
- })
|