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

207 lines
6.4 KiB
TypeScript
Raw Normal View History

2018-06-15 18:28:42 -06:00
jest.mock("../../../history", () => ({ history: { push: jest.fn() } }));
2018-05-02 02:00:30 -06:00
2019-12-30 13:13:23 -07:00
jest.mock("../../../api/crud", () => ({
edit: jest.fn(),
save: jest.fn(),
}));
2017-06-29 12:54:02 -06:00
import * as React from "react";
2018-06-15 18:28:42 -06:00
import {
2019-12-30 13:13:23 -07:00
PlantPanel, PlantPanelProps, EditPlantStatusProps,
2019-01-13 18:41:23 -07:00
EditDatePlantedProps, EditDatePlanted, EditPlantLocationProps,
2019-12-30 13:13:23 -07:00
EditPlantLocation,
2018-06-15 18:28:42 -06:00
} from "../plant_panel";
2018-07-31 15:21:21 -06:00
import { shallow, mount } from "enzyme";
2018-03-06 21:54:26 -07:00
import { FormattedPlantInfo } from "../map_state_to_props";
2018-05-21 22:43:32 -06:00
import { Actions } from "../../../constants";
2018-06-15 17:59:32 -06:00
import { clickButton } from "../../../__test_support__/helpers";
2018-06-15 18:28:42 -06:00
import { history } from "../../../history";
2019-02-04 13:54:17 -07:00
import moment from "moment";
2019-04-09 23:17:03 -06:00
import { fakeTimeSettings } from "../../../__test_support__/fake_time_settings";
2019-12-30 13:13:23 -07:00
import { fakePlant } from "../../../__test_support__/fake_state/resources";
import { edit } from "../../../api/crud";
import {
EditPlantStatus, PlantStatusBulkUpdateProps, PlantStatusBulkUpdate
} from "../edit_plant_status";
2017-06-29 12:54:02 -06:00
describe("<PlantPanel/>", () => {
2018-03-06 21:54:26 -07:00
const info: FormattedPlantInfo = {
2017-08-25 19:02:07 -06:00
x: 12,
y: 34,
id: undefined,
name: "tomato",
2018-03-06 21:54:26 -07:00
uuid: "Plant.0.0",
2017-08-25 19:02:07 -06:00
daysOld: 1,
2019-01-13 18:41:23 -07:00
plantedAt: moment("2017-06-19T08:02:22.466-05:00"),
2018-03-06 21:54:26 -07:00
slug: "tomato",
plantStatus: "planned",
};
2019-06-07 18:26:12 -06:00
const fakeProps = (): PlantPanelProps => ({
info,
onDestroy: jest.fn(),
updatePlant: jest.fn(),
dispatch: jest.fn(),
inSavedGarden: false,
timeSettings: fakeTimeSettings(),
});
2017-06-29 12:54:02 -06:00
2017-08-25 19:02:07 -06:00
it("renders: editing", () => {
2018-03-06 21:54:26 -07:00
const p = fakeProps();
2018-07-31 15:21:21 -06:00
const wrapper = mount(<PlantPanel {...p} />);
2018-03-06 21:54:26 -07:00
const txt = wrapper.text().toLowerCase();
2017-06-29 12:54:02 -06:00
expect(txt).toContain("1 days old");
2019-01-13 18:41:23 -07:00
const x = wrapper.find("input").at(1).props().value;
const y = wrapper.find("input").at(2).props().value;
2019-06-14 16:59:11 -06:00
expect(x).toEqual(12);
expect(y).toEqual(34);
2018-03-06 21:54:26 -07:00
});
it("calls destroy", () => {
const p = fakeProps();
2019-06-07 18:26:12 -06:00
const wrapper = mount(<PlantPanel {...p} />);
clickButton(wrapper, 2, "Delete");
2018-03-06 21:54:26 -07:00
expect(p.onDestroy).toHaveBeenCalledWith("Plant.0.0");
});
2018-05-02 02:00:30 -06:00
it("renders", () => {
2018-09-13 16:00:14 -06:00
const p = fakeProps();
const wrapper = mount(<PlantPanel {...p} />);
2018-05-02 02:00:30 -06:00
const txt = wrapper.text().toLowerCase();
expect(txt).toContain("1 days old");
2019-06-14 16:59:11 -06:00
expect(wrapper.find("button").length).toEqual(4);
});
it("renders in saved garden", () => {
const p = fakeProps();
p.inSavedGarden = true;
const wrapper = mount(<PlantPanel {...p} />);
const txt = wrapper.text().toLowerCase();
2019-06-21 15:43:46 -06:00
expect(txt).not.toContain("days old");
2019-06-14 16:59:11 -06:00
expect(wrapper.find("button").length).toEqual(3);
2018-05-02 02:00:30 -06:00
});
it("enters select mode", () => {
2018-09-13 16:00:14 -06:00
const p = fakeProps();
const wrapper = mount(<PlantPanel {...p} />);
2019-06-14 16:59:11 -06:00
clickButton(wrapper, 3, "Delete multiple");
2018-06-15 18:28:42 -06:00
expect(history.push).toHaveBeenCalledWith("/app/designer/plants/select");
2018-05-02 02:00:30 -06:00
});
2018-05-21 22:43:32 -06:00
2018-12-05 16:53:56 -07:00
it("navigates to 'move to' mode", async () => {
2018-09-13 16:00:14 -06:00
const p = fakeProps();
2018-12-05 16:53:56 -07:00
const innerDispatch = jest.fn();
p.dispatch = jest.fn(x => x(innerDispatch));
2018-09-13 16:00:14 -06:00
const wrapper = mount(<PlantPanel {...p} />);
2018-12-05 16:53:56 -07:00
await clickButton(wrapper, 0, "Move FarmBot to this plant");
2019-03-05 11:59:22 -07:00
expect(history.push).toHaveBeenCalledWith("/app/designer/move_to");
2018-12-05 16:53:56 -07:00
expect(innerDispatch).toHaveBeenLastCalledWith({
type: Actions.CHOOSE_LOCATION,
payload: { x: 12, y: 34, z: undefined }
2018-05-21 22:43:32 -06:00
});
});
2018-05-02 02:00:30 -06:00
});
describe("<EditPlantStatus />", () => {
2019-06-14 16:59:11 -06:00
const fakeProps = (): EditPlantStatusProps => ({
uuid: "Plant.0.0",
plantStatus: "planned",
updatePlant: jest.fn(),
});
2018-05-02 02:00:30 -06:00
2018-03-06 21:54:26 -07:00
it("changes stage to planted", () => {
const p = fakeProps();
2018-05-02 02:00:30 -06:00
const wrapper = shallow(<EditPlantStatus {...p} />);
2018-03-06 21:54:26 -07:00
wrapper.find("FBSelect").simulate("change", { value: "planted" });
expect(p.updatePlant).toHaveBeenCalledWith("Plant.0.0", {
plant_stage: "planted",
planted_at: expect.stringContaining("Z")
});
});
it("changes stage to planned", () => {
const p = fakeProps();
2018-05-02 02:00:30 -06:00
const wrapper = shallow(<EditPlantStatus {...p} />);
2018-03-06 21:54:26 -07:00
wrapper.find("FBSelect").simulate("change", { value: "planned" });
expect(p.updatePlant).toHaveBeenCalledWith("Plant.0.0", {
plant_stage: "planned",
planted_at: undefined
});
2017-07-26 17:25:08 -06:00
});
});
2019-01-13 18:41:23 -07:00
2019-12-30 13:13:23 -07:00
describe("<PlantStatusBulkUpdate />", () => {
const fakeProps = (): PlantStatusBulkUpdateProps => ({
plants: [],
selected: [],
dispatch: jest.fn(),
});
it("doesn't update plant statuses", () => {
const p = fakeProps();
const plant1 = fakePlant();
const plant2 = fakePlant();
p.plants = [plant1, plant2];
p.selected = [plant1.uuid];
const wrapper = shallow(<PlantStatusBulkUpdate {...p} />);
window.confirm = jest.fn(() => false);
wrapper.find("FBSelect").simulate("change", { label: "", value: "planted" });
expect(window.confirm).toHaveBeenCalled();
expect(edit).not.toHaveBeenCalled();
});
it("updates plant statuses", () => {
const p = fakeProps();
const plant1 = fakePlant();
const plant2 = fakePlant();
const plant3 = fakePlant();
p.plants = [plant1, plant2, plant3];
p.selected = [plant1.uuid, plant2.uuid];
const wrapper = shallow(<PlantStatusBulkUpdate {...p} />);
window.confirm = jest.fn(() => true);
wrapper.find("FBSelect").simulate("change", { label: "", value: "planted" });
expect(window.confirm).toHaveBeenCalledWith(
"Change the plant status to 'planted' for 2 plants?");
expect(edit).toHaveBeenCalledTimes(2);
});
});
2019-01-13 18:41:23 -07:00
describe("<EditDatePlanted />", () => {
const fakeProps = (): EditDatePlantedProps => ({
uuid: "Plant.0.0",
datePlanted: moment("2017-06-19T08:02:22.466-05:00"),
updatePlant: jest.fn(),
2019-04-09 23:17:03 -06:00
timeSettings: fakeTimeSettings(),
2019-01-13 18:41:23 -07:00
});
it("changes date planted", () => {
const p = fakeProps();
const wrapper = shallow(<EditDatePlanted {...p} />);
wrapper.find("BlurableInput").simulate("commit", {
currentTarget: { value: "2010-10-10" }
});
expect(p.updatePlant).toHaveBeenCalledWith("Plant.0.0", {
planted_at: expect.stringContaining("Z")
});
});
});
describe("<EditPlantLocation />", () => {
const fakeProps = (): EditPlantLocationProps => ({
uuid: "Plant.0.0",
2020-02-28 09:34:28 -07:00
xyLocation: { x: 1, y: 2 },
2019-01-13 18:41:23 -07:00
updatePlant: jest.fn(),
});
it("changes location", () => {
const p = fakeProps();
const wrapper = shallow(<EditPlantLocation {...p} />);
wrapper.find("BlurableInput").first().simulate("commit", {
currentTarget: { value: "100" }
});
expect(p.updatePlant).toHaveBeenCalledWith("Plant.0.0", {
x: 100
});
});
});