2022-03-01 11:48:57 +08:00
|
|
|
import { nextTick, reactive } from 'vue'
|
2020-10-04 15:44:05 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
|
|
|
import { NOOP } from '@vue/shared'
|
2022-04-19 12:46:57 +08:00
|
|
|
import { beforeEach, describe, expect, it, test, vi } from 'vitest'
|
2022-02-04 14:59:58 +08:00
|
|
|
import { POPPER_CONTAINER_SELECTOR } from '@element-plus/hooks'
|
2022-03-01 11:48:57 +08:00
|
|
|
import Autocomplete from '../src/autocomplete.vue'
|
2020-10-04 15:44:05 +08:00
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.unmock('lodash')
|
2021-09-17 15:27:31 +08:00
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.useFakeTimers()
|
2022-02-06 00:37:20 +08:00
|
|
|
|
2022-03-21 15:51:39 +08:00
|
|
|
const _mount = (
|
|
|
|
payload = {},
|
|
|
|
type: 'fn-cb' | 'fn-promise' | 'fn-arr' | 'arr' = 'fn-cb'
|
|
|
|
) =>
|
2021-09-04 19:29:28 +08:00
|
|
|
mount({
|
2022-03-01 11:48:57 +08:00
|
|
|
setup() {
|
|
|
|
const state = reactive({
|
|
|
|
value: '',
|
2021-09-04 19:29:28 +08:00
|
|
|
list: [
|
|
|
|
{ value: 'Java', tag: 'java' },
|
|
|
|
{ value: 'Go', tag: 'go' },
|
|
|
|
{ value: 'JavaScript', tag: 'javascript' },
|
|
|
|
{ value: 'Python', tag: 'python' },
|
|
|
|
],
|
|
|
|
payload,
|
2022-03-01 11:48:57 +08:00
|
|
|
})
|
|
|
|
|
2022-03-21 15:51:39 +08:00
|
|
|
function filterList(queryString: string) {
|
|
|
|
return queryString
|
|
|
|
? state.list.filter(
|
|
|
|
(i) => i.value.indexOf(queryString.toLowerCase()) === 0
|
|
|
|
)
|
|
|
|
: state.list
|
2022-03-01 11:48:57 +08:00
|
|
|
}
|
|
|
|
|
2022-03-21 15:51:39 +08:00
|
|
|
const querySearch = (() => {
|
|
|
|
switch (type) {
|
|
|
|
case 'fn-cb':
|
|
|
|
return (
|
|
|
|
queryString: string,
|
|
|
|
cb: (arg: typeof state.list) => void
|
|
|
|
) => {
|
|
|
|
cb(filterList(queryString))
|
|
|
|
}
|
|
|
|
case 'fn-promise':
|
|
|
|
return (queryString: string) =>
|
|
|
|
Promise.resolve(filterList(queryString))
|
|
|
|
case 'fn-arr':
|
|
|
|
return (queryString: string) => filterList(queryString)
|
|
|
|
case 'arr':
|
|
|
|
return state.list
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
|
2022-03-01 11:48:57 +08:00
|
|
|
return () => (
|
|
|
|
<Autocomplete
|
|
|
|
ref="autocomplete"
|
|
|
|
v-model={state.value}
|
|
|
|
fetch-suggestions={querySearch}
|
|
|
|
{...state.payload}
|
|
|
|
/>
|
|
|
|
)
|
2021-09-04 19:29:28 +08:00
|
|
|
},
|
|
|
|
})
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
describe('Autocomplete.vue', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
document.body.innerHTML = ''
|
|
|
|
})
|
|
|
|
|
|
|
|
test('placeholder', async () => {
|
|
|
|
const wrapper = _mount()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.setProps({ placeholder: 'autocomplete' })
|
|
|
|
expect(wrapper.find('input').attributes('placeholder')).toBe('autocomplete')
|
|
|
|
|
|
|
|
await wrapper.setProps({ placeholder: 'placeholder' })
|
|
|
|
expect(wrapper.find('input').attributes('placeholder')).toBe('placeholder')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('triggerOnFocus', async () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
const fetchSuggestions = vi.fn()
|
2020-10-22 14:02:28 +08:00
|
|
|
const wrapper = _mount({
|
|
|
|
debounce: 10,
|
|
|
|
fetchSuggestions,
|
|
|
|
})
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.setProps({ triggerOnFocus: false })
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
2020-10-22 14:02:28 +08:00
|
|
|
expect(fetchSuggestions).toHaveBeenCalledTimes(0)
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.find('input').trigger('blur')
|
|
|
|
|
|
|
|
await wrapper.setProps({ triggerOnFocus: true })
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
2020-10-22 14:02:28 +08:00
|
|
|
expect(fetchSuggestions).toHaveBeenCalledTimes(1)
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('popperClass', async () => {
|
|
|
|
const wrapper = _mount()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.setProps({ popperClass: 'error' })
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
2022-03-01 11:48:57 +08:00
|
|
|
document.body.querySelector('.el-popper')?.classList.contains('error')
|
2021-09-04 19:29:28 +08:00
|
|
|
).toBe(true)
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.setProps({ popperClass: 'success' })
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
2022-03-01 11:48:57 +08:00
|
|
|
document.body.querySelector('.el-popper')?.classList.contains('error')
|
2021-09-04 19:29:28 +08:00
|
|
|
).toBe(false)
|
|
|
|
expect(
|
2022-03-01 11:48:57 +08:00
|
|
|
document.body.querySelector('.el-popper')?.classList.contains('success')
|
2021-09-04 19:29:28 +08:00
|
|
|
).toBe(true)
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
|
|
|
|
2022-03-01 11:48:57 +08:00
|
|
|
test('teleported', async () => {
|
|
|
|
_mount({ teleported: false })
|
2020-10-22 14:02:28 +08:00
|
|
|
expect(document.body.querySelector('.el-popper__mask')).toBeNull()
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('debounce / fetchSuggestions', async () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
const fetchSuggestions = vi.fn()
|
2020-10-04 15:44:05 +08:00
|
|
|
const wrapper = _mount({
|
2020-10-22 14:02:28 +08:00
|
|
|
debounce: 10,
|
2020-10-04 15:44:05 +08:00
|
|
|
fetchSuggestions,
|
|
|
|
})
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.find('input').trigger('focus')
|
|
|
|
await wrapper.find('input').trigger('blur')
|
|
|
|
await wrapper.find('input').trigger('focus')
|
|
|
|
await wrapper.find('input').trigger('blur')
|
|
|
|
await wrapper.find('input').trigger('focus')
|
|
|
|
await wrapper.find('input').trigger('blur')
|
|
|
|
expect(fetchSuggestions).toHaveBeenCalledTimes(0)
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
|
|
|
|
2020-10-04 15:44:05 +08:00
|
|
|
expect(fetchSuggestions).toHaveBeenCalledTimes(1)
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
|
|
|
|
2020-10-04 15:44:05 +08:00
|
|
|
expect(fetchSuggestions).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
2022-03-21 15:51:39 +08:00
|
|
|
test('fetchSuggestions with fn-promise', async () => {
|
|
|
|
const wrapper = _mount({ debounce: 10 }, 'fn-promise')
|
|
|
|
await nextTick()
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-03-21 15:51:39 +08:00
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
|
|
|
|
typeof Autocomplete
|
|
|
|
>
|
|
|
|
|
|
|
|
expect(target.suggestions.length).toBe(4)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('fetchSuggestions with fn-arr', async () => {
|
|
|
|
const wrapper = _mount({ debounce: 10 }, 'fn-arr')
|
|
|
|
await nextTick()
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-03-21 15:51:39 +08:00
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
|
|
|
|
typeof Autocomplete
|
|
|
|
>
|
|
|
|
|
|
|
|
expect(target.suggestions.length).toBe(4)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('fetchSuggestions with arr', async () => {
|
|
|
|
const wrapper = _mount({ debounce: 10 }, 'arr')
|
|
|
|
await nextTick()
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-03-21 15:51:39 +08:00
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
|
|
|
|
typeof Autocomplete
|
|
|
|
>
|
|
|
|
|
|
|
|
expect(target.suggestions.length).toBe(4)
|
|
|
|
})
|
|
|
|
|
2020-10-04 15:44:05 +08:00
|
|
|
test('valueKey / modelValue', async () => {
|
|
|
|
const wrapper = _mount()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
2020-10-22 14:02:28 +08:00
|
|
|
|
2022-03-01 11:48:57 +08:00
|
|
|
const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
|
|
|
|
typeof Autocomplete
|
|
|
|
>
|
|
|
|
|
|
|
|
await target.handleSelect({ value: 'Go', tag: 'go' })
|
|
|
|
|
|
|
|
expect(target.modelValue).toBe('Go')
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.setProps({ valueKey: 'tag' })
|
2020-10-22 14:02:28 +08:00
|
|
|
|
2022-03-01 11:48:57 +08:00
|
|
|
await target.handleSelect({ value: 'Go', tag: 'go' })
|
|
|
|
expect(target.modelValue).toBe('go')
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('hideLoading', async () => {
|
|
|
|
const wrapper = _mount({
|
|
|
|
hideLoading: false,
|
|
|
|
fetchSuggestions: NOOP,
|
2020-10-22 14:02:28 +08:00
|
|
|
debounce: 10,
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
2020-10-04 15:44:05 +08:00
|
|
|
await wrapper.find('input').trigger('focus')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
|
|
|
|
2020-10-22 14:02:28 +08:00
|
|
|
expect(document.body.querySelector('.el-icon-loading')).toBeDefined()
|
2020-10-04 15:44:05 +08:00
|
|
|
await wrapper.setProps({ hideLoading: true })
|
2020-10-22 14:02:28 +08:00
|
|
|
expect(document.body.querySelector('.el-icon-loading')).toBeNull()
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('selectWhenUnmatched', async () => {
|
2022-03-01 11:48:57 +08:00
|
|
|
const wrapper = _mount({
|
|
|
|
selectWhenUnmatched: true,
|
|
|
|
debounce: 10,
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
2022-03-01 11:48:57 +08:00
|
|
|
const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
|
|
|
|
typeof Autocomplete
|
|
|
|
>
|
2022-02-06 00:37:20 +08:00
|
|
|
|
2022-03-01 11:48:57 +08:00
|
|
|
target.highlightedIndex = 0
|
|
|
|
target.handleKeyEnter()
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
|
|
|
|
2022-03-01 11:48:57 +08:00
|
|
|
expect(target.highlightedIndex).toBe(-1)
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('highlightFirstItem', async () => {
|
2020-10-22 14:02:28 +08:00
|
|
|
const wrapper = _mount({
|
|
|
|
highlightFirstItem: false,
|
|
|
|
debounce: 10,
|
|
|
|
})
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
|
|
|
|
2020-10-22 14:02:28 +08:00
|
|
|
expect(document.body.querySelector('.highlighted')).toBeNull()
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.setProps({ highlightFirstItem: true })
|
2020-10-22 14:02:28 +08:00
|
|
|
|
2020-10-04 15:44:05 +08:00
|
|
|
await wrapper.find('input').trigger('focus')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
2022-02-06 00:37:20 +08:00
|
|
|
await nextTick()
|
|
|
|
|
2020-10-22 14:02:28 +08:00
|
|
|
expect(document.body.querySelector('.highlighted')).toBeDefined()
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
2022-02-04 14:59:58 +08:00
|
|
|
|
|
|
|
describe('teleported API', () => {
|
|
|
|
it('should mount on popper container', async () => {
|
|
|
|
expect(document.body.innerHTML).toBe('')
|
|
|
|
_mount()
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
expect(
|
2022-03-01 11:48:57 +08:00
|
|
|
document.body.querySelector(POPPER_CONTAINER_SELECTOR)?.innerHTML
|
2022-02-04 14:59:58 +08:00
|
|
|
).not.toBe('')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not mount on the popper container', async () => {
|
|
|
|
expect(document.body.innerHTML).toBe('')
|
|
|
|
_mount({
|
|
|
|
teleported: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
expect(
|
2022-03-01 11:48:57 +08:00
|
|
|
document.body.querySelector(POPPER_CONTAINER_SELECTOR)?.innerHTML
|
2022-02-04 14:59:58 +08:00
|
|
|
).toBe('')
|
|
|
|
})
|
|
|
|
})
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|