All files / src/tests/integrationTests helpers.js

96% Statements 24/25
66.67% Branches 4/6
80% Functions 8/10
95.83% Lines 23/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x   1x         2x 2x 2x 4x 4x 2x 2x       2x 1x 1x       1x 2x 2x     4x   1x 4x 4x 7x 7x 7x       1x            
const log = require('../../log')
 
const waitForState = (
  chainInterface,
  predicate = () => false,
  timeout = 2000
) =>
  new Promise((resolve, reject) => {
    let unsubscribe = () => {}
    unsubscribe = chainInterface.subscribe(() => {
      const state = chainInterface.getState()
      if (predicate(state)) {
        unsubscribe()
        resolve()
      }
    })
 
    setTimeout(() => {
      unsubscribe()
      reject(new Error(`Waiting for state timed out after ${timeout}ms`))
    }, timeout)
  })
 
const dispatchAction = async (chainInterface, action) => {
  log.action(action)
  await chainInterface.dispatch(action)
}
 
const getChainId = (alias, chains) => chains[alias].chainId || chains[alias]
 
const logStateUpdates = (alias, chainInterface) => {
  const logChainState = log.any(alias)
  chainInterface.subscribe(() => {
    const state = chainInterface.getState()
    logChainState(state)
    log.info('Done.')
  })
}
 
module.exports = {
  waitForState,
  dispatchAction,
  getChainId,
  logStateUpdates
}