add firmware selection UI

pull/482/head
gabrielburnworth 2017-10-03 23:53:26 -07:00
parent 6b1ae7db0b
commit c25e62e538
3 changed files with 86 additions and 9 deletions

View File

@ -55,7 +55,7 @@
"deep-freeze": "^0.0.1",
"enzyme": "^2.9.1",
"extract-text-webpack-plugin": "^3.0.0",
"farmbot": "5.0.0",
"farmbot": "5.0.1-rc3",
"farmbot-toastr": "^1.0.3",
"fastclick": "^1.0.6",
"file-loader": "^0.11.2",

View File

@ -1,6 +1,21 @@
jest.mock("../../../device", () => ({
devices: {
current: {
updateConfig: jest.fn(() => { return Promise.resolve(); })
}
}
}));
const mockToast = jest.fn();
jest.mock("farmbot-toastr", () => ({
success: mockToast,
info: mockToast,
error: mockToast
}));
import * as React from "react";
import { mount } from "enzyme";
import { mount, shallow } from "enzyme";
import { BoardType } from "../board_type";
import { devices } from "../../../device";
describe("<BoardType/>", () => {
it("Farmduino", () => {
@ -18,18 +33,27 @@ describe("<BoardType/>", () => {
it("Other", () => {
const wrapper = mount(<BoardType
firmwareVersion={"4.0.2"} />);
expect(wrapper.text()).toContain("Present");
expect(wrapper.text()).toContain("Arduino/RAMPS");
});
it("Undefined", () => {
const wrapper = mount(<BoardType
firmwareVersion={undefined} />);
expect(wrapper.text()).toContain("unknown");
expect(wrapper.text()).toContain("None");
});
it("Disconnected", () => {
const wrapper = mount(<BoardType
firmwareVersion={"Arduino Disconnected!"} />);
expect(wrapper.text()).toContain("unknown");
expect(wrapper.text()).toContain("None");
});
it("calls updateConfig", () => {
const updateConfig = devices.current.updateConfig as jest.Mock<{}>;
const wrapper = shallow(<BoardType
firmwareVersion={"Arduino Disconnected!"} />);
wrapper.find("FBSelect").simulate("change",
{ label: "firmware_hardware", value: "farmduino" });
expect(updateConfig).toBeCalledWith({ firmware_hardware: "farmduino" });
});
});

View File

@ -1,11 +1,30 @@
import * as React from "react";
import { Row, Col } from "../../ui/index";
import { Row, Col, DropDownItem } from "../../ui/index";
import { t } from "i18next";
import { FBSelect } from "../../ui/new_fb_select";
import { devices } from "../../device";
import { info, success, error } from "farmbot-toastr";
export interface BoardTypeProps {
firmwareVersion: string | undefined;
}
const FIRMWARE_CHOICES = [
{ label: "Arduino/RAMPS (Genesis v1.2)", value: "arduino" },
{ label: "Farmduino (Genesis v1.3)", value: "farmduino" }
];
const FIRMWARE_CHOICES_DDI = {
[FIRMWARE_CHOICES[0].value]: {
label: FIRMWARE_CHOICES[0].label,
value: FIRMWARE_CHOICES[0].value
},
[FIRMWARE_CHOICES[1].value]: {
label: FIRMWARE_CHOICES[1].label,
value: FIRMWARE_CHOICES[1].value
}
};
export class BoardType
extends React.Component<BoardTypeProps, {}> {
@ -27,6 +46,35 @@ export class BoardType
}
}
selectedBoard(): DropDownItem | undefined {
const board = this.getBoardType();
switch (board) {
case "Arduino/RAMPS":
case "Present":
return FIRMWARE_CHOICES_DDI["arduino"];
case "Farmduino":
return FIRMWARE_CHOICES_DDI["farmduino"];
default:
return undefined;
}
}
sendOffConfig = (selectedBoard: DropDownItem) => {
if (selectedBoard) {
const firmware = selectedBoard.value;
info(t("Sending firmware configuration..."), t("Sending"));
devices
.current
// TODO: remove type assertion when farmbot-js is updated
// tslint:disable-next-line:no-any
.updateConfig({ firmware_hardware: firmware as any })
.then(() => {
success(t("Successfully configured firmware!"));
})
.catch(() => error(t("An error occurred during configuration.")));
}
}
render() {
return <Row>
<Col xs={2}>
@ -35,9 +83,14 @@ export class BoardType
</label>
</Col>
<Col xs={7}>
<p>
{this.getBoardType()}
</p>
<div>
<FBSelect
allowEmpty={true}
list={FIRMWARE_CHOICES}
selectedItem={this.selectedBoard()}
placeholder={this.getBoardType()}
onChange={this.sendOffConfig} />
</div>
</Col>
</Row>;
}