2020-10-04 15:44:05 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
|
|
|
import { sleep } from '@element-plus/test-utils'
|
|
|
|
import { NOOP } from '@vue/shared'
|
2020-10-30 23:26:33 +08:00
|
|
|
|
|
|
|
jest.unmock('lodash/debounce')
|
|
|
|
|
2020-10-04 15:44:05 +08:00
|
|
|
import Autocomplete from '../src/index.vue'
|
|
|
|
|
|
|
|
const _mount = (payload = {}) => mount({
|
|
|
|
components: {
|
|
|
|
'el-autocomplete': Autocomplete,
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
state: '',
|
|
|
|
list: [
|
|
|
|
{ value: 'Java', tag: 'java' },
|
|
|
|
{ value: 'Go', tag: 'go' },
|
|
|
|
{ value: 'JavaScript', tag: 'javascript' },
|
|
|
|
{ value: 'Python', tag: 'python' },
|
|
|
|
],
|
|
|
|
payload,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
querySearch(queryString, cb) {
|
|
|
|
cb(queryString ? this.list.filter(i => i.value.indexOf(queryString.toLowerCase()) === 0) : this.list)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
template: `
|
|
|
|
<el-autocomplete
|
|
|
|
ref="autocomplete"
|
|
|
|
v-model="state"
|
|
|
|
:fetch-suggestions="querySearch"
|
|
|
|
v-bind="payload"
|
|
|
|
/>
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Autocomplete.vue', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
document.body.innerHTML = ''
|
|
|
|
})
|
|
|
|
|
|
|
|
test('placeholder', async () => {
|
|
|
|
const wrapper = _mount()
|
|
|
|
|
|
|
|
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 () => {
|
2020-10-22 14:02:28 +08:00
|
|
|
const fetchSuggestions = jest.fn()
|
|
|
|
const wrapper = _mount({
|
|
|
|
debounce: 10,
|
|
|
|
fetchSuggestions,
|
|
|
|
})
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.setProps({ triggerOnFocus: false })
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2020-10-22 14:02:28 +08:00
|
|
|
await sleep(30)
|
|
|
|
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')
|
2020-10-22 14:02:28 +08:00
|
|
|
await sleep(30)
|
|
|
|
expect(fetchSuggestions).toHaveBeenCalledTimes(1)
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('popperClass', async () => {
|
|
|
|
const wrapper = _mount()
|
|
|
|
|
|
|
|
await wrapper.setProps({ popperClass: 'error' })
|
2020-12-05 12:30:15 +08:00
|
|
|
expect(document.body.querySelector('.el-popper').classList.contains('error')).toBe(true)
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.setProps({ popperClass: 'success' })
|
2020-12-05 12:30:15 +08:00
|
|
|
expect(document.body.querySelector('.el-popper').classList.contains('error')).toBe(false)
|
|
|
|
expect(document.body.querySelector('.el-popper').classList.contains('success')).toBe(true)
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('popperAppendToBody', async () => {
|
2020-10-22 14:02:28 +08:00
|
|
|
_mount({ popperAppendToBody: false })
|
|
|
|
expect(document.body.querySelector('.el-popper__mask')).toBeNull()
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('debounce / fetchSuggestions', async () => {
|
|
|
|
const fetchSuggestions = jest.fn()
|
|
|
|
const wrapper = _mount({
|
2020-10-22 14:02:28 +08:00
|
|
|
debounce: 10,
|
2020-10-04 15:44:05 +08:00
|
|
|
fetchSuggestions,
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2020-10-22 14:02:28 +08:00
|
|
|
await sleep(30)
|
2020-10-04 15:44:05 +08:00
|
|
|
expect(fetchSuggestions).toHaveBeenCalledTimes(1)
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2020-10-22 14:02:28 +08:00
|
|
|
await sleep(30)
|
2020-10-04 15:44:05 +08:00
|
|
|
expect(fetchSuggestions).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('valueKey / modelValue', async () => {
|
|
|
|
const wrapper = _mount()
|
2020-10-22 14:02:28 +08:00
|
|
|
const target = wrapper.findComponent({ ref: 'autocomplete' }).vm as any
|
|
|
|
|
|
|
|
await target.select({ value: 'Go', tag: 'go' })
|
|
|
|
expect(wrapper.vm.state).toBe('Go')
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.setProps({ valueKey: 'tag' })
|
2020-10-22 14:02:28 +08:00
|
|
|
|
|
|
|
await target.select({ value: 'Go', tag: 'go' })
|
2020-10-04 15:44:05 +08:00
|
|
|
expect(wrapper.vm.state).toBe('go')
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2020-10-22 14:02:28 +08:00
|
|
|
await sleep(30)
|
|
|
|
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 () => {
|
|
|
|
const wrapper = mount(Autocomplete, {
|
|
|
|
props: {
|
|
|
|
selectWhenUnmatched: true,
|
2020-10-22 14:02:28 +08:00
|
|
|
debounce: 10,
|
2020-10-04 15:44:05 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
wrapper.vm.highlightedIndex = 0
|
|
|
|
wrapper.vm.handleKeyEnter()
|
2020-10-22 14:02:28 +08:00
|
|
|
await sleep(30)
|
2020-10-04 15:44:05 +08:00
|
|
|
expect(wrapper.vm.highlightedIndex).toBe(-1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('highlightFirstItem', async () => {
|
2020-10-22 14:02:28 +08:00
|
|
|
const wrapper = _mount({
|
|
|
|
highlightFirstItem: false,
|
|
|
|
debounce: 10,
|
|
|
|
})
|
2020-10-04 15:44:05 +08:00
|
|
|
|
|
|
|
await wrapper.find('input').trigger('focus')
|
2020-10-22 14:02:28 +08:00
|
|
|
await sleep(30)
|
|
|
|
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')
|
2020-10-22 14:02:28 +08:00
|
|
|
await sleep(30)
|
|
|
|
expect(document.body.querySelector('.highlighted')).toBeDefined()
|
2020-10-04 15:44:05 +08:00
|
|
|
})
|
|
|
|
})
|