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

260 lines
8.6 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()
}));
2019-11-07 12:17:50 -07:00
let mockPath = "/app/designer/points/add";
jest.mock("../../../history", () => ({
2020-02-26 11:10:59 -07:00
history: { push: jest.fn() },
2019-11-07 12:17:50 -07:00
push: jest.fn(),
getPathArray: () => mockPath.split("/"),
}));
2018-02-20 17:13:14 -07:00
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,
2020-02-28 09:35:32 -07:00
mapStateToProps,
2019-09-16 14:15:15 -06:00
} 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 { DrawnPointPayl } from "../../interfaces";
2020-02-07 16:05:16 -07:00
import { inputEvent } from "../../../__test_support__/fake_html_events";
2019-11-07 12:17:50 -07:00
import { cloneDeep } from "lodash";
2019-09-16 14:15:15 -06:00
const FAKE_POINT: DrawnPointPayl =
2019-09-16 14:15:15 -06:00
({ name: "My Point", cx: 13, cy: 22, r: 345, color: "red" });
describe("mapStateToProps", () => {
it("maps state to props: drawn point", () => {
2019-09-16 14:15:15 -06:00
const state = fakeState();
state.resources.consumers.farm_designer.drawnPoint = FAKE_POINT;
const props = mapStateToProps(state);
expect(props.drawnPoint?.cx).toEqual(13);
expect(props.drawnPoint?.cy).toEqual(22);
});
it("maps state to props: drawn weed", () => {
const state = fakeState();
state.resources.consumers.farm_designer.drawnPoint = undefined;
state.resources.consumers.farm_designer.drawnWeed = FAKE_POINT;
const props = mapStateToProps(state);
expect(props.drawnPoint?.cx).toEqual(13);
expect(props.drawnPoint?.cy).toEqual(22);
2019-09-16 14:15:15 -06:00
});
});
2018-02-20 17:13:14 -07:00
describe("<CreatePoints />", () => {
2019-11-07 12:17:50 -07:00
beforeEach(() => {
mockPath = "/app/designer/points/add";
});
2019-09-23 12:56:35 -06:00
const fakeProps = (): CreatePointsProps => ({
dispatch: jest.fn(),
drawnPoint: undefined,
2019-09-23 12:56:35 -06:00
deviceY: 1.23,
deviceX: 3.21
});
2018-08-01 20:03:16 -06:00
2019-11-07 12:17:50 -07:00
it("renders for points", () => {
mockPath = "/app/designer";
2018-05-15 20:38:52 -06:00
const wrapper = mount(<CreatePoints {...fakeProps()} />);
2020-04-13 13:24:38 -06:00
["add point", "delete", "x", "y", "radius"]
2019-11-07 12:17:50 -07:00
.map(string => expect(wrapper.text().toLowerCase()).toContain(string));
});
it("renders for weeds", () => {
mockPath = "/app/designer/weeds/add";
const wrapper = mount(<CreatePoints {...fakeProps()} />);
2020-04-13 13:24:38 -06:00
["add weed", "delete", "x", "y", "radius"]
2018-02-20 17:13:14 -07:00
.map(string => expect(wrapper.text().toLowerCase()).toContain(string));
});
2019-09-16 14:15:15 -06:00
it("updates specific fields", () => {
const p = fakeProps();
p.drawnPoint = FAKE_POINT;
2020-05-06 16:03:15 -06:00
const wrapper = mount<CreatePoints>(<CreatePoints {...p} />);
wrapper.instance().updateValue("color")(inputEvent("cheerful hue"));
expect(wrapper.instance().props.drawnPoint).toBeTruthy();
2019-11-07 12:17:50 -07:00
const expected = cloneDeep(FAKE_POINT);
expected.color = "cheerful hue";
2020-05-06 16:03:15 -06:00
expect(wrapper.instance().props.dispatch).toHaveBeenCalledWith({
type: "SET_DRAWN_POINT_DATA",
2019-11-07 12:17:50 -07:00
payload: expected,
2019-09-16 14:15:15 -06:00
});
});
2019-11-07 12:17:50 -07:00
it("doesn't update fields without current point", () => {
const p = fakeProps();
const wrapper = mount<CreatePoints>(<CreatePoints {...p} />);
jest.clearAllMocks();
wrapper.instance().updateValue("r")(inputEvent("1"));
expect(p.dispatch).not.toHaveBeenCalled();
});
it("loads default point data", () => {
const p = fakeProps();
p.drawnPoint = FAKE_POINT;
const i = new CreatePoints(p);
2019-11-07 12:17:50 -07:00
i.loadDefaultPoint();
expect(i.props.dispatch).toHaveBeenCalledWith({
type: "SET_DRAWN_POINT_DATA",
2019-11-07 12:17:50 -07:00
payload: { name: "Created Point", color: "green", cx: 1, cy: 1, r: 15 },
});
});
it("updates weed name", () => {
2019-11-07 12:17:50 -07:00
mockPath = "/app/designer/weeds/add";
const p = fakeProps();
p.drawnPoint = { cx: 0, cy: 0, r: 100 };
2019-11-07 12:17:50 -07:00
const panel = mount<CreatePoints>(<CreatePoints {...p} />);
2019-11-20 12:48:55 -07:00
const wrapper = shallow(panel.instance().PointProperties());
wrapper.find("BlurableInput").first().simulate("commit", {
2019-11-07 12:17:50 -07:00
currentTarget: { value: "new name" }
});
expect(p.dispatch).toHaveBeenCalledWith({
type: Actions.SET_DRAWN_WEED_DATA, payload: {
2019-11-07 12:17:50 -07:00
cx: 0, cy: 0, r: 100, name: "new name", color: "red",
}
2019-09-16 14:15:15 -06:00
});
});
2019-11-07 12:17:50 -07:00
2018-02-20 17:13:14 -07:00
it("creates point", () => {
2019-11-07 12:17:50 -07:00
mockPath = "/app/designer/points/add";
2018-05-15 20:38:52 -06:00
const wrapper = mount(<CreatePoints {...fakeProps()} />);
2019-11-07 12:17:50 -07:00
wrapper.setState({ cx: 10, cy: 20, r: 30 });
clickButton(wrapper, 0, "save");
expect(initSave).toHaveBeenCalledWith("Point", {
meta: { color: "green", created_by: "farm-designer", type: "point" },
name: "Created Point",
pointer_type: "GenericPointer",
radius: 30, x: 10, y: 20, z: 0,
});
});
it("creates weed", () => {
mockPath = "/app/designer/weeds/add";
const wrapper = mount(<CreatePoints {...fakeProps()} />);
wrapper.setState({ cx: 10, cy: 20, r: 30 });
clickButton(wrapper, 0, "save");
expect(initSave).toHaveBeenCalledWith("Point", {
meta: { color: "red", created_by: "farm-designer", type: "weed" },
name: "Created Weed",
pointer_type: "Weed",
2019-11-07 12:17:50 -07:00
radius: 30, x: 10, y: 20, z: 0,
});
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"));
2019-11-07 12:17:50 -07:00
expect(deletePoints).not.toHaveBeenCalled();
window.confirm = () => true;
p.dispatch = jest.fn(x => x());
button.simulate("click");
expect(deletePoints).toHaveBeenCalledWith("points", {
pointer_type: "GenericPointer",
meta: { created_by: "farm-designer" }
2019-11-07 12:17:50 -07:00
});
});
it("deletes all weeds", () => {
mockPath = "/app/designer/weeds/add";
const p = fakeProps();
const wrapper = mount(<CreatePoints {...p} />);
const button = wrapper.find("button").last();
expect(button.text()).toEqual("Delete all created weeds");
window.confirm = jest.fn();
button.simulate("click");
expect(window.confirm).toHaveBeenCalledWith(
expect.stringContaining("all the weeds"));
expect(deletePoints).not.toHaveBeenCalled();
2018-02-20 17:13:14 -07:00
window.confirm = () => true;
p.dispatch = jest.fn(x => x());
button.simulate("click");
2019-11-07 12:17:50 -07:00
expect(deletePoints).toHaveBeenCalledWith("points", {
pointer_type: "Weed",
meta: { created_by: "farm-designer" }
2019-11-07 12:17:50 -07:00
});
2018-02-20 17:13:14 -07:00
});
it("changes point color", () => {
const p = fakeProps();
p.drawnPoint = { cx: 0, cy: 0, r: 0 };
const wrapper = mount<CreatePoints>(<CreatePoints {...p} />);
const PP = wrapper.instance().PointProperties;
const component = shallow(<PP />);
component.find("ColorPicker").simulate("change", "red");
expect(p.dispatch).toHaveBeenCalledWith({
payload: { color: "red", cx: 0, cy: 0, r: 0 },
type: Actions.SET_DRAWN_POINT_DATA
});
});
it("changes weed color", () => {
mockPath = "/app/designer/weeds/add";
2018-02-20 17:13:14 -07:00
const p = fakeProps();
p.drawnPoint = { 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_DRAWN_WEED_DATA
2018-02-20 17:13:14 -07:00
});
});
it("updates value", () => {
const p = fakeProps();
p.drawnPoint = { 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 />);
2019-11-20 12:48:55 -07:00
component.find("BlurableInput").at(1).simulate("commit", {
2018-02-20 17:13:14 -07:00
currentTarget: { value: "10" }
});
expect(p.dispatch).toHaveBeenCalledWith({
2019-11-07 12:17:50 -07:00
payload: { cx: 10, cy: 0, r: 0, color: "green" },
type: Actions.SET_DRAWN_POINT_DATA
2018-02-20 17:13:14 -07:00
});
});
2018-08-01 20:03:16 -06:00
it("fills the state with point data", () => {
const p = fakeProps();
p.drawnPoint = { 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_DRAWN_POINT_DATA,
payload: undefined
});
});
2018-02-20 17:13:14 -07:00
});