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

60 lines
2.0 KiB
TypeScript
Raw Normal View History

2018-09-13 16:00:14 -06:00
jest.mock("axios", () => {
return { default: { post: jest.fn(() => Promise.resolve()) } };
});
jest.mock("../actions", () => ({
snapshotGarden: jest.fn(),
2018-11-01 11:56:41 -06:00
newSavedGarden: jest.fn(),
2018-11-14 17:36:52 -07:00
copySavedGarden: jest.fn(),
2018-09-13 16:00:14 -06:00
}));
import * as React from "react";
import { mount, shallow } from "enzyme";
import { GardenSnapshotProps, GardenSnapshot } from "../garden_snapshot";
import { clickButton } from "../../../__test_support__/helpers";
2018-11-14 17:36:52 -07:00
import { snapshotGarden, newSavedGarden, copySavedGarden } from "../actions";
2018-11-01 11:56:41 -06:00
import { fakeSavedGarden } from "../../../__test_support__/fake_state/resources";
2018-09-13 16:00:14 -06:00
describe("<GardenSnapshot />", () => {
const fakeProps = (): GardenSnapshotProps => ({
2018-11-01 11:56:41 -06:00
currentSavedGarden: undefined,
plantTemplates: [],
dispatch: jest.fn(),
2018-09-13 16:00:14 -06:00
});
it("saves garden", () => {
const wrapper = mount(<GardenSnapshot {...fakeProps()} />);
2018-11-01 11:56:41 -06:00
clickButton(wrapper, 0, "snapshot current garden");
expect(snapshotGarden).toHaveBeenCalledWith("");
});
2018-11-14 17:36:52 -07:00
it("copies saved garden", () => {
2018-11-01 11:56:41 -06:00
const p = fakeProps();
p.currentSavedGarden = fakeSavedGarden();
const wrapper = mount(<GardenSnapshot {...p} />);
clickButton(wrapper, 0, "snapshot current garden");
expect(snapshotGarden).not.toHaveBeenCalled();
2018-11-14 17:36:52 -07:00
expect(copySavedGarden).toHaveBeenCalledWith({
newSGName: "",
plantTemplates: [],
savedGarden: p.currentSavedGarden
});
2018-09-13 16:00:14 -06:00
});
it("changes name", () => {
const wrapper = shallow<GardenSnapshot>(<GardenSnapshot {...fakeProps()} />);
wrapper.find("input").first().simulate("change", {
currentTarget: { value: "new name" }
});
2020-02-28 09:34:28 -07:00
expect(wrapper.instance().state.gardenName).toEqual("new name");
2018-09-13 16:00:14 -06:00
});
2018-11-01 11:56:41 -06:00
it("creates new garden", () => {
const wrapper = shallow<GardenSnapshot>(<GardenSnapshot {...fakeProps()} />);
2020-02-28 09:34:28 -07:00
wrapper.setState({ gardenName: "new saved garden" });
2018-11-01 11:56:41 -06:00
wrapper.find("button").last().simulate("click");
expect(newSavedGarden).toHaveBeenCalledWith("new saved garden");
2020-02-28 09:34:28 -07:00
expect(wrapper.instance().state.gardenName).toEqual("");
2018-11-01 11:56:41 -06:00
});
2018-09-13 16:00:14 -06:00
});