Farmbot-Web-App/frontend/farm_designer/map/layers/farmbot/__tests__/bot_figure_test.tsx

126 lines
4.6 KiB
TypeScript
Raw Normal View History

2017-10-31 23:14:21 -06:00
import * as React from "react";
import { shallow } from "enzyme";
2018-10-25 18:02:54 -06:00
import { BotOriginQuadrant } from "../../../../interfaces";
2017-10-31 23:14:21 -06:00
import { BotFigure, BotFigureProps } from "../bot_figure";
2018-10-25 18:02:54 -06:00
import { Color } from "../../../../../ui/index";
import {
fakeMapTransformProps
} from "../../../../../__test_support__/map_transform_props";
2017-10-31 23:14:21 -06:00
describe("<BotFigure/>", () => {
2020-01-03 13:06:28 -07:00
const fakeProps = (): BotFigureProps => ({
2020-02-28 09:34:28 -07:00
figureName: "",
2020-01-03 13:06:28 -07:00
position: { x: 0, y: 0, z: 0 },
mapTransformProps: fakeMapTransformProps(),
plantAreaOffset: { x: 100, y: 100 },
});
2017-10-31 23:14:21 -06:00
2020-02-18 12:21:09 -07:00
const EXPECTED_MOTORS_OPACITY = 0.5;
2020-01-03 13:06:28 -07:00
it.each<[
string, BotOriginQuadrant, Record<"x" | "y", number>, boolean, number
]>([
2020-02-18 12:21:09 -07:00
["motors", 1, { x: 3000, y: 0 }, false, EXPECTED_MOTORS_OPACITY],
["motors", 2, { x: 0, y: 0 }, false, EXPECTED_MOTORS_OPACITY],
["motors", 3, { x: 0, y: 1500 }, false, EXPECTED_MOTORS_OPACITY],
["motors", 4, { x: 3000, y: 1500 }, false, EXPECTED_MOTORS_OPACITY],
["motors", 1, { x: 0, y: 1500 }, true, EXPECTED_MOTORS_OPACITY],
["motors", 2, { x: 0, y: 0 }, true, EXPECTED_MOTORS_OPACITY],
["motors", 3, { x: 3000, y: 0 }, true, EXPECTED_MOTORS_OPACITY],
["motors", 4, { x: 3000, y: 1500 }, true, EXPECTED_MOTORS_OPACITY],
2020-01-03 13:06:28 -07:00
["encoders", 2, { x: 0, y: 0 }, false, 0.25],
])("shows %s in correct location for quadrant %i",
2020-02-28 09:34:28 -07:00
(figureName, quadrant, expected, xySwap, opacity) => {
2017-10-31 23:14:21 -06:00
const p = fakeProps();
p.mapTransformProps.quadrant = quadrant;
2018-04-13 03:54:00 -06:00
p.mapTransformProps.xySwap = xySwap;
2020-02-28 09:34:28 -07:00
p.figureName = figureName;
2018-07-02 15:43:51 -06:00
const result = shallow<BotFigure>(<BotFigure {...p} />);
2017-10-31 23:14:21 -06:00
2018-03-14 15:57:38 -06:00
const expectedGantryProps = expect.objectContaining({
2017-10-31 23:14:21 -06:00
id: "gantry",
2018-04-13 03:54:00 -06:00
x: xySwap ? -100 : expected.x - 10,
y: xySwap ? expected.x - 10 : -100,
width: xySwap ? 1700 : 20,
height: xySwap ? 20 : 1700,
2017-11-08 12:52:57 -07:00
fill: Color.darkGray,
2017-10-31 23:14:21 -06:00
fillOpacity: opacity
2018-03-14 15:57:38 -06:00
});
2017-10-31 23:14:21 -06:00
const gantryProps = result.find("rect").props();
expect(gantryProps).toEqual(expectedGantryProps);
2018-03-14 15:57:38 -06:00
const expectedUTMProps = expect.objectContaining({
2017-10-31 23:14:21 -06:00
id: "UTM",
2018-04-13 03:54:00 -06:00
cx: xySwap ? expected.y : expected.x,
cy: xySwap ? expected.x : expected.y,
2017-10-31 23:14:21 -06:00
r: 35,
2017-11-08 12:52:57 -07:00
fill: Color.darkGray,
2017-10-31 23:14:21 -06:00
fillOpacity: opacity
2018-03-14 15:57:38 -06:00
});
2017-10-31 23:14:21 -06:00
const UTMProps = result.find("circle").props();
expect(UTMProps).toEqual(expectedUTMProps);
});
it("changes location", () => {
const p = fakeProps();
p.mapTransformProps.quadrant = 2;
p.position = { x: 100, y: 200, z: 0 };
2018-07-02 15:43:51 -06:00
const result = shallow<BotFigure>(<BotFigure {...p} />);
2017-10-31 23:14:21 -06:00
const gantry = result.find("#gantry");
expect(gantry.length).toEqual(1);
expect(gantry.props().x).toEqual(90);
const UTM = result.find("circle").props();
expect(UTM.cx).toEqual(100);
expect(UTM.cy).toEqual(200);
});
it("changes color on e-stop", () => {
const p = fakeProps();
p.eStopStatus = true;
2018-07-02 15:43:51 -06:00
const wrapper = shallow<BotFigure>(<BotFigure {...p} />);
2017-11-08 12:52:57 -07:00
expect(wrapper.find("#gantry").props().fill).toEqual(Color.virtualRed);
});
2018-03-14 15:57:38 -06:00
it("shows coordinates on hover", () => {
2018-04-17 13:46:53 -06:00
const p = fakeProps();
p.position.x = 100;
2018-07-02 15:43:51 -06:00
const wrapper = shallow<BotFigure>(<BotFigure {...p} />);
expect(wrapper.instance().state.hovered).toBeFalsy();
2020-02-20 19:38:50 -07:00
const utm = wrapper.find("#UTM-wrapper");
2018-03-14 15:57:38 -06:00
utm.simulate("mouseOver");
expect(wrapper.instance().state.hovered).toBeTruthy();
2018-04-17 13:46:53 -06:00
expect(wrapper.find("text").props()).toEqual(expect.objectContaining({
x: 100, y: 0, dx: 40, dy: 0,
textAnchor: "start", visibility: "visible",
}));
expect(wrapper.text()).toEqual("(100, 0, 0)");
2018-03-14 15:57:38 -06:00
utm.simulate("mouseLeave");
expect(wrapper.instance().state.hovered).toBeFalsy();
2018-04-17 13:46:53 -06:00
expect(wrapper.find("text").props()).toEqual(
expect.objectContaining({ visibility: "hidden" }));
});
it("shows coordinates on hover: X&Y swapped", () => {
const p = fakeProps();
p.position.x = 100;
p.mapTransformProps.xySwap = true;
2018-07-02 15:43:51 -06:00
const wrapper = shallow<BotFigure>(<BotFigure {...p} />);
2020-02-20 19:38:50 -07:00
const utm = wrapper.find("#UTM-wrapper");
2018-04-17 13:46:53 -06:00
utm.simulate("mouseOver");
expect(wrapper.instance().state.hovered).toBeTruthy();
2018-04-17 13:46:53 -06:00
expect(wrapper.find("text").props()).toEqual(expect.objectContaining({
x: 0, y: 100, dx: 0, dy: 55,
textAnchor: "middle", visibility: "visible",
}));
expect(wrapper.text()).toEqual("(100, 0, 0)");
2018-03-14 15:57:38 -06:00
});
2020-02-20 19:38:50 -07:00
it("shows mounted tool", () => {
const p = fakeProps();
p.mountedToolName = "Seeder";
const wrapper = shallow<BotFigure>(<BotFigure {...p} />);
expect(wrapper.find("#UTM-wrapper").find("#mounted-tool").length)
.toEqual(1);
});
2017-10-31 23:14:21 -06:00
});