Farmbot-Web-App/frontend/sequences/step_tiles/tile_if/__tests__/index_test.tsx

170 lines
4.9 KiB
TypeScript
Raw Normal View History

2017-12-01 01:47:17 -07:00
jest.mock("../../../../api/crud", () => ({
overwrite: jest.fn(),
}));
import * as React from "react";
import { mount } from "enzyme";
import {
seqDropDown,
InnerIf,
IfParams,
IfBlockDropDownHandler,
2019-02-11 19:44:45 -07:00
LHSOptions,
2020-02-28 09:35:32 -07:00
ThenElseParams,
2017-12-01 01:47:17 -07:00
} from "../index";
import {
2020-02-28 09:35:32 -07:00
buildResourceIndex, FAKE_RESOURCES,
2017-12-01 01:47:17 -07:00
} from "../../../../__test_support__/resource_index_builder";
2019-02-22 19:09:40 -07:00
import { Execute, If, TaggedSequence, ParameterApplication } from "farmbot";
2017-12-01 01:47:17 -07:00
import { overwrite } from "../../../../api/crud";
2018-08-01 18:20:50 -06:00
import {
2020-02-28 09:35:32 -07:00
fakeSensor, fakePeripheral,
2018-08-01 18:20:50 -06:00
} from "../../../../__test_support__/fake_state/resources";
2017-12-01 01:47:17 -07:00
const fakeResourceIndex = buildResourceIndex(FAKE_RESOURCES).index;
const fakeTaggedSequence = fakeResourceIndex
.references[Object.keys(fakeResourceIndex.byKind.Sequence)[0]] as TaggedSequence;
2020-01-03 13:04:45 -07:00
const fakeId = fakeTaggedSequence.body.id || 0;
const fakeName = fakeTaggedSequence.body.name || "";
2017-12-01 01:47:17 -07:00
const expectedItem = { label: fakeName, value: fakeId };
function fakeProps(): IfParams {
const currentStep: If = {
kind: "_if",
args: {
lhs: "pin0",
op: "is",
rhs: 0,
_then: { kind: "nothing", args: {} },
_else: { kind: "nothing", args: {} }
}
};
return {
currentSequence: fakeTaggedSequence,
currentStep,
dispatch: jest.fn(),
index: 0,
resources: fakeResourceIndex,
2018-03-07 20:42:34 -07:00
shouldDisplay: jest.fn(),
2018-08-30 19:25:58 -06:00
confirmStepDeletion: false,
2019-04-18 16:58:29 -06:00
showPins: true,
2017-12-01 01:47:17 -07:00
};
}
const execute: Execute = { kind: "execute", args: { sequence_id: fakeId } };
describe("seqDropDown()", () => {
it("returns list", () => {
const list = seqDropDown(fakeResourceIndex);
expect(list).toEqual([expectedItem]);
});
});
describe("LHSOptions()", () => {
it("returns positions, peripherals, sensors, pins", () => {
const s = fakeSensor();
const p = fakePeripheral();
s.body.label = "displayed";
p.body.label = "displayed";
const ri = buildResourceIndex([s, p]);
2019-05-15 10:13:17 -06:00
const result = JSON.stringify(LHSOptions(ri.index, true));
expect(result).toContain("displayed");
2019-04-18 16:58:29 -06:00
expect(result).toContain("Pin 25");
});
});
2017-12-01 01:47:17 -07:00
describe("<InnerIf />", () => {
it("renders", () => {
const wrapper = mount(<InnerIf {...fakeProps()} />);
2020-04-30 17:55:14 -06:00
const inputs = wrapper.find("input");
const labels = wrapper.find("label");
const buttons = wrapper.find("button");
expect(inputs.length).toEqual(2);
expect(labels.length).toEqual(5);
expect(buttons.length).toEqual(4);
expect(inputs.first().props().placeholder).toEqual("If ...");
expect(labels.at(0).text()).toEqual("Variable");
expect(buttons.at(0).text()).toEqual("Pin 0");
expect(labels.at(1).text()).toEqual("Operator");
expect(buttons.at(1).text()).toEqual("is");
expect(labels.at(2).text()).toEqual("Value");
expect(inputs.at(1).props().value).toEqual(0);
expect(labels.at(3).text()).toEqual("Then Execute");
expect(buttons.at(2).text()).toEqual("None");
expect(labels.at(4).text()).toEqual("Else Execute");
expect(buttons.at(3).text()).toEqual("None");
2017-12-01 01:47:17 -07:00
});
it("is recursive", () => {
const p = fakeProps();
p.currentStep.args._then = execute;
const wrapper = mount(<InnerIf {...p} />);
2017-12-01 01:47:17 -07:00
expect(wrapper.text()).toContain("Recursive condition");
});
});
describe("IfBlockDropDownHandler()", () => {
2019-02-11 19:44:45 -07:00
const fakeThenElseProps = (thenElseKey: "_then" | "_else"): ThenElseParams => ({
...fakeProps(),
thenElseKey,
});
2017-12-01 01:47:17 -07:00
it("onChange()", () => {
2019-02-11 19:44:45 -07:00
const { onChange } = IfBlockDropDownHandler(fakeThenElseProps("_else"));
2017-12-14 16:03:50 -07:00
2017-12-01 01:47:17 -07:00
onChange(expectedItem);
2017-12-14 16:03:50 -07:00
expect(overwrite).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
body: [{
kind: "_if",
args: expect.objectContaining({ _else: execute })
}]
}));
2017-12-01 01:47:17 -07:00
jest.clearAllMocks();
2017-12-14 16:03:50 -07:00
2017-12-01 01:47:17 -07:00
onChange({ label: "None", value: "" });
2017-12-14 16:03:50 -07:00
expect(overwrite).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
body: [{
kind: "_if",
args: expect.objectContaining({
_else: { kind: "nothing", args: {} }
})
}]
}));
2017-12-01 01:47:17 -07:00
});
it("selectedItem()", () => {
2019-02-11 19:44:45 -07:00
const p = fakeThenElseProps("_then");
2017-12-01 01:47:17 -07:00
p.currentStep.args._then = execute;
2019-02-11 19:44:45 -07:00
const { selectedItem } = IfBlockDropDownHandler(p);
2017-12-01 01:47:17 -07:00
const item = selectedItem();
expect(item).toEqual(expectedItem);
});
it("selectedItem(): null", () => {
2019-02-11 19:44:45 -07:00
const { selectedItem } = IfBlockDropDownHandler(fakeThenElseProps("_then"));
2017-12-01 01:47:17 -07:00
const item = selectedItem();
expect(item).toEqual({ label: "None", value: "" });
});
2019-02-11 19:44:45 -07:00
2019-02-22 19:09:40 -07:00
it("edits variables", () => {
const variables: ParameterApplication = {
kind: "parameter_application",
2019-02-11 19:44:45 -07:00
args: {
label: "label", data_value: {
kind: "coordinate", args: { x: 0, y: 0, z: 0 }
}
}
};
const p = fakeThenElseProps("_then");
const { assignVariable } =
IfBlockDropDownHandler(p);
2019-02-22 19:09:40 -07:00
assignVariable([])(variables);
expect(p.currentStep.args._then.body).toEqual([variables]);
2019-02-11 19:44:45 -07:00
});
2017-12-01 01:47:17 -07:00
});