繁体   English   中英

微信小程序测试capture-catch:tap事件

[英]wechat miniprogram testing capture-catch:tap event

我需要用jest模拟微信小程序测试中的capture-catch:tap事件。

我的组件应该接收“ tap ”事件,做一些事情,然后触发“ click ”事件。 这是我的代码:

<!-- WXML -->
<view
  ...
  class='edit-component'
  capture-catch:tap="onTap"
  >
  <!-- other things -->
</view>



// .TS
methods: {
    onTap(event){
      console.log('onTap event')
       //...
       that.triggerEvent("click",event.detail)
    }
}
//test .js file
const path = require('path')
const simulate = require('miniprogram-simulate');

  it('test capture-catch:tap event', done => {
    let id = simulate.load(path.resolve(__dirname, '../../ui/controls/edit/edit'))
    let component = simulate.render(id)
    let input = component.querySelector('.edit-component')
    component.addEventListener('click', evt => {
      done()
    })
    input.instance.triggerEvent('tap', {})
  })

一方面,组件的操作是正确的。 当小程序运行时它工作正常,然而,当用 jest 运行测试时它失败,抛出这个错误:

 'thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."'.

如果在 wxml 中而不是使用capture-catch:tap="onTap"我把bind:tap="onTap"然后测试工作。

我认为错误是在启动事件时,也许是在放置capture-catch:tap时,它不能用input.instance.triggerEvent('tap', {})完成,但它必须是另一种方式。 我在官方文档和.net 上搜索过,都没有找到。

正如我所怀疑的那样,错误是在启动事件的过程中发生的。 而不是使用: input.instance.triggerEvent('tap')

我愿意:

input.dispatchEvent('tap')

测试工作正常。

我真的不知道一个和另一个之间的区别。

暂无
暂无

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

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