Merged changes

This commit is contained in:
Philipp Czora
2018-07-17 16:31:42 +02:00
4 changed files with 51 additions and 33 deletions

View File

@@ -1,19 +1,20 @@
// @flow
import React from "react";
import type {
User
} from "../types/User";
import {
confirmAlert
} from '../../components/ConfirmAlert';
import type { User } from "../types/User";
import { confirmAlert } from '../../components/ConfirmAlert';
type Props = {
user: User,
confirmDialog?: boolean,
deleteUser: (link: string) => void,
};
class DeleteUserButton extends React.Component<Props> {
static defaultProps = {
confirmDialog: true
};
deleteUser = () => {
this.props.deleteUser(this.props.user._links.delete.href);
};
@@ -22,7 +23,8 @@ class DeleteUserButton extends React.Component < Props > {
confirmAlert({
title: 'Delete user',
message: 'Do you really want to delete the user?',
buttons: [{
buttons: [
{
label: 'Yes',
onClick: () => this.deleteUser()
},
@@ -31,26 +33,24 @@ class DeleteUserButton extends React.Component < Props > {
onClick: () => null
}
]
})
}
});
};
isDeletable = () => {
return this.props.user._links.delete;
};
render() {
const { confirmDialog } = this.props;
const action = confirmDialog ? this.confirmDelete : this.deleteUser;
if (!this.isDeletable()) {
return;
}
return ( <
button type = "button"
onClick = {
(e) => {
this.confirmDelete()
}
} >
Delete User <
/button>
return (
<button type="button" onClick={(e) => { action() } }>
Delete User
</button>
);
}
}

View File

@@ -3,6 +3,9 @@ import { configure, shallow } from "enzyme";
import DeleteUserButton from "./DeleteUserButton";
import Adapter from "enzyme-adapter-react-16";
import { confirmAlert } from '../../components/ConfirmAlert';
jest.mock('../../components/ConfirmAlert');
import "raf/polyfill";
configure({ adapter: new Adapter() });
@@ -12,7 +15,7 @@ it("should render nothing, if the delete link is missing", () => {
_links: {}
};
const button = shallow(<DeleteUserButton user={user} />);
const button = shallow(<DeleteUserButton user={user} deleteUser={() => {}} />);
expect(button.text()).toBe("");
});
@@ -25,11 +28,26 @@ it("should render the button", () => {
}
};
const button = shallow(<DeleteUserButton user={user} />);
const button = shallow(<DeleteUserButton user={user} deleteUser={() => {}} />);
expect(button.text()).not.toBe("");
});
//TODO: Fix wrong test!
it("should open the confirm dialog on button click", () => {
const user = {
_links: {
delete: {
href: "/users"
}
}
};
const button = shallow(<DeleteUserButton user={user} deleteUser={() => {}} />);
button.simulate("click");
expect(confirmAlert.mock.calls.length).toBe(1);
});
it("should call the delete user function with delete url", () => {
const user = {
_links: {
@@ -40,12 +58,11 @@ it("should call the delete user function with delete url", () => {
};
let calledUrl = null;
function capture(url) {
calledUrl = url;
}
const button = shallow(<DeleteUserButton user={user} deleteUser={capture} />);
const button = shallow(<DeleteUserButton user={user} confirmDialog={false} deleteUser={capture} />);
button.simulate("click");
expect(calledUrl).toBe("/users");

View File

@@ -26,6 +26,7 @@ export const DELETE_USER_FAILURE = "scm/users/DELETE_FAILURE";
const USERS_URL = "users";
const CONTENT_TYPE_USER = "application/vnd.scmm-user+json;v=2";
function requestUsers() {
return {
type: FETCH_USERS

View File

@@ -220,8 +220,8 @@ describe("fetch tests", () => {
describe("reducer tests", () => {
test("users request", () => {
var newState = reducer({}, { type: FETCH_USERS });
expect(newState.loading).toBeTruthy();
expect(newState.error).toBeNull();
expect(newState.users.loading).toBeTruthy();
expect(newState.users.error).toBeNull();
});
test("fetch users successful", () => {