use list/byNames in user state, merged heads

This commit is contained in:
Philipp Czora
2018-07-25 13:57:24 +02:00
parent fe0b7ea986
commit 7b921da174
4 changed files with 50 additions and 64 deletions

View File

@@ -153,8 +153,6 @@ describe("auth actions", () => {
status: 400 status: 400
}); });
const expectedActions = [{ type: LOGIN_REQUEST }, { type: LOGIN_FAILURE }];
const store = mockStore({}); const store = mockStore({});
return store.dispatch(login("tricia", "secret123")).then(() => { return store.dispatch(login("tricia", "secret123")).then(() => {
const actions = store.getActions(); const actions = store.getActions();
@@ -244,11 +242,6 @@ describe("auth actions", () => {
status: 500 status: 500
}); });
const expectedActions = [
{ type: LOGOUT_REQUEST },
{ type: LOGOUT_FAILURE }
];
const store = mockStore({}); const store = mockStore({});
return store.dispatch(logout()).then(() => { return store.dispatch(logout()).then(() => {
const actions = store.getActions(); const actions = store.getActions();

View File

@@ -1,5 +1,5 @@
import React from "react"; import React from "react";
import { configure, shallow } from "enzyme"; import { shallow } from "enzyme";
import "../../tests/enzyme"; import "../../tests/enzyme";
import "../../tests/i18n"; import "../../tests/i18n";
import EditUserButton from "./EditUserButton"; import EditUserButton from "./EditUserButton";

View File

@@ -272,7 +272,7 @@ export function getUsersFromState(state: any) {
const userEntries: Array<UserEntry> = []; const userEntries: Array<UserEntry> = [];
for (let userName of userNames) { for (let userName of userNames) {
userEntries.push(state.users.usersByNames[userName]); userEntries.push(state.users.byNames[userName]);
} }
return userEntries; return userEntries;
@@ -318,13 +318,13 @@ const reduceUsersByNames = (
newUserState: any newUserState: any
) => { ) => {
const newUsersByNames = { const newUsersByNames = {
...state.usersByNames, ...state.byNames,
[username]: newUserState [username]: newUserState
}; };
return { return {
...state, ...state,
usersByNames: newUsersByNames byNames: newUsersByNames
}; };
}; };
@@ -342,25 +342,21 @@ export default function reducer(state: any = {}, action: any = {}) {
// return red(state, action.payload._embedded.users); // return red(state, action.payload._embedded.users);
const users = action.payload._embedded.users; const users = action.payload._embedded.users;
const userNames = users.map(user => user.name); const userNames = users.map(user => user.name);
const usersByNames = extractUsersByNames( const byNames = extractUsersByNames(users, userNames, state.byNames);
users,
userNames,
state.usersByNames
);
return { return {
...state, ...state,
users: {
userCreatePermission: action.payload._links.create ? true : false, userCreatePermission: action.payload._links.create ? true : false,
list: {
error: null, error: null,
entries: userNames, entries: userNames,
loading: false loading: false
}, },
usersByNames byNames
}; };
case FETCH_USERS_FAILURE: case FETCH_USERS_FAILURE:
return { return {
...state, ...state,
users: { list: {
...state.users, ...state.users,
loading: false, loading: false,
error: action.payload.error error: action.payload.error
@@ -421,8 +417,8 @@ export default function reducer(state: any = {}, action: any = {}) {
case CREATE_USER_PENDING: case CREATE_USER_PENDING:
return { return {
...state, ...state,
users: { list: {
...state.users, ...state.list,
loading: true, loading: true,
error: null error: null
} }
@@ -430,7 +426,7 @@ export default function reducer(state: any = {}, action: any = {}) {
case CREATE_USER_SUCCESS: case CREATE_USER_SUCCESS:
return { return {
...state, ...state,
users: { list: {
...state.users, ...state.users,
loading: false, loading: false,
error: null error: null
@@ -439,7 +435,7 @@ export default function reducer(state: any = {}, action: any = {}) {
case CREATE_USER_FAILURE: case CREATE_USER_FAILURE:
return { return {
...state, ...state,
users: { list: {
...state.users, ...state.users,
loading: false, loading: false,
error: action.payload error: action.payload

View File

@@ -32,11 +32,11 @@ import {
createUserSuccess, createUserSuccess,
createUserFailure, createUserFailure,
modifyUserFailure, modifyUserFailure,
fetchUserSuccess,
deleteUserSuccess, deleteUserSuccess,
fetchUsersPending, fetchUsersPending,
fetchUserPending, fetchUserPending,
fetchUserFailure, fetchUserFailure
fetchUserSuccess
} from "./users"; } from "./users";
import reducer from "./users"; import reducer from "./users";
@@ -209,6 +209,9 @@ describe("users fetch()", () => {
status: 204 status: 204
}); });
// after create, the users are fetched again
fetchMock.getOnce(USERS_URL, response);
const store = mockStore({}); const store = mockStore({});
return store.dispatch(createUser(userZaphod)).then(() => { return store.dispatch(createUser(userZaphod)).then(() => {
const actions = store.getActions(); const actions = store.getActions();
@@ -283,6 +286,8 @@ describe("users fetch()", () => {
fetchMock.deleteOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", { fetchMock.deleteOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", {
status: 204 status: 204
}); });
// after update, the users are fetched again
fetchMock.getOnce(USERS_URL, response);
const store = mockStore({}); const store = mockStore({});
return store.dispatch(deleteUser(userZaphod)).then(() => { return store.dispatch(deleteUser(userZaphod)).then(() => {
@@ -319,14 +324,13 @@ describe("users reducer", () => {
it("should update state correctly according to FETCH_USERS_SUCCESS action", () => { it("should update state correctly according to FETCH_USERS_SUCCESS action", () => {
const newState = reducer({}, fetchUsersSuccess(responseBody)); const newState = reducer({}, fetchUsersSuccess(responseBody));
expect(newState.users).toEqual({ expect(newState.list).toEqual({
entries: ["zaphod", "ford"], entries: ["zaphod", "ford"],
error: null, error: null,
loading: false, loading: false
userCreatePermission: true
}); });
expect(newState.usersByNames).toEqual({ expect(newState.byNames).toEqual({
zaphod: { zaphod: {
entry: userZaphod entry: userZaphod
}, },
@@ -334,6 +338,8 @@ describe("users reducer", () => {
entry: userFord entry: userFord
} }
}); });
expect(newState.userCreatePermission).toBeTruthy();
}); });
test("should update state correctly according to DELETE_USER action", () => { test("should update state correctly according to DELETE_USER action", () => {
@@ -348,7 +354,7 @@ describe("users reducer", () => {
}; };
const newState = reducer(state, deleteUserPending(userZaphod)); const newState = reducer(state, deleteUserPending(userZaphod));
const zaphod = newState.usersByNames["zaphod"]; const zaphod = newState.byNames["zaphod"];
expect(zaphod.loading).toBeTruthy(); expect(zaphod.loading).toBeTruthy();
expect(zaphod.entry).toBe(userZaphod); expect(zaphod.entry).toBe(userZaphod);
}); });
@@ -384,7 +390,7 @@ describe("users reducer", () => {
const error = new Error("error"); const error = new Error("error");
const newState = reducer(state, deleteUserFailure(userZaphod, error)); const newState = reducer(state, deleteUserFailure(userZaphod, error));
const zaphod = newState.usersByNames["zaphod"]; const zaphod = newState.byNames["zaphod"];
expect(zaphod.loading).toBeFalsy(); expect(zaphod.loading).toBeFalsy();
expect(zaphod.error).toBe(error); expect(zaphod.error).toBe(error);
}); });
@@ -409,9 +415,9 @@ describe("users reducer", () => {
expect(ford.loading).toBeFalsy(); expect(ford.loading).toBeFalsy();
}); });
it("should not replace whole usersByNames map when fetching users", () => { it("should not replace whole byNames map when fetching users", () => {
const oldState = { const oldState = {
usersByNames: { byNames: {
ford: { ford: {
entry: userFord entry: userFord
} }
@@ -419,40 +425,40 @@ describe("users reducer", () => {
}; };
const newState = reducer(oldState, fetchUsersSuccess(responseBody)); const newState = reducer(oldState, fetchUsersSuccess(responseBody));
expect(newState.usersByNames["zaphod"]).toBeDefined(); expect(newState.byNames["zaphod"]).toBeDefined();
expect(newState.usersByNames["ford"]).toBeDefined(); expect(newState.byNames["ford"]).toBeDefined();
}); });
it("should set userCreatePermission to true if update link is present", () => { it("should set userCreatePermission to true if update link is present", () => {
const newState = reducer({}, fetchUsersSuccess(responseBody)); const newState = reducer({}, fetchUsersSuccess(responseBody));
expect(newState.users.userCreatePermission).toBeTruthy(); expect(newState.userCreatePermission).toBeTruthy();
}); });
it("should update state correctly according to CREATE_USER_PENDING action", () => { it("should update state correctly according to CREATE_USER_PENDING action", () => {
const newState = reducer({}, createUserPending(userZaphod)); const newState = reducer({}, createUserPending(userZaphod));
expect(newState.users.loading).toBeTruthy(); expect(newState.list.loading).toBeTruthy();
expect(newState.users.error).toBeNull(); expect(newState.list.error).toBeNull();
}); });
it("should update state correctly according to CREATE_USER_SUCCESS action", () => { it("should update state correctly according to CREATE_USER_SUCCESS action", () => {
const newState = reducer({ loading: true }, createUserSuccess()); const newState = reducer({ loading: true }, createUserSuccess());
expect(newState.users.loading).toBeFalsy(); expect(newState.list.loading).toBeFalsy();
expect(newState.users.error).toBeNull(); expect(newState.list.error).toBeNull();
}); });
it("should set the loading to false and the error if user could not be added", () => { it("should set the loading to false and the error if user could not be created", () => {
const newState = reducer( const newState = reducer(
{ loading: true, error: null }, { loading: true, error: null },
createUserFailure(userFord, new Error("kaputt kaputt")) createUserFailure(userFord, new Error("kaputt kaputt"))
); );
expect(newState.users.loading).toBeFalsy(); expect(newState.list.loading).toBeFalsy();
expect(newState.users.error).toEqual(new Error("kaputt kaputt")); expect(newState.list.error).toEqual(new Error("kaputt kaputt"));
}); });
it("should update state according to FETCH_USER_PENDING action", () => { it("should update state according to FETCH_USER_PENDING action", () => {
const newState = reducer({}, fetchUserPending("zaphod")); const newState = reducer({}, fetchUserPending("zaphod"));
expect(newState.usersByNames["zaphod"].loading).toBeTruthy(); expect(newState.byNames["zaphod"].loading).toBeTruthy();
}); });
it("should not affect users state", () => { it("should not affect users state", () => {
@@ -464,43 +470,34 @@ describe("users reducer", () => {
}, },
fetchUserPending("zaphod") fetchUserPending("zaphod")
); );
expect(newState.usersByNames["zaphod"].loading).toBeTruthy(); expect(newState.byNames["zaphod"].loading).toBeTruthy();
expect(newState.users.entries).toEqual(["ford"]); expect(newState.users.entries).toEqual(["ford"]);
}); });
it("should update state according to FETCH_USER_FAILURE action", () => { it("should update state according to FETCH_USER_FAILURE action", () => {
const error = new Error("kaputt!"); const error = new Error("kaputt!");
const newState = reducer( const newState = reducer({}, fetchUserFailure(userFord.name, error));
{ expect(newState.byNames["ford"].error).toBe(error);
usersByNames: { expect(newState.byNames["ford"].loading).toBeFalsy();
ford: {
loading: true
}
}
},
fetchUserFailure(userFord.name, error)
);
expect(newState.usersByNames["ford"].error).toBe(error);
expect(newState.usersByNames["ford"].loading).toBeFalsy();
}); });
it("should update state according to FETCH_USER_SUCCESS action", () => { it("should update state according to FETCH_USER_SUCCESS action", () => {
const newState = reducer({}, fetchUserSuccess(userFord)); const newState = reducer({}, fetchUserSuccess(userFord));
expect(newState.usersByNames["ford"].loading).toBeFalsy(); expect(newState.byNames["ford"].loading).toBeFalsy();
expect(newState.usersByNames["ford"].entry).toBe(userFord); expect(newState.byNames["ford"].entry).toBe(userFord);
}); });
it("should affect users state nor the state of other users", () => { it("should affect users state nor the state of other users", () => {
const newState = reducer( const newState = reducer(
{ {
users: { list: {
entries: ["zaphod"] entries: ["zaphod"]
} }
}, },
fetchUserSuccess(userFord) fetchUserSuccess(userFord)
); );
expect(newState.usersByNames["ford"].loading).toBeFalsy(); expect(newState.byNames["ford"].loading).toBeFalsy();
expect(newState.usersByNames["ford"].entry).toBe(userFord); expect(newState.byNames["ford"].entry).toBe(userFord);
expect(newState.users.entries).toEqual(["zaphod"]); expect(newState.list.entries).toEqual(["zaphod"]);
}); });
}); });