Skip to content

Instantly share code, notes, and snippets.

@mastilver
Created August 17, 2017 10:51
Show Gist options
  • Save mastilver/36f9d523a92a1542a1c07e89e1671c4c to your computer and use it in GitHub Desktop.
Save mastilver/36f9d523a92a1542a1c07e89e1671c4c to your computer and use it in GitHub Desktop.
sagas: unit vs integration tests
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();
});
})
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)
)
});
})
@lifehackett
Copy link

lifehackett commented Jan 9, 2018

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