Added tests and fixed bugs

This commit is contained in:
Philipp Czora
2018-07-17 11:29:58 +02:00
parent ea8f8b23c5
commit 4ecc4338d6
6 changed files with 55 additions and 33 deletions

View File

@@ -33,7 +33,6 @@ class UserForm extends React.Component<Props, User> {
}
render() {
const { submitForm } = this.props;
const user = this.state;
return (
<div className="container">

View File

@@ -26,7 +26,7 @@ export default class UserRow extends React.Component<Props> {
<DeleteUserButton user={user} deleteUser={deleteUser} />
</td>
<td>
<EditUserButton user={user} editUser={this.props.editUser} />
<EditUserButton user={user} editUser={editUser} />
</td>
</tr>
);

View File

@@ -1,11 +1,11 @@
// @flow
import React from "react";
import UserRow from "./UserRow";
import { editUser } from "../modules/users";
import type { User } from "../types/User";
import type { UserEntry } from "../types/UserEntry";
type Props = {
entries: [{ loading: boolean, error: Error, user: User }],
entries: Array<UserEntry>,
deleteUser: string => void,
editUser: User => void
};

View File

@@ -6,19 +6,14 @@ import {
fetchUsers,
addUser,
updateUser,
editUser,
deleteUser,
editUser,
getUsersFromState
} from "../modules/users";
import UserForm from "./UserForm";
import UserTable from "./UserTable";
import type { User } from "../types/User";
type UserEntry = {
loading: boolean,
error: Error,
user: User
};
import type { UserEntry } from "../types/UserEntry";
type Props = {
login: boolean,
@@ -60,7 +55,7 @@ class Users extends React.Component<Props, User> {
};
render() {
const { userEntries, deleteUser, editUser } = this.props;
const { userEntries, deleteUser, editUser, userToEdit } = this.props;
if (userEntries) {
return (
<section className="section">
@@ -70,13 +65,11 @@ class Users extends React.Component<Props, User> {
<UserTable
entries={userEntries}
deleteUser={deleteUser}
editUser={(user: User) => {
this.props.editUser(user);
}}
editUser={user => editUser(user)}
/>
<UserForm
submitForm={user => this.submitUser(user)}
user={this.props.userToEdit}
user={userToEdit}
/>
</div>
</section>

View File

@@ -1,7 +1,7 @@
// @flow
import { apiClient, NOT_FOUND_ERROR } from "../../apiclient";
import type { User } from "../types/User";
import { ThunkDispatch } from "redux-thunk";
import type { UserEntry } from "../types/UserEntry";
import { Dispatch } from "redux";
export const FETCH_USERS = "scm/users/FETCH";
@@ -148,19 +148,6 @@ function updateUserFailure(user: User, error: Error) {
};
}
export function editUser(user: User) {
return function(dispatch: ThunkDispatch) {
dispatch(requestAddUser(user));
return apiClient
.putWithContentType(USERS_URL + "/" + user.name, user, CONTENT_TYPE_USER)
.then(() => {
dispatch(addUserSuccess());
dispatch(fetchUsers());
})
.catch(err => dispatch(addUserFailure(user, err)));
};
}
function requestDeleteUser(url: string) {
return {
type: DELETE_USER,
@@ -203,7 +190,7 @@ export function getUsersFromState(state) {
if (!userNames) {
return null;
}
var userEntries = new Array();
var userEntries: Array<UserEntry> = [];
for (let userName of userNames) {
userEntries.push(state.users.usersByNames[userName]);
@@ -231,7 +218,7 @@ function extractUsersByNames(
return usersByNames;
}
function editUser(user: User) {
export function editUser(user: User) {
return {
type: EDIT_USER,
user

View File

@@ -10,7 +10,12 @@ import {
FETCH_USERS,
FETCH_USERS_SUCCESS,
fetchUsers,
FETCH_USERS_FAILURE
FETCH_USERS_FAILURE,
updateUser,
UPDATE_USER,
UPDATE_USER_FAILURE,
UPDATE_USER_SUCCESS,
EDIT_USER
} from "./users";
import reducer from "./users";
@@ -152,6 +157,33 @@ describe("fetch tests", () => {
expect(actions[1].payload).toBeDefined();
});
});
test("successful user update", () => {
fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", {
status: 204
});
const store = mockStore({});
return store.dispatch(updateUser(userZaphod)).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(UPDATE_USER);
expect(actions[1].type).toEqual(UPDATE_USER_SUCCESS);
});
});
test("user update failed", () => {
fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", {
status: 500
});
const store = mockStore({});
return store.dispatch(updateUser(userZaphod)).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(UPDATE_USER);
expect(actions[1].type).toEqual(UPDATE_USER_FAILURE);
expect(actions[1].payload).toBeDefined();
});
});
});
describe("reducer tests", () => {
@@ -199,4 +231,15 @@ describe("reducer tests", () => {
expect(newState.usersByNames["zaphod"]).toBeDefined();
expect(newState.usersByNames["ford"]).toBeDefined();
});
test("edit user", () => {
const newState = reducer(
{},
{
type: EDIT_USER,
user: userZaphod
}
);
expect(newState.editUser).toEqual(userZaphod);
});
});