const fetchPosts = () => async dispatch => { try { dispatch(loading()); const posts = await getPosts(); dispatch(fetchSuccess(posts)); } catch (err) { dispatch(fetchError({ err })); } }
const getUsers = (amount) => (dispatch) => { loading(true); axios({ method: "get", url: "https://reqres.in/api/users", }) .then((res) => { dispatch(loading(false)); dispatch(setUsers(res.data)); }) .catch((err) => { dispatch(loading(false)); dispatch(setError(err)); // alert("an error occur"); }); }
describe("user slice", () => { describe("reducer, actions and selectors", () => { it("should return the initial state on first run", () => { const nextState = initialState; const result = reducer(undefined, {}); expect(result).toEqual(nextState); }); it("should loading state change", () => { const nextState = reducer(initialState, loading(true)); const rootState = { users: nextState }; expect(selectLoading(rootState)).toEqual(true); }); it("should user state change", () => { axios({ method: "get", url: "https://reqres.in/api/users", }).then((res) => { const nextState = reducer(initialState, setUsers(res.data)); const rootState = { users: nextState }; expect(selectUsers(rootState)).toEqual(data); }); }); }); });