1. 程式人生 > >[Javascript] Wrap fireEvent with fireEventAsync

[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}