diff --git a/scm-ui/package.json b/scm-ui/package.json index c91b237aa5..84776009b5 100644 --- a/scm-ui/package.json +++ b/scm-ui/package.json @@ -32,7 +32,10 @@ } }, "devDependencies": { - "prettier": "^1.13.7" + "enzyme": "^3.3.0", + "enzyme-adapter-react-16": "^1.1.1", + "prettier": "^1.13.7", + "react-test-renderer": "^16.4.1" }, "babel": { "presets": [ diff --git a/scm-ui/src/modules/login.js b/scm-ui/src/modules/login.js index d3fc8c64e2..b4f505296d 100644 --- a/scm-ui/src/modules/login.js +++ b/scm-ui/src/modules/login.js @@ -1,7 +1,6 @@ //@flow import { apiClient } from "../apiclient"; -import { isRegExp } from "util"; const LOGIN_URL = "/auth/access_token"; const AUTHENTICATION_INFO_URL = "/me"; @@ -22,7 +21,7 @@ export function getIsAuthenticatedRequest() { } export function getIsAuthenticated() { - return function(dispatch: any) { + return function(dispatch: (any) => void) { dispatch(getIsAuthenticatedRequest()); return apiClient .get(AUTHENTICATION_INFO_URL) @@ -61,9 +60,9 @@ export function login(username: string, password: string) { cookie: true, grant_type: "password", username, - password + password, }; - return function(dispatch: any) { + return function(dispatch: (any) => void) { dispatch(loginRequest()); return apiClient.post(LOGIN_URL, login_data).then(response => { if (response.ok) { diff --git a/scm-ui/src/users/containers/DeleteUserButton.js b/scm-ui/src/users/containers/DeleteUserButton.js index 195e603ae8..c26dc56fb6 100644 --- a/scm-ui/src/users/containers/DeleteUserButton.js +++ b/scm-ui/src/users/containers/DeleteUserButton.js @@ -1,46 +1,36 @@ // @flow import React from "react"; -import { deleteUser } from '../modules/users'; -import {connect} from "react-redux"; type Props = { user: any, - deleteUser: (username: string) => void + deleteUser: (link: string) => void }; class DeleteUser extends React.Component { deleteUser = () => { - this.props.deleteUser(this.props.user.name); + this.props.deleteUser(this.props.user._links.delete.href); + }; + + if(deleteButtonClicked) { + let deleteButtonAsk =
You really want to remove this user?
+ } + + isDeletable = () => { + return this.props.user._links.delete; }; render() { - if(this.props.user._links.delete) { - return ( - - - ); + if (!this.isDeletable()) { + return; } + return ( + + + ); } } -const mapStateToProps = state => { - return { - users: state.users.users - }; -}; - -const mapDispatchToProps = dispatch => { - return { - deleteUser: (username: string) => { - dispatch(deleteUser(username)); - } - }; -}; - -export default connect( - mapStateToProps, - mapDispatchToProps -)(DeleteUser); +export default DeleteUser; diff --git a/scm-ui/src/users/containers/DeleteUserButton.test.js b/scm-ui/src/users/containers/DeleteUserButton.test.js new file mode 100644 index 0000000000..efda995c86 --- /dev/null +++ b/scm-ui/src/users/containers/DeleteUserButton.test.js @@ -0,0 +1,54 @@ +import React from 'react'; +import {configure, shallow} from 'enzyme'; +import DeleteUserButton from "./DeleteUserButton"; +import Adapter from 'enzyme-adapter-react-16'; + +import 'raf/polyfill'; + +configure({ adapter: new Adapter() }); + +it('should render nothing, if the delete link is missing', () => { + + const user = { + _links: {} + }; + + const button = shallow(); + expect(button.text()).toBe(""); +}); + +it('should render the button', () => { + + const user = { + _links: { + "delete": { + "href": "/users" + } + } + }; + + const button = shallow(); + expect(button.text()).not.toBe(""); +}); + +it('should call the delete user function with delete url', () => { + + const user = { + _links: { + "delete": { + "href": "/users" + } + } + }; + + let calledUrl = null; + + function capture(url) { + calledUrl = url; + } + + const button = shallow(); + button.simulate("click"); + + expect(calledUrl).toBe("/users"); +});