Farmbot-Web-App/frontend/farmware/weed_detector/__tests__/weed_detector_test.tsx

118 lines
3.7 KiB
TypeScript
Raw Normal View History

2017-12-16 01:53:48 -07:00
const mockDevice = {
2018-04-03 12:53:45 -06:00
execScript: jest.fn(() => Promise.resolve()),
2018-11-01 11:17:18 -06:00
setUserEnv: jest.fn(() => Promise.resolve()),
2017-12-16 01:53:48 -07:00
};
2019-09-23 12:56:35 -06:00
jest.mock("../../../device", () => ({ getDevice: () => mockDevice }));
2017-09-15 23:51:12 -06:00
2018-11-01 11:17:18 -06:00
jest.mock("../../images/actions", () => ({ selectImage: jest.fn() }));
2017-09-15 23:51:12 -06:00
import * as React from "react";
2017-12-16 01:53:48 -07:00
import { mount, shallow } from "enzyme";
2019-09-23 12:56:35 -06:00
import { WeedDetector, namespace } from "../index";
2017-09-15 23:51:12 -06:00
import { FarmwareProps } from "../../../devices/interfaces";
2018-06-21 15:04:21 -06:00
import { API } from "../../../api";
2018-11-01 11:17:18 -06:00
import { selectImage } from "../../images/actions";
import { clickButton } from "../../../__test_support__/helpers";
2019-04-09 23:17:03 -06:00
import { fakeTimeSettings } from "../../../__test_support__/fake_time_settings";
2017-09-15 23:51:12 -06:00
describe("<WeedDetector />", () => {
2018-06-21 15:04:21 -06:00
API.setBaseUrl("http://localhost:3000");
2018-11-01 11:17:18 -06:00
const fakeProps = (): FarmwareProps => ({
2019-04-09 23:17:03 -06:00
timeSettings: fakeTimeSettings(),
2017-12-16 01:53:48 -07:00
farmwares: {},
botToMqttStatus: "up",
2017-12-16 01:53:48 -07:00
env: {},
user_env: {},
2018-11-01 11:17:18 -06:00
dispatch: jest.fn(),
2017-12-16 01:53:48 -07:00
currentImage: undefined,
2018-01-19 10:49:07 -07:00
images: [],
syncStatus: "synced",
2019-04-09 23:17:03 -06:00
getConfigValue: jest.fn(),
2018-06-21 15:04:21 -06:00
firstPartyFarmwareNames: [],
currentFarmware: undefined,
2018-11-01 11:17:18 -06:00
shouldDisplay: () => false,
saveFarmwareEnv: jest.fn(),
2018-11-05 18:37:09 -07:00
taggedFarmwareInstallations: [],
imageJobs: [],
2019-04-09 19:45:59 -06:00
infoOpen: false,
2018-11-01 11:17:18 -06:00
});
2017-12-16 01:53:48 -07:00
2017-09-15 23:51:12 -06:00
it("renders", () => {
2018-11-01 11:17:18 -06:00
const wrapper = mount(<WeedDetector {...fakeProps()} />);
2018-06-21 15:04:21 -06:00
["Color Range",
2017-10-16 22:22:35 -06:00
"HUE01793090",
"SATURATION025550255",
"VALUE025550255",
"Processing Parameters",
"Scan image"
].map(string =>
expect(wrapper.text()).toContain(string));
2017-09-15 23:51:12 -06:00
});
2017-12-16 01:53:48 -07:00
it("executes plant detection", () => {
2018-11-01 11:17:18 -06:00
const p = fakeProps();
p.dispatch = jest.fn(x => x());
const wrapper = shallow(<WeedDetector {...p} />);
clickButton(wrapper, 0, "detect weeds");
2017-12-16 01:53:48 -07:00
expect(mockDevice.execScript).toHaveBeenCalledWith("plant-detection");
});
it("executes clear weeds", () => {
const wrapper =
2018-11-01 11:17:18 -06:00
shallow<WeedDetector>(<WeedDetector {...fakeProps()} />);
expect(wrapper.instance().state.deletionProgress).toBeUndefined();
2018-11-01 11:17:18 -06:00
clickButton(wrapper, 1, "clear weeds");
expect(wrapper.instance().state.deletionProgress).toEqual("Deleting...");
2017-12-16 01:53:48 -07:00
});
2018-11-01 11:17:18 -06:00
it("saves changes", () => {
const p = fakeProps();
p.shouldDisplay = () => false;
const wrapper = shallow(<WeedDetector {...p} />);
wrapper.find("ImageWorkspace").simulate("change", "H_LO", 3);
expect(mockDevice.setUserEnv)
.toHaveBeenCalledWith({ WEED_DETECTOR_H_LO: "3" });
});
it("saves ImageWorkspace changes: API", () => {
const p = fakeProps();
p.shouldDisplay = () => true;
const wrapper = shallow(<WeedDetector {...p} />);
wrapper.find("ImageWorkspace").simulate("change", "H_LO", 3);
expect(p.saveFarmwareEnv)
.toHaveBeenCalledWith("WEED_DETECTOR_H_LO", "3");
});
it("calls scanImage", () => {
const p = fakeProps();
p.dispatch = jest.fn(x => x());
const wrapper = shallow(<WeedDetector {...p} />);
wrapper.find("ImageWorkspace").simulate("processPhoto", 1);
expect(mockDevice.execScript).toHaveBeenCalledWith(
"historical-plant-detection",
[expect.objectContaining({
kind: "pair",
args: expect.objectContaining({ value: "1" })
})]);
});
it("calls selectImage", () => {
const wrapper = shallow(<WeedDetector {...fakeProps()} />);
wrapper.find("ImageWorkspace").simulate("flip", "image0001");
expect(selectImage).toHaveBeenCalledWith("image0001");
});
});
describe("namespace()", () => {
it("returns namespaced key", () => {
expect(namespace("CAMERA_CALIBRATION_")("H_LO"))
.toEqual("CAMERA_CALIBRATION_H_LO");
});
it("throws error", () => {
expect(() => namespace("TEST_")("key"))
.toThrowError("TEST_key is not a WDENVKey");
});
2017-09-15 23:51:12 -06:00
});