From db9e468877e090868ad9f41bb9821d94a8705a80 Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Tue, 17 Jul 2018 16:26:20 +0200 Subject: [PATCH 1/2] Added unit tests --- scm-ui/src/users/modules/users.js | 2 +- scm-ui/src/users/modules/users.test.js | 31 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/scm-ui/src/users/modules/users.js b/scm-ui/src/users/modules/users.js index 27e1052604..919863e116 100644 --- a/scm-ui/src/users/modules/users.js +++ b/scm-ui/src/users/modules/users.js @@ -170,7 +170,7 @@ function deleteUserFailure(url: string, err: Error) { } export function deleteUser(link: string) { - return function(dispatch: ThunkDispatch) { + return function(dispatch: Dispatch) { dispatch(requestDeleteUser(link)); return apiClient .delete(link) diff --git a/scm-ui/src/users/modules/users.test.js b/scm-ui/src/users/modules/users.test.js index bef47ade79..89a4abc721 100644 --- a/scm-ui/src/users/modules/users.test.js +++ b/scm-ui/src/users/modules/users.test.js @@ -11,6 +11,10 @@ import { FETCH_USERS_SUCCESS, fetchUsers, FETCH_USERS_FAILURE, + addUser, + ADD_USER, + ADD_USER_SUCCESS, + ADD_USER_FAILURE, updateUser, UPDATE_USER, UPDATE_USER_FAILURE, @@ -158,6 +162,33 @@ describe("fetch tests", () => { }); }); + test("successful user add", () => { + fetchMock.postOnce("/scm/api/rest/v2/users", { + status: 204 + }); + + const store = mockStore({}); + return store.dispatch(addUser(userZaphod)).then(() => { + const actions = store.getActions(); + expect(actions[0].type).toEqual(ADD_USER); + expect(actions[1].type).toEqual(ADD_USER_SUCCESS); + }); + }); + + test("user add failed", () => { + fetchMock.postOnce("/scm/api/rest/v2/users", { + status: 500 + }); + + const store = mockStore({}); + return store.dispatch(addUser(userZaphod)).then(() => { + const actions = store.getActions(); + expect(actions[0].type).toEqual(ADD_USER); + expect(actions[1].type).toEqual(ADD_USER_FAILURE); + expect(actions[1].payload).toBeDefined(); + }); + }); + test("successful user update", () => { fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", { status: 204 From b85776cb1dcd69e1db09a509c1588fe407f6e8e3 Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Tue, 17 Jul 2018 16:27:45 +0200 Subject: [PATCH 2/2] Added missing files hg did not show them as added --- scm-ui/jest.config.js | 4 +++ scm-ui/src/users/containers/EditUserButton.js | 29 +++++++++++++++++ .../users/containers/EditUserButton.test.js | 31 +++++++++++++++++++ scm-ui/src/users/types/UserEntry.js | 7 +++++ 4 files changed, 71 insertions(+) create mode 100644 scm-ui/jest.config.js create mode 100644 scm-ui/src/users/containers/EditUserButton.js create mode 100644 scm-ui/src/users/containers/EditUserButton.test.js create mode 100644 scm-ui/src/users/types/UserEntry.js diff --git a/scm-ui/jest.config.js b/scm-ui/jest.config.js new file mode 100644 index 0000000000..a5e135724b --- /dev/null +++ b/scm-ui/jest.config.js @@ -0,0 +1,4 @@ +module.exports = { + collectCoverage: true, + coverageFormats: ["json", "html"] +}; diff --git a/scm-ui/src/users/containers/EditUserButton.js b/scm-ui/src/users/containers/EditUserButton.js new file mode 100644 index 0000000000..37a65bbbe3 --- /dev/null +++ b/scm-ui/src/users/containers/EditUserButton.js @@ -0,0 +1,29 @@ +//@flow +import React from "react"; +import type { User } from "../types/User"; + +type Props = { + user: User, + editUser: User => void +}; + +type State = {}; + +class EditUserButton extends React.Component { + render() { + if (!this.isEditable()) { + return ""; + } + return ( + + ); + } + + isEditable = () => { + return this.props.user._links.update; + }; +} + +export default EditUserButton; diff --git a/scm-ui/src/users/containers/EditUserButton.test.js b/scm-ui/src/users/containers/EditUserButton.test.js new file mode 100644 index 0000000000..a916be12f0 --- /dev/null +++ b/scm-ui/src/users/containers/EditUserButton.test.js @@ -0,0 +1,31 @@ +import React from "react"; +import { configure, shallow } from "enzyme"; +import EditUserButton from "./EditUserButton"; +import Adapter from "enzyme-adapter-react-16"; + +import "raf/polyfill"; + +configure({ adapter: new Adapter() }); + +it("should render nothing, if the edit link is missing", () => { + const user = { + _links: {} + }; + + const button = shallow(); + expect(button.text()).toBe(""); +}); + +it("should render the button", () => { + const user = { + _links: { + update: { + href: "/users" + } + } + }; + + const button = shallow(); + expect(button.text()).not.toBe(""); +}); + diff --git a/scm-ui/src/users/types/UserEntry.js b/scm-ui/src/users/types/UserEntry.js new file mode 100644 index 0000000000..5ab609d02c --- /dev/null +++ b/scm-ui/src/users/types/UserEntry.js @@ -0,0 +1,7 @@ +type UserEntry = { + loading: boolean, + error: Error, + user: User +}; + +export type UserEntry; \ No newline at end of file