Farmbot-Web-App/frontend/devices/components/fbos_settings/__tests__/fbos_details_test.tsx

275 lines
9.7 KiB
TypeScript
Raw Normal View History

2019-11-01 15:25:39 -06:00
jest.mock("../../../actions", () => ({ updateConfig: jest.fn() }));
2018-07-10 20:37:50 -06:00
import * as React from "react";
2019-04-24 17:27:04 -06:00
import {
2019-11-01 15:25:39 -06:00
FbosDetails, colorFromTemp, colorFromThrottle, ThrottleType,
BetaReleaseOptInButtonProps, BetaReleaseOptIn,
2019-04-24 17:27:04 -06:00
} from "../fbos_details";
2018-07-10 20:37:50 -06:00
import { shallow, mount } from "enzyme";
import { bot } from "../../../../__test_support__/fake_state/bot";
import { FbosDetailsProps } from "../interfaces";
2019-02-26 20:12:02 -07:00
import { fakeFbosConfig } from "../../../../__test_support__/fake_state/resources";
2018-07-10 20:37:50 -06:00
import { fakeState } from "../../../../__test_support__/fake_state";
2019-02-26 20:12:02 -07:00
import {
2020-02-28 09:35:32 -07:00
buildResourceIndex, fakeDevice,
2019-02-26 20:12:02 -07:00
} from "../../../../__test_support__/resource_index_builder";
2019-06-07 18:26:12 -06:00
import { fakeTimeSettings } from "../../../../__test_support__/fake_time_settings";
2019-11-01 15:25:39 -06:00
import { updateConfig } from "../../../actions";
2018-07-10 20:37:50 -06:00
describe("<FbosDetails/>", () => {
2019-02-26 20:12:02 -07:00
const fakeConfig = fakeFbosConfig();
const state = fakeState();
state.resources = buildResourceIndex([fakeConfig]);
const fakeProps = (): FbosDetailsProps => ({
botInfoSettings: bot.hardware.informational_settings,
dispatch: jest.fn(x => x(jest.fn(), () => state)),
sourceFbosConfig: () => ({ value: true, consistent: true }),
shouldDisplay: () => false,
botToMqttLastSeen: 0,
2019-06-07 18:26:12 -06:00
deviceAccount: fakeDevice(),
timeSettings: fakeTimeSettings(),
2019-02-26 20:12:02 -07:00
});
2018-07-10 20:37:50 -06:00
it("renders", () => {
const p = fakeProps();
p.botInfoSettings.env = "fakeEnv";
p.botInfoSettings.commit = "fakeCommit";
p.botInfoSettings.target = "fakeTarget";
p.botInfoSettings.node_name = "fakeName";
2020-02-07 16:06:40 -07:00
p.botInfoSettings.firmware_version = "0.0.0.R.ramps";
2018-07-10 20:37:50 -06:00
p.botInfoSettings.firmware_commit = "fakeFwCommit";
p.botInfoSettings.soc_temp = 48.3;
p.botInfoSettings.wifi_level = -49;
2018-09-12 19:01:59 -06:00
p.botInfoSettings.uptime = 0;
p.botInfoSettings.memory_usage = 0;
p.botInfoSettings.disk_usage = 0;
2018-07-10 20:37:50 -06:00
const wrapper = mount(<FbosDetails {...p} />);
["Environment", "fakeEnv",
"Commit", "fakeComm",
"Target", "fakeTarget",
"Node name", "fakeName",
2020-02-07 16:06:40 -07:00
"Firmware", "0.0.0 Arduino/RAMPS (Genesis v1.2)",
2018-07-10 20:37:50 -06:00
"Firmware commit", "fakeFwCo",
2020-02-07 16:06:40 -07:00
"Firmware code", "0.0.0.R.ramps",
2018-07-10 20:37:50 -06:00
"FAKETARGET CPU temperature", "48.3", "C",
"WiFi strength", "-49dBm",
2019-11-01 15:25:39 -06:00
"OS release channel",
2018-09-12 19:01:59 -06:00
"Uptime", "0 seconds",
2018-09-10 16:27:41 -06:00
"Memory usage", "0MB",
"Disk usage", "0%",
2018-07-10 20:37:50 -06:00
]
.map(string => expect(wrapper.text()).toContain(string));
});
it("simplifies node name", () => {
const p = fakeProps();
p.botInfoSettings.node_name = "name@nodeName";
const wrapper = shallow(<FbosDetails {...p} />);
expect(wrapper.text()).toContain("nodeName");
expect(wrapper.text()).not.toContain("name@");
});
2020-03-13 15:06:40 -06:00
it("handles missing data", () => {
2020-02-07 16:06:40 -07:00
const p = fakeProps();
p.botInfoSettings.firmware_version = undefined;
2020-03-13 15:06:40 -06:00
p.botInfoSettings.node_name = "";
p.botInfoSettings.commit = "";
2020-02-07 16:06:40 -07:00
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text()).toContain("---");
});
it("handles unknown firmware version", () => {
const p = fakeProps();
p.botInfoSettings.firmware_version = "0.0.0.S.S";
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text()).toContain("0.0.0");
});
2020-02-13 17:53:20 -07:00
it("displays firmware commit link from firmware_commit", () => {
const p = fakeProps();
const commit = "abcdefgh";
p.botInfoSettings.firmware_commit = commit;
2020-02-20 19:38:50 -07:00
p.botInfoSettings.firmware_version = "1.0.0";
2020-02-13 17:53:20 -07:00
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.find("a").last().text()).toEqual(commit);
expect(wrapper.find("a").last().props().href?.split("/").slice(-1)[0])
.toEqual(commit);
});
it("displays firmware commit link from version", () => {
const p = fakeProps();
const commit = "abcdefgh";
p.botInfoSettings.firmware_version = `1.2.3.R.x-${commit}+`;
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.find("a").last().text()).toEqual(commit);
expect(wrapper.find("a").last().props().href?.split("/").slice(-1)[0])
.toEqual(commit);
});
2018-09-12 19:01:59 -06:00
it("displays commit link", () => {
const p = fakeProps();
p.botInfoSettings.commit = "abcdefgh";
p.botInfoSettings.firmware_commit = "abcdefgh";
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.find("a").length).toEqual(2);
});
it("doesn't display link without commit", () => {
const p = fakeProps();
2020-02-20 19:38:50 -07:00
p.botInfoSettings.firmware_version = undefined;
2018-09-12 19:01:59 -06:00
p.botInfoSettings.commit = "---";
p.botInfoSettings.firmware_commit = "---";
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.find("a").length).toEqual(0);
});
2018-07-20 14:23:31 -06:00
it("displays N/A when wifi strength value is undefined", () => {
2018-07-10 20:37:50 -06:00
const p = fakeProps();
p.botInfoSettings.wifi_level = undefined;
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text()).toContain("WiFi strength: N/A");
2018-07-10 20:37:50 -06:00
expect(wrapper.text()).not.toContain("dBm");
});
2018-07-20 14:23:31 -06:00
it("displays unknown when cpu temp value is undefined", () => {
const p = fakeProps();
p.botInfoSettings.soc_temp = undefined;
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text()).toContain("CPU temperature: unknown");
expect(wrapper.text()).not.toContain("&deg;C");
});
2018-09-10 14:51:40 -06:00
it("doesn't display extra metrics when bot is offline", () => {
const p = fakeProps();
p.botInfoSettings.uptime = undefined;
p.botInfoSettings.memory_usage = undefined;
p.botInfoSettings.disk_usage = undefined;
2018-09-10 14:51:40 -06:00
const wrapper = mount(<FbosDetails {...p} />);
["uptime", "usage"].map(metric =>
expect(wrapper.text().toLowerCase()).not.toContain(metric));
});
2018-09-12 19:01:59 -06:00
it("displays uptime in minutes", () => {
const p = fakeProps();
p.botInfoSettings.uptime = 120;
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text()).toContain("2 minutes");
});
it("displays uptime in hours", () => {
const p = fakeProps();
p.botInfoSettings.uptime = 7200;
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text()).toContain("2 hours");
});
it("displays uptime in days", () => {
const p = fakeProps();
p.botInfoSettings.uptime = 172800;
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text()).toContain("2 days");
});
2019-04-24 17:27:04 -06:00
it("doesn't display when throttled value is undefined", () => {
const p = fakeProps();
p.botInfoSettings.throttled = undefined;
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text().toLowerCase()).not.toContain("voltage");
});
it("displays voltage indicator", () => {
const p = fakeProps();
p.botInfoSettings.throttled = "0x0";
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text().toLowerCase()).toContain("voltage");
});
2019-09-24 15:50:32 -06:00
it("displays cpu usage", () => {
const p = fakeProps();
2019-10-08 13:23:39 -06:00
p.botInfoSettings.cpu_usage = 10;
2019-09-24 15:50:32 -06:00
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text().toLowerCase()).toContain("cpu usage: 10%");
});
it("displays ip address", () => {
const p = fakeProps();
p.botInfoSettings.private_ip = "192.168.0.100";
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text().toLowerCase()).toContain("ip address");
});
it("displays last OTA check date", () => {
const p = fakeProps();
p.deviceAccount.body.last_ota_checkup = "2018-01-11T20:20:38.362Z";
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text().toLowerCase())
.toContain("last checked for updates: january");
});
it("displays last OTA date", () => {
const p = fakeProps();
p.deviceAccount.body.last_ota = "2018-02-11T20:20:38.362Z";
const wrapper = mount(<FbosDetails {...p} />);
expect(wrapper.text().toLowerCase()).toContain("last updated: february");
});
2018-07-10 20:37:50 -06:00
});
2019-11-01 15:25:39 -06:00
describe("<BetaReleaseOptIn />", () => {
const fakeProps = (): BetaReleaseOptInButtonProps => ({
dispatch: jest.fn(),
sourceFbosConfig: () => ({ value: true, consistent: true }),
2018-11-26 20:39:35 -07:00
});
2019-11-01 15:25:39 -06:00
it("changes to beta channel", () => {
const p = fakeProps();
p.sourceFbosConfig = () => ({ value: "stable", consistent: true });
const wrapper = shallow(<BetaReleaseOptIn {...p} />);
window.confirm = jest.fn();
wrapper.find("FBSelect").simulate("change", { label: "", value: "" });
expect(window.confirm).toHaveBeenCalledWith(
expect.stringContaining("you sure?"));
expect(updateConfig).not.toHaveBeenCalled();
window.confirm = () => true;
wrapper.find("FBSelect").simulate("change", { label: "", value: "beta" });
expect(updateConfig).toHaveBeenCalledWith({ update_channel: "beta" });
2018-11-26 20:39:35 -07:00
});
2019-11-01 15:25:39 -06:00
it("changes to stable channel", () => {
const p = fakeProps();
p.sourceFbosConfig = () => ({ value: "beta", consistent: true });
const wrapper = shallow(<BetaReleaseOptIn {...p} />);
window.confirm = () => false;
wrapper.find("FBSelect").simulate("change", { label: "", value: "stable" });
expect(updateConfig).toHaveBeenCalledWith({ update_channel: "stable" });
2018-11-26 20:39:35 -07:00
});
});
2018-07-10 20:37:50 -06:00
describe("colorFromTemp()", () => {
it("temperature is good or none", () => {
expect(colorFromTemp(30)).toEqual("green");
expect(colorFromTemp(undefined)).toEqual("gray");
});
it("temperature is hot", () => {
expect(colorFromTemp(61)).toEqual("yellow");
expect(colorFromTemp(76)).toEqual("red");
});
it("temperature is cold", () => {
expect(colorFromTemp(9)).toEqual("blue");
expect(colorFromTemp(-1)).toEqual("lightblue");
});
});
2019-04-24 17:27:04 -06:00
describe("colorFromThrottle()", () => {
it("is currently throttled", () => {
expect(colorFromThrottle("0x40004", ThrottleType.Throttled)).toEqual("red");
});
it("was throttled", () => {
expect(colorFromThrottle("0x40000", ThrottleType.Throttled)).toEqual("yellow");
});
it("hasn't been throttled", () => {
expect(colorFromThrottle("0x0", ThrottleType.Throttled)).toEqual("green");
});
});