test output cleanup

pull/1220/head
gabrielburnworth 2019-06-05 13:48:18 -07:00
parent 0c90e09230
commit fb48165d68
20 changed files with 177 additions and 174 deletions

View File

@ -0,0 +1,6 @@
import * as React from "react";
import { mount } from "enzyme";
export function svgMount(element: React.ReactElement) {
return mount(<svg>{element}</svg>);
}

View File

@ -1,47 +0,0 @@
jest.mock("../util/stop_ie", () => {
return {
stopIE: jest.fn()
};
});
jest.mock("../util/util", () => {
return {
shortRevision: jest.fn(),
trim: jest.fn((s: unknown) => s),
defensiveClone: jest.fn((s: unknown) => s)
};
});
jest.mock("../i18n", () => {
return {
detectLanguage: jest.fn(() => Promise.resolve())
};
});
jest.mock("i18next", () => {
return {
init: jest.fn()
};
});
jest.mock("../routes", () => {
return {
attachAppToDom: { mock: "Yeah" }
};
});
import { stopIE } from "../util/stop_ie";
import { shortRevision } from "../util/util";
import { detectLanguage } from "../i18n";
import I from "i18next";
describe("entry file", () => {
it("Calls the expected callbacks", async () => {
await import("../entry");
expect(stopIE).toHaveBeenCalled();
expect(shortRevision).toHaveBeenCalled();
expect(detectLanguage).toHaveBeenCalled();
expect(I.init).toHaveBeenCalled();
});
});

View File

@ -0,0 +1,32 @@
jest.mock("../util/util", () => ({
shortRevision: jest.fn(() => "ABCD"),
trim: jest.fn((s: unknown) => s),
defensiveClone: jest.fn((s: unknown) => s)
}));
jest.mock("../i18n", () => ({
detectLanguage: jest.fn(() => Promise.resolve())
}));
jest.mock("../util/stop_ie", () => ({ stopIE: jest.fn() }));
jest.mock("i18next", () => ({ init: jest.fn() }));
jest.mock("../routes", () => ({ attachAppToDom: { mock: "Yeah" } }));
import { stopIE } from "../util/stop_ie";
import { shortRevision } from "../util/util";
import { detectLanguage } from "../i18n";
import I from "i18next";
describe("entry file", () => {
it("Calls the expected callbacks", async () => {
console.log = jest.fn();
await import("../entry");
expect(stopIE).toHaveBeenCalled();
expect(shortRevision).toHaveBeenCalled();
expect(detectLanguage).toHaveBeenCalled();
expect(I.init).toHaveBeenCalled();
expect(console.log).toHaveBeenCalledWith("ABCD");
});
});

View File

@ -19,11 +19,15 @@ class Kaboom extends React.Component<{}, {}> {
describe("<ErrorBoundary/>", () => { describe("<ErrorBoundary/>", () => {
it("handles exceptions", () => { it("handles exceptions", () => {
console.error = jest.fn();
const nodes = <ErrorBoundary><Kaboom /></ErrorBoundary>; const nodes = <ErrorBoundary><Kaboom /></ErrorBoundary>;
const el = mount<ErrorBoundary>(nodes); const el = mount<ErrorBoundary>(nodes);
expect(el.text()).toContain("can't render this part of the page"); expect(el.text()).toContain("can't render this part of the page");
const i = el.instance(); const i = el.instance();
expect(i.state.hasError).toBe(true); expect(i.state.hasError).toBe(true);
expect(catchErrors).toHaveBeenCalled(); expect(catchErrors).toHaveBeenCalled();
expect(console.error).toHaveBeenCalledTimes(2);
expect(console.error).toHaveBeenCalledWith(expect.any(String), Error("ALWAYS"));
expect(console.error).toHaveBeenCalledWith(expect.stringContaining("Kaboom"));
}); });
}); });

View File

@ -1,6 +1,5 @@
jest.mock("react-redux", () => ({ jest.mock("react-redux", () => ({ connect: jest.fn() }));
connect: jest.fn() jest.mock("../labs/labs_features", () => ({ LabsFeatures: () => <div /> }));
}));
import * as React from "react"; import * as React from "react";
import { fakeState } from "../../__test_support__/fake_state"; import { fakeState } from "../../__test_support__/fake_state";

View File

@ -30,7 +30,7 @@ const LengthInput = (props: LengthInputProps) =>
<Col xs={7}> <Col xs={7}>
<input <input
type="number" type="number"
value={props.value} value={"" + props.value}
onChange={e => props.dispatch(setWebAppConfigValue( onChange={e => props.dispatch(setWebAppConfigValue(
props.setting, e.currentTarget.value))} /> props.setting, e.currentTarget.value))} />
</Col> </Col>

View File

@ -40,6 +40,7 @@ describe("refresh()", () => {
const thunk = refresh(device1); const thunk = refresh(device1);
const dispatch = jest.fn(); const dispatch = jest.fn();
const { mock } = dispatch; const { mock } = dispatch;
console.error = jest.fn();
thunk(dispatch); thunk(dispatch);
setImmediate(() => { setImmediate(() => {
expect(dispatch).toHaveBeenCalledTimes(2); expect(dispatch).toHaveBeenCalledTimes(2);
@ -56,6 +57,9 @@ describe("refresh()", () => {
"payload.err.message", "payload.err.message",
"NO ERR MSG FOUND"); "NO ERR MSG FOUND");
expect(dispatchPayl).toEqual("Unable to refresh"); expect(dispatchPayl).toEqual("Unable to refresh");
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(
expect.stringContaining("Device"));
done(); done();
}); });
}); });
@ -70,8 +74,12 @@ describe("updateViaAjax()", () => {
index: buildResourceIndex([fakePeripheral()]).index index: buildResourceIndex([fakePeripheral()]).index
}; };
payload.uuid = Object.keys(payload.index.all)[0]; payload.uuid = Object.keys(payload.index.all)[0];
console.error = jest.fn();
updateViaAjax(payload).catch(e => { updateViaAjax(payload).catch(e => {
expect("" + e).toEqual("Error: Just saved a malformed TR."); expect("" + e).toEqual("Error: Just saved a malformed TR.");
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(
expect.stringContaining("Peripheral"));
}); });
}); });
}); });

View File

@ -40,6 +40,7 @@ import { fakeState } from "../../../__test_support__/fake_state";
import { talk } from "browser-speech"; import { talk } from "browser-speech";
import { globalQueue } from "../../batch_queue"; import { globalQueue } from "../../batch_queue";
import { MessageType } from "../../../sequences/interfaces"; import { MessageType } from "../../../sequences/interfaces";
import { FbjsEventName } from "farmbot/dist/constants";
const A_STRING = expect.any(String); const A_STRING = expect.any(String);
describe("readStatus()", () => { describe("readStatus()", () => {
@ -238,10 +239,16 @@ describe("onLogs", () => {
}); });
describe("onPublicBroadcast", () => { describe("onPublicBroadcast", () => {
const expectBroadcastLog = () =>
expect(console.log).toHaveBeenCalledWith(
FbjsEventName.publicBroadcast, expect.any(Object));
it("triggers when appropriate", () => { it("triggers when appropriate", () => {
location.assign = jest.fn(); location.assign = jest.fn();
window.confirm = jest.fn(() => true); window.confirm = jest.fn(() => true);
console.log = jest.fn();
onPublicBroadcast({}); onPublicBroadcast({});
expectBroadcastLog();
expect(window.confirm).toHaveBeenCalledWith(Content.FORCE_REFRESH_CONFIRM); expect(window.confirm).toHaveBeenCalledWith(Content.FORCE_REFRESH_CONFIRM);
expect(location.assign).toHaveBeenCalled(); expect(location.assign).toHaveBeenCalled();
}); });
@ -249,7 +256,9 @@ describe("onPublicBroadcast", () => {
it("allows cancellation of refresh", () => { it("allows cancellation of refresh", () => {
window.confirm = jest.fn(() => false); window.confirm = jest.fn(() => false);
window.alert = jest.fn(); window.alert = jest.fn();
console.log = jest.fn();
onPublicBroadcast({}); onPublicBroadcast({});
expectBroadcastLog();
expect(window.alert).toHaveBeenCalledWith(Content.FORCE_REFRESH_CANCEL_WARNING); expect(window.alert).toHaveBeenCalledWith(Content.FORCE_REFRESH_CANCEL_WARNING);
expect(location.assign).not.toHaveBeenCalled(); expect(location.assign).not.toHaveBeenCalled();
}); });

View File

@ -163,10 +163,8 @@ export const onOnline =
export const onReconnect = export const onReconnect =
() => warning(t("Attempting to reconnect to the message broker"), t("Offline")); () => warning(t("Attempting to reconnect to the message broker"), t("Offline"));
export const BROADCAST_CHANNEL = "public_broadcast";
export function onPublicBroadcast(payl: unknown) { export function onPublicBroadcast(payl: unknown) {
console.log(BROADCAST_CHANNEL, payl); console.log(FbjsEventName.publicBroadcast, payl);
if (confirm(t(Content.FORCE_REFRESH_CONFIRM))) { if (confirm(t(Content.FORCE_REFRESH_CONFIRM))) {
location.assign(window.location.origin || "/"); location.assign(window.location.origin || "/");
} else { } else {

View File

@ -12,6 +12,7 @@ import {
getConnectionColor getConnectionColor
} from "../diagram"; } from "../diagram";
import { Color } from "../../../ui/index"; import { Color } from "../../../ui/index";
import { svgMount } from "../../../__test_support__/svg_mount";
describe("<ConnectivityDiagram/>", () => { describe("<ConnectivityDiagram/>", () => {
function fakeProps(): ConnectivityDiagramProps { function fakeProps(): ConnectivityDiagramProps {
@ -58,14 +59,14 @@ describe("<ConnectivityDiagram/>", () => {
} }
it("renders diagram", () => { it("renders diagram", () => {
const wrapper = mount(<ConnectivityDiagram {...fakeProps()} />); const wrapper = svgMount(<ConnectivityDiagram {...fakeProps()} />);
expect(wrapper.text()) expect(wrapper.text())
.toContain("BrowserWeb AppMessage BrokerFarmBotRaspberry PiF"); .toContain("BrowserWeb AppMessage BrokerFarmBotRaspberry PiF");
}); });
it("hover", () => { it("hover", () => {
const p = fakeProps(); const p = fakeProps();
const wrapper = mount(<ConnectivityDiagram {...p} />); const wrapper = svgMount(<ConnectivityDiagram {...p} />);
wrapper.find(".connector-hover-area").first().simulate("mouseEnter"); wrapper.find(".connector-hover-area").first().simulate("mouseEnter");
expect(p.hover).toHaveBeenCalledWith("EF"); expect(p.hover).toHaveBeenCalledWith("EF");
}); });
@ -130,7 +131,7 @@ describe("<Connector/>", () => {
} }
it("renders", () => { it("renders", () => {
const wrapper = mount(<Connector {...fakeProps()} />); const wrapper = svgMount(<Connector {...fakeProps()} />);
const lines = wrapper.find("line"); const lines = wrapper.find("line");
expect(lines.length).toEqual(3); expect(lines.length).toEqual(3);
expect(lines.at(0).props()) expect(lines.at(0).props())
@ -154,7 +155,7 @@ describe("<Connector/>", () => {
it("renders connected color", () => { it("renders connected color", () => {
const p = fakeProps(); const p = fakeProps();
p.connectionData.connectionStatus = true; p.connectionData.connectionStatus = true;
const wrapper = mount(<Connector {...p} />); const wrapper = svgMount(<Connector {...p} />);
expect(wrapper.find("line").at(1).props().stroke).toEqual(Color.green); expect(wrapper.find("line").at(1).props().stroke).toEqual(Color.green);
}); });
}); });

View File

@ -1,25 +1,23 @@
import * as React from "react"; import * as React from "react";
import { DrawnPoint, DrawnPointProps } from "../drawn_point"; import { DrawnPoint, DrawnPointProps } from "../drawn_point";
import { mount } from "enzyme";
import { import {
fakeMapTransformProps fakeMapTransformProps
} from "../../../../__test_support__/map_transform_props"; } from "../../../../__test_support__/map_transform_props";
import { svgMount } from "../../../../__test_support__/svg_mount";
describe("<DrawnPoint/>", () => { describe("<DrawnPoint/>", () => {
function fakeProps(): DrawnPointProps { const fakeProps = (): DrawnPointProps => ({
return { mapTransformProps: fakeMapTransformProps(),
mapTransformProps: fakeMapTransformProps(), data: {
data: { cx: 10,
cx: 10, cy: 20,
cy: 20, r: 30,
r: 30, color: "red"
color: "red" }
} });
};
}
it("renders point", () => { it("renders point", () => {
const wrapper = mount(<DrawnPoint {...fakeProps()} />); const wrapper = svgMount(<DrawnPoint {...fakeProps()} />);
expect(wrapper.find("g").props().stroke).toEqual("red"); expect(wrapper.find("g").props().stroke).toEqual("red");
expect(wrapper.find("circle").first().props()).toEqual({ expect(wrapper.find("circle").first().props()).toEqual({
id: "point-radius", strokeDasharray: "4 5", id: "point-radius", strokeDasharray: "4 5",

View File

@ -1,5 +1,5 @@
import * as React from "react"; import * as React from "react";
import { shallow, mount } from "enzyme"; import { shallow } from "enzyme";
import { import {
Bugs, BugsProps, showBugResetButton, showBugs, resetBugs, BugsControls Bugs, BugsProps, showBugResetButton, showBugs, resetBugs, BugsControls
} from "../bugs"; } from "../bugs";
@ -8,6 +8,7 @@ import { range } from "lodash";
import { import {
fakeMapTransformProps fakeMapTransformProps
} from "../../../../__test_support__/map_transform_props"; } from "../../../../__test_support__/map_transform_props";
import { svgMount } from "../../../../__test_support__/svg_mount";
const expectAlive = (value: string) => const expectAlive = (value: string) =>
expect(getEggStatus(EggKeys.BUGS_ARE_STILL_ALIVE)).toEqual(value); expect(getEggStatus(EggKeys.BUGS_ARE_STILL_ALIVE)).toEqual(value);
@ -22,7 +23,7 @@ describe("<Bugs />", () => {
}); });
it("renders", () => { it("renders", () => {
const wrapper = shallow(<Bugs {...fakeProps()} />); const wrapper = svgMount(<Bugs {...fakeProps()} />);
expect(wrapper.find("image").length).toEqual(10); expect(wrapper.find("image").length).toEqual(10);
const firstBug = wrapper.find("image").first(); const firstBug = wrapper.find("image").first();
expect(firstBug.props()).toEqual(expect.objectContaining({ expect(firstBug.props()).toEqual(expect.objectContaining({
@ -36,8 +37,8 @@ describe("<Bugs />", () => {
it("kills bugs", () => { it("kills bugs", () => {
setEggStatus(EggKeys.BUGS_ARE_STILL_ALIVE, ""); setEggStatus(EggKeys.BUGS_ARE_STILL_ALIVE, "");
expectAlive(""); expectAlive("");
const wrapper = mount<Bugs>(<Bugs {...fakeProps()} />); const wrapper = svgMount(<Bugs {...fakeProps()} />);
wrapper.instance().state.bugs[0].r = 101; wrapper.find(Bugs).state().bugs[0].r = 101;
range(10).map(b => range(10).map(b =>
wrapper.find("image").at(b).simulate("click")); wrapper.find("image").at(b).simulate("click"));
expectAlive(""); expectAlive("");
@ -50,7 +51,7 @@ describe("<Bugs />", () => {
className: expect.stringContaining("dead"), className: expect.stringContaining("dead"),
filter: expect.stringContaining("grayscale") filter: expect.stringContaining("grayscale")
})); }));
expect(wrapper.instance().state.bugs[0]).toEqual(expect.objectContaining({ expect(wrapper.find(Bugs).state().bugs[0]).toEqual(expect.objectContaining({
alive: false, hp: 50 alive: false, hp: 50
})); }));
}); });

View File

@ -5,7 +5,6 @@ jest.mock("../../../../../history", () => ({
import * as React from "react"; import * as React from "react";
import { PlantLayer } from "../plant_layer"; import { PlantLayer } from "../plant_layer";
import { shallow } from "enzyme";
import { import {
fakePlant, fakePlantTemplate fakePlant, fakePlantTemplate
} from "../../../../../__test_support__/fake_state/resources"; } from "../../../../../__test_support__/fake_state/resources";
@ -13,29 +12,28 @@ import { PlantLayerProps, GardenPlantProps } from "../../../interfaces";
import { import {
fakeMapTransformProps fakeMapTransformProps
} from "../../../../../__test_support__/map_transform_props"; } from "../../../../../__test_support__/map_transform_props";
import { svgMount } from "../../../../../__test_support__/svg_mount";
describe("<PlantLayer/>", () => { describe("<PlantLayer/>", () => {
function fakeProps(): PlantLayerProps { const fakeProps = (): PlantLayerProps => ({
return { visible: true,
visible: true, plants: [fakePlant()],
plants: [fakePlant()], mapTransformProps: fakeMapTransformProps(),
mapTransformProps: fakeMapTransformProps(), currentPlant: undefined,
currentPlant: undefined, dragging: false,
dragging: false, editing: false,
editing: false, selectedForDel: undefined,
selectedForDel: undefined, dispatch: jest.fn(),
dispatch: jest.fn(), zoomLvl: 1,
zoomLvl: 1, activeDragXY: { x: undefined, y: undefined, z: undefined },
activeDragXY: { x: undefined, y: undefined, z: undefined }, animate: true,
animate: true, });
};
}
it("shows plants", () => { it("shows plants", () => {
const p = fakeProps(); const p = fakeProps();
const wrapper = shallow(<PlantLayer {...p} />); const wrapper = svgMount(<PlantLayer {...p} />);
const layer = wrapper.find("#plant-layer"); const layer = wrapper.find("#plant-layer");
expect(layer.find(".plant-link-wrapper").length).toEqual(1); expect(layer.find(".plant-link-wrapper").length).toEqual(2);
["soil-cloud", ["soil-cloud",
"plant-icon", "plant-icon",
"image visibility=\"visible\"", "image visibility=\"visible\"",
@ -50,21 +48,21 @@ describe("<PlantLayer/>", () => {
it("toggles visibility off", () => { it("toggles visibility off", () => {
const p = fakeProps(); const p = fakeProps();
p.visible = false; p.visible = false;
const wrapper = shallow(<PlantLayer {...p} />); const wrapper = svgMount(<PlantLayer {...p} />);
expect(wrapper.html()).toEqual("<g id=\"plant-layer\"></g>"); expect(wrapper.html()).toEqual("<svg><g id=\"plant-layer\"></g></svg>");
}); });
it("is in clickable mode", () => { it("is in clickable mode", () => {
mockPath = "/app/designer/plants"; mockPath = "/app/designer/plants";
const p = fakeProps(); const p = fakeProps();
const wrapper = shallow(<PlantLayer {...p} />); const wrapper = svgMount(<PlantLayer {...p} />);
expect(wrapper.find("Link").props().style).toEqual({}); expect(wrapper.find("Link").props().style).toEqual({});
}); });
it("is in non-clickable mode", () => { it("is in non-clickable mode", () => {
mockPath = "/app/designer/plants/select"; mockPath = "/app/designer/plants/select";
const p = fakeProps(); const p = fakeProps();
const wrapper = shallow(<PlantLayer {...p} />); const wrapper = svgMount(<PlantLayer {...p} />);
expect(wrapper.find("Link").props().style) expect(wrapper.find("Link").props().style)
.toEqual({ pointerEvents: "none" }); .toEqual({ pointerEvents: "none" });
}); });
@ -73,7 +71,7 @@ describe("<PlantLayer/>", () => {
mockPath = "/app/designer/plants"; mockPath = "/app/designer/plants";
const p = fakeProps(); const p = fakeProps();
p.plants[0].body.id = 5; p.plants[0].body.id = 5;
const wrapper = shallow(<PlantLayer {...p} />); const wrapper = svgMount(<PlantLayer {...p} />);
expect(wrapper.find("Link").props().to) expect(wrapper.find("Link").props().to)
.toEqual("/app/designer/plants/5"); .toEqual("/app/designer/plants/5");
}); });
@ -83,7 +81,7 @@ describe("<PlantLayer/>", () => {
const p = fakeProps(); const p = fakeProps();
p.plants = [fakePlantTemplate()]; p.plants = [fakePlantTemplate()];
p.plants[0].body.id = 5; p.plants[0].body.id = 5;
const wrapper = shallow(<PlantLayer {...p} />); const wrapper = svgMount(<PlantLayer {...p} />);
expect(wrapper.find("Link").props().to) expect(wrapper.find("Link").props().to)
.toEqual("/app/designer/saved_gardens/templates/5"); .toEqual("/app/designer/saved_gardens/templates/5");
}); });
@ -94,7 +92,7 @@ describe("<PlantLayer/>", () => {
const plant = fakePlant(); const plant = fakePlant();
p.plants = [plant]; p.plants = [plant];
p.currentPlant = plant; p.currentPlant = plant;
const wrapper = shallow(<PlantLayer {...p} />); const wrapper = svgMount(<PlantLayer {...p} />);
expect(wrapper.find("GardenPlant").props().selected).toEqual(true); expect(wrapper.find("GardenPlant").props().selected).toEqual(true);
}); });
@ -104,7 +102,7 @@ describe("<PlantLayer/>", () => {
const plant = fakePlant(); const plant = fakePlant();
p.plants = [plant]; p.plants = [plant];
p.selectedForDel = [plant.uuid]; p.selectedForDel = [plant.uuid];
const wrapper = shallow(<PlantLayer {...p} />); const wrapper = svgMount(<PlantLayer {...p} />);
expect((wrapper.find("GardenPlant").props() as GardenPlantProps).grayscale) expect((wrapper.find("GardenPlant").props() as GardenPlantProps).grayscale)
.toEqual(true); .toEqual(true);
}); });
@ -114,7 +112,7 @@ describe("<PlantLayer/>", () => {
const plant = fakePlant(); const plant = fakePlant();
plant.body.id = 1; plant.body.id = 1;
p.plants = [plant]; p.plants = [plant];
const wrapper = shallow(<PlantLayer {...p} />); const wrapper = svgMount(<PlantLayer {...p} />);
expect((wrapper.find("Link").props()).style).toEqual({}); expect((wrapper.find("Link").props()).style).toEqual({});
}); });
@ -123,7 +121,7 @@ describe("<PlantLayer/>", () => {
const plant = fakePlant(); const plant = fakePlant();
plant.body.id = 0; plant.body.id = 0;
p.plants = [plant]; p.plants = [plant];
const wrapper = shallow(<PlantLayer {...p} />); const wrapper = svgMount(<PlantLayer {...p} />);
expect((wrapper.find("Link").props()).style) expect((wrapper.find("Link").props()).style)
.toEqual({ pointerEvents: "none" }); .toEqual({ pointerEvents: "none" });
}); });

View File

@ -1,5 +1,5 @@
import * as React from "react"; import * as React from "react";
import { mount, shallow } from "enzyme"; import { shallow } from "enzyme";
import { import {
GardenSensorReading, GardenSensorReadingProps GardenSensorReading, GardenSensorReadingProps
} from "../garden_sensor_reading"; } from "../garden_sensor_reading";
@ -12,6 +12,7 @@ import {
import { import {
fakeTimeSettings fakeTimeSettings
} from "../../../../../__test_support__/fake_time_settings"; } from "../../../../../__test_support__/fake_time_settings";
import { svgMount } from "../../../../../__test_support__/svg_mount";
describe("<GardenSensorReading />", () => { describe("<GardenSensorReading />", () => {
const fakeProps = (): GardenSensorReadingProps => ({ const fakeProps = (): GardenSensorReadingProps => ({
@ -23,7 +24,7 @@ describe("<GardenSensorReading />", () => {
}); });
it("renders", () => { it("renders", () => {
const wrapper = mount(<GardenSensorReading {...fakeProps()} />); const wrapper = svgMount(<GardenSensorReading {...fakeProps()} />);
expect(wrapper.html()).toContain("sensor-reading-"); expect(wrapper.html()).toContain("sensor-reading-");
expect(wrapper.find("circle").length).toEqual(2); expect(wrapper.find("circle").length).toEqual(2);
}); });
@ -31,21 +32,21 @@ describe("<GardenSensorReading />", () => {
it("doesn't render", () => { it("doesn't render", () => {
const p = fakeProps(); const p = fakeProps();
p.sensorReading.body.x = undefined; p.sensorReading.body.x = undefined;
const wrapper = mount(<GardenSensorReading {...p} />); const wrapper = svgMount(<GardenSensorReading {...p} />);
expect(wrapper.find("circle").length).toEqual(0); expect(wrapper.find("circle").length).toEqual(0);
}); });
it("renders sensor name", () => { it("renders sensor name", () => {
const p = fakeProps(); const p = fakeProps();
p.sensorLookup = { 1: "Sensor Name" }; p.sensorLookup = { 1: "Sensor Name" };
const wrapper = mount(<GardenSensorReading {...p} />); const wrapper = svgMount(<GardenSensorReading {...p} />);
expect(wrapper.text()).toContain("Sensor Name (pin 1)"); expect(wrapper.text()).toContain("Sensor Name (pin 1)");
}); });
it("renders analog reading", () => { it("renders analog reading", () => {
const p = fakeProps(); const p = fakeProps();
p.sensorReading.body.mode = 1; p.sensorReading.body.mode = 1;
const wrapper = mount(<GardenSensorReading {...p} />); const wrapper = svgMount(<GardenSensorReading {...p} />);
expect(wrapper.text()).toContain("value 0 (analog)"); expect(wrapper.text()).toContain("value 0 (analog)");
}); });

View File

@ -1,5 +1,4 @@
import * as React from "react"; import * as React from "react";
import { mount } from "enzyme";
import { import {
SensorReadingsLayer, SensorReadingsLayerProps SensorReadingsLayer, SensorReadingsLayerProps
} from "../sensor_readings_layer"; } from "../sensor_readings_layer";
@ -12,6 +11,7 @@ import {
import { import {
fakeTimeSettings fakeTimeSettings
} from "../../../../../__test_support__/fake_time_settings"; } from "../../../../../__test_support__/fake_time_settings";
import { svgMount } from "../../../../../__test_support__/svg_mount";
describe("<SensorReadingsLayer />", () => { describe("<SensorReadingsLayer />", () => {
const fakeProps = (): SensorReadingsLayerProps => ({ const fakeProps = (): SensorReadingsLayerProps => ({
@ -23,7 +23,7 @@ describe("<SensorReadingsLayer />", () => {
}); });
it("renders", () => { it("renders", () => {
const wrapper = mount(<SensorReadingsLayer {...fakeProps()} />); const wrapper = svgMount(<SensorReadingsLayer {...fakeProps()} />);
expect(wrapper.html()).toContain("sensor-readings-layer"); expect(wrapper.html()).toContain("sensor-readings-layer");
}); });
}); });

View File

@ -1,22 +1,20 @@
import * as React from "react"; import * as React from "react";
import { mount } from "enzyme";
import { import {
ToolbaySlot, Tool, ToolProps, ToolGraphicProps, ToolSlotGraphicProps ToolbaySlot, Tool, ToolProps, ToolGraphicProps, ToolSlotGraphicProps
} from "../tool_graphics"; } from "../tool_graphics";
import { BotOriginQuadrant } from "../../../../interfaces"; import { BotOriginQuadrant } from "../../../../interfaces";
import { Color } from "../../../../../ui"; import { Color } from "../../../../../ui";
import { svgMount } from "../../../../../__test_support__/svg_mount";
describe("<ToolbaySlot />", () => { describe("<ToolbaySlot />", () => {
const fakeProps = (): ToolSlotGraphicProps => { const fakeProps = (): ToolSlotGraphicProps => ({
return { id: undefined,
id: undefined, x: 10,
x: 10, y: 20,
y: 20, pulloutDirection: 0,
pulloutDirection: 0, quadrant: 2,
quadrant: 2, xySwap: false,
xySwap: false, });
};
};
const checkSlotDirection = const checkSlotDirection =
(direction: number, (direction: number,
@ -29,7 +27,7 @@ describe("<ToolbaySlot />", () => {
p.pulloutDirection = direction; p.pulloutDirection = direction;
p.quadrant = quadrant; p.quadrant = quadrant;
p.xySwap = xySwap; p.xySwap = xySwap;
const wrapper = mount(<ToolbaySlot {...p} />); const wrapper = svgMount(<ToolbaySlot {...p} />);
expect(wrapper.find("use").props().transform).toEqual(expected); expect(wrapper.find("use").props().transform).toEqual(expected);
}); });
}; };
@ -59,27 +57,23 @@ describe("<ToolbaySlot />", () => {
}); });
describe("<Tool/>", () => { describe("<Tool/>", () => {
const fakeToolProps = (): ToolGraphicProps => { const fakeToolProps = (): ToolGraphicProps => ({
return { x: 10,
x: 10, y: 20,
y: 20, hovered: false,
hovered: false, setHoverState: jest.fn(),
setHoverState: jest.fn(), xySwap: false,
xySwap: false, });
};
};
const fakeProps = (): ToolProps => { const fakeProps = (): ToolProps => ({
return { tool: "fake tool",
tool: "fake tool", toolProps: fakeToolProps()
toolProps: fakeToolProps() });
};
};
const testHoverActions = (toolName: string) => { const testHoverActions = (toolName: string) => {
const p = fakeProps(); const p = fakeProps();
p.tool = toolName; p.tool = toolName;
const wrapper = mount(<Tool {...p} />); const wrapper = svgMount(<Tool {...p} />);
wrapper.find("g").simulate("mouseOver"); wrapper.find("g").simulate("mouseOver");
expect(p.toolProps.setHoverState).toHaveBeenCalledWith(true); expect(p.toolProps.setHoverState).toHaveBeenCalledWith(true);
wrapper.find("g").simulate("mouseLeave"); wrapper.find("g").simulate("mouseLeave");
@ -87,7 +81,7 @@ describe("<Tool/>", () => {
}; };
it("renders standard tool styling", () => { it("renders standard tool styling", () => {
const wrapper = mount(<Tool {...fakeProps()} />); const wrapper = svgMount(<Tool {...fakeProps()} />);
const props = wrapper.find("circle").last().props(); const props = wrapper.find("circle").last().props();
expect(props.r).toEqual(35); expect(props.r).toEqual(35);
expect(props.cx).toEqual(10); expect(props.cx).toEqual(10);
@ -98,7 +92,7 @@ describe("<Tool/>", () => {
it("renders tool hover styling", () => { it("renders tool hover styling", () => {
const p = fakeProps(); const p = fakeProps();
p.toolProps.hovered = true; p.toolProps.hovered = true;
const wrapper = mount(<Tool {...p} />); const wrapper = svgMount(<Tool {...p} />);
const props = wrapper.find("circle").last().props(); const props = wrapper.find("circle").last().props();
expect(props.fill).toEqual(Color.darkGray); expect(props.fill).toEqual(Color.darkGray);
}); });
@ -110,7 +104,7 @@ describe("<Tool/>", () => {
it("renders special tool styling: bin", () => { it("renders special tool styling: bin", () => {
const p = fakeProps(); const p = fakeProps();
p.tool = "seedBin"; p.tool = "seedBin";
const wrapper = mount(<Tool {...p} />); const wrapper = svgMount(<Tool {...p} />);
const elements = wrapper.find("#seed-bin").find("circle"); const elements = wrapper.find("#seed-bin").find("circle");
expect(elements.length).toEqual(2); expect(elements.length).toEqual(2);
expect(elements.last().props().fill).toEqual("url(#SeedBinGradient)"); expect(elements.last().props().fill).toEqual("url(#SeedBinGradient)");
@ -120,7 +114,7 @@ describe("<Tool/>", () => {
const p = fakeProps(); const p = fakeProps();
p.tool = "seedBin"; p.tool = "seedBin";
p.toolProps.hovered = true; p.toolProps.hovered = true;
const wrapper = mount(<Tool {...p} />); const wrapper = svgMount(<Tool {...p} />);
p.toolProps.hovered = true; p.toolProps.hovered = true;
expect(wrapper.find("#seed-bin").find("circle").length).toEqual(3); expect(wrapper.find("#seed-bin").find("circle").length).toEqual(3);
}); });
@ -132,7 +126,7 @@ describe("<Tool/>", () => {
it("renders special tool styling: tray", () => { it("renders special tool styling: tray", () => {
const p = fakeProps(); const p = fakeProps();
p.tool = "seedTray"; p.tool = "seedTray";
const wrapper = mount(<Tool {...p} />); const wrapper = svgMount(<Tool {...p} />);
const elements = wrapper.find("#seed-tray"); const elements = wrapper.find("#seed-tray");
expect(elements.find("circle").length).toEqual(2); expect(elements.find("circle").length).toEqual(2);
expect(elements.find("rect").length).toEqual(1); expect(elements.find("rect").length).toEqual(1);
@ -143,7 +137,7 @@ describe("<Tool/>", () => {
const p = fakeProps(); const p = fakeProps();
p.tool = "seedTray"; p.tool = "seedTray";
p.toolProps.hovered = true; p.toolProps.hovered = true;
const wrapper = mount(<Tool {...p} />); const wrapper = svgMount(<Tool {...p} />);
p.toolProps.hovered = true; p.toolProps.hovered = true;
expect(wrapper.find("#seed-tray").find("circle").length).toEqual(3); expect(wrapper.find("#seed-tray").find("circle").length).toEqual(3);
}); });
@ -155,7 +149,7 @@ describe("<Tool/>", () => {
it("renders special tool styling: trough", () => { it("renders special tool styling: trough", () => {
const p = fakeProps(); const p = fakeProps();
p.tool = "seedTrough"; p.tool = "seedTrough";
const wrapper = mount(<Tool {...p} />); const wrapper = svgMount(<Tool {...p} />);
const elements = wrapper.find("#seed-trough"); const elements = wrapper.find("#seed-trough");
expect(elements.find("circle").length).toEqual(0); expect(elements.find("circle").length).toEqual(0);
expect(elements.find("rect").length).toEqual(1); expect(elements.find("rect").length).toEqual(1);
@ -165,7 +159,7 @@ describe("<Tool/>", () => {
const p = fakeProps(); const p = fakeProps();
p.tool = "seedTrough"; p.tool = "seedTrough";
p.toolProps.hovered = true; p.toolProps.hovered = true;
const wrapper = mount(<Tool {...p} />); const wrapper = svgMount(<Tool {...p} />);
p.toolProps.hovered = true; p.toolProps.hovered = true;
expect(wrapper.find("#seed-trough").find("circle").length).toEqual(0); expect(wrapper.find("#seed-trough").find("circle").length).toEqual(0);
expect(wrapper.find("#seed-trough").find("rect").length).toEqual(1); expect(wrapper.find("#seed-trough").find("rect").length).toEqual(1);

View File

@ -1,21 +1,19 @@
import * as React from "react"; import * as React from "react";
import { ToolSlotPoint, TSPProps } from "../tool_slot_point"; import { ToolSlotPoint, TSPProps } from "../tool_slot_point";
import { mount } from "enzyme";
import { import {
fakeToolSlot, fakeTool fakeToolSlot, fakeTool
} from "../../../../../__test_support__/fake_state/resources"; } from "../../../../../__test_support__/fake_state/resources";
import { import {
fakeMapTransformProps fakeMapTransformProps
} from "../../../../../__test_support__/map_transform_props"; } from "../../../../../__test_support__/map_transform_props";
import { svgMount } from "../../../../../__test_support__/svg_mount";
describe("<ToolSlotPoint/>", () => { describe("<ToolSlotPoint/>", () => {
function fakeProps(): TSPProps { const fakeProps = (): TSPProps => ({
return { mapTransformProps: fakeMapTransformProps(),
mapTransformProps: fakeMapTransformProps(), botPositionX: undefined,
botPositionX: undefined, slot: { toolSlot: fakeToolSlot(), tool: fakeTool() }
slot: { toolSlot: fakeToolSlot(), tool: fakeTool() } });
};
}
const testToolSlotGraphics = (tool: 0 | 1, slot: 0 | 1) => { const testToolSlotGraphics = (tool: 0 | 1, slot: 0 | 1) => {
it(`renders ${tool ? "" : "no"} tool and ${slot ? "" : "no"} slot`, () => { it(`renders ${tool ? "" : "no"} tool and ${slot ? "" : "no"} slot`, () => {
@ -23,7 +21,7 @@ describe("<ToolSlotPoint/>", () => {
const p = fakeProps(); const p = fakeProps();
if (!tool) { p.slot.tool = undefined; } if (!tool) { p.slot.tool = undefined; }
p.slot.toolSlot.body.pullout_direction = slot; p.slot.toolSlot.body.pullout_direction = slot;
const wrapper = mount(<ToolSlotPoint {...p} />); const wrapper = svgMount(<ToolSlotPoint {...p} />);
expect(wrapper.find("circle").length).toEqual(tool); expect(wrapper.find("circle").length).toEqual(tool);
expect(wrapper.find("use").length).toEqual(slot); expect(wrapper.find("use").length).toEqual(slot);
}); });
@ -36,8 +34,8 @@ describe("<ToolSlotPoint/>", () => {
it("displays tool name", () => { it("displays tool name", () => {
const p = fakeProps(); const p = fakeProps();
p.slot.toolSlot.body.pullout_direction = 2; p.slot.toolSlot.body.pullout_direction = 2;
const wrapper = mount(<ToolSlotPoint {...p} />); const wrapper = svgMount(<ToolSlotPoint {...p} />);
wrapper.setState({ hovered: true }); wrapper.find(ToolSlotPoint).setState({ hovered: true });
expect(wrapper.find("text").props().visibility).toEqual("visible"); expect(wrapper.find("text").props().visibility).toEqual("visible");
expect(wrapper.find("text").text()).toEqual("Foo"); expect(wrapper.find("text").text()).toEqual("Foo");
expect(wrapper.find("text").props().dx).toEqual(-40); expect(wrapper.find("text").props().dx).toEqual(-40);
@ -46,28 +44,28 @@ describe("<ToolSlotPoint/>", () => {
it("displays 'no tool'", () => { it("displays 'no tool'", () => {
const p = fakeProps(); const p = fakeProps();
p.slot.tool = undefined; p.slot.tool = undefined;
const wrapper = mount(<ToolSlotPoint {...p} />); const wrapper = svgMount(<ToolSlotPoint {...p} />);
wrapper.setState({ hovered: true }); wrapper.find(ToolSlotPoint).setState({ hovered: true });
expect(wrapper.find("text").text()).toEqual("no tool"); expect(wrapper.find("text").text()).toEqual("no tool");
expect(wrapper.find("text").props().dx).toEqual(40); expect(wrapper.find("text").props().dx).toEqual(40);
}); });
it("doesn't display tool name", () => { it("doesn't display tool name", () => {
const wrapper = mount(<ToolSlotPoint {...fakeProps()} />); const wrapper = svgMount(<ToolSlotPoint {...fakeProps()} />);
expect(wrapper.find("text").props().visibility).toEqual("hidden"); expect(wrapper.find("text").props().visibility).toEqual("hidden");
}); });
it("renders bin", () => { it("renders bin", () => {
const p = fakeProps(); const p = fakeProps();
if (p.slot.tool) { p.slot.tool.body.name = "seed bin"; } if (p.slot.tool) { p.slot.tool.body.name = "seed bin"; }
const wrapper = mount(<ToolSlotPoint {...p} />); const wrapper = svgMount(<ToolSlotPoint {...p} />);
expect(wrapper.find("#SeedBinGradient").length).toEqual(1); expect(wrapper.find("#SeedBinGradient").length).toEqual(1);
}); });
it("renders tray", () => { it("renders tray", () => {
const p = fakeProps(); const p = fakeProps();
if (p.slot.tool) { p.slot.tool.body.name = "seed tray"; } if (p.slot.tool) { p.slot.tool.body.name = "seed tray"; }
const wrapper = mount(<ToolSlotPoint {...p} />); const wrapper = svgMount(<ToolSlotPoint {...p} />);
expect(wrapper.find("#SeedTrayPattern").length).toEqual(1); expect(wrapper.find("#SeedTrayPattern").length).toEqual(1);
}); });
@ -75,14 +73,14 @@ describe("<ToolSlotPoint/>", () => {
const p = fakeProps(); const p = fakeProps();
p.slot.toolSlot.body.gantry_mounted = true; p.slot.toolSlot.body.gantry_mounted = true;
if (p.slot.tool) { p.slot.tool.body.name = "seed trough"; } if (p.slot.tool) { p.slot.tool.body.name = "seed trough"; }
const wrapper = mount(<ToolSlotPoint {...p} />); const wrapper = svgMount(<ToolSlotPoint {...p} />);
expect(wrapper.find("#seed-trough").length).toEqual(1); expect(wrapper.find("#seed-trough").length).toEqual(1);
}); });
it("sets hover", () => { it("sets hover", () => {
const wrapper = mount<ToolSlotPoint>(<ToolSlotPoint {...fakeProps()} />); const wrapper = svgMount(<ToolSlotPoint {...fakeProps()} />);
expect(wrapper.state().hovered).toBeFalsy(); expect(wrapper.find(ToolSlotPoint).state().hovered).toBeFalsy();
wrapper.instance().setHover(true); (wrapper.find(ToolSlotPoint).instance() as ToolSlotPoint).setHover(true);
expect(wrapper.state().hovered).toBeTruthy(); expect(wrapper.find(ToolSlotPoint).state().hovered).toBeTruthy();
}); });
}); });

View File

@ -22,6 +22,7 @@ describe("<RunTour />", () => {
}); });
describe("<Tour />", () => { describe("<Tour />", () => {
console.log = jest.fn();
const fakeCallbackData = (data: Partial<CallBackProps>): CallBackProps => ({ const fakeCallbackData = (data: Partial<CallBackProps>): CallBackProps => ({
action: data.action || "start", action: data.action || "start",
index: data.index || 0, index: data.index || 0,

View File

@ -10,28 +10,28 @@ const FIRMWARE_MISSING_ALERT: Alert = {
created_at: 123, created_at: 123,
problem_tag: "farmbot_os.firmware.missing", problem_tag: "farmbot_os.firmware.missing",
priority: 100, priority: 100,
slug: "slug", slug: "slug1",
}; };
const SEED_DATA_MISSING_ALERT: Alert = { const SEED_DATA_MISSING_ALERT: Alert = {
created_at: 123, created_at: 123,
problem_tag: "api.seed_data.missing", problem_tag: "api.seed_data.missing",
priority: 300, priority: 300,
slug: "slug", slug: "slug3",
}; };
const UNKNOWN_ALERT: Alert = { const UNKNOWN_ALERT: Alert = {
created_at: 123, created_at: 123,
problem_tag: "farmbot_os.firmware.alert", problem_tag: "farmbot_os.firmware.alert",
priority: 200, priority: 200,
slug: "slug", slug: "slug2",
}; };
const UNKNOWN_ALERT_2: Alert = { const UNKNOWN_ALERT_2: Alert = {
created_at: 456, created_at: 456,
problem_tag: "farmbot_os.firmware.alert", problem_tag: "farmbot_os.firmware.alert",
priority: 100, priority: 100,
slug: "slug", slug: "slug4",
}; };
describe("<Alerts />", () => { describe("<Alerts />", () => {
@ -85,8 +85,8 @@ describe("<FirmwareAlerts />", () => {
it("renders alerts", () => { it("renders alerts", () => {
const p = fakeProps(); const p = fakeProps();
p.bot.hardware.alerts = { p.bot.hardware.alerts = {
"slug1": FIRMWARE_MISSING_ALERT, [FIRMWARE_MISSING_ALERT.slug]: FIRMWARE_MISSING_ALERT,
"slug2": UNKNOWN_ALERT [UNKNOWN_ALERT.slug]: UNKNOWN_ALERT
}; };
const wrapper = mount(<FirmwareAlerts {...p} />); const wrapper = mount(<FirmwareAlerts {...p} />);
expect(wrapper.text()).toContain("1"); expect(wrapper.text()).toContain("1");

View File

@ -31,6 +31,8 @@ describe("getArrayStatus()", () => {
describe("sanityCheck", () => { describe("sanityCheck", () => {
it("crashes on malformed TaggedResources", () => { it("crashes on malformed TaggedResources", () => {
console.error = jest.fn();
expect(() => sanityCheck({})).toThrow("Bad kind, uuid, or body: {}"); expect(() => sanityCheck({})).toThrow("Bad kind, uuid, or body: {}");
expect(console.error).toHaveBeenCalledWith("{}");
}); });
}); });