lundi 30 octobre 2017

Branching test of an ES6 generator function

Suppose we have the following generator function:

function *myGenerator(payload) {
  try {
    yield call(someFunction, payload)

    yield call(success)
  }
  catch (err) {
    yield call(error)
  }
}

I know I can test this function like so (using Mocha):

describe('myGenerator', () => {
  const iter = myGenerator({ prop1: 0 })

  let next = null
  it('should call someFunction', () => {
    next = iter.next()
    expect(next.value).to.deep.equal(call(someFunction, { prop1: 0 }))
  })
  it('should call success', () => {
    next = iter.next()
    expect(next.value).to.deep.equal(call(success))
  })
})

describe('myGenerator when an error occurs', () => {
   const iter = myGenerator({ prop1: 0 })

  let next = null
  it('should call someFunction', () => {
    next = iter.next()
    expect(next.value).to.deep.equal(call(someFunction, { prop1: 0 }))
  })
  it('should call error', () => {
    next = iter.throw('some error')
    expect(next.value).to.deep.equal(call(error))
  })
})

The question is: how can I combine the first part of these tests? Notice that the assertion on calling someFunction is duplicated.

Aucun commentaire:

Enregistrer un commentaire