Lint and make App (Component) functional

This commit is contained in:
Nathan Barber
2021-04-07 15:36:17 -04:00
parent 1d705193cb
commit cadcb686c9
9 changed files with 82 additions and 123 deletions

File diff suppressed because one or more lines are too long

View File

@@ -1,4 +1,4 @@
import React, { Component } from "react"; import React, { Component, useEffect } from "react";
import ReactDOM from "react-dom"; import ReactDOM from "react-dom";
import { Provider } from "react-redux"; import { Provider } from "react-redux";
import { createStore } from "redux"; import { createStore } from "redux";
@@ -17,11 +17,10 @@ import EditUser from "./components/EditUser/EditUser";
import "./style/root.css"; import "./style/root.css";
const store = createStore(reducers, initialState), const store = createStore(reducers, initialState)
routerHistory = createBrowserHistory();
class App extends Component { const App = (props) => {
componentDidMount() { useEffect(() => {
jhapiRequest("/users", "GET") jhapiRequest("/users", "GET")
.then((data) => data.json()) .then((data) => data.json())
.then((data) => store.dispatch({ type: "USER_DATA", value: data })) .then((data) => store.dispatch({ type: "USER_DATA", value: data }))
@@ -31,26 +30,24 @@ class App extends Component {
.then((data) => data.json()) .then((data) => data.json())
.then((data) => store.dispatch({ type: "GROUPS_DATA", value: data })) .then((data) => store.dispatch({ type: "GROUPS_DATA", value: data }))
.catch((err) => console.log(err)); .catch((err) => console.log(err));
} });
render() { return (
return ( <div className="resets">
<div className="resets"> <Provider store={store}>
<Provider store={store}> <HashRouter>
<HashRouter> <Switch>
<Switch> <Route exact path="/" component={ServerDashboard} />
<Route exact path="/" component={ServerDashboard} /> <Route exact path="/groups" component={Groups} />
<Route exact path="/groups" component={Groups} /> <Route exact path="/group-edit" component={GroupEdit} />
<Route exact path="/group-edit" component={GroupEdit} /> <Route exact path="/create-group" component={CreateGroup} />
<Route exact path="/create-group" component={CreateGroup} /> <Route exact path="/add-users" component={AddUser} />
<Route exact path="/add-users" component={AddUser} /> <Route exact path="/edit-user" component={EditUser} />
<Route exact path="/edit-user" component={EditUser} /> </Switch>
</Switch> </HashRouter>
</HashRouter> </Provider>
</Provider> </div>
</div> );
); };
}
}
ReactDOM.render(<App />, document.getElementById("react-admin-hook")); ReactDOM.render(<App />, document.getElementById("react-admin-hook"));

View File

@@ -19,27 +19,27 @@ describe("AddUser Component: ", () => {
); );
it("Renders", () => { it("Renders", () => {
let component = shallow(addUserJsx(mockAsync())) let component = shallow(addUserJsx(mockAsync()));
expect(component.find(".container").length).toBe(1) expect(component.find(".container").length).toBe(1);
}) });
it("Removes users when they fail Regex", () => { it("Removes users when they fail Regex", () => {
let callbackSpy = mockAsync(), let callbackSpy = mockAsync(),
component = shallow(addUserJsx(callbackSpy)), component = shallow(addUserJsx(callbackSpy)),
textarea = component.find("textarea").first() textarea = component.find("textarea").first();
textarea.simulate("blur", { target: { value: "foo\nbar\n!!*&*"}}) textarea.simulate("blur", { target: { value: "foo\nbar\n!!*&*" } });
let submit = component.find("#submit") let submit = component.find("#submit");
submit.simulate("click") submit.simulate("click");
expect(callbackSpy).toHaveBeenCalledWith(["foo", "bar"], false) expect(callbackSpy).toHaveBeenCalledWith(["foo", "bar"], false);
}) });
it("Correctly submits admin", () => { it("Correctly submits admin", () => {
let callbackSpy = mockAsync(), let callbackSpy = mockAsync(),
component = shallow(addUserJsx(callbackSpy)), component = shallow(addUserJsx(callbackSpy)),
input = component.find("input").first() input = component.find("input").first();
input.simulate("change", { target: { checked: true }}) input.simulate("change", { target: { checked: true } });
let submit = component.find("#submit") let submit = component.find("#submit");
submit.simulate("click") submit.simulate("click");
expect(callbackSpy).toHaveBeenCalledWith([], true) expect(callbackSpy).toHaveBeenCalledWith([], true);
}) });
}); });

View File

@@ -9,26 +9,27 @@ describe("CreateGroup Component: ", () => {
var mockAsync = () => var mockAsync = () =>
jest.fn().mockImplementation(() => Promise.resolve({ key: "value" })); jest.fn().mockImplementation(() => Promise.resolve({ key: "value" }));
var createGroupJsx = (callbackSpy) => var createGroupJsx = (callbackSpy) => (
<CreateGroup <CreateGroup
createGroup={callbackSpy} createGroup={callbackSpy}
refreshGroupsData={callbackSpy} refreshGroupsData={callbackSpy}
history={{push: () => {}}} history={{ push: () => {} }}
/> />
);
it("Renders", () => { it("Renders", () => {
let component = shallow(createGroupJsx()) let component = shallow(createGroupJsx());
expect(component.find(".container").length).toBe(1) expect(component.find(".container").length).toBe(1);
}) });
it("Calls createGroup and refreshGroupsData on submit", () => { it("Calls createGroup and refreshGroupsData on submit", () => {
let callbackSpy = mockAsync(), let callbackSpy = mockAsync(),
component = shallow(createGroupJsx(callbackSpy)), component = shallow(createGroupJsx(callbackSpy)),
input = component.find("input").first(), input = component.find("input").first(),
submit = component.find("#submit").first() submit = component.find("#submit").first();
input.simulate("change", { target: { value: "" } }) input.simulate("change", { target: { value: "" } });
submit.simulate("click") submit.simulate("click");
expect(callbackSpy).toHaveBeenNthCalledWith(1, "") expect(callbackSpy).toHaveBeenNthCalledWith(1, "");
expect(callbackSpy).toHaveBeenNthCalledWith(2) expect(callbackSpy).toHaveBeenNthCalledWith(2);
}) });
}) });

View File

@@ -15,7 +15,7 @@ const withUserAPI = withProps((props) => ({
"Cannot change username - either contains special characters or is too short." "Cannot change username - either contains special characters or is too short."
), ),
noChangeEvent: () => { noChangeEvent: () => {
returns returns;
}, },
refreshUserData: () => refreshUserData: () =>
jhapiRequest("/users", "GET") jhapiRequest("/users", "GET")

View File

@@ -81,7 +81,7 @@ const EditUser = (props) => {
className="btn btn-primary" className="btn btn-primary"
onClick={() => { onClick={() => {
if (updatedUsername == "" && admin == has_admin) { if (updatedUsername == "" && admin == has_admin) {
noChangeEvent() noChangeEvent();
return; return;
} else if (updatedUsername != "") { } else if (updatedUsername != "") {
if ( if (
@@ -136,6 +136,7 @@ EditUser.propTypes = {
editUser: PropTypes.func, editUser: PropTypes.func,
deleteUser: PropTypes.func, deleteUser: PropTypes.func,
failRegexEvent: PropTypes.func, failRegexEvent: PropTypes.func,
noChangeEvent: PropTypes.func,
refreshUserData: PropTypes.func, refreshUserData: PropTypes.func,
}; };

View File

@@ -2,7 +2,7 @@ import React from "react";
import Enzyme, { mount, shallow } from "enzyme"; import Enzyme, { mount, shallow } from "enzyme";
import GroupEdit from "./GroupEdit.pre"; import GroupEdit from "./GroupEdit.pre";
import Adapter from "@wojtekmaj/enzyme-adapter-react-17"; import Adapter from "@wojtekmaj/enzyme-adapter-react-17";
import { HashRouter } from 'react-router-dom' import { HashRouter } from "react-router-dom";
Enzyme.configure({ adapter: new Adapter() }); Enzyme.configure({ adapter: new Adapter() });
@@ -10,7 +10,7 @@ describe("GroupEdit Component: ", () => {
var mockAsync = () => var mockAsync = () =>
jest.fn().mockImplementation(() => Promise.resolve({ key: "value" })); jest.fn().mockImplementation(() => Promise.resolve({ key: "value" }));
var groupEditJsx = (callbackSpy) => var groupEditJsx = (callbackSpy) => (
<GroupEdit <GroupEdit
location={{ location={{
state: { state: {
@@ -25,38 +25,37 @@ describe("GroupEdit Component: ", () => {
history={{ push: (a) => callbackSpy }} history={{ push: (a) => callbackSpy }}
refreshGroupsData={() => {}} refreshGroupsData={() => {}}
/> />
);
var deepGroupEditJsx = (callbackSpy) => ( var deepGroupEditJsx = (callbackSpy) => (
<HashRouter> <HashRouter>{groupEditJsx(callbackSpy)}</HashRouter>
{groupEditJsx(callbackSpy)}
</HashRouter>
); );
it("Adds a newly selected user to group on submit", () => { it("Adds a newly selected user to group on submit", () => {
let callbackSpy = mockAsync(), let callbackSpy = mockAsync(),
component = mount(deepGroupEditJsx(callbackSpy)), component = mount(deepGroupEditJsx(callbackSpy)),
unselected = component.find(".unselected"), unselected = component.find(".unselected"),
submit = component.find("#submit") submit = component.find("#submit");
unselected.simulate("click") unselected.simulate("click");
submit.simulate("click") submit.simulate("click");
expect(callbackSpy).toHaveBeenCalledWith(['bar'], "group") expect(callbackSpy).toHaveBeenCalledWith(["bar"], "group");
}) });
it("Removes a user from group on submit", () => { it("Removes a user from group on submit", () => {
let callbackSpy = mockAsync(), let callbackSpy = mockAsync(),
component = mount(deepGroupEditJsx(callbackSpy)), component = mount(deepGroupEditJsx(callbackSpy)),
selected = component.find(".selected"), selected = component.find(".selected"),
submit = component.find("#submit") submit = component.find("#submit");
selected.simulate("click") selected.simulate("click");
submit.simulate("click") submit.simulate("click");
expect(callbackSpy).toHaveBeenCalledWith(['foo'], "group") expect(callbackSpy).toHaveBeenCalledWith(["foo"], "group");
}) });
it("Calls deleteGroup on button click", () => { it("Calls deleteGroup on button click", () => {
let callbackSpy = mockAsync(), let callbackSpy = mockAsync(),
component = shallow(groupEditJsx(callbackSpy)), component = shallow(groupEditJsx(callbackSpy)),
deleteGroup = component.find("#delete-group").first() deleteGroup = component.find("#delete-group").first();
deleteGroup.simulate("click") deleteGroup.simulate("click");
expect(callbackSpy).toHaveBeenCalled() expect(callbackSpy).toHaveBeenCalled();
}) });
}); });

View File

@@ -8,7 +8,8 @@ Enzyme.configure({ adapter: new Adapter() });
describe("Multiselect Component: ", () => { describe("Multiselect Component: ", () => {
var multiselectJsx = () => ( var multiselectJsx = () => (
<Multiselect <Multiselect
options={["foo", "bar", "wombat"]}s options={["foo", "bar", "wombat"]}
s
value={["wombat"]} value={["wombat"]}
onChange={() => {}} onChange={() => {}}
/> />

File diff suppressed because one or more lines are too long