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

165 lines
4.6 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,
ThenElseParams
2017-12-01 01:47:17 -07:00
} from "../index";
import {
buildResourceIndex, FAKE_RESOURCES
} from "../../../../__test_support__/resource_index_builder";
2019-02-11 19:44:45 -07:00
import { Execute, If, TaggedSequence, VariableDeclaration } from "farmbot";
2017-12-01 01:47:17 -07:00
import { overwrite } from "../../../../api/crud";
2018-08-01 18:20:50 -06:00
import {
fakeSensor, fakePeripheral
} 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;
2017-12-01 01:47:17 -07:00
const fakeId = fakeTaggedSequence && fakeTaggedSequence.body.id || 0;
const fakeName = fakeTaggedSequence && fakeTaggedSequence.body.name || "";
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,
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 and pins", () => {
const s = fakeSensor();
const p = fakePeripheral();
s.body.label = "not displayed";
p.body.label = "not displayed";
const ri = buildResourceIndex([s, p]);
const result = JSON.stringify(LHSOptions(ri.index, () => false));
expect(result).not.toContain("not displayed");
expect(result).toContain("X position");
expect(result).toContain("Pin 25");
});
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]);
const result = JSON.stringify(LHSOptions(ri.index, () => true));
expect(result).toContain("displayed");
});
});
2017-12-01 01:47:17 -07:00
describe("<InnerIf />", () => {
it("renders", () => {
const wrapper = mount(<InnerIf {...fakeProps()} />);
2017-12-01 01:47:17 -07:00
["IF", "THEN", "ELSE"].map(string =>
expect(wrapper.text()).toContain(string));
});
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
it("edits declarations", () => {
const declaration: VariableDeclaration = {
kind: "variable_declaration",
args: {
label: "label", data_value: {
kind: "coordinate", args: { x: 0, y: 0, z: 0 }
}
}
};
const p = fakeThenElseProps("_then");
const { assignVariable } =
IfBlockDropDownHandler(p);
assignVariable([])(declaration);
expect(p.currentStep.args._then.body).toEqual([declaration]);
});
2017-12-01 01:47:17 -07:00
});