【发布时间】:2019-05-02 16:14:39
【问题描述】:
我有一个复选框组件,由于 Safari 错误,我更改了
@input=input() to @change='input'
因为 Safari 没有输入事件。
复选框
<template>
<div
class="checkbox">
<input
....
@change="input">
</div>
</template>
<script>
export default {
....
methods: {
input () {
/**
* Input event on change
*
* @event input
* @type {Boolean}
*/
this.$emit('input', this.$refs.checkbox.checked)
}
}
}
</script>
单元测试
describe('...', () => {
beforeEach(async () => {
const input = wrapper.find('input')
jest.spyOn(wrapper.vm, 'input')
input.trigger('change') // told this is incorrect
jest.runAllTimers()
})
it('[positive] should emit an input event with the input\'s value', () => {
expect(wrapper.emitted().input).toBeTruthy()
expect(wrapper.emitted().input).toHaveLength(1)
expect(wrapper.emitted().input[0]).toEqual([false])
})
it('[positive] should call the input() method with the target value', () => {
// this is wrong also, because the expectation will always be true
wrapper.vm.input()
expect(wrapper.vm.input).toHaveBeenCalled()
})
})
我应该如何正确设置第二个测试?为什么单元测试中 input.trigger('change') 出错了?
【问题讨论】:
-
对于哪个版本的 Safari,
input事件不起作用?我检查了 Can I Use 网站,似乎支持了一段时间。 caniuse.com/#feat=input-event -
我关于这个错误的另一个问题。 stackoverflow.com/questions/55905406/…
-
@SamuelVaillant 它不适用于复选框。
-
input.trigger('change')有什么问题?来自文档。
标签: javascript vue.js jestjs vue-test-utils