Farmbot-Web-App/frontend/farm_designer/saved_gardens/__tests__/saved_gardens_test.tsx

131 lines
4.3 KiB
TypeScript
Raw Normal View History

2018-09-13 16:00:14 -06:00
jest.mock("../actions", () => ({
2018-08-01 18:13:44 -06:00
snapshotGarden: jest.fn(),
applyGarden: jest.fn(),
2018-09-13 16:00:14 -06:00
destroySavedGarden: jest.fn(),
openOrCloseGarden: jest.fn(),
closeSavedGarden: jest.fn(),
2018-08-01 18:13:44 -06:00
}));
2019-03-05 11:59:22 -07:00
jest.mock("../../../history", () => ({
history: { push: jest.fn() },
getPathArray: () => [],
}));
2018-08-01 18:13:44 -06:00
2018-11-01 11:56:41 -06:00
jest.mock("../../../api/crud", () => ({ edit: jest.fn() }));
2018-08-01 18:13:44 -06:00
import * as React from "react";
import { mount, shallow } from "enzyme";
import {
2020-04-13 19:15:11 -06:00
RawSavedGardens as SavedGardens, mapStateToProps, SavedGardenHUD,
2018-08-01 18:13:44 -06:00
} from "../saved_gardens";
import { clickButton } from "../../../__test_support__/helpers";
import {
2020-02-28 09:35:32 -07:00
fakePlantTemplate, fakeSavedGarden,
2018-08-01 18:13:44 -06:00
} from "../../../__test_support__/fake_state/resources";
import { history } from "../../../history";
import { fakeState } from "../../../__test_support__/fake_state";
2018-09-13 16:00:14 -06:00
import {
2020-02-28 09:35:32 -07:00
buildResourceIndex,
2018-09-13 16:00:14 -06:00
} from "../../../__test_support__/resource_index_builder";
import { SavedGardensProps } from "../interfaces";
2019-11-06 10:01:05 -07:00
import { closeSavedGarden } from "../actions";
2018-09-21 16:22:17 -06:00
import { Actions } from "../../../constants";
2020-04-13 19:15:11 -06:00
import { SearchField } from "../../../ui/search_field";
2018-08-01 18:13:44 -06:00
describe("<SavedGardens />", () => {
const fakeProps = (): SavedGardensProps => ({
dispatch: jest.fn(),
2018-11-14 17:36:52 -07:00
plantPointerCount: 1,
2018-08-01 18:13:44 -06:00
savedGardens: [fakeSavedGarden()],
plantTemplates: [fakePlantTemplate(), fakePlantTemplate()],
2018-09-13 16:00:14 -06:00
openedSavedGarden: undefined,
2018-08-01 18:13:44 -06:00
});
it("renders saved gardens", () => {
2019-11-06 10:01:05 -07:00
const p = fakeProps();
p.plantTemplates[0].body.saved_garden_id = p.savedGardens[0].body.id || 0;
p.plantTemplates[1].body.saved_garden_id = p.savedGardens[0].body.id || 0;
const wrapper = mount(<SavedGardens {...p} />);
["saved garden 1", "2 plants"].map(string =>
2018-11-01 11:56:41 -06:00
expect(wrapper.html().toLowerCase()).toContain(string));
2018-08-01 18:13:44 -06:00
});
2019-11-06 10:01:05 -07:00
it("has no saved gardens yet", () => {
2018-08-01 18:13:44 -06:00
const p = fakeProps();
2019-11-06 10:01:05 -07:00
p.savedGardens = [];
2018-08-01 18:13:44 -06:00
const wrapper = mount(<SavedGardens {...p} />);
2019-11-06 10:01:05 -07:00
expect(wrapper.text().toLowerCase()).toContain("no saved gardens yet");
2018-08-01 18:13:44 -06:00
});
2019-11-06 10:01:05 -07:00
it("changes search term", () => {
const wrapper = shallow<SavedGardens>(<SavedGardens {...fakeProps()} />);
expect(wrapper.state().searchTerm).toEqual("");
2020-04-13 19:15:11 -06:00
wrapper.find(SearchField).simulate("change", "spring");
2019-11-06 10:01:05 -07:00
expect(wrapper.state().searchTerm).toEqual("spring");
2018-08-01 18:13:44 -06:00
});
2019-11-06 10:01:05 -07:00
it("shows filtered gardens", () => {
2018-08-01 18:13:44 -06:00
const p = fakeProps();
2019-11-06 10:01:05 -07:00
p.savedGardens = [fakeSavedGarden(), fakeSavedGarden()];
p.savedGardens[0].body.name = "winter";
p.savedGardens[1].body.name = "spring";
2018-08-01 18:13:44 -06:00
const wrapper = mount(<SavedGardens {...p} />);
2019-11-06 10:01:05 -07:00
wrapper.setState({ searchTerm: "winter" });
expect(wrapper.text()).toContain("winter");
expect(wrapper.text()).not.toContain("spring");
2018-08-01 18:13:44 -06:00
});
2019-11-06 10:01:05 -07:00
it("shows when garden is open", () => {
2018-08-01 18:13:44 -06:00
const p = fakeProps();
2019-11-06 10:01:05 -07:00
p.savedGardens = [fakeSavedGarden(), fakeSavedGarden()];
p.openedSavedGarden = p.savedGardens[0].uuid;
2018-08-01 18:13:44 -06:00
const wrapper = mount(<SavedGardens {...p} />);
2019-11-06 10:01:05 -07:00
expect(wrapper.html()).toContain("selected");
2019-03-05 11:59:22 -07:00
});
2018-08-01 18:13:44 -06:00
});
describe("mapStateToProps()", () => {
it("has no plants in garden", () => {
const state = fakeState();
state.resources = buildResourceIndex([]);
const result = mapStateToProps(state);
2018-11-14 17:36:52 -07:00
expect(result.plantPointerCount).toEqual(0);
2018-08-01 18:13:44 -06:00
});
it("has plants in garden", () => {
const result = mapStateToProps(fakeState());
2018-11-14 17:36:52 -07:00
expect(result.plantPointerCount).toBeGreaterThan(0);
2018-08-01 18:13:44 -06:00
});
});
2018-08-27 13:29:01 -06:00
2018-09-13 16:00:14 -06:00
describe("<SavedGardenHUD />", () => {
it("renders", () => {
const wrapper = mount(<SavedGardenHUD dispatch={jest.fn()} />);
["viewing saved garden", "menu", "edit", "exit"].map(string =>
expect(wrapper.text().toLowerCase()).toContain(string));
});
it("opens menu", () => {
const wrapper = mount(<SavedGardenHUD dispatch={jest.fn()} />);
clickButton(wrapper, 0, "menu");
2019-11-06 10:01:05 -07:00
expect(history.push).toHaveBeenCalledWith("/app/designer/gardens");
2018-09-13 16:00:14 -06:00
});
it("navigates to plants", () => {
2018-09-21 16:22:17 -06:00
const dispatch = jest.fn();
const wrapper = mount(<SavedGardenHUD dispatch={dispatch} />);
2018-09-13 16:00:14 -06:00
clickButton(wrapper, 1, "edit");
expect(history.push).toHaveBeenCalledWith("/app/designer/plants");
2018-09-21 16:22:17 -06:00
expect(dispatch).toHaveBeenCalledWith({
type: Actions.SELECT_POINT,
2018-09-21 16:22:17 -06:00
payload: undefined
});
2018-09-13 16:00:14 -06:00
});
it("exits garden", () => {
const wrapper = mount(<SavedGardenHUD dispatch={jest.fn()} />);
clickButton(wrapper, 2, "exit");
expect(closeSavedGarden).toHaveBeenCalled();
});
});