Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input 组件 #13

Open
Jackwxiao opened this issue Apr 19, 2020 · 0 comments
Open

input 组件 #13

Jackwxiao opened this issue Apr 19, 2020 · 0 comments

Comments

@Jackwxiao
Copy link
Owner

Jackwxiao commented Apr 19, 2020

测试 input 组件时,如何触发事件?(change、input、focus、blbur)

 describe('事件', () => {
        const Constructor = Vue.extend(Input)
        it('支持 change/input/focus/blur 事件', () => {
            ['change','input','focus','blur'].forEach((eventName)=>{
                let vm = new Constructor({}).$mount()
                const callback = sinon.fake()
                vm.$on(eventName,callback)
                //触发 input 的事件
                let event = new Event(eventName)
                Object.defineProperty(
                    event,'target', {
                        value:{value:'hi'}, enumerable: true
                    }
                )
                let inputElement = vm.$el.querySelector('input')
                inputElement.dispatchEvent(event)
                expect(callback).to.have.been.calledWith('hi')
                vm.$destroy()
            })
            })
    })

让组件支持 v-model 的重要之处

<input  :value = "value" type="text" @input="$emit('input', event.target.value)">

兼容 v-model 后,测试用例过不了,原因是兼容之前 calledwith('event'),但兼容之后必须要触发event的值才能测试,而这时event没有这个值,在浏览器中会自动补全这个值的,但在测试时没有这个值,会报它是只读属性的错,于是需要用到

Object.defineProperty(event,'target', {
     value: { vaule: 'hi' },
     enumerable: true
})

这样可以了,原因可能是破坏了它的可读属性?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant