mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 15:05:44 +01:00
Added tests and fixed bugs
This commit is contained in:
@@ -33,7 +33,6 @@ class UserForm extends React.Component<Props, User> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { submitForm } = this.props;
|
|
||||||
const user = this.state;
|
const user = this.state;
|
||||||
return (
|
return (
|
||||||
<div className="container">
|
<div className="container">
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export default class UserRow extends React.Component<Props> {
|
|||||||
<DeleteUserButton user={user} deleteUser={deleteUser} />
|
<DeleteUserButton user={user} deleteUser={deleteUser} />
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<EditUserButton user={user} editUser={this.props.editUser} />
|
<EditUserButton user={user} editUser={editUser} />
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import UserRow from "./UserRow";
|
import UserRow from "./UserRow";
|
||||||
import { editUser } from "../modules/users";
|
|
||||||
import type { User } from "../types/User";
|
import type { User } from "../types/User";
|
||||||
|
import type { UserEntry } from "../types/UserEntry";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
entries: [{ loading: boolean, error: Error, user: User }],
|
entries: Array<UserEntry>,
|
||||||
deleteUser: string => void,
|
deleteUser: string => void,
|
||||||
editUser: User => void
|
editUser: User => void
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,19 +6,14 @@ import {
|
|||||||
fetchUsers,
|
fetchUsers,
|
||||||
addUser,
|
addUser,
|
||||||
updateUser,
|
updateUser,
|
||||||
editUser,
|
|
||||||
deleteUser,
|
deleteUser,
|
||||||
|
editUser,
|
||||||
getUsersFromState
|
getUsersFromState
|
||||||
} from "../modules/users";
|
} from "../modules/users";
|
||||||
import UserForm from "./UserForm";
|
import UserForm from "./UserForm";
|
||||||
import UserTable from "./UserTable";
|
import UserTable from "./UserTable";
|
||||||
import type { User } from "../types/User";
|
import type { User } from "../types/User";
|
||||||
|
import type { UserEntry } from "../types/UserEntry";
|
||||||
type UserEntry = {
|
|
||||||
loading: boolean,
|
|
||||||
error: Error,
|
|
||||||
user: User
|
|
||||||
};
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
login: boolean,
|
login: boolean,
|
||||||
@@ -60,7 +55,7 @@ class Users extends React.Component<Props, User> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { userEntries, deleteUser, editUser } = this.props;
|
const { userEntries, deleteUser, editUser, userToEdit } = this.props;
|
||||||
if (userEntries) {
|
if (userEntries) {
|
||||||
return (
|
return (
|
||||||
<section className="section">
|
<section className="section">
|
||||||
@@ -70,13 +65,11 @@ class Users extends React.Component<Props, User> {
|
|||||||
<UserTable
|
<UserTable
|
||||||
entries={userEntries}
|
entries={userEntries}
|
||||||
deleteUser={deleteUser}
|
deleteUser={deleteUser}
|
||||||
editUser={(user: User) => {
|
editUser={user => editUser(user)}
|
||||||
this.props.editUser(user);
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
<UserForm
|
<UserForm
|
||||||
submitForm={user => this.submitUser(user)}
|
submitForm={user => this.submitUser(user)}
|
||||||
user={this.props.userToEdit}
|
user={userToEdit}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import { apiClient, NOT_FOUND_ERROR } from "../../apiclient";
|
import { apiClient, NOT_FOUND_ERROR } from "../../apiclient";
|
||||||
import type { User } from "../types/User";
|
import type { User } from "../types/User";
|
||||||
import { ThunkDispatch } from "redux-thunk";
|
import type { UserEntry } from "../types/UserEntry";
|
||||||
import { Dispatch } from "redux";
|
import { Dispatch } from "redux";
|
||||||
|
|
||||||
export const FETCH_USERS = "scm/users/FETCH";
|
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) {
|
function requestDeleteUser(url: string) {
|
||||||
return {
|
return {
|
||||||
type: DELETE_USER,
|
type: DELETE_USER,
|
||||||
@@ -203,7 +190,7 @@ export function getUsersFromState(state) {
|
|||||||
if (!userNames) {
|
if (!userNames) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var userEntries = new Array();
|
var userEntries: Array<UserEntry> = [];
|
||||||
|
|
||||||
for (let userName of userNames) {
|
for (let userName of userNames) {
|
||||||
userEntries.push(state.users.usersByNames[userName]);
|
userEntries.push(state.users.usersByNames[userName]);
|
||||||
@@ -231,7 +218,7 @@ function extractUsersByNames(
|
|||||||
return usersByNames;
|
return usersByNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
function editUser(user: User) {
|
export function editUser(user: User) {
|
||||||
return {
|
return {
|
||||||
type: EDIT_USER,
|
type: EDIT_USER,
|
||||||
user
|
user
|
||||||
|
|||||||
@@ -10,7 +10,12 @@ import {
|
|||||||
FETCH_USERS,
|
FETCH_USERS,
|
||||||
FETCH_USERS_SUCCESS,
|
FETCH_USERS_SUCCESS,
|
||||||
fetchUsers,
|
fetchUsers,
|
||||||
FETCH_USERS_FAILURE
|
FETCH_USERS_FAILURE,
|
||||||
|
updateUser,
|
||||||
|
UPDATE_USER,
|
||||||
|
UPDATE_USER_FAILURE,
|
||||||
|
UPDATE_USER_SUCCESS,
|
||||||
|
EDIT_USER
|
||||||
} from "./users";
|
} from "./users";
|
||||||
|
|
||||||
import reducer from "./users";
|
import reducer from "./users";
|
||||||
@@ -152,6 +157,33 @@ describe("fetch tests", () => {
|
|||||||
expect(actions[1].payload).toBeDefined();
|
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", () => {
|
describe("reducer tests", () => {
|
||||||
@@ -199,4 +231,15 @@ describe("reducer tests", () => {
|
|||||||
expect(newState.usersByNames["zaphod"]).toBeDefined();
|
expect(newState.usersByNames["zaphod"]).toBeDefined();
|
||||||
expect(newState.usersByNames["ford"]).toBeDefined();
|
expect(newState.usersByNames["ford"]).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("edit user", () => {
|
||||||
|
const newState = reducer(
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
type: EDIT_USER,
|
||||||
|
user: userZaphod
|
||||||
|
}
|
||||||
|
);
|
||||||
|
expect(newState.editUser).toEqual(userZaphod);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user