【发布时间】:2017-07-20 20:25:30
【问题描述】:
当您有一个基于模型显示的按钮时,如下所示:
<button v-show="title != ''">Add it</button>
你会如何测试这个?我一辈子都想不通。
我尝试了以下方法(使用 Jest,但运行器/框架无关紧要):
describe('btn test', () => {
it('should hide the add button initially', () => {
// vm is set up here to be the vue component
expect(vm.$el.querySelector('button').style.display).toBe('none') // works
// Update the input field (which has v-model="title")
vm.$el.querySelectorAll('input').value = 'fd'
expect(vm.$el.querySelector('button').style.display).toBe('block') // is still 'none'
// Update directly through vm prop
vm.title = 'sheep'
Vue.nextTick(() => {
expect(vm.$el.querySelector('button').style.display).toBe('block') // is still 'none'
})
})
})
【问题讨论】:
标签: javascript unit-testing testing vue.js jestjs