things I keep forgetting how to do in Jest (with React, Redux-Saga etc)
import * as module from 'path/module'
const spy = jest.spyOn(module, 'method')
.mockImplementation(() => {
return 'whatever you want to return'
})
// call method under test here and do assertions
// retore the original implementation of the method spied on.
spy.mockRestore()
import * as module from 'path/module'
const spy = jest.spyOn(module, 'method')
// call method under test here and do assertions
// retore the original implementation of the method spied on.
spy.mockRestore()
export function * generatorName () {
try {
yield call(doSomethingPositive)
yield call(whatIsYourFavColour)
} catch (error) {
yield put(errorActionCreator)
throw error
}
}
test('catch block for generator/saga', () => {
const mockError = {
message: 'boom!'
}
const generatorName = sagaFileContainingGeneratorAbove.generatorName()
try {
expect(generatorName.next().value).toEqual(call(doSomethingPositive))
expect(generatorName.throw(mockError).value).toEqual(put(errorActionCreator))
} catch (error) {
expect(error).toEqual(mockError)
}
// expect(generatorName.next().done).toEqual(true) - only needed if we didn't throw error in the generator
})