简体   繁体   English

为vue-multiselect编写单元测试

[英]writing unit tests for vue-multiselect

I'm trying to work out how to write tests for vue-multiselect and having trouble. 我正在尝试找出如何为vue-multiselect编写测试并遇到麻烦的方法。

Specifically, I'm trying to "click" the input to open the dropdown (before then selecting an option), however nothing is working 具体来说,我试图“单击”输入以打开下拉菜单(然后选择选项之前),但是没有任何作用

I'ved tried 我试过了

vm.$el.querySelector('.multiselect').click()

and in the browser: 并在浏览器中:

document.querySelector('.multiselect').click()

and lots of other combinations but nothing's working. 和许多其他组合,但无济于事。

Odd for a library which emphasises it's test coverage not to give an docs (I could find on how) to use it in unit tests. 奇怪的是,一个库强调它的测试范围不提供文档(我可以找到方法)在单元测试中使用它。

At last got this working, the key was .dispatchEvent(new window.Event('focus')) not .focus() . 终于成功了,关键是.dispatchEvent(new window.Event('focus'))而不是.focus()

Relevant bit of final test: 最终测试的相关内容:

expect(vm.$el.querySelectorAll('input')).to.have.lengthOf(1)

vm.$el.querySelector('.multiselect').dispatchEvent(new window.Event('focus'))
await tick()
const s = vm.$el.querySelectorAll('.multiselect__element')[2].querySelector('span')
expect(s.innerText).to.equal('Science')
s.dispatchEvent(new window.Event('mousedown'))
await tick()
expect(routes_visited).to.deep.equal(['/s/3-science'])

vm.$el.querySelector('.cross').click()
await tick()
expect(routes_visited).to.deep.equal(['/s/3-science', '/'])

(note my component changes the vue-router route, so I'm watch ing routes and pushing into routes_visited in the test to track changes.) (请注意,我的组件更改了vue-router路由,因此,我正在watch路由,并在测试中推入routes_visited来跟踪更改。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM