fixed warnings on test execution

This commit is contained in:
Sebastian Sdorra
2018-07-18 14:02:07 +02:00
parent 12d71827b4
commit 2889c6598d
5 changed files with 67 additions and 26 deletions

View File

@@ -0,0 +1,13 @@
import "raf/polyfill";
// Temporary hack to suppress error
// https://github.com/facebook/create-react-app/issues/3199#issuecomment-345024029
window.requestAnimationFrame = function(callback) {
setTimeout(callback, 0);
return 0;
};
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
configure({ adapter: new Adapter() });

View File

@@ -1,15 +1,11 @@
import React from "react";
import { configure, mount, shallow } from "enzyme";
import { mount, shallow } from "enzyme";
import "../../tests/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() });
describe("DeleteUserButton", () => {
it("should render nothing, if the delete link is missing", () => {
const entry = {
@@ -38,9 +34,6 @@ describe("DeleteUserButton", () => {
const button = mount(
<DeleteUserButton entry={entry} deleteUser={() => {}} />
);
console.log(button);
expect(button.text()).not.toBe("");
});

View File

@@ -1,11 +1,7 @@
import React from "react";
import { configure, shallow } from "enzyme";
import "../../tests/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 entry = {

View File

@@ -11,6 +11,7 @@ import {
getUsersFromState
} from "../modules/users";
import Loading from "../../components/Loading";
import ErrorNotification from "../../components/ErrorNotification";
import UserForm from "./UserForm";
import UserTable from "./UserTable";
import type { User } from "../types/User";
@@ -68,10 +69,11 @@ class Users extends React.Component<Props, User> {
}
renderContent() {
const { userEntries, deleteUser, editUser, userToEdit } = this.props;
const { userEntries, deleteUser, editUser, userToEdit, error } = this.props;
if (userEntries) {
return (
<div>
<ErrorNotification error={error} />
<UserTable
entries={userEntries}
deleteUser={deleteUser}
@@ -97,7 +99,8 @@ const mapStateToProps = state => {
}
return {
userEntries,
userToEdit
userToEdit,
error: state.users.error
};
};

View File

@@ -1,7 +1,4 @@
//@flow
import React from "react";
import { configure, shallow } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import fetchMock from "fetch-mock";
@@ -21,15 +18,15 @@ import {
UPDATE_USER_SUCCESS,
EDIT_USER,
requestDeleteUser,
deleteUserFailure
deleteUserFailure,
DELETE_USER,
DELETE_USER_SUCCESS,
DELETE_USER_FAILURE,
deleteUser
} from "./users";
import reducer from "./users";
import "raf/polyfill";
configure({ adapter: new Adapter() });
const userZaphod = {
active: true,
admin: true,
@@ -165,15 +162,20 @@ describe("users fetch()", () => {
});
it("should add a user successfully", () => {
// unmatched
fetchMock.postOnce("/scm/api/rest/v2/users", {
status: 204
});
// after create, the users are fetched again
fetchMock.getOnce("/scm/api/rest/v2/users", response);
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);
expect(actions[2].type).toEqual(FETCH_USERS);
});
});
@@ -195,12 +197,15 @@ describe("users fetch()", () => {
fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", {
status: 204
});
// after update, the users are fetched again
fetchMock.getOnce("/scm/api/rest/v2/users", response);
const store = mockStore({});
return store.dispatch(updateUser(userZaphod)).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(UPDATE_USER);
expect(actions[1].type).toEqual(UPDATE_USER_SUCCESS);
expect(actions[2].type).toEqual(FETCH_USERS);
});
});
@@ -217,6 +222,38 @@ describe("users fetch()", () => {
expect(actions[1].payload).toBeDefined();
});
});
it("should delete successfully user zaphod", () => {
fetchMock.deleteOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", {
status: 204
});
// after update, the users are fetched again
fetchMock.getOnce("/scm/api/rest/v2/users", response);
const store = mockStore({});
return store.dispatch(deleteUser(userZaphod)).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(DELETE_USER);
expect(actions[0].payload).toBe(userZaphod);
expect(actions[1].type).toEqual(DELETE_USER_SUCCESS);
expect(actions[2].type).toEqual(FETCH_USERS);
});
});
it("should fail to delete user zaphod", () => {
fetchMock.deleteOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", {
status: 500
});
const store = mockStore({});
return store.dispatch(deleteUser(userZaphod)).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(DELETE_USER);
expect(actions[0].payload).toBe(userZaphod);
expect(actions[1].type).toEqual(DELETE_USER_FAILURE);
expect(actions[1].payload).toBeDefined();
});
});
});
describe("users reducer", () => {
@@ -288,8 +325,7 @@ describe("users reducer", () => {
const state = {
usersByNames: {
zaphod: {
loading: false,
error: null,
loading: true,
entry: userZaphod
}
}