mardi 26 avril 2016

Unit testing nested promises fails with timeout

I've got a wrapper around the Axios XHR library, and I'm trying to write some unit tests for my code. However, when I run my tests, they all fail telling me that the timeout exceeded. How do I need to structure my tests in order to run my assertions?

Here's the wrapper code:

export const clientRequest = (xhrClient, endpoint, params = {}) => {
  const method = params.method || 'get'
  const {config, data, noOrg, unrestricted} = params

  let reqParams = data
  if (!isNil(data) && method === 'get') {
    reqParams = {params: data}
  }

  const authConfig = unrestricted ? {...config, withCredentials: false} :
    {...config, withCredentials: true}

  const concatEndpoint = noOrg ? endpoint :
    `${Cookies.get('organization') || 'default' }${endpoint}`

  return new Promise((resolve, reject) => {
    xhrClient[method](concatEndpoint, reqParams, authConfig)
      .then(response => resolve(response))
      .catch(err => reject(err))
  })
}

And the test in question:

  describe('clientRequest()', () => {
    const resolveSpy = sinon.spy()
    const fakeClient = {
      get: () => new Promise(resolveSpy),
    }

    it.only('should make a call to the supplied method', (done) => {
      const result = xhr.clientRequest(fakeClient, '/foobar', {method: 'get'})
      result.then(() => {
        expect(resolveSpy).to.have.beenCalledWith('/foobar', undefined, {withCredentials: true})
        done()
      })
    })
  })

Aucun commentaire:

Enregistrer un commentaire