describe('githubRepoFormSaga Saga', () => { const githubRepoFormIterator = githubRepoFormSaga(); it('should start task to watch for loadRepos action', () => { const takeLatestDescriptor = githubRepoFormIterator.next().value; expect(takeLatestDescriptor).toEqual( takeLatest(slice.actions.loadRepos.type, getRepos), ); }); });
const incrementAsync = amount => dispatch => { setTimeout(() => { dispatch(incrementByAmount(amount)); }, 1000); }
updateUser(state, action) { const { firstName, lastName, email } = action.payload; state.user.firstName = firstName; state.user.lastName = lastName; state.user.email = email; state.user.valid = true; }
const addUser = name => dispatch => { dispatch(setAdding()); Api.addUser(name) .then(user => dispatch(addUserAction(user))) .catch(() => dispatch(setAddingError())); }
export function createStore(initialState) { const store = configureStore({ reducer: rootReducer, preloadedState: initialState, }); if (process.env.NODE_ENV === 'development' && module.hot) { module.hot.accept('./rootReducer', () => { const newRootReducer = require('./rootReducer').default; store.replaceReducer(newRootReducer); }); } return store; }
/** * Root saga manages watcher lifecycle */ export function* githubRepoFormSaga() { // Watches for loadRepos actions and calls getRepos when one comes in. // By using `takeLatest` only the result of the latest API call is applied. // It returns task descriptor (just like fork) so we can continue execution // It will be cancelled automatically on component unmount yield takeLatest(actions.loadRepos.type, getRepos); }
it("shouldn't fetch repos on mount if username is empty", () => { store.dispatch(actions.changeUsername('')); store.dispatch(actions.reposLoaded([])); component.unmount(); component = renderGithubRepoForm(store); expect(store.getState().githubRepoForm.loading).toBe(false); });
reposLoaded(state, action) { const repos = action.payload; state.repositories = repos; state.loading = false; }
const fetchBeers = (page = 1) => async (dispatch) => { try { dispatch(getBeersStart()); const beers = await Api.getBeers(page); dispatch(getBeersSuccess({ beers, page })); } catch (err) { dispatch(getBeersFailure(err.toString())); } }
beforeEach(() => { store = configureAppStore(); component = renderGithubRepoForm(store); store.dispatch(actions.reposLoaded([])); expect(store.getState().githubRepoForm).toEqual(initialState); });
const incrementAsync = amount => dispatch => { setTimeout(() => { dispatch(incrementByAmount(amount)); }, 1000); }
repoError(state, action) { state.error = action.payload; state.loading = false; }
const incrementAsync = amount => dispatch => { setTimeout(() => { dispatch(incrementByAmount(amount)); }, 1000); }
beforeEach(() => { store = configureAppStore(); component = renderGithubRepoForm(store); store.dispatch(actions.reposLoaded([])); expect(store.getState().githubRepoForm).toEqual(initialState); });
reposLoaded(state, action) { const repos = action.payload; state.repositories = repos; state.loading = false; }