簡體   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