Farmbot-Web-App/webpack/controls/peripherals/__tests__/index_test.tsx

87 lines
2.8 KiB
TypeScript
Raw Normal View History

2017-10-16 12:58:54 -06:00
jest.mock("farmbot-toastr", () => ({
2018-06-15 17:59:32 -06:00
error: jest.fn()
2017-10-16 12:58:54 -06:00
}));
import * as React from "react";
import { mount } from "enzyme";
import { Peripherals } from "../index";
import { bot } from "../../../__test_support__/fake_state/bot";
import { PeripheralsProps } from "../../../devices/interfaces";
import { fakePeripheral } from "../../../__test_support__/fake_state/resources";
2018-06-15 17:59:32 -06:00
import { clickButton } from "../../../__test_support__/helpers";
import { SpecialStatus } from "../../../resources/tagged_resources";
import { error } from "farmbot-toastr";
2017-10-16 12:58:54 -06:00
describe("<Peripherals />", () => {
beforeEach(function () {
jest.clearAllMocks();
});
function fakeProps(): PeripheralsProps {
return {
bot,
peripherals: [fakePeripheral()],
dispatch: jest.fn(),
disabled: false
};
}
it("renders", () => {
2018-06-15 17:59:32 -06:00
const wrapper = mount(<Peripherals {...fakeProps()} />);
2017-11-08 13:08:53 -07:00
["Peripherals", "Edit", "Save", "Fake Pin", "1"].map(string =>
2017-10-16 22:22:35 -06:00
expect(wrapper.text()).toContain(string));
2017-10-16 12:58:54 -06:00
const saveButton = wrapper.find("button").at(1);
expect(saveButton.text()).toContain("Save");
expect(saveButton.props().hidden).toBeTruthy();
});
it("isEditing", () => {
2018-06-15 17:59:32 -06:00
const wrapper = mount(<Peripherals {...fakeProps()} />);
2017-10-16 12:58:54 -06:00
expect(wrapper.state().isEditing).toBeFalsy();
2018-06-15 17:59:32 -06:00
clickButton(wrapper, 0, "edit");
2017-10-16 12:58:54 -06:00
expect(wrapper.state().isEditing).toBeTruthy();
});
2018-06-15 17:59:32 -06:00
function attemptSave(num: number, errorString: string) {
2017-10-16 12:58:54 -06:00
const p = fakeProps();
p.peripherals[0].body.pin = num;
2018-06-15 17:59:32 -06:00
p.peripherals[0].specialStatus = SpecialStatus.DIRTY;
const wrapper = mount(<Peripherals {...p} />);
clickButton(wrapper, 1, "save", { partial_match: true });
expect(error).toHaveBeenLastCalledWith(errorString);
2017-10-16 12:58:54 -06:00
}
it("save attempt: pin number too small", () => {
attemptSave(0, "Pin numbers are required and must be positive and unique.");
});
it("save attempt: pin number too large", () => {
attemptSave(9999, "Pin numbers must be less than 1000.");
});
it("saves", () => {
const p = fakeProps();
p.peripherals[0].body.pin = 1;
2018-06-15 17:59:32 -06:00
p.peripherals[0].specialStatus = SpecialStatus.DIRTY;
const wrapper = mount(<Peripherals {...p} />);
clickButton(wrapper, 1, "save", { partial_match: true });
2017-10-16 12:58:54 -06:00
expect(p.dispatch).toHaveBeenCalled();
});
2017-11-27 16:20:34 -07:00
it("adds empty peripheral", () => {
const p = fakeProps();
2018-06-15 17:59:32 -06:00
const wrapper = mount(<Peripherals {...p} />);
2017-11-27 16:20:34 -07:00
wrapper.setState({ isEditing: true });
2018-06-15 17:59:32 -06:00
clickButton(wrapper, 2, "");
2017-11-27 16:20:34 -07:00
expect(p.dispatch).toHaveBeenCalled();
});
it("adds farmduino peripherals", () => {
const p = fakeProps();
2018-06-15 17:59:32 -06:00
const wrapper = mount(<Peripherals {...p} />);
2017-11-27 16:20:34 -07:00
wrapper.setState({ isEditing: true });
2018-06-15 17:59:32 -06:00
clickButton(wrapper, 3, "farmduino");
2017-11-27 16:20:34 -07:00
expect(p.dispatch).toHaveBeenCalledTimes(5);
});
2017-10-16 12:58:54 -06:00
});