merging heads

This commit is contained in:
Maren Süwer
2018-07-17 16:35:01 +02:00
6 changed files with 113 additions and 19 deletions

4
scm-ui/jest.config.js Normal file
View File

@@ -0,0 +1,4 @@
module.exports = {
collectCoverage: true,
coverageFormats: ["json", "html"]
};

View File

@@ -0,0 +1,29 @@
//@flow
import React from "react";
import type { User } from "../types/User";
type Props = {
user: User,
editUser: User => void
};
type State = {};
class EditUserButton extends React.Component<Props, State> {
render() {
if (!this.isEditable()) {
return "";
}
return (
<button type="button" onClick={e => this.props.editUser(this.props.user)}>
Edit user
</button>
);
}
isEditable = () => {
return this.props.user._links.update;
};
}
export default EditUserButton;

View File

@@ -0,0 +1,31 @@
import React from "react";
import { configure, shallow } from "enzyme";
import EditUserButton from "./EditUserButton";
import Adapter from "enzyme-adapter-react-16";
import "raf/polyfill";
configure({ adapter: new Adapter() });
it("should render nothing, if the edit link is missing", () => {
const user = {
_links: {}
};
const button = shallow(<EditUserButton user={user} />);
expect(button.text()).toBe("");
});
it("should render the button", () => {
const user = {
_links: {
update: {
href: "/users"
}
}
};
const button = shallow(<EditUserButton user={user} />);
expect(button.text()).not.toBe("");
});

View File

@@ -182,6 +182,7 @@ export function deleteUserFailure(user: User, error: Error) {
}; };
} }
export function deleteUser(user: User) { export function deleteUser(user: User) {
return function (dispatch: any) { return function (dispatch: any) {
dispatch(requestDeleteUser(user)); dispatch(requestDeleteUser(user));
@@ -241,7 +242,7 @@ export function editUser(user: User) {
const reduceUsersByNames = (state: any, username: string, newUserState: any) => { const reduceUsersByNames = (state: any, username: string, newUserState: any) => {
const newUsersByNames = { const newUsersByNames = {
...state.usersByNames, ...state.usersByNames,
[username] : newUserState [username]: newUserState
}; };
return { return {
@@ -254,12 +255,8 @@ export default function reducer(state: any = {}, action: any = {}) {
switch (action.type) { switch (action.type) {
case FETCH_USERS: case FETCH_USERS:
return { return {
...state, loading: true,
users: { error: null
error: null,
entries: null,
loading: true
}
}; };
case DELETE_USER: case DELETE_USER:
return reduceUsersByNames(state, action.payload.name, { return reduceUsersByNames(state, action.payload.name, {
@@ -287,11 +284,6 @@ export default function reducer(state: any = {}, action: any = {}) {
}; };
case FETCH_USERS_FAILURE: case FETCH_USERS_FAILURE:
return {
...state,
error: action.payload,
loading: false
};
case DELETE_USER_FAILURE: case DELETE_USER_FAILURE:
return reduceUsersByNames(state, action.payload.user.name, { return reduceUsersByNames(state, action.payload.user.name, {
loading: false, loading: false,

View File

@@ -11,6 +11,10 @@ import {
FETCH_USERS_SUCCESS, FETCH_USERS_SUCCESS,
fetchUsers, fetchUsers,
FETCH_USERS_FAILURE, FETCH_USERS_FAILURE,
addUser,
ADD_USER,
ADD_USER_SUCCESS,
ADD_USER_FAILURE,
updateUser, updateUser,
UPDATE_USER, UPDATE_USER,
UPDATE_USER_FAILURE, UPDATE_USER_FAILURE,
@@ -160,6 +164,33 @@ describe("fetch tests", () => {
}); });
}); });
test("successful user add", () => {
fetchMock.postOnce("/scm/api/rest/v2/users", {
status: 204
});
const store = mockStore({});
return store.dispatch(addUser(userZaphod)).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(ADD_USER);
expect(actions[1].type).toEqual(ADD_USER_SUCCESS);
});
});
test("user add failed", () => {
fetchMock.postOnce("/scm/api/rest/v2/users", {
status: 500
});
const store = mockStore({});
return store.dispatch(addUser(userZaphod)).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(ADD_USER);
expect(actions[1].type).toEqual(ADD_USER_FAILURE);
expect(actions[1].payload).toBeDefined();
});
});
test("successful user update", () => { test("successful user update", () => {
fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", { fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", {
status: 204 status: 204

View File

@@ -0,0 +1,7 @@
type UserEntry = {
loading: boolean,
error: Error,
user: User
};
export type UserEntry;