diff --git a/scm-ui/src/users/containers/DeleteUserButton.js b/scm-ui/src/users/containers/DeleteUserButton.js index 0ecaaa74be..a3a9e66022 100644 --- a/scm-ui/src/users/containers/DeleteUserButton.js +++ b/scm-ui/src/users/containers/DeleteUserButton.js @@ -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 { - deleteUser = () => { - this.props.deleteUser(this.props.user._links.delete.href); + static defaultProps = { + confirmDialog: true }; - confirmDelete = () => { - confirmAlert({ + deleteUser = () => { + this.props.deleteUser(this.props.user._links.delete.href); + }; + + 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 ( + ); } } diff --git a/scm-ui/src/users/containers/DeleteUserButton.test.js b/scm-ui/src/users/containers/DeleteUserButton.test.js index 937ee81172..944c739c35 100644 --- a/scm-ui/src/users/containers/DeleteUserButton.test.js +++ b/scm-ui/src/users/containers/DeleteUserButton.test.js @@ -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(); + const button = shallow( {}} />); expect(button.text()).toBe(""); }); @@ -25,11 +28,26 @@ it("should render the button", () => { } }; - const button = shallow(); + const button = shallow( {}} />); 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( {}} />); + 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(); + const button = shallow(); button.simulate("click"); expect(calledUrl).toBe("/users"); diff --git a/scm-ui/src/users/modules/users.js b/scm-ui/src/users/modules/users.js index 919863e116..d860a03cb0 100644 --- a/scm-ui/src/users/modules/users.js +++ b/scm-ui/src/users/modules/users.js @@ -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) diff --git a/scm-ui/src/users/modules/users.test.js b/scm-ui/src/users/modules/users.test.js index 89a4abc721..6bd3707b5c 100644 --- a/scm-ui/src/users/modules/users.test.js +++ b/scm-ui/src/users/modules/users.test.js @@ -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", () => {