mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 22:45:45 +01:00
Merged changes
This commit is contained in:
@@ -1,28 +1,30 @@
|
||||
// @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 > {
|
||||
class DeleteUserButton extends React.Component<Props> {
|
||||
|
||||
static defaultProps = {
|
||||
confirmDialog: true
|
||||
};
|
||||
|
||||
deleteUser = () => {
|
||||
this.props.deleteUser(this.props.user._links.delete.href);
|
||||
};
|
||||
|
||||
confirmDelete = () => {
|
||||
confirmDelete = () =>{
|
||||
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>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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
|
||||
@@ -48,7 +49,7 @@ function usersNotFound(url: string) {
|
||||
}
|
||||
|
||||
export function fetchUsers() {
|
||||
return function(dispatch: any) {
|
||||
return function (dispatch: any) {
|
||||
dispatch(requestUsers());
|
||||
return apiClient
|
||||
.get(USERS_URL)
|
||||
@@ -88,7 +89,7 @@ function requestAddUser(user: User) {
|
||||
}
|
||||
|
||||
export function addUser(user: User) {
|
||||
return function(dispatch: Dispatch) {
|
||||
return function (dispatch: Dispatch) {
|
||||
dispatch(requestAddUser(user));
|
||||
return apiClient
|
||||
.postWithContentType(USERS_URL, user, CONTENT_TYPE_USER)
|
||||
@@ -122,7 +123,7 @@ function requestUpdateUser(user: User) {
|
||||
}
|
||||
|
||||
export function updateUser(user: User) {
|
||||
return function(dispatch: Dispatch) {
|
||||
return function (dispatch: Dispatch) {
|
||||
dispatch(requestUpdateUser(user));
|
||||
return apiClient
|
||||
.putWithContentType(user._links.update.href, user, CONTENT_TYPE_USER)
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
Reference in New Issue
Block a user