Created
August 17, 2017 10:51
-
-
Save mastilver/36f9d523a92a1542a1c07e89e1671c4c to your computer and use it in GitHub Desktop.
sagas: unit vs integration tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createAction } from 'redux-act'; | |
import { call, takeLatest } from 'redux-saga/effects'; | |
import { createStore, applyMiddleware } from 'redux' | |
import api from './api'; | |
const action = createAction('action'); | |
function *doSomething() { | |
yield call(api); | |
} | |
function *daemon() { | |
yield [takeLatest(action.getType(), doSomething())] | |
} | |
/* tests */ | |
function getStoreForSaga(saga) { | |
const sagaMiddleware = createSagaMiddleware() | |
const store = createStore( | |
() => {}, // TODO: put real reducer there | |
applyMiddleware(sagaMiddleware) | |
) | |
sagaMiddleware.run(saga) | |
return store; | |
} | |
descrive('saga', () => { | |
beforeAll(() => { | |
jest.mock('./api', () => Promise.resolve()); | |
}); | |
it('should call api', () => { | |
const store = getStoreForSaga(daemon); | |
store.dispactch(action()); | |
expect(api).toHaveBeenCalled(); | |
}); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createAction } from 'redux-act'; | |
import { call, takeLatest } from 'redux-saga/effects'; | |
import api from './api'; | |
const action = createAction('action'); | |
function *doSomething() { | |
yield call(api); | |
} | |
function *daemon() { | |
yield [takeLatest(action.getType(), doSomething())] | |
} | |
/* tests */ | |
descrive('saga', () => { | |
it('should call api', () => { | |
const iterator = daemon(); | |
iterator.next(); | |
expect( | |
interator.next(action()).value | |
).toEqual( | |
call(api) | |
) | |
}); | |
}) |
Also this, https://github.com/jfairbank/redux-saga-test-plan/
UPDATE: Actually, I'm not sure this is all that different from the unit tests.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Didn't know if you had seen this.
https://redux-saga.js.org/docs/advanced/Testing.html#testing-the-full-saga
https://redux-saga.js.org/docs/api/#runsagaoptions-saga-args