[Javascript] Wrap fireEvent with fireEventAsync

The idea is wrap a object with all its function methods and add some additional handling into a new object.

For example, we have a object 'fireEvent':

import {fireEvent, wait} from 'dom-testing-library'
async function render() {
  fireEvent.click(handleClick)
  await wait()
}

 

What we want to achieve is:

fireEventAsync.click(handleClick);

 

import {fireEvent, wait} from 'dom-testing-library'

const fireEventAsync = {}

Object.entries(fireEvent).reduce((obj, [key, val]) => {
  obj[key] = async (...args) => {
     const ret = val(...args)
     await wait()
     return ret
  }

  return obj
}, fireEventAsync)

export {fireEventAsync}    

 

posted @ 2018-12-11 03:29  Zhentiw  阅读(125)  评论(0编辑  收藏  举报