From c0a22f58766120d70caae8853e8ab4a3e20368ef Mon Sep 17 00:00:00 2001 From: tjz <415800467@qq.com> Date: Fri, 18 May 2018 21:09:52 +0800 Subject: [PATCH] test: add auto-complate test --- .../auto-complete/__tests__/index.test.js | 32 ++++++++++++ components/auto-complete/index.jsx | 16 +++--- tests/shared/focusTest.js | 52 +++++++++++++++++++ 3 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 components/auto-complete/__tests__/index.test.js create mode 100644 tests/shared/focusTest.js diff --git a/components/auto-complete/__tests__/index.test.js b/components/auto-complete/__tests__/index.test.js new file mode 100644 index 000000000..0d4b9a58a --- /dev/null +++ b/components/auto-complete/__tests__/index.test.js @@ -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 + + + }, + }, { 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() + }) + }) +}) diff --git a/components/auto-complete/index.jsx b/components/auto-complete/index.jsx index 89a2628ba..e7dcc99ef 100644 --- a/components/auto-complete/index.jsx +++ b/components/auto-complete/index.jsx @@ -63,19 +63,15 @@ export default { }, focus () { - this.$nextTick(() => { - if (this.$refs.select) { - this.$refs.select.focus() - } - }) + if (this.$refs.select) { + this.$refs.select.focus() + } }, blur () { - this.$nextTick(() => { - if (this.$refs.select) { - this.$refs.select.blur() - } - }) + if (this.$refs.select) { + this.$refs.select.blur() + } }, }, diff --git a/tests/shared/focusTest.js b/tests/shared/focusTest.js new file mode 100644 index 000000000..943b8e302 --- /dev/null +++ b/tests/shared/focusTest.js @@ -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 + }, + }, { 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 + }, + }, { 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 + }, + }, { attachToDocument: true, sync: false }) + setTimeout(() => { + expect(handleFocus).toBeCalled() + done() + }, 1000) + }) + }) +}