test: add auto-complate test

pull/22/head
tjz 7 years ago
parent 09e25a2889
commit b2d330894a

@ -0,0 +1,32 @@
import { mount } from '@vue/test-utils'
import { render } from '@vue/server-test-utils'
import Vue from 'vue'
import AutoComplete from '..'
import focusTest from '../../../tests/shared/focusTest'
describe('AutoComplete with Custom Input Element Render', () => {
focusTest(AutoComplete)
function $$ (className) {
return document.body.querySelectorAll(className)
}
it('AutoComplete with custom Input render perfectly', (done) => {
const wrapper = mount({
render (h) {
return <AutoComplete ref='component' dataSource={['12345', '23456', '34567']}>
<input />
</AutoComplete>
},
}, { attachToDocument: true, sync: false })
expect(wrapper.findAll('input').length).toBe(1)
const input = wrapper.find('input')
input.element.value = '123'
input.trigger('input')
Vue.nextTick(() => {
const popupComponent = wrapper.find({ name: 'Trigger' }).vm._component
expect($$('.ant-select-dropdown-menu-item').length).toBe(3)
expect(popupComponent).not.toBe(null)
expect(popupComponent).not.toBe(undefined)
done()
})
})
})

@ -63,19 +63,15 @@ export default {
},
focus () {
this.$nextTick(() => {
if (this.$refs.select) {
this.$refs.select.focus()
}
})
},
blur () {
this.$nextTick(() => {
if (this.$refs.select) {
this.$refs.select.blur()
}
})
},
},

@ -0,0 +1,52 @@
import { mount } from '@vue/test-utils'
import Vue from 'vue'
export default function focusTest (Component) {
describe('focus and blur', () => {
beforeAll(() => {
jest.useFakeTimers()
})
afterAll(() => {
jest.useRealTimers()
})
it('focus() and onFocus', () => {
const handleFocus = jest.fn()
const wrapper = mount({
render (h) {
return <Component ref='component' onFocus={handleFocus} />
},
}, { attachToDocument: true, sync: false })
wrapper.vm.$refs.component.focus()
jest.runAllTimers()
expect(handleFocus).toBeCalled()
})
it('blur() and onBlur', () => {
const handleBlur = jest.fn()
const wrapper = mount({
render (h) {
return <Component ref='component' onBlur={handleBlur} />
},
}, { attachToDocument: true, sync: false })
wrapper.vm.$refs.component.focus()
wrapper.vm.$refs.component.blur()
jest.runAllTimers()
expect(handleBlur).toBeCalled()
})
it('autoFocus', (done) => {
jest.useRealTimers()
const handleFocus = jest.fn()
mount({
render (h) {
return <Component autoFocus onFocus={handleFocus} />
},
}, { attachToDocument: true, sync: false })
setTimeout(() => {
expect(handleFocus).toBeCalled()
done()
}, 1000)
})
})
}
Loading…
Cancel
Save