Farmbot-Web-App/frontend/__tests__/app_test.tsx

169 lines
5.0 KiB
TypeScript
Raw Normal View History

let mockPath = "";
jest.mock("../history", () => ({
2019-04-11 21:17:18 -06:00
getPathArray: jest.fn(() => mockPath.split("/")),
history: { getCurrentLocation: () => ({ pathname: mockPath }) }
}));
2017-09-08 12:38:46 -06:00
import * as React from "react";
2019-09-23 12:56:35 -06:00
import { RawApp as App, AppProps, mapStateToProps } from "../app";
2017-09-08 12:38:46 -06:00
import { mount } from "enzyme";
import { bot } from "../__test_support__/fake_state/bot";
2019-03-04 15:14:35 -07:00
import {
2020-02-28 09:35:32 -07:00
fakeUser, fakeWebAppConfig, fakeFbosConfig, fakeFarmwareEnv,
2019-03-04 15:14:35 -07:00
} from "../__test_support__/fake_state/resources";
import { fakeState } from "../__test_support__/fake_state";
2019-04-09 18:45:51 -06:00
import {
2020-02-28 09:35:32 -07:00
buildResourceIndex,
2019-04-09 18:45:51 -06:00
} from "../__test_support__/resource_index_builder";
2019-03-04 15:14:35 -07:00
import { ResourceName } from "farmbot";
2019-04-09 23:17:03 -06:00
import { fakeTimeSettings } from "../__test_support__/fake_time_settings";
2019-06-24 15:39:49 -06:00
import { error } from "../toast/toast";
import { fakePings } from "../__test_support__/fake_state/pings";
2019-03-04 15:14:35 -07:00
const FULLY_LOADED: ResourceName[] = [
2019-04-09 18:45:51 -06:00
"Sequence", "Regimen", "FarmEvent", "Point", "Tool", "Device"];
2017-09-08 12:38:46 -06:00
2019-09-23 12:56:35 -06:00
const fakeProps = (): AppProps => ({
timeSettings: fakeTimeSettings(),
dispatch: jest.fn(),
loaded: [],
logs: [],
user: fakeUser(),
bot: bot,
consistent: true,
axisInversion: { x: false, y: false, z: false },
firmwareConfig: undefined,
xySwap: false,
animate: false,
getConfigValue: jest.fn(),
tour: undefined,
resources: buildResourceIndex().index,
autoSync: false,
alertCount: 0,
2019-12-27 11:37:54 -07:00
pings: fakePings(),
env: {},
2019-09-23 12:56:35 -06:00
});
2017-09-08 12:38:46 -06:00
2018-04-12 17:55:38 -06:00
describe("<App />: Controls Pop-Up", () => {
2020-01-03 13:06:28 -07:00
it.each<["renders" | "doesn't render", string]>([
["renders", "designer"],
["renders", "designer/plants"],
["doesn't render", "controls"],
["renders", "device"],
["renders", "sequences"],
["renders", "sequences/for_regimens"],
["doesn't render", "regimens"],
["renders", "tools"],
["renders", "farmware"],
["renders", "messages"],
["renders", "logs"],
["renders", "help"],
["doesn't render", "account"],
])("%s controls pop-up on %s page", (expected, page) => {
mockPath = "/app/" + page;
const wrapper = mount(<App {...fakeProps()} />);
if (expected == "renders") {
expect(wrapper.html()).toContain("controls-popup");
} else {
expect(wrapper.html()).not.toContain("controls-popup");
}
});
2017-09-08 12:38:46 -06:00
});
2018-03-19 22:14:49 -06:00
describe("<App />: Loading", () => {
2017-09-08 12:38:46 -06:00
it("MUST_LOADs not loaded", () => {
2018-03-09 02:34:24 -07:00
const wrapper = mount(<App {...fakeProps()} />);
2018-03-19 22:14:49 -06:00
expect(wrapper.text()).toContain("Loading...");
2018-10-12 12:14:49 -06:00
wrapper.unmount();
2017-09-08 12:38:46 -06:00
});
it("MUST_LOADs partially loaded", () => {
const p = fakeProps();
2017-10-27 07:31:25 -06:00
p.loaded = ["Sequence"];
2018-03-09 02:34:24 -07:00
const wrapper = mount(<App {...p} />);
2018-03-19 22:14:49 -06:00
expect(wrapper.text()).toContain("Loading...");
2018-10-12 12:14:49 -06:00
wrapper.unmount();
2017-09-08 12:38:46 -06:00
});
it("MUST_LOADs loaded", () => {
const p = fakeProps();
2019-03-04 15:14:35 -07:00
p.loaded = FULLY_LOADED;
2018-03-09 02:34:24 -07:00
const wrapper = mount(<App {...p} />);
2018-03-19 22:14:49 -06:00
expect(wrapper.text()).not.toContain("Loading...");
2018-10-12 12:14:49 -06:00
wrapper.unmount();
2017-09-08 12:38:46 -06:00
});
2019-03-04 15:14:35 -07:00
it("times out while loading", () => {
jest.useFakeTimers();
const wrapper = mount(<App {...fakeProps()} />);
jest.runAllTimers();
expect(error).toHaveBeenCalledWith(
expect.stringContaining("App could not be fully loaded"), "Warning");
wrapper.unmount();
});
it("loads before timeout", () => {
const p = fakeProps();
p.loaded = FULLY_LOADED;
jest.useFakeTimers();
const wrapper = mount(<App {...p} />);
jest.runAllTimers();
expect(error).not.toHaveBeenCalled();
wrapper.unmount();
});
2017-09-08 12:38:46 -06:00
});
describe("<App />: NavBar", () => {
it("displays links", () => {
2019-04-09 18:45:51 -06:00
const p = fakeProps();
p.loaded = FULLY_LOADED;
const wrapper = mount(<App {...p} />);
const t = wrapper.text();
const strings = [
"Farm Designer",
"Controls",
"Device",
"Sequences",
"Regimens",
2020-02-28 09:35:32 -07:00
"Farmware",
];
strings.map(string => expect(t).toContain(string));
2018-10-12 12:14:49 -06:00
wrapper.unmount();
2017-09-08 12:38:46 -06:00
});
it("displays ticker", () => {
2019-04-09 18:45:51 -06:00
const p = fakeProps();
p.loaded = FULLY_LOADED;
const wrapper = mount(<App {...p} />);
2017-09-08 12:38:46 -06:00
expect(wrapper.text()).toContain("No logs yet.");
2018-10-12 12:14:49 -06:00
wrapper.unmount();
2017-09-08 12:38:46 -06:00
});
});
2019-03-04 15:14:35 -07:00
describe("mapStateToProps()", () => {
it("returns props", () => {
const state = fakeState();
const config = fakeWebAppConfig();
config.body.x_axis_inverted = true;
state.resources = buildResourceIndex([config]);
2019-12-27 11:37:54 -07:00
state.bot.hardware.user_env = { fake: "value" };
2019-03-04 15:14:35 -07:00
const result = mapStateToProps(state);
expect(result.axisInversion.x).toEqual(true);
2019-12-27 11:37:54 -07:00
expect(result.autoSync).toEqual(false);
expect(result.env).toEqual({ fake: "value" });
});
it("returns api props", () => {
const state = fakeState();
const config = fakeFbosConfig();
config.body.auto_sync = true;
const fakeEnv = fakeFarmwareEnv();
state.resources = buildResourceIndex([config, fakeEnv]);
state.bot.minOsFeatureData = { api_farmware_env: "8.0.0" };
state.bot.hardware.informational_settings.controller_version = "8.0.0";
const result = mapStateToProps(state);
expect(result.autoSync).toEqual(true);
expect(result.env).toEqual({ [fakeEnv.body.key]: fakeEnv.body.value });
2019-03-04 15:14:35 -07:00
});
});