Skip to content

Commit

Permalink
Merge 4620eb6 into f07ed2b
Browse files Browse the repository at this point in the history
  • Loading branch information
Musigwa committed May 9, 2019
2 parents f07ed2b + 4620eb6 commit 693e5f1
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 112 deletions.
72 changes: 72 additions & 0 deletions src/__tests__/__actions__/following.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import moxios from "moxios";
import thunk from "redux-thunk";
import configureMockStore from "redux-mock-store";
import axios from "../../utils/axios";

import { followUser } from "../../redux/actions/followingActions";
import {
FOLLOWING_FAILED,
FOLLOWING_SUCCESS,
WAITING_RESPONSE
} from "../../redux/actionTypes";

const mockStore = configureMockStore([thunk]);
let store;

describe("test the following actions", () => {
beforeEach(() => {
moxios.install(axios);
store = mockStore({});
});

afterEach(() => {
moxios.uninstall(axios);
});

describe("test fetch following", () => {
test("should dispatch the success action after successfully following the user", () => {
store = mockStore({});
const expectedActions = [
{ type: WAITING_RESPONSE },
{ type: FOLLOWING_SUCCESS, payload: true }
];
moxios.stubRequest(`${process.env.API_URL}/profiles/claude/follow`, {
status: 201,
response: { message: "Follow successful" }
});
return store.dispatch(followUser("claude")).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});

test("should dispatch the success action after successfully unfollowing the user", () => {
store = mockStore({});
const expectedActions = [
{ type: WAITING_RESPONSE },
{ type: FOLLOWING_SUCCESS, payload: false }
];
moxios.stubRequest(`${process.env.API_URL}/profiles/claude/follow`, {
status: 202,
response: { message: "You have unfollowed this author" }
});
return store.dispatch(followUser("claude")).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});

test("should dispatch the failed action if there was a problem while following the user", () => {
store = mockStore({});
const expectedActions = [
{ type: WAITING_RESPONSE },
{ type: FOLLOWING_FAILED, payload: false }
];
moxios.stubRequest(`${process.env.API_URL}/profiles/claude/follow`, {
status: 500,
response: { message: "Error while following the user" }
});
return store.dispatch(followUser("claude")).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
});
});
24 changes: 23 additions & 1 deletion src/__tests__/__mocks__/testData.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export const props1 = {
image: "",
username: "Yves2019"
},
following: {
status: true,
isFetching: false
},
followAuthor: jest.fn(),
followUser: jest.fn(),
history: {
push: jest.fn()
},
Expand All @@ -55,7 +61,7 @@ export const props1 = {
createdAt: "2019-04-20T09:37:50.006Z",
readTime: 1,
author: {
username: "Yves2019",
username: "Yves2018",
firstName: "Kagarama",
lastName: "Iraguha"
},
Expand Down Expand Up @@ -86,6 +92,10 @@ export const props2 = {
image: "",
username: "Yves2019"
},
following: {
status: true,
isFetching: false
},
history: {
push: jest.fn()
},
Expand Down Expand Up @@ -134,6 +144,10 @@ export const props3 = {
image: "",
username: "John203"
},
following: {
status: true,
isFetching: false
},
history: {
push: jest.fn()
},
Expand All @@ -157,6 +171,10 @@ export const props4 = {
image: "",
username: "John203"
},
following: {
status: true,
isFetching: false
},
history: {
push: jest.fn()
},
Expand All @@ -180,6 +198,10 @@ export const props5 = {
image: "",
username: "John203"
},
following: {
status: true,
isFetching: false
},
history: {
push: jest.fn()
},
Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/__reducers__/authReducers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LOGIN_FAILED,
LOGIN_SUCCESS,
LOGIN_INPUT_CHANGE,
SET_CURRENT_USER,
SUBMITTING_LOGIN_CREDENTIALS,
IS_OPENING_SOCIAL_AUTH_PROVIDER,
CANCEL_SOCIAL_AUTH
Expand All @@ -19,6 +20,7 @@ describe("Login reducers", () => {
...INITIAL_STATE
});
});

it("should handle LOGIN_INPUT_CHANGE", () => {
expect(
loginReducers(INITIAL_STATE, {
Expand All @@ -39,6 +41,7 @@ describe("Login reducers", () => {
[passwordInput.name]: passwordInput.value
});
});

it("should handle SUBMITTING_LOGIN_CREDENTIALS", () => {
expect(
loginReducers(INITIAL_STATE, {
Expand All @@ -49,6 +52,7 @@ describe("Login reducers", () => {
isSubmitting: true
});
});

it("should handle LOGIN_FAILED", () => {
const errorPayload = { message: "Invalid email or password", errors: {} };
expect(
Expand All @@ -61,6 +65,7 @@ describe("Login reducers", () => {
errors: { message: errorPayload.message, ...errorPayload.errors }
});
});

it("should handle LOGIN_SUCCESS", () => {
const successPayload = {
message: "Sign in successfully",
Expand All @@ -77,6 +82,7 @@ describe("Login reducers", () => {
token: successPayload.token
});
});

it("should handle IS_OPENING_SOCIAL_AUTH_PROVIDER", () => {
expect(
loginReducers(INITIAL_STATE, {
Expand All @@ -95,4 +101,16 @@ describe("Login reducers", () => {
isSubmitting: false
});
});

it("should handle SET_CURRENT_USER", () => {
expect(
loginReducers(INITIAL_STATE, {
type: SET_CURRENT_USER,
payload: { name: "musigwa" }
})
).toEqual({
...INITIAL_STATE,
currentUser: { name: "musigwa" }
});
});
});
48 changes: 48 additions & 0 deletions src/__tests__/__reducers__/following.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import followingReducer, {
initialState
} from "../../redux/reducers/followingReducer";
import {
FOLLOWING_FAILED,
FOLLOWING_SUCCESS,
WAITING_RESPONSE
} from "../../redux/actionTypes";

describe("Following reducers", () => {
it("should return initial state", () => {
expect(followingReducer(undefined, {})).toEqual(initialState);
});

it("should handle FOLLOWING", () => {
expect(followingReducer(initialState, { type: WAITING_RESPONSE })).toEqual({
...initialState,
isFetching: true
});
});

it("should handle FOLLOWING SUCCESS, if followed, the status must be true", () => {
expect(
followingReducer(initialState, {
type: FOLLOWING_SUCCESS,
payload: true
})
).toEqual({ ...initialState, status: true });
});

it("should handle FOLLOWING SUCCESS, if unfollowed, the status must be false", () => {
expect(
followingReducer(initialState, {
type: FOLLOWING_SUCCESS,
payload: false
})
).toEqual({ ...initialState, status: false });
});

it("should handle WAITING_RESPONSE FAILED", () => {
expect(
followingReducer(initialState, {
type: FOLLOWING_FAILED,
payload: false
})
).toEqual({ ...initialState, status: false });
});
});
Loading

0 comments on commit 693e5f1

Please sign in to comment.