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

118 lines
4.0 KiB
TypeScript
Raw Normal View History

let mockPath = "";
jest.mock("../../../history", () => ({
2018-03-14 23:08:51 -06:00
getPathArray: jest.fn(() => { return mockPath.split("/"); }),
2018-06-15 18:28:42 -06:00
history: { push: jest.fn() }
2017-09-15 23:51:12 -06:00
}));
2019-09-23 12:56:35 -06:00
jest.mock("../../../api/crud", () => ({ initSave: jest.fn() }));
jest.mock("../../actions", () => ({
unselectPlant: jest.fn(() => jest.fn()),
setDragIcon: jest.fn(),
}));
2017-09-15 23:51:12 -06:00
import * as React from "react";
2019-09-23 12:56:35 -06:00
import { RawCropInfo as CropInfo, searchForCurrentCrop } from "../crop_info";
import { mount } from "enzyme";
2018-03-14 23:08:51 -06:00
import { CropInfoProps } from "../../interfaces";
import { initSave } from "../../../api/crud";
2018-06-15 18:28:42 -06:00
import { history } from "../../../history";
2018-11-28 15:29:46 -07:00
import {
fakeCropLiveSearchResult
} from "../../../__test_support__/fake_crop_search_result";
import { unselectPlant } from "../../actions";
2019-06-03 20:09:49 -06:00
import { svgToUrl } from "../../../open_farm/icons";
2017-09-15 23:51:12 -06:00
describe("<CropInfo />", () => {
2018-03-14 23:08:51 -06:00
const fakeProps = (): CropInfoProps => {
2018-11-28 15:29:46 -07:00
const cropSearchResult = fakeCropLiveSearchResult();
cropSearchResult.crop.svg_icon = "fake_mint_svg";
cropSearchResult.crop.row_spacing = 100;
2018-03-14 23:08:51 -06:00
return {
openfarmSearch: jest.fn(),
2018-03-14 23:08:51 -06:00
dispatch: jest.fn(),
2018-12-03 20:05:45 -07:00
cropSearchQuery: undefined,
2018-11-28 15:29:46 -07:00
cropSearchResults: [cropSearchResult],
cropSearchInProgress: false,
2018-09-13 16:00:14 -06:00
openedSavedGarden: undefined,
botPosition: { x: undefined, y: undefined, z: undefined },
2018-03-14 23:08:51 -06:00
};
};
2017-09-15 23:51:12 -06:00
it("renders", () => {
mockPath = "/app/designer/plants/crop_search/mint";
2018-03-14 23:08:51 -06:00
const wrapper = mount(<CropInfo {...fakeProps()} />);
2017-09-15 23:51:12 -06:00
expect(wrapper.text()).toContain("Mint");
expect(wrapper.text()).toContain("Drag and drop into map");
2018-03-14 23:08:51 -06:00
expect(wrapper.text()).toContain("Row Spacing1000mm");
2017-09-15 23:51:12 -06:00
expect(wrapper.find("img").last().props().src)
2019-06-03 20:09:49 -06:00
.toEqual(svgToUrl("fake_mint_svg"));
2017-09-15 23:51:12 -06:00
});
2018-03-14 23:08:51 -06:00
it("navigates to /add", () => {
mockPath = "/app/designer/plants/crop_search/mint";
const wrapper = mount(<CropInfo {...fakeProps()} />);
2018-03-14 23:08:51 -06:00
wrapper.find(".right-button").simulate("click");
2018-06-15 18:28:42 -06:00
expect(history.push).toHaveBeenCalledWith(
2018-03-14 23:08:51 -06:00
"/app/designer/plants/crop_search/mint/add");
});
it("returns to crop search", () => {
mockPath = "/app/designer/plants/crop_search/mint";
const wrapper = mount(<CropInfo {...fakeProps()} />);
wrapper.find(".back-arrow").simulate("click");
2018-06-15 18:28:42 -06:00
expect(history.push).toHaveBeenCalledWith(
"/app/designer/plants/crop_search/");
});
it("disables 'add plant @ UTM' button", () => {
const wrapper = mount(<CropInfo {...fakeProps()} />);
expect(wrapper.text()).toContain("location (unknown)");
});
it("adds a plant at the current bot position", () => {
const p = fakeProps();
p.botPosition = { x: 100, y: 200, z: undefined };
const wrapper = mount(<CropInfo {...p} />);
const button = wrapper.find("button").last();
expect(button.text()).toContain("location (100, 200)");
button.simulate("click");
2018-10-31 17:49:05 -06:00
expect(initSave).toHaveBeenCalledWith("Point",
expect.objectContaining({
name: "Mint",
x: 100,
y: 200,
z: 0
2018-10-31 17:49:05 -06:00
}));
});
it("renders cm in mm", () => {
const p = fakeProps();
p.cropSearchResults[0].crop.spread = 1234;
const wrapper = mount(<CropInfo {...p} />);
expect(wrapper.text().toLowerCase()).toContain("12340mm");
});
it("renders missing values", () => {
const p = fakeProps();
p.cropSearchResults[0].crop.svg_icon = undefined;
p.cropSearchResults[0].crop.row_spacing = undefined;
p.cropSearchResults[0].crop.common_names = [];
const wrapper = mount(<CropInfo {...p} />);
expect(wrapper.text().toLowerCase()).toContain("iconnot set");
expect(wrapper.text().toLowerCase()).toContain("spacingnot set");
expect(wrapper.text().toLowerCase()).toContain("common namesnot set");
});
});
describe("searchForCurrentCrop()", () => {
it("searches", () => {
mockPath = "/app/designer/plants/crop_search/mint";
const dispatch = jest.fn();
const fakeOFSearch = jest.fn(() => jest.fn());
searchForCurrentCrop(fakeOFSearch)(dispatch);
expect(fakeOFSearch).toHaveBeenCalledWith("mint");
expect(unselectPlant).toHaveBeenCalled();
});
2017-09-15 23:51:12 -06:00
});