Reconfigure tests to work with hook approach

This commit is contained in:
Nathan Barber
2021-04-07 15:25:21 -04:00
parent b230745d64
commit 4768751125
13 changed files with 129 additions and 87 deletions

File diff suppressed because one or more lines are too long

View File

@@ -45,6 +45,7 @@
"react-redux": "^7.2.2",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-testing-library": "^8.0.1",
"recompose": "^0.30.0",
"redux": "^4.0.5",
"style-loader": "^2.0.0",

View File

@@ -18,39 +18,28 @@ describe("AddUser Component: ", () => {
/>
);
it("Updates state.users on textbox blur", () => {
let component = shallow(addUserJsx(mockAsync())),
textarea = component.find("textarea");
textarea.simulate("blur", { target: { value: "foo" } });
expect(JSON.stringify(component.state("users"))).toBe('["foo"]');
});
it("Renders", () => {
let component = shallow(addUserJsx(mockAsync()))
expect(component.find(".container").length).toBe(1)
})
it("Can separate newline spaced names into an array", () => {
let component = shallow(addUserJsx(mockAsync())),
textarea = component.find("textarea");
textarea.simulate("blur", { target: { value: "foo\nbar\nsoforth" } });
expect(JSON.stringify(component.state("users"))).toBe(
'["foo","bar","soforth"]'
);
});
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)
})
it("Deliminates names with special / less than 3 characters", () => {
let component = shallow(addUserJsx(mockAsync())),
textarea = component.find("textarea"),
submit = component.find("#submit");
textarea.simulate("blur", {
target: { value: "foo\nbar\nb%%%\n$andy\nhalfdecent" },
});
submit.simulate("click");
expect(JSON.stringify(component.state("users"))).toBe(
'["foo","bar","halfdecent"]'
);
});
it("Recognizes admin user selection", () => {
let component = shallow(addUserJsx(mockAsync())),
admin = component.find("#admin-check");
admin.simulate("change", { target: { checked: true } });
expect(component.state("admin")).toBe(true);
});
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)
})
});

View File

@@ -21,8 +21,8 @@ const CreateGroup = (props) => {
<input
className="group-name-input"
type="text"
value={groupName}
id="group-name"
value={groupName}
placeholder="group name..."
onChange={(e) => {
setGroupName(e.target.value);

View File

@@ -0,0 +1,34 @@
import React from "react";
import Enzyme, { mount, shallow } from "enzyme";
import CreateGroup from "./CreateGroup.pre";
import Adapter from "@wojtekmaj/enzyme-adapter-react-17";
Enzyme.configure({ adapter: new Adapter() });
describe("CreateGroup Component: ", () => {
var mockAsync = () =>
jest.fn().mockImplementation(() => Promise.resolve({ key: "value" }));
var createGroupJsx = (callbackSpy) =>
<CreateGroup
createGroup={callbackSpy}
refreshGroupsData={callbackSpy}
history={{push: () => {}}}
/>
it("Renders", () => {
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)
})
})

View File

@@ -14,6 +14,9 @@ const withUserAPI = withProps((props) => ({
alert(
"Cannot change username - either contains special characters or is too short."
),
noChangeEvent: () => {
returns
},
refreshUserData: () =>
jhapiRequest("/users", "GET")
.then((data) => data.json())

View File

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

View File

@@ -18,31 +18,10 @@ describe("EditUser Component: ", () => {
refreshUserData={mockSync()}
history={{ push: (a) => {} }}
failRegexEvent={callbackSpy}
noChangeEvent={callbackSpy}
/>
);
it("Updates the state whenever a key is pressed on the textarea", () => {
let component = shallow(editUserJsx(mockAsync())),
textarea = component.find("textarea");
textarea.simulate("keydown", { target: { value: "test" } });
expect(component.state("updated_username")).toBe("test");
});
it("Updates the state whenever the admin box changes", () => {
let component = shallow(editUserJsx(mockAsync())),
admin = component.find("#admin-check");
admin.simulate("change", { target: { checked: true } });
expect(component.state("admin")).toBe(true);
});
it("Delimits the input from the textarea", () => {
let component = shallow(editUserJsx(mockAsync())),
submit = component.find("#submit");
component.setState({ updated_username: "%!@$#&" });
submit.simulate("click");
expect(component.state("updated_username")).toBe("");
});
it("Calls the delete user function when the button is pressed", () => {
let callbackSpy = mockAsync(),
component = shallow(editUserJsx(callbackSpy)),
@@ -52,11 +31,9 @@ describe("EditUser Component: ", () => {
});
it("Submits the edits when the button is pressed", () => {
let callbackSpy = mockAsync(),
let callbackSpy = mockSync(),
component = shallow(editUserJsx(callbackSpy)),
submit = component.find("#submit"),
textarea = component.find("textarea");
textarea.simulate("keydown", { target: { value: "test" } });
submit = component.find("#submit");
submit.simulate("click");
expect(callbackSpy).toHaveBeenCalled();
});

View File

@@ -85,6 +85,7 @@ const GroupEdit = (props) => {
Apply
</button>
<button
id="delete-group"
className="btn btn-danger"
style={{ float: "right" }}
onClick={() => {

View File

@@ -1,32 +1,62 @@
import React from "react";
import Enzyme, { shallow } from "enzyme";
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'
Enzyme.configure({ adapter: new Adapter() });
describe("GroupEdit Component: ", () => {
var groupEditJsx = () => (
var mockAsync = () =>
jest.fn().mockImplementation(() => Promise.resolve({ key: "value" }));
var groupEditJsx = (callbackSpy) =>
<GroupEdit
location={{
state: {
user_data: [{ name: "foo" }, { name: "bar" }],
group_data: { users: ["foo"] },
group_data: { users: ["foo"], name: "group" },
callback: () => {},
},
}}
addToGroup={() => {}}
removeFromGroup={() => {}}
history={{ push: (a) => {} }}
addToGroup={callbackSpy}
removeFromGroup={callbackSpy}
deleteGroup={callbackSpy}
history={{ push: (a) => callbackSpy }}
refreshGroupsData={() => {}}
/>
var deepGroupEditJsx = (callbackSpy) => (
<HashRouter>
{groupEditJsx(callbackSpy)}
</HashRouter>
);
it("Can cleanly separate added and removed users", () => {
let component = shallow(groupEditJsx()),
submit = component.find("#submit");
component.setState({ selected: ["bar"], changed: true });
submit.simulate("click");
expect(component.state("added")[0]).toBe("bar");
expect(component.state("removed")[0]).toBe("foo");
});
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")
})
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")
})
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()
})
});

View File

@@ -8,7 +8,7 @@ Enzyme.configure({ adapter: new Adapter() });
describe("Multiselect Component: ", () => {
var multiselectJsx = () => (
<Multiselect
options={["foo", "bar", "wombat"]}
options={["foo", "bar", "wombat"]}s
value={["wombat"]}
onChange={() => {}}
/>
@@ -16,22 +16,22 @@ describe("Multiselect Component: ", () => {
it("Renders with initial value selected", () => {
let component = shallow(multiselectJsx()),
selected = component.state("selected");
expect(selected.length == 1 && selected[0] == "wombat").toBe(true);
selected = component.find(".item.selected").first();
expect(selected.text()).toBe("wombat");
});
it("Deselects a value when div.item.selected is clicked", () => {
let component = shallow(multiselectJsx()),
selected = component.find(".item.selected").first();
selected.simulate("click");
expect(component.state("selected").length).toBe(0);
expect(component.find(".item.selected").length).toBe(0);
});
it("Selects a an option when div.item.unselected is clicked", () => {
let component = shallow(multiselectJsx()),
unselected = component.find(".item.unselected").first();
unselected.simulate("click");
expect(component.state("selected").length).toBe(2);
expect(component.find(".item.selected").length).toBe(2);
});
it("Triggers callback on any sort of change", () => {

View File

@@ -6252,6 +6252,11 @@ react-test-renderer@^17.0.0, react-test-renderer@^17.0.1:
react-shallow-renderer "^16.13.1"
scheduler "^0.20.1"
react-testing-library@^8.0.1:
version "8.0.1"
resolved "https://registry.yarnpkg.com/react-testing-library/-/react-testing-library-8.0.1.tgz#b3dd43bce3fa88423cf0a23292fb819023c227cc"
integrity sha512-Gq4JC9r3prA4hYwo7afcbHHMFckO29+5Nrh2KblAEPuK/DWaU0bJE1vtpAgLhzhY9bBirmcgjjIHljHEwGAXKw==
react-transition-group@^4.4.1:
version "4.4.1"
resolved "https://registry.yarnpkg.com/react-transition-group/-/react-transition-group-4.4.1.tgz#63868f9325a38ea5ee9535d828327f85773345c9"

File diff suppressed because one or more lines are too long