Farmbot-Web-App/frontend/farm_designer/plants/__tests__/create_points_test.tsx

179 lines
5.4 KiB
TypeScript
Raw Normal View History

2019-09-23 12:56:35 -06:00
jest.mock("../../../api/crud", () => ({ initSave: jest.fn() }));
2018-02-20 17:13:14 -07:00
jest.mock("../../../farmware/weed_detector/actions", () => ({
deletePoints: jest.fn()
}));
import * as React from "react";
import { mount, shallow } from "enzyme";
2019-09-16 14:15:15 -06:00
import {
2019-09-19 13:09:00 -06:00
RawCreatePoints as CreatePoints,
2019-09-16 14:15:15 -06:00
CreatePointsProps,
mapStateToProps
} from "../create_points";
2018-02-20 17:13:14 -07:00
import { initSave } from "../../../api/crud";
import { deletePoints } from "../../../farmware/weed_detector/actions";
import { Actions } from "../../../constants";
2018-06-15 17:59:32 -06:00
import { clickButton } from "../../../__test_support__/helpers";
2019-09-16 14:15:15 -06:00
import { fakeState } from "../../../__test_support__/fake_state";
import { DeepPartial } from "redux";
import { CurrentPointPayl } from "../../interfaces";
const FAKE_POINT: CurrentPointPayl =
({ name: "My Point", cx: 13, cy: 22, r: 345, color: "red" });
describe("mapStateToProps", () => {
it("maps state to props", () => {
const state = fakeState();
state
.resources
.consumers
.farm_designer
.currentPoint = FAKE_POINT;
const result = mapStateToProps(state);
const { currentPoint } = result;
expect(currentPoint).toBeTruthy();
if (currentPoint) {
expect(currentPoint.cx).toEqual(13);
expect(currentPoint.cy).toEqual(22);
} else {
fail("Nope");
}
});
});
2018-02-20 17:13:14 -07:00
describe("<CreatePoints />", () => {
2019-09-23 12:56:35 -06:00
const fakeProps = (): CreatePointsProps => ({
dispatch: jest.fn(),
currentPoint: undefined,
deviceY: 1.23,
deviceX: 3.21
});
2018-08-01 20:03:16 -06:00
2019-09-16 14:15:15 -06:00
const fakeInstance = () => {
const props = fakeProps();
props.currentPoint = FAKE_POINT;
return new CreatePoints(props);
};
2018-02-20 17:13:14 -07:00
it("renders", () => {
2018-05-15 20:38:52 -06:00
const wrapper = mount(<CreatePoints {...fakeProps()} />);
2018-02-20 17:13:14 -07:00
["create point", "cancel", "delete", "x", "y", "radius", "color"]
.map(string => expect(wrapper.text().toLowerCase()).toContain(string));
});
2019-09-16 14:15:15 -06:00
it("updates specific fields", () => {
const i = fakeInstance();
const updater = i.updateValue("color");
type Event = React.SyntheticEvent<HTMLInputElement>;
const e: DeepPartial<Event> = {
currentTarget: {
value: "cheerful hue"
}
};
updater(e as Event);
expect(i.props.currentPoint).toBeTruthy();
expect(i.props.dispatch).toHaveBeenCalledWith({
payload: {
color: "cheerful hue",
cx: 13,
cy: 22,
name: "My Point",
r: 345,
},
type: "SET_CURRENT_POINT_DATA",
});
});
it("updates current point", () => {
const p = fakeInstance();
p.updateCurrentPoint();
expect(p.props.dispatch).toHaveBeenCalledWith({
type: "SET_CURRENT_POINT_DATA",
payload: { cx: 13, cy: 22, name: "My Point", r: 345, color: "red" },
});
});
2018-02-20 17:13:14 -07:00
it("creates point", () => {
2018-05-15 20:38:52 -06:00
const wrapper = mount(<CreatePoints {...fakeProps()} />);
2018-02-20 17:13:14 -07:00
wrapper.setState({ cx: 10, cy: 20, r: 30, color: "red" });
2018-06-15 17:59:32 -06:00
clickButton(wrapper, 0, "create point");
2018-10-31 17:49:05 -06:00
expect(initSave).toHaveBeenCalledWith("Point",
{
2018-02-20 17:13:14 -07:00
meta: { color: "red", created_by: "farm-designer" },
name: "Created Point",
pointer_type: "GenericPointer",
radius: 30, x: 10, y: 20, z: 0
2018-10-31 17:49:05 -06:00
});
2018-02-20 17:13:14 -07:00
});
it("deletes all points", () => {
const p = fakeProps();
const wrapper = mount(<CreatePoints {...p} />);
const button = wrapper.find("button").last();
expect(button.text()).toEqual("Delete all created points");
2018-05-15 20:38:52 -06:00
window.confirm = jest.fn();
2018-02-20 17:13:14 -07:00
button.simulate("click");
2018-05-15 20:38:52 -06:00
expect(window.confirm).toHaveBeenCalledWith(
expect.stringContaining("all the points"));
2018-02-20 17:13:14 -07:00
expect(p.dispatch).not.toHaveBeenCalled();
window.confirm = () => true;
p.dispatch = jest.fn(x => x());
button.simulate("click");
expect(deletePoints).toHaveBeenCalledWith("points", "farm-designer");
});
it("changes color", () => {
const p = fakeProps();
p.currentPoint = { cx: 0, cy: 0, r: 0 };
2018-11-09 13:43:51 -07:00
const wrapper = mount<CreatePoints>(<CreatePoints {...p} />);
const PP = wrapper.instance().PointProperties;
const component = shallow(<PP />);
2018-08-01 19:27:19 -06:00
component.find("ColorPicker").simulate("change", "red");
2018-02-20 17:13:14 -07:00
expect(p.dispatch).toHaveBeenCalledWith({
payload: { color: "red", cx: 0, cy: 0, r: 0 },
type: Actions.SET_CURRENT_POINT_DATA
});
});
it("updates value", () => {
const p = fakeProps();
p.currentPoint = { cx: 0, cy: 0, r: 0 };
2018-11-09 13:43:51 -07:00
const wrapper = shallow<CreatePoints>(<CreatePoints {...p} />);
const PP = wrapper.instance().PointProperties;
const component = shallow(<PP />);
2018-08-01 19:27:19 -06:00
component.find("BlurableInput").first().simulate("commit", {
2018-02-20 17:13:14 -07:00
currentTarget: { value: "10" }
});
expect(p.dispatch).toHaveBeenCalledWith({
payload: { cx: 10, cy: 0, r: 0 },
type: Actions.SET_CURRENT_POINT_DATA
});
});
2018-08-01 20:03:16 -06:00
it("fills the state with point data", () => {
const p = fakeProps();
p.currentPoint = { cx: 1, cy: 2, r: 3, color: "blue" };
2018-11-09 13:43:51 -07:00
const wrapper = shallow<CreatePoints>(<CreatePoints {...p} />);
2019-09-16 14:15:15 -06:00
const i = wrapper.instance();
expect(i.state).toEqual({});
expect(i.getPointData()).toEqual({
name: undefined,
cx: 1,
cy: 2,
r: 3,
color: "blue"
2018-11-09 13:43:51 -07:00
});
2018-08-01 20:03:16 -06:00
});
it("unmounts", () => {
const p = fakeProps();
const wrapper = shallow(<CreatePoints {...p} />);
jest.clearAllMocks();
wrapper.unmount();
expect(p.dispatch).toHaveBeenCalledWith({
type: Actions.SET_CURRENT_POINT_DATA,
payload: undefined
});
});
2018-02-20 17:13:14 -07:00
});