Farmbot-Web-App/frontend/regimens/bulk_scheduler/__tests__/actions_test.ts

178 lines
5.8 KiB
TypeScript
Raw Normal View History

2017-07-21 13:06:16 -06:00
jest.mock("i18next", () => ({ t: (i: string) => i }));
2019-02-22 19:09:40 -07:00
jest.mock("../../../api/crud", () => ({ overwrite: jest.fn() }));
2019-06-06 17:54:48 -06:00
import {
2020-02-28 09:35:32 -07:00
commitBulkEditor, setTimeOffset, toggleDay, setSequence,
2019-06-06 17:54:48 -06:00
} from "../actions";
2017-06-29 12:54:02 -06:00
import { fakeState } from "../../../__test_support__/fake_state";
2019-06-06 17:54:48 -06:00
import {
2020-02-28 09:35:32 -07:00
buildResourceIndex,
2019-06-06 17:54:48 -06:00
} from "../../../__test_support__/resource_index_builder";
import {
2020-02-28 09:35:32 -07:00
TaggedResource, TaggedSequence, TaggedRegimen, Coordinate,
2019-06-06 17:54:48 -06:00
} from "farmbot";
2017-10-13 21:25:05 -06:00
import { Actions } from "../../../constants";
import { Everything } from "../../../interfaces";
import { ToggleDayParams } from "../interfaces";
2018-10-30 15:15:49 -06:00
import { newTaggedResource } from "../../../sync/actions";
2018-10-30 15:32:17 -06:00
import { arrayUnwrap } from "../../../resources/util";
2019-02-22 19:09:40 -07:00
import { overwrite } from "../../../api/crud";
import { fakeVariableNameSet } from "../../../__test_support__/fake_variables";
2019-06-24 15:39:49 -06:00
import { error, warning } from "../../../toast/toast";
2020-04-23 13:12:04 -06:00
import { newWeek } from "../../reducer";
2017-06-29 12:54:02 -06:00
2018-11-24 09:40:14 -07:00
const sequence_id = 23;
const regimen_id = 32;
2017-06-29 12:54:02 -06:00
describe("commitBulkEditor()", () => {
2017-10-13 21:25:05 -06:00
function newFakeState() {
const state = fakeState();
2018-11-24 09:40:14 -07:00
const regBody: TaggedRegimen["body"] = {
2019-06-06 17:54:48 -06:00
id: regimen_id,
name: "Test Regimen",
color: "gray",
regimen_items: [
2020-02-28 09:35:32 -07:00
{ regimen_id, sequence_id, time_offset: 1000 },
2019-01-10 20:10:55 -07:00
],
body: [],
2018-11-24 09:40:14 -07:00
};
const reg = newTaggedResource("Regimen", regBody)[0];
const seqBody: TaggedSequence["body"] = {
2019-06-06 17:54:48 -06:00
id: sequence_id,
name: "Test Sequence",
color: "gray",
2019-12-17 13:33:47 -07:00
folder_id: undefined,
2019-06-06 17:54:48 -06:00
body: [{ kind: "wait", args: { milliseconds: 100 } }],
args: { "locals": { kind: "scope_declaration", args: {} }, "version": 4 },
kind: "sequence"
2018-11-24 09:40:14 -07:00
};
const seq = arrayUnwrap(newTaggedResource("Sequence", seqBody));
2018-11-25 11:45:22 -07:00
const regimenUuid = reg.uuid;
const sequenceUuid = seq.uuid;
2018-11-24 09:40:14 -07:00
const fakeResources: TaggedResource[] = [reg, seq];
2017-10-13 21:25:05 -06:00
state.resources.index = buildResourceIndex(fakeResources).index;
state.resources.consumers.regimens.currentRegimen = regimenUuid;
state.resources.consumers.regimens.selectedSequenceUUID = sequenceUuid;
state.resources.consumers.regimens.dailyOffsetMs = 2000;
2020-04-23 13:12:04 -06:00
const week = newWeek();
week.days.day1 = true;
state.resources.consumers.regimens.weeks = [week];
2017-10-13 21:25:05 -06:00
return state;
}
2018-05-07 10:18:16 -06:00
function returnsError(state: Everything, message: string, title?: string) {
2017-10-13 21:25:05 -06:00
const getState = () => state;
const dispatch = jest.fn();
commitBulkEditor()(dispatch, getState);
expect(dispatch).not.toHaveBeenCalled();
2018-05-07 10:18:16 -06:00
if (title) {
expect(error).toBeCalledWith(message, title);
2018-05-07 10:18:16 -06:00
} else {
expect(error).toBeCalledWith(message);
2018-05-07 10:18:16 -06:00
}
2017-10-13 21:25:05 -06:00
}
2017-06-29 12:54:02 -06:00
it("does nothing if no regimen is selected", () => {
2017-10-13 21:25:05 -06:00
const state = newFakeState();
state.resources.consumers.regimens.currentRegimen = undefined;
returnsError(state, "Select a regimen first or create one.");
});
it("does nothing if no sequence is selected", () => {
const state = newFakeState();
state.resources.consumers.regimens.selectedSequenceUUID = undefined;
2018-05-07 10:18:16 -06:00
returnsError(state,
2019-07-02 11:03:16 -06:00
"Select a sequence from the dropdown first.");
2017-10-13 21:25:05 -06:00
});
it("does nothing if no days are selected", () => {
const state = newFakeState();
state.resources.consumers.regimens.weeks[0].days.day1 = false;
returnsError(state, "No day(s) selected.");
});
it("does nothing if no weeks", () => {
const state = newFakeState();
state.resources.consumers.regimens.weeks = [];
returnsError(state, "No day(s) selected.");
});
2018-11-25 12:05:21 -07:00
it("adds items", () => {
2019-06-06 17:54:48 -06:00
console.log = jest.fn();
2017-10-13 21:25:05 -06:00
const state = newFakeState();
const getState = () => state;
2017-08-28 05:49:13 -06:00
const dispatch = jest.fn();
2017-08-02 09:14:08 -06:00
commitBulkEditor()(dispatch, getState);
2018-11-24 09:40:14 -07:00
const expected = [
2018-11-25 12:05:21 -07:00
{ regimen_id, sequence_id, time_offset: 1000 },
2020-02-28 09:35:32 -07:00
{ sequence_id, time_offset: 2000 },
2018-11-24 09:40:14 -07:00
];
2019-02-22 19:09:40 -07:00
expect(overwrite).toHaveBeenCalledWith(expect.any(Object),
expect.objectContaining({
regimen_items: expect.arrayContaining(expected)
}));
expect(error).not.toHaveBeenCalled();
});
it("merges variables", () => {
2019-06-06 17:54:48 -06:00
console.log = jest.fn();
2019-02-22 19:09:40 -07:00
const state = newFakeState();
const seqUUID = state.resources.consumers.regimens.selectedSequenceUUID;
const label = "variable_label";
const varData = fakeVariableNameSet(label);
const variable = varData[label];
const COORDINATE: Coordinate =
({ kind: "coordinate", args: { x: 0, y: 0, z: 0 } });
variable && (variable.celeryNode = {
kind: "parameter_declaration",
args: { label, default_value: COORDINATE }
2017-12-14 16:03:50 -07:00
});
2019-02-22 19:09:40 -07:00
state.resources.index.sequenceMetas[seqUUID || ""] = varData;
const dispatch = jest.fn();
commitBulkEditor()(dispatch, () => state);
expect(overwrite).toHaveBeenCalledWith(expect.any(Object),
expect.objectContaining({
body: [{
kind: "variable_declaration",
args: { label, data_value: COORDINATE }
}]
}));
expect(error).not.toHaveBeenCalled();
2017-10-13 21:25:05 -06:00
});
});
describe("setTimeOffset()", () => {
it("returns action", () => {
const action = setTimeOffset(100);
expect(action).toEqual({ payload: 100, type: Actions.SET_TIME_OFFSET });
});
it("throws error for NaN", () => {
expect(() => setTimeOffset(NaN))
.toThrowError("Bad time input on regimen page: null");
expect(warning).toBeCalledWith(
2017-10-13 21:25:05 -06:00
"Time is not properly formatted.", "Bad Input");
});
});
describe("toggleDay()", () => {
it("returns action", () => {
const params: ToggleDayParams = { week: 0, day: 0 };
const action = toggleDay(params);
expect(action).toEqual({
payload: { day: 0, week: 0 },
type: Actions.TOGGLE_DAY
});
});
});
describe("setSequence()", () => {
it("returns action", () => {
2017-10-27 07:31:25 -06:00
const action = setSequence("Sequence");
2017-10-13 21:25:05 -06:00
expect(action).toEqual({
2017-10-27 07:31:25 -06:00
payload: "Sequence",
2017-10-13 21:25:05 -06:00
type: Actions.SET_SEQUENCE
});
2017-06-29 12:54:02 -06:00
});
});