Created
August 29, 2017 18:30
-
-
Save mattraykowski/6123e09d822877da8aed6b227134ed42 to your computer and use it in GitHub Desktop.
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
export const types { | |
FETCH_PATCH: 'FETCH_PATCH', | |
SET_PATCH: 'SET_PATCH', | |
SET_PATCH_ERROR: 'SET_PATCH_ERROR', | |
}; | |
export const actions = { | |
fetchPatch: () => ({ type: types.FETCH_PATCH }), | |
setPatch: patch => ({ type: types.SET_PATCH, payload: patch }), | |
setPatchError: error => ([ type: types.SET_PATCH_ERROR, payload: error }), | |
}; | |
export const defaultState = { | |
loading: false, | |
error: null, | |
patch: {}, | |
}; | |
export default function(state = defaultState, { type, payload }) { | |
switch (type) { | |
case types.FETCH_PATCH: | |
return { ...state, loading: true }; | |
case types.SET_PATCH: | |
return { ...state, loading: false, patch: payload }; | |
case types.SET_PATCH_ERROR: | |
return { ...state, loading: false, error: payload }; | |
default: | |
return state; | |
}; | |
} |
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 axios from 'axios'; | |
import { types, actions } from '../modules/patch'; | |
const fetchPatch = () => axios.get('/api/patches').then(({ data }) => ({ patch: data })).catch(error => ({ error })); | |
export function* fetchPatchSaga() { | |
const { patch, error } = yield call(fetchPatch); | |
if (error) { | |
yield put(actions.fetchPatchError(error)); | |
} else { | |
yield put(actions.setPatch(patch)); | |
} | |
} | |
export function *watchPatch() { | |
yield takeEvery(types.FETCH_PATCH, fetchPatchSaga); | |
} |
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 { call, put } from 'redux-saga/effects'; | |
import { fetchPatchSaga, fetchPatch } from './patch'; | |
describe('patch saga', () => { | |
describe('#fetchPatchSaga', () => { | |
describe('when successful', () => { | |
test('dispatches an error action', () => { | |
const saga = fetchPatchSaga(); | |
const response = { error: 'fake error' }; | |
// The first thing is the fetch, expect that to happen. | |
expect(saga.next()).toBe(call(fetchPatch)); | |
// Next pretend like a failure happened, and expect it to dispatch | |
// an error action. | |
expect(saga.next(response)).toBe(put(actions.setPatchError(response.error)); | |
}); | |
}); | |
describe('when successful', () => { | |
test('it dispatches a set action', () => { | |
const saga = fetchPatchSaga(); | |
const response = { patch: { a: 'patch' } }; | |
expect(saga.next()).toBe(call(fetchPatch)); | |
expect(saga.next(response)).toBe(put(actions.setPatch(response.patch)); | |
}); | |
}); | |
}); |
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 { watchPatch } from './sagas/patch'; | |
export function* sagas() { | |
yield [ | |
watchPatch(), | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment