const loadUsers = () => dispatch => { dispatch(setLoading()); Api.getUsers() .then(users => dispatch(setUsers(users))) .catch(() => dispatch(setError())); }
const getUsers = () => dispatch => { axios.get("https://jsonplaceholder.typicode.com/users") .then((response) => { dispatch(setUsers({response})) }) .catch((response) => {return Promise.reject(response);}); }
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); }); }); }); });