|
1 | | -import {IStarsAction} from "models/starsModel"; |
2 | | -import {fetchMock, mockStore} from "../../helpers/TestHelper"; |
3 | | -import * as stars from "./starsModule"; |
4 | | - |
5 | | -/** Mock Data */ |
6 | | -const githubResponse = { |
7 | | - stargazers_count: 999 |
8 | | -}; |
9 | | - |
10 | | -const errResponse = { |
11 | | - message: "ERROR :-O" |
12 | | -}; |
13 | | - |
14 | | -/** Stargazers Module */ |
15 | | -describe("Stars Module", () => { |
16 | | - |
17 | | - /** Action Creators */ |
18 | | - describe("Action Creators", () => { |
19 | | - |
20 | | - describe("Get Stars (Async)", () => { |
21 | | - |
22 | | - afterEach(() => { |
23 | | - fetchMock.restore(); |
24 | | - }); |
25 | | - |
26 | | - /** 200 */ |
27 | | - it("dispatches Request and Success Actions on OK requests", (done) => { |
28 | | - |
29 | | - fetchMock.mock("https://api.github.com/repos/barbar/vortigern", { |
30 | | - body: githubResponse, |
31 | | - status: 200 |
32 | | - }); |
33 | | - |
34 | | - const expectedActions: IStarsAction[] = [ |
35 | | - {type: stars.GET_REQUEST}, |
36 | | - {type: stars.GET_SUCCESS, payload: {count: githubResponse.stargazers_count}} |
37 | | - ]; |
38 | | - |
39 | | - const store = mockStore({}); |
40 | | - |
41 | | - store.dispatch(stars.getStars()) |
42 | | - .then(() => expect(store.getActions()).toBe(expectedActions)) |
43 | | - .then(() => done()) |
44 | | - .catch((err) => done(err)); |
45 | | - }); |
46 | | - |
47 | | - /** 400 */ |
48 | | - it("dispatches Failure on failed requests", (done) => { |
49 | | - |
50 | | - fetchMock.mock("https://api.github.com/repos/barbar/vortigern", { |
51 | | - body: errResponse, |
52 | | - status: 400 |
53 | | - }); |
54 | | - |
55 | | - const expectedActions: IStarsAction[] = [ |
56 | | - {type: stars.GET_REQUEST}, |
57 | | - {type: stars.GET_FAILURE, payload: {message: errResponse}} |
58 | | - ]; |
59 | | - |
60 | | - const store = mockStore({}); |
| 1 | +import {STARS_REQUEST, starsReducer} from "./starsModule"; |
| 2 | + |
| 3 | +describe("Stars Reducer", () => { |
| 4 | + |
| 5 | + it("handles action of type STARS_REQUEST_PENDING", () => { |
| 6 | + const action = { |
| 7 | + type: STARS_REQUEST + "_PENDING" |
| 8 | + }; |
| 9 | + const stateBefore = {}; |
| 10 | + const stateAfter = { |
| 11 | + isFetching: true |
| 12 | + }; |
| 13 | + expect(starsReducer(stateBefore, action)).toEqual(stateAfter); |
| 14 | + }); |
61 | 15 |
|
62 | | - store.dispatch(stars.getStars()) |
63 | | - .then(() => expect(store.getActions()).toBe(expectedActions)) |
64 | | - .then(() => done()) |
65 | | - .catch((err) => done(err)); |
66 | | - }); |
| 16 | + it("handles action of type STARS_REQUEST_FULFILLED", () => { |
| 17 | + const action = { |
| 18 | + payload: { |
| 19 | + stargazers_count: 99 |
| 20 | + }, |
| 21 | + type: STARS_REQUEST + "_FULFILLED" |
| 22 | + }; |
| 23 | + const stateBefore = {}; |
| 24 | + const stateAfter = { |
| 25 | + isFetching: false, |
| 26 | + payload: { |
| 27 | + stargazers_count: 99 |
| 28 | + } |
| 29 | + }; |
| 30 | + expect(starsReducer(stateBefore, action)).toEqual(stateAfter); |
| 31 | + }); |
67 | 32 |
|
68 | | - }); |
| 33 | + it("handles action of type STARS_REQUEST_REJECTED", () => { |
| 34 | + const action = { |
| 35 | + type: STARS_REQUEST + "_REJECTED" |
| 36 | + }; |
| 37 | + const stateBefore = {}; |
| 38 | + const stateAfter = { |
| 39 | + error: true, |
| 40 | + isFetching: false |
| 41 | + }; |
| 42 | + expect(starsReducer(stateBefore, action)).toEqual(stateAfter); |
| 43 | + }); |
69 | 44 |
|
| 45 | + it("handles actions with unknown type", () => { |
| 46 | + const action = { |
| 47 | + type: "" |
| 48 | + }; |
| 49 | + const stateBefore = { |
| 50 | + isFetching: false |
| 51 | + }; |
| 52 | + const stateAfter = { |
| 53 | + isFetching: false |
| 54 | + }; |
| 55 | + expect(starsReducer(stateBefore, action)).toEqual(stateAfter); |
70 | 56 | }); |
71 | 57 |
|
72 | 58 | }); |
0 commit comments