Farmbot-Web-App/frontend/controls/sensors/__tests__/index_test.tsx

76 lines
2.5 KiB
TypeScript
Raw Normal View History

2018-03-10 00:17:53 -07:00
import * as React from "react";
import { mount } from "enzyme";
import { Sensors } from "../index";
import { bot } from "../../../__test_support__/fake_state/bot";
import { SensorsProps } from "../../../devices/interfaces";
import { fakeSensor } from "../../../__test_support__/fake_state/resources";
2018-06-15 17:59:32 -06:00
import { clickButton } from "../../../__test_support__/helpers";
import { SpecialStatus } from "farmbot";
2019-06-24 15:39:49 -06:00
import { error } from "../../../toast/toast";
2018-03-10 00:17:53 -07:00
describe("<Sensors />", () => {
function fakeProps(): SensorsProps {
const fakeSensor1 = fakeSensor();
const fakeSensor2 = fakeSensor();
fakeSensor1.body.pin = 1;
fakeSensor2.body.pin = 2;
return {
bot,
sensors: [fakeSensor1, fakeSensor2],
dispatch: jest.fn(),
disabled: false
};
}
it("renders", () => {
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Sensors {...fakeProps()} />);
2018-03-10 00:17:53 -07:00
["Sensors", "Edit", "Save", "Fake Pin", "1"].map(string =>
expect(wrapper.text()).toContain(string));
const saveButton = wrapper.find("button").at(1);
expect(saveButton.text()).toContain("Save");
expect(saveButton.props().hidden).toBeTruthy();
});
it("isEditing", () => {
2018-07-02 15:43:51 -06:00
const wrapper = mount<Sensors>(<Sensors {...fakeProps()} />);
expect(wrapper.instance().state.isEditing).toBeFalsy();
2018-06-15 17:59:32 -06:00
clickButton(wrapper, 0, "edit");
expect(wrapper.instance().state.isEditing).toBeTruthy();
2018-03-10 00:17:53 -07:00
});
it("save attempt: pin number too small", () => {
const p = fakeProps();
p.sensors[0].body.pin = 1;
p.sensors[1].body.pin = 1;
2018-06-15 17:59:32 -06:00
p.sensors[0].specialStatus = SpecialStatus.DIRTY;
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Sensors {...p} />);
2018-06-15 17:59:32 -06:00
clickButton(wrapper, 1, "save", { partial_match: true });
expect(error).toHaveBeenLastCalledWith("Pin numbers must be unique.");
2018-03-10 00:17:53 -07:00
});
it("saves", () => {
const p = fakeProps();
p.sensors[0].body.pin = 1;
2018-06-15 17:59:32 -06:00
p.sensors[0].specialStatus = SpecialStatus.DIRTY;
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Sensors {...p} />);
2018-06-15 17:59:32 -06:00
clickButton(wrapper, 1, "save", { partial_match: true });
2018-03-10 00:17:53 -07:00
expect(p.dispatch).toHaveBeenCalled();
});
it("adds empty sensor", () => {
const p = fakeProps();
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Sensors {...p} />);
2018-03-10 00:17:53 -07:00
wrapper.setState({ isEditing: true });
2018-06-15 17:59:32 -06:00
clickButton(wrapper, 2, "");
2018-03-10 00:17:53 -07:00
expect(p.dispatch).toHaveBeenCalled();
});
it("adds stock sensors", () => {
const p = fakeProps();
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Sensors {...p} />);
2018-03-10 00:17:53 -07:00
wrapper.setState({ isEditing: true });
2018-06-15 17:59:32 -06:00
clickButton(wrapper, 3, "stock sensors");
2018-03-10 00:17:53 -07:00
expect(p.dispatch).toHaveBeenCalledTimes(2);
});
});