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 { Provider } from "react-redux";
import { createStore } from "redux";
@@ -17,11 +17,10 @@ import EditUser from "./components/EditUser/EditUser";
import "./style/root.css";
const store = createStore(reducers, initialState),
routerHistory = createBrowserHistory();
const store = createStore(reducers, initialState)
class App extends Component {
componentDidMount() {
const App = (props) => {
useEffect(() => {
jhapiRequest("/users", "GET")
.then((data) => data.json())
.then((data) => store.dispatch({ type: "USER_DATA", value: data }))
@@ -31,9 +30,8 @@ class App extends Component {
.then((data) => data.json())
.then((data) => store.dispatch({ type: "GROUPS_DATA", value: data }))
.catch((err) => console.log(err));
}
});
render() {
return (
<div className="resets">
<Provider store={store}>
@@ -50,7 +48,6 @@ class App extends Component {
</Provider>
</div>
);
}
}
};
ReactDOM.render(<App />, document.getElementById("react-admin-hook"));

View File

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

View File

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

View File

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

View File

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

View File

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

File diff suppressed because one or more lines are too long