-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
292 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 notification actions", () => { | ||
beforeEach(() => { | ||
moxios.install(axios); | ||
store = mockStore({}); | ||
}); | ||
|
||
afterEach(() => { | ||
moxios.uninstall(axios); | ||
}); | ||
|
||
describe("test fetch notifications", () => { | ||
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_BASE_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_BASE_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_BASE_URL}/profiles/claude/follow`, { | ||
status: 500, | ||
response: { message: "Error while following the user" } | ||
}); | ||
return store.dispatch(followUser("claude")).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import "@babel/polyfill"; | ||
import axios from "../../utils/axios"; | ||
import { | ||
FOLLOWING_FAILED, | ||
FOLLOWING_SUCCESS, | ||
WAITING_RESPONSE | ||
} from "../actionTypes"; | ||
|
||
export const followed = () => ({ | ||
type: FOLLOWING_SUCCESS, | ||
payload: true | ||
}); | ||
|
||
export const unfollowed = () => ({ | ||
type: FOLLOWING_SUCCESS, | ||
payload: false | ||
}); | ||
|
||
export const failed = () => ({ type: FOLLOWING_FAILED, payload: false }); | ||
|
||
export const followUser = username => async dispatch => { | ||
try { | ||
dispatch({ type: WAITING_RESPONSE }); | ||
const { status } = await axios.post(`/profiles/${username}/follow`); | ||
if (status === 201) dispatch(followed()); | ||
if (status === 202) dispatch(unfollowed()); | ||
} catch (error) { | ||
dispatch(failed()); | ||
} | ||
}; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
FOLLOWING_FAILED, | ||
FOLLOWING_SUCCESS, | ||
WAITING_RESPONSE | ||
} from "../actionTypes"; | ||
|
||
export const initialState = { | ||
isFetching: false, | ||
status: null | ||
}; | ||
|
||
export default (state = initialState, action) => { | ||
const { type, payload } = action; | ||
switch (type) { | ||
case WAITING_RESPONSE: | ||
return { | ||
...state, | ||
isFetching: true | ||
}; | ||
case FOLLOWING_SUCCESS: | ||
return { | ||
...state, | ||
isFetching: false, | ||
status: payload | ||
}; | ||
case FOLLOWING_FAILED: | ||
return { | ||
...state, | ||
isFetching: false, | ||
status: payload | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.