|
| 1 | +/* eslint-disable no-undef */ |
| 2 | +import configureMockStore from "redux-mock-store"; |
| 3 | +import thunk from "redux-thunk"; |
| 4 | +import axios from "axios"; |
| 5 | +import Cookies from "universal-cookie"; |
| 6 | +import mockData from "../constants/mockData"; |
| 7 | +import * as actions from "../actions/index"; |
| 8 | +import * as types from "../constants/ActionTypes"; |
| 9 | +import * as reducers from "../reducers/StoriesReducer"; |
| 10 | + |
| 11 | +const cookies = new Cookies(); |
| 12 | +jest.mock("axios"); |
| 13 | +const middlewares = [thunk.withExtraArgument({ |
| 14 | + api: axios, |
| 15 | + cookies |
| 16 | +})]; |
| 17 | +const mockStore = configureMockStore(middlewares); |
| 18 | + |
| 19 | +const store = mockStore(reducers.INITIAL_STATE); |
| 20 | + |
| 21 | +describe("TESTING ACTIONS", () => { |
| 22 | + const response = mockData; |
| 23 | + const payload = response.hits[0]; |
| 24 | + const expectedAction = [ |
| 25 | + { |
| 26 | + type: types.FETCH_STORIES_PENDING |
| 27 | + }, { |
| 28 | + type: types.FETCH_STORIES_SUCCESS, |
| 29 | + payload: response |
| 30 | + }, |
| 31 | + { |
| 32 | + type: types.UPVOTE_STORY, |
| 33 | + payload: payload.objectID |
| 34 | + } |
| 35 | + ]; |
| 36 | + axios.get.mockResolvedValue({ |
| 37 | + ...response |
| 38 | + }); |
| 39 | + it(`${types.UPVOTE_STORY} ->`, () => { |
| 40 | + store |
| 41 | + .dispatch(actions.fetchStories(1)) |
| 42 | + .then(() => expect(actions.updateUpVote(payload)).toEqual(expectedAction)); |
| 43 | + }); |
| 44 | + |
| 45 | + it(`${types.HIDE_STORY} ->`, () => { |
| 46 | + store |
| 47 | + .dispatch(actions.fetchStories(1)) |
| 48 | + .then(() => expect(actions.hideStory(payload)).toEqual(expectedAction)); |
| 49 | + }); |
| 50 | +}); |
0 commit comments