From 727318124c1d832cc51d65b3b00a7930f0c4e28c Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Tue, 17 Jul 2018 17:00:45 +0200 Subject: [PATCH] Fixed tests --- .../users/containers/DeleteUserButton.test.js | 25 +++++++---- scm-ui/src/users/modules/users.js | 42 ++++++++----------- scm-ui/src/users/modules/users.test.js | 28 ++++++------- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/scm-ui/src/users/containers/DeleteUserButton.test.js b/scm-ui/src/users/containers/DeleteUserButton.test.js index 944c739c35..718b41be8d 100644 --- a/scm-ui/src/users/containers/DeleteUserButton.test.js +++ b/scm-ui/src/users/containers/DeleteUserButton.test.js @@ -3,8 +3,8 @@ 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 { confirmAlert } from "../../components/ConfirmAlert"; +jest.mock("../../components/ConfirmAlert"); import "raf/polyfill"; @@ -15,7 +15,9 @@ it("should render nothing, if the delete link is missing", () => { _links: {} }; - const button = shallow( {}} />); + const button = shallow( + {}} /> + ); expect(button.text()).toBe(""); }); @@ -28,12 +30,13 @@ it("should render the button", () => { } }; - const button = shallow( {}} />); + const button = shallow( + {}} /> + ); expect(button.text()).not.toBe(""); }); it("should open the confirm dialog on button click", () => { - const user = { _links: { delete: { @@ -42,7 +45,9 @@ it("should open the confirm dialog on button click", () => { } }; - const button = shallow( {}} />); + const button = shallow( + {}} /> + ); button.simulate("click"); expect(confirmAlert.mock.calls.length).toBe(1); @@ -58,11 +63,13 @@ it("should call the delete user function with delete url", () => { }; let calledUrl = null; - function capture(url) { - calledUrl = url; + function capture(user) { + calledUrl = user._links.delete.href; } - 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 d4b7085c6a..e89ce3358b 100644 --- a/scm-ui/src/users/modules/users.js +++ b/scm-ui/src/users/modules/users.js @@ -1,17 +1,8 @@ // @flow -import { - apiClient, - NOT_FOUND_ERROR -} from "../../apiclient"; -import type { - User -} from "../types/User"; -import type { - UserEntry -} from "../types/UserEntry"; -import { - Dispatch -} from "redux"; +import { apiClient, NOT_FOUND_ERROR } from "../../apiclient"; +import type { User } from "../types/User"; +import type { UserEntry } from "../types/UserEntry"; +import { Dispatch } from "redux"; export const FETCH_USERS = "scm/users/FETCH"; export const FETCH_USERS_SUCCESS = "scm/users/FETCH_SUCCESS"; @@ -58,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) @@ -98,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) @@ -132,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) @@ -182,9 +173,8 @@ export function deleteUserFailure(user: User, error: Error) { }; } - export function deleteUser(user: User) { - return function (dispatch: any) { + return function(dispatch: any) { dispatch(requestDeleteUser(user)); return apiClient .delete(user._links.delete.href) @@ -204,7 +194,7 @@ export function getUsersFromState(state) { if (!userNames) { return null; } - var userEntries: Array < UserEntry > = []; + const userEntries: Array = []; for (let userName of userNames) { userEntries.push(state.users.usersByNames[userName]); @@ -214,11 +204,11 @@ export function getUsersFromState(state) { } function extractUsersByNames( - users: Array < User > , - userNames: Array < string > , + users: Array, + userNames: Array, oldUsersByNames: {} ) { - var usersByNames = {}; + const usersByNames = {}; for (let user of users) { usersByNames[user.name] = { @@ -226,7 +216,7 @@ function extractUsersByNames( }; } - for (var userName in oldUsersByNames) { + for (let userName in oldUsersByNames) { usersByNames[userName] = oldUsersByNames[userName]; } return usersByNames; @@ -239,7 +229,11 @@ export function editUser(user: User) { }; } -const reduceUsersByNames = (state: any, username: string, newUserState: any) => { +const reduceUsersByNames = ( + state: any, + username: string, + newUserState: any +) => { const newUsersByNames = { ...state.usersByNames, [username]: newUserState diff --git a/scm-ui/src/users/modules/users.test.js b/scm-ui/src/users/modules/users.test.js index e462491e98..e39998cb39 100644 --- a/scm-ui/src/users/modules/users.test.js +++ b/scm-ui/src/users/modules/users.test.js @@ -222,8 +222,8 @@ describe("fetch tests", () => { describe("reducer tests", () => { test("users request", () => { var newState = reducer({}, { type: FETCH_USERS }); - expect(newState.users.loading).toBeTruthy(); - expect(newState.users.error).toBeNull(); + expect(newState.loading).toBeTruthy(); + expect(newState.error).toBeNull(); }); test("fetch users successful", () => { @@ -250,8 +250,8 @@ describe("reducer tests", () => { test("delete user requested", () => { const state = { - usersByNames : { - "zaphod": { + usersByNames: { + zaphod: { loading: false, error: null, entry: userZaphod @@ -263,17 +263,17 @@ describe("reducer tests", () => { const zaphod = newState.usersByNames["zaphod"]; expect(zaphod.loading).toBeTruthy(); expect(zaphod.entry).toBe(userZaphod); - }) + }); it("should not effect other users if one user will be deleted", () => { const state = { - usersByNames : { - "zaphod": { + usersByNames: { + zaphod: { loading: false, error: null, entry: userZaphod }, - "ford": { + ford: { loading: false } } @@ -286,8 +286,8 @@ describe("reducer tests", () => { it("should set the error of user which could not be deleted", () => { const state = { - usersByNames : { - "zaphod": { + usersByNames: { + zaphod: { loading: false, error: null, entry: userZaphod @@ -304,13 +304,13 @@ describe("reducer tests", () => { it("should not effect other users if one user could not be deleted", () => { const state = { - usersByNames : { - "zaphod": { + usersByNames: { + zaphod: { loading: false, error: null, entry: userZaphod }, - "ford": { + ford: { loading: false } } @@ -322,7 +322,6 @@ describe("reducer tests", () => { expect(ford.loading).toBeFalsy(); }); - test("reducer does not replace whole usersByNames map", () => { const oldState = { usersByNames: { @@ -339,7 +338,6 @@ describe("reducer tests", () => { expect(newState.usersByNames["zaphod"]).toBeDefined(); expect(newState.usersByNames["ford"]).toBeDefined(); }); - test("edit user", () => { const newState = reducer(