mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 15:05:44 +01:00
Merged changes
This commit is contained in:
@@ -1,28 +1,30 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import type {
|
import type { User } from "../types/User";
|
||||||
User
|
import { confirmAlert } from '../../components/ConfirmAlert';
|
||||||
} from "../types/User";
|
|
||||||
import {
|
|
||||||
confirmAlert
|
|
||||||
} from '../../components/ConfirmAlert';
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
user: User,
|
user: User,
|
||||||
|
confirmDialog?: boolean,
|
||||||
deleteUser: (link: string) => void,
|
deleteUser: (link: string) => void,
|
||||||
};
|
};
|
||||||
|
|
||||||
class DeleteUserButton extends React.Component < Props > {
|
class DeleteUserButton extends React.Component<Props> {
|
||||||
|
|
||||||
|
static defaultProps = {
|
||||||
|
confirmDialog: true
|
||||||
|
};
|
||||||
|
|
||||||
deleteUser = () => {
|
deleteUser = () => {
|
||||||
this.props.deleteUser(this.props.user._links.delete.href);
|
this.props.deleteUser(this.props.user._links.delete.href);
|
||||||
};
|
};
|
||||||
|
|
||||||
confirmDelete = () => {
|
confirmDelete = () =>{
|
||||||
confirmAlert({
|
confirmAlert({
|
||||||
title: 'Delete user',
|
title: 'Delete user',
|
||||||
message: 'Do you really want to delete the user?',
|
message: 'Do you really want to delete the user?',
|
||||||
buttons: [{
|
buttons: [
|
||||||
|
{
|
||||||
label: 'Yes',
|
label: 'Yes',
|
||||||
onClick: () => this.deleteUser()
|
onClick: () => this.deleteUser()
|
||||||
},
|
},
|
||||||
@@ -31,26 +33,24 @@ class DeleteUserButton extends React.Component < Props > {
|
|||||||
onClick: () => null
|
onClick: () => null
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
isDeletable = () => {
|
isDeletable = () => {
|
||||||
return this.props.user._links.delete;
|
return this.props.user._links.delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
const { confirmDialog } = this.props;
|
||||||
|
const action = confirmDialog ? this.confirmDelete : this.deleteUser;
|
||||||
|
|
||||||
if (!this.isDeletable()) {
|
if (!this.isDeletable()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return ( <
|
return (
|
||||||
button type = "button"
|
<button type="button" onClick={(e) => { action() } }>
|
||||||
onClick = {
|
Delete User
|
||||||
(e) => {
|
</button>
|
||||||
this.confirmDelete()
|
|
||||||
}
|
|
||||||
} >
|
|
||||||
Delete User <
|
|
||||||
/button>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ import { configure, shallow } from "enzyme";
|
|||||||
import DeleteUserButton from "./DeleteUserButton";
|
import DeleteUserButton from "./DeleteUserButton";
|
||||||
import Adapter from "enzyme-adapter-react-16";
|
import Adapter from "enzyme-adapter-react-16";
|
||||||
|
|
||||||
|
import { confirmAlert } from '../../components/ConfirmAlert';
|
||||||
|
jest.mock('../../components/ConfirmAlert');
|
||||||
|
|
||||||
import "raf/polyfill";
|
import "raf/polyfill";
|
||||||
|
|
||||||
configure({ adapter: new Adapter() });
|
configure({ adapter: new Adapter() });
|
||||||
@@ -12,7 +15,7 @@ it("should render nothing, if the delete link is missing", () => {
|
|||||||
_links: {}
|
_links: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
const button = shallow(<DeleteUserButton user={user} />);
|
const button = shallow(<DeleteUserButton user={user} deleteUser={() => {}} />);
|
||||||
expect(button.text()).toBe("");
|
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("");
|
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", () => {
|
it("should call the delete user function with delete url", () => {
|
||||||
const user = {
|
const user = {
|
||||||
_links: {
|
_links: {
|
||||||
@@ -40,12 +58,11 @@ it("should call the delete user function with delete url", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let calledUrl = null;
|
let calledUrl = null;
|
||||||
|
|
||||||
function capture(url) {
|
function capture(url) {
|
||||||
calledUrl = url;
|
calledUrl = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
const button = shallow(<DeleteUserButton user={user} deleteUser={capture} />);
|
const button = shallow(<DeleteUserButton user={user} confirmDialog={false} deleteUser={capture} />);
|
||||||
button.simulate("click");
|
button.simulate("click");
|
||||||
|
|
||||||
expect(calledUrl).toBe("/users");
|
expect(calledUrl).toBe("/users");
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ export const DELETE_USER_FAILURE = "scm/users/DELETE_FAILURE";
|
|||||||
const USERS_URL = "users";
|
const USERS_URL = "users";
|
||||||
|
|
||||||
const CONTENT_TYPE_USER = "application/vnd.scmm-user+json;v=2";
|
const CONTENT_TYPE_USER = "application/vnd.scmm-user+json;v=2";
|
||||||
|
|
||||||
function requestUsers() {
|
function requestUsers() {
|
||||||
return {
|
return {
|
||||||
type: FETCH_USERS
|
type: FETCH_USERS
|
||||||
@@ -48,7 +49,7 @@ function usersNotFound(url: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function fetchUsers() {
|
export function fetchUsers() {
|
||||||
return function(dispatch: any) {
|
return function (dispatch: any) {
|
||||||
dispatch(requestUsers());
|
dispatch(requestUsers());
|
||||||
return apiClient
|
return apiClient
|
||||||
.get(USERS_URL)
|
.get(USERS_URL)
|
||||||
@@ -88,7 +89,7 @@ function requestAddUser(user: User) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function addUser(user: User) {
|
export function addUser(user: User) {
|
||||||
return function(dispatch: Dispatch) {
|
return function (dispatch: Dispatch) {
|
||||||
dispatch(requestAddUser(user));
|
dispatch(requestAddUser(user));
|
||||||
return apiClient
|
return apiClient
|
||||||
.postWithContentType(USERS_URL, user, CONTENT_TYPE_USER)
|
.postWithContentType(USERS_URL, user, CONTENT_TYPE_USER)
|
||||||
@@ -122,7 +123,7 @@ function requestUpdateUser(user: User) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function updateUser(user: User) {
|
export function updateUser(user: User) {
|
||||||
return function(dispatch: Dispatch) {
|
return function (dispatch: Dispatch) {
|
||||||
dispatch(requestUpdateUser(user));
|
dispatch(requestUpdateUser(user));
|
||||||
return apiClient
|
return apiClient
|
||||||
.putWithContentType(user._links.update.href, user, CONTENT_TYPE_USER)
|
.putWithContentType(user._links.update.href, user, CONTENT_TYPE_USER)
|
||||||
|
|||||||
@@ -220,8 +220,8 @@ describe("fetch tests", () => {
|
|||||||
describe("reducer tests", () => {
|
describe("reducer tests", () => {
|
||||||
test("users request", () => {
|
test("users request", () => {
|
||||||
var newState = reducer({}, { type: FETCH_USERS });
|
var newState = reducer({}, { type: FETCH_USERS });
|
||||||
expect(newState.loading).toBeTruthy();
|
expect(newState.users.loading).toBeTruthy();
|
||||||
expect(newState.error).toBeNull();
|
expect(newState.users.error).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("fetch users successful", () => {
|
test("fetch users successful", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user