Farmbot-Web-App/frontend/sequences/step_tiles/__tests__/tile_execute_script_test.tsx

145 lines
5.1 KiB
TypeScript
Raw Normal View History

2017-07-30 16:19:45 -06:00
import * as React from "react";
import { TileExecuteScript } from "../tile_execute_script";
2018-02-14 22:07:36 -07:00
import { mount, shallow } from "enzyme";
2017-07-30 16:19:45 -06:00
import { fakeSequence } from "../../../__test_support__/fake_state/resources";
import { ExecuteScript } from "farmbot/dist";
2018-02-14 22:07:36 -07:00
import { StepParams } from "../../interfaces";
2019-12-27 11:37:54 -07:00
import { Actions, Content } from "../../../constants";
import { emptyState } from "../../../resources/reducer";
2019-12-27 11:37:54 -07:00
import {
fakeFarmwareData
} from "../../../__test_support__/fake_sequence_step_data";
2017-07-30 16:19:45 -06:00
describe("<TileExecuteScript/>", () => {
2018-02-14 22:07:36 -07:00
const fakeProps = (): StepParams => {
2017-07-30 16:19:45 -06:00
const currentStep: ExecuteScript = {
kind: "execute_script",
args: {
label: "farmware-to-execute"
}
};
2019-12-27 11:37:54 -07:00
const farmwareData = fakeFarmwareData();
farmwareData.farmwareNames = ["one", "two", "three"];
farmwareData.firstPartyFarmwareNames = ["one"];
farmwareData.farmwareConfigs = { "farmware-to-execute": [] };
2017-07-30 16:19:45 -06:00
return {
2018-02-14 22:07:36 -07:00
currentSequence: fakeSequence(),
currentStep,
dispatch: jest.fn(),
index: 0,
resources: emptyState().index,
2019-12-27 11:37:54 -07:00
farmwareData,
2018-08-30 19:25:58 -06:00
confirmStepDeletion: false,
2017-07-30 16:19:45 -06:00
};
2018-02-14 22:07:36 -07:00
};
2017-07-30 16:19:45 -06:00
it("renders inputs", () => {
2018-05-15 08:19:55 -06:00
const wrapper = mount(<TileExecuteScript {...fakeProps()} />);
2018-02-14 22:07:36 -07:00
const inputs = wrapper.find("input");
const labels = wrapper.find("label");
2017-07-30 16:19:45 -06:00
expect(inputs.length).toEqual(2);
2018-02-14 22:07:36 -07:00
expect(labels.length).toEqual(2);
2017-07-30 16:19:45 -06:00
expect(inputs.first().props().placeholder).toEqual("Run Farmware");
expect(labels.at(0).text()).toEqual("Package Name");
2018-02-14 22:07:36 -07:00
expect(labels.at(1).text()).toEqual("Manual input");
2017-07-30 16:19:45 -06:00
expect(inputs.at(1).props().value).toEqual("farmware-to-execute");
});
2018-02-14 22:07:36 -07:00
2018-05-18 16:34:12 -06:00
it("renders error on wrong step", () => {
const p = fakeProps();
p.currentStep = { kind: "wait", args: { milliseconds: 100 } };
const wrapper = mount(<TileExecuteScript {...p} />);
expect(wrapper.text()).toContain("ERROR");
});
2018-02-14 22:07:36 -07:00
it("renders farmware list", () => {
2018-05-15 08:19:55 -06:00
const wrapper = shallow(<TileExecuteScript {...fakeProps()} />);
2018-02-14 22:07:36 -07:00
expect(wrapper.find("FBSelect").props().list).toEqual([
{ label: "two", value: "two" },
2019-05-15 10:12:26 -06:00
{ label: "three", value: "three" },
{ label: "Weed Detector", value: "plant-detection" },
]);
2018-02-14 22:07:36 -07:00
});
it("shows 1st party in list", () => {
const p = fakeProps();
2019-12-27 11:37:54 -07:00
p.farmwareData && (p.farmwareData.showFirstPartyFarmware = true);
2018-02-14 22:07:36 -07:00
const wrapper = shallow(<TileExecuteScript {...p} />);
expect(wrapper.find("FBSelect").props().list).toEqual([
{ label: "one", value: "one" },
{ label: "two", value: "two" },
2019-05-15 10:12:26 -06:00
{ label: "three", value: "three" },
{ label: "Weed Detector", value: "plant-detection" },
]);
2018-02-14 22:07:36 -07:00
});
it("doesn't show manual input if installed farmware is selected", () => {
const p = fakeProps();
(p.currentStep as ExecuteScript).args.label = "two";
const wrapper = mount(<TileExecuteScript {...p} />);
expect(wrapper.find("label").length).toEqual(1);
});
2019-05-15 10:12:26 -06:00
it("shows special 1st-party Farmware name", () => {
const p = fakeProps();
(p.currentStep as ExecuteScript).args.label = "plant-detection";
2020-01-03 13:04:45 -07:00
p.farmwareData?.farmwareNames.push("plant-detection");
2019-05-15 10:12:26 -06:00
const wrapper = mount(<TileExecuteScript {...p} />);
expect(wrapper.find("label").length).toEqual(1);
expect(wrapper.text()).toContain("Weed Detector");
});
2018-02-14 22:07:36 -07:00
it("renders manual input", () => {
const p = fakeProps();
2019-12-27 11:37:54 -07:00
p.farmwareData = undefined;
2018-02-14 22:07:36 -07:00
const wrapper = mount(<TileExecuteScript {...p} />);
2018-05-15 17:05:35 -06:00
expect(wrapper.find("button").text()).toEqual("Manual Input");
2018-02-14 22:07:36 -07:00
expect(wrapper.find("label").at(1).text()).toEqual("Manual input");
expect(wrapper.find("input").at(1).props().value).toEqual("farmware-to-execute");
});
it("uses drop-down to update step", () => {
const p = fakeProps();
const wrapper = shallow(<TileExecuteScript {...p} />);
wrapper.find("FBSelect").simulate("change", {
label: "farmware-name",
value: "farmware-name"
});
expect(p.dispatch).toHaveBeenCalledWith({
payload: expect.objectContaining({
update: expect.objectContaining({
2018-06-06 13:47:03 -06:00
body: [{ kind: "execute_script", args: { label: "farmware-name" } }]
})
}),
type: Actions.OVERWRITE_RESOURCE
});
});
it("clears body when switching Farmware", () => {
const p = fakeProps();
p.currentStep.body = [
{ kind: "pair", args: { label: "x", value: 1 }, comment: "X" }];
const wrapper = shallow(<TileExecuteScript {...p} />);
wrapper.find("FBSelect").simulate("change", {
label: "farmware-name",
value: "farmware-name"
});
expect(p.dispatch).toHaveBeenCalledWith({
payload: expect.objectContaining({
update: expect.objectContaining({
body: [{ kind: "execute_script", args: { label: "farmware-name" } }]
2018-02-14 22:07:36 -07:00
})
}),
type: Actions.OVERWRITE_RESOURCE
});
});
2019-12-27 11:37:54 -07:00
it("displays warning when camera is disabled", () => {
const p = fakeProps();
(p.currentStep as ExecuteScript).args.label = "plant-detection";
p.farmwareData && (p.farmwareData.cameraDisabled = true);
const wrapper = mount(<TileExecuteScript {...p} />);
expect(wrapper.text()).toContain(Content.NO_CAMERA_SELECTED);
});
2017-07-30 16:19:45 -06:00
});