Cleanup in users.js

This commit is contained in:
Philipp Czora
2018-07-24 16:45:09 +02:00
parent 1237f49d9f
commit 181a9e9786
3 changed files with 81 additions and 34 deletions

View File

@@ -0,0 +1,5 @@
// @flow
export type Action = {
type: string,
payload?: any
};

View File

@@ -3,6 +3,7 @@ import { apiClient } from "../../apiclient";
import type { User } from "../types/User"; import type { User } from "../types/User";
import type { UserEntry } from "../types/UserEntry"; import type { UserEntry } from "../types/UserEntry";
import { Dispatch } from "redux"; import { Dispatch } from "redux";
import { Action } from "../../types/Action";
export const FETCH_USERS_PENDING = "scm/users/FETCH_USERS_PENDING"; export const FETCH_USERS_PENDING = "scm/users/FETCH_USERS_PENDING";
export const FETCH_USERS_SUCCESS = "scm/users/FETCH_USERS_SUCCESS"; export const FETCH_USERS_SUCCESS = "scm/users/FETCH_USERS_SUCCESS";
@@ -25,6 +26,7 @@ export const DELETE_USER_SUCCESS = "scm/users/DELETE_SUCCESS";
export const DELETE_USER_FAILURE = "scm/users/DELETE_FAILURE"; export const DELETE_USER_FAILURE = "scm/users/DELETE_FAILURE";
const USERS_URL = "users"; const USERS_URL = "users";
const USER_URL = "users/";
const CONTENT_TYPE_USER = "application/vnd.scmm-user+json;v=2"; const CONTENT_TYPE_USER = "application/vnd.scmm-user+json;v=2";
@@ -53,20 +55,20 @@ export function fetchUsers() {
}; };
} }
export function fetchUsersPending() { export function fetchUsersPending(): Action {
return { return {
type: FETCH_USERS_PENDING type: FETCH_USERS_PENDING
}; };
} }
export function fetchUsersSuccess(users: any) { export function fetchUsersSuccess(users: any): Action {
return { return {
type: FETCH_USERS_SUCCESS, type: FETCH_USERS_SUCCESS,
payload: users payload: users
}; };
} }
export function fetchUsersFailure(url: string, error: Error) { export function fetchUsersFailure(url: string, error: Error): Action {
return { return {
type: FETCH_USERS_FAILURE, type: FETCH_USERS_FAILURE,
payload: { payload: {
@@ -81,7 +83,7 @@ export function fetchUsersFailure(url: string, error: Error) {
export function fetchUser(name: string) { export function fetchUser(name: string) {
const userUrl = USERS_URL + "/" + name; const userUrl = USERS_URL + "/" + name;
return function(dispatch: any) { return function(dispatch: any) {
dispatch(fetchUsersPending()); dispatch(fetchUserPending(name));
return apiClient return apiClient
.get(userUrl) .get(userUrl)
.then(response => { .then(response => {
@@ -97,30 +99,32 @@ export function fetchUser(name: string) {
}) })
.catch(cause => { .catch(cause => {
const error = new Error(`could not fetch user: ${cause.message}`); const error = new Error(`could not fetch user: ${cause.message}`);
dispatch(fetchUsersFailure(USERS_URL, error)); dispatch(fetchUserFailure(USERS_URL, error));
}); });
}; };
} }
export function fetchUserPending(name: string) { export function fetchUserPending(name: string): Action {
return { return {
type: FETCH_USER_PENDING, type: FETCH_USER_PENDING,
payload: { name } payload: { name }
}; };
} }
export function fetchUserSuccess(user: any) { export function fetchUserSuccess(user: any): Action {
return { return {
type: FETCH_USER_SUCCESS, type: FETCH_USER_SUCCESS,
payload: user payload: user
}; };
} }
export function fetchUserFailure(user: User, error: Error) { export function fetchUserFailure(username: string, error: Error): Action {
return { return {
type: FETCH_USER_FAILURE, type: FETCH_USER_FAILURE,
user, payload: {
error username,
error
}
}; };
} }
@@ -146,20 +150,20 @@ export function createUser(user: User) {
}; };
} }
export function createUserPending(user: User) { export function createUserPending(user: User): Action {
return { return {
type: CREATE_USER_PENDING, type: CREATE_USER_PENDING,
user user
}; };
} }
export function createUserSuccess() { export function createUserSuccess(): Action {
return { return {
type: CREATE_USER_SUCCESS type: CREATE_USER_SUCCESS
}; };
} }
export function createUserFailure(user: User, err: Error) { export function createUserFailure(user: User, err: Error): Action {
return { return {
type: CREATE_USER_FAILURE, type: CREATE_USER_FAILURE,
payload: err, payload: err,
@@ -184,25 +188,31 @@ export function modifyUser(user: User) {
}; };
} }
function modifyUserPending(user: User) { function modifyUserPending(user: User): Action {
return { return {
type: MODIFY_USER_PENDING, type: MODIFY_USER_PENDING,
user payload: {
user
}
}; };
} }
function modifyUserSuccess(user: User) { function modifyUserSuccess(user: User): Action {
return { return {
type: MODIFY_USER_SUCCESS, type: MODIFY_USER_SUCCESS,
user payload: {
user
}
}; };
} }
export function modifyUserFailure(user: User, error: Error) { export function modifyUserFailure(user: User, error: Error): Action {
return { return {
type: MODIFY_USER_FAILURE, type: MODIFY_USER_FAILURE,
payload: error, payload: {
user error,
user
}
}; };
} }
@@ -226,21 +236,21 @@ export function deleteUser(user: User) {
}; };
} }
export function deleteUserPending(user: User) { export function deleteUserPending(user: User): Action {
return { return {
type: DELETE_USER, type: DELETE_USER,
payload: user payload: user
}; };
} }
export function deleteUserSuccess(user: User) { export function deleteUserSuccess(user: User): Action {
return { return {
type: DELETE_USER_SUCCESS, type: DELETE_USER_SUCCESS,
payload: user payload: user
}; };
} }
export function deleteUserFailure(user: User, error: Error) { export function deleteUserFailure(user: User, error: Error): Action {
return { return {
type: DELETE_USER_FAILURE, type: DELETE_USER_FAILURE,
payload: { payload: {
@@ -380,9 +390,9 @@ export default function reducer(state: any = {}, action: any = {}) {
usersByNames: ubn usersByNames: ubn
}; };
case FETCH_USER_FAILURE: case FETCH_USER_FAILURE:
return reduceUsersByNames(state, action.user.name, { return reduceUsersByNames(state, action.payload.username, {
loading: true, loading: true,
error: action.error error: action.payload.error
}); });
// Delete single user cases // Delete single user cases
case DELETE_USER: case DELETE_USER:

View File

@@ -24,6 +24,10 @@ import {
deleteUser, deleteUser,
fetchUsersFailure, fetchUsersFailure,
fetchUsersSuccess, fetchUsersSuccess,
fetchUser,
FETCH_USER_PENDING,
FETCH_USER_SUCCESS,
FETCH_USER_FAILURE,
createUser, createUser,
createUserSuccess, createUserSuccess,
createUserFailure, createUserFailure,
@@ -131,6 +135,8 @@ const response = {
responseBody responseBody
}; };
const USERS_URL = "/scm/api/rest/v2/users";
describe("users fetch()", () => { describe("users fetch()", () => {
const mockStore = configureMockStore([thunk]); const mockStore = configureMockStore([thunk]);
afterEach(() => { afterEach(() => {
@@ -139,7 +145,7 @@ describe("users fetch()", () => {
}); });
it("should successfully fetch users", () => { it("should successfully fetch users", () => {
fetchMock.getOnce("/scm/api/rest/v2/users", response); fetchMock.getOnce(USERS_URL, response);
const expectedActions = [ const expectedActions = [
{ type: FETCH_USERS_PENDING }, { type: FETCH_USERS_PENDING },
@@ -157,7 +163,7 @@ describe("users fetch()", () => {
}); });
it("should fail getting users on HTTP 500", () => { it("should fail getting users on HTTP 500", () => {
fetchMock.getOnce("/scm/api/rest/v2/users", { fetchMock.getOnce(USERS_URL, {
status: 500 status: 500
}); });
@@ -170,14 +176,40 @@ describe("users fetch()", () => {
}); });
}); });
it("should sucessfully fetch single user", () => {
fetchMock.getOnce(USERS_URL + "/zaphod", userZaphod);
const store = mockStore({});
return store.dispatch(fetchUser("zaphod")).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
expect(actions[1].type).toEqual(FETCH_USER_SUCCESS);
expect(actions[1].payload).toBeDefined();
});
});
it("should fail fetching single user on HTTP 500", () => {
fetchMock.getOnce(USERS_URL + "/zaphod", {
status: 500
});
const store = mockStore({});
return store.dispatch(fetchUser("zaphod")).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
expect(actions[1].type).toEqual(FETCH_USER_FAILURE);
expect(actions[1].payload).toBeDefined();
});
});
it("should add a user successfully", () => { it("should add a user successfully", () => {
// unmatched // unmatched
fetchMock.postOnce("/scm/api/rest/v2/users", { fetchMock.postOnce(USERS_URL, {
status: 204 status: 204
}); });
// after create, the users are fetched again // after create, the users are fetched again
fetchMock.getOnce("/scm/api/rest/v2/users", response); fetchMock.getOnce(USERS_URL, response);
const store = mockStore({}); const store = mockStore({});
return store.dispatch(createUser(userZaphod)).then(() => { return store.dispatch(createUser(userZaphod)).then(() => {
@@ -189,7 +221,7 @@ describe("users fetch()", () => {
}); });
it("should fail adding a user on HTTP 500", () => { it("should fail adding a user on HTTP 500", () => {
fetchMock.postOnce("/scm/api/rest/v2/users", { fetchMock.postOnce(USERS_URL, {
status: 500 status: 500
}); });
@@ -207,7 +239,7 @@ describe("users fetch()", () => {
status: 204 status: 204
}); });
// after update, the users are fetched again // after update, the users are fetched again
fetchMock.getOnce("/scm/api/rest/v2/users", response); fetchMock.getOnce(USERS_URL, response);
const store = mockStore({}); const store = mockStore({});
return store.dispatch(modifyUser(userZaphod)).then(() => { return store.dispatch(modifyUser(userZaphod)).then(() => {
@@ -237,7 +269,7 @@ describe("users fetch()", () => {
status: 204 status: 204
}); });
// after update, the users are fetched again // after update, the users are fetched again
fetchMock.getOnce("/scm/api/rest/v2/users", response); fetchMock.getOnce(USERS_URL, response);
const store = mockStore({}); const store = mockStore({});
return store.dispatch(deleteUser(userZaphod)).then(() => { return store.dispatch(deleteUser(userZaphod)).then(() => {
@@ -411,10 +443,10 @@ describe("users reducer", () => {
expect(newState.usersByNames["zaphod"].loading).toBeTruthy(); expect(newState.usersByNames["zaphod"].loading).toBeTruthy();
}); });
it("should uppdate state according to FETCH_USER_FAILURE action", () => { it("should update state according to FETCH_USER_FAILURE action", () => {
const newState = reducer( const newState = reducer(
{}, {},
fetchUserFailure(userFord, new Error("kaputt!")) fetchUserFailure(userFord.name, new Error("kaputt!"))
); );
expect(newState.usersByNames["ford"].error).toBeTruthy; expect(newState.usersByNames["ford"].error).toBeTruthy;
}); });