app setting: confirm step deletion

This commit is contained in:
gabrielburnworth 2017-10-04 19:05:58 -07:00
parent e115ba3ad5
commit e9e74bc211
5 changed files with 62 additions and 7 deletions

View file

@ -82,7 +82,7 @@ describe("maybeToggleFeature()", () => {
describe("fetchLabFeatures", () => {
it("basically just initializes stuff", () => {
const val = fetchLabFeatures();
expect(val.length).toBe(1);
expect(val.length).toBe(2);
expect(val[0].value).toBeFalsy();
});
});

View file

@ -19,6 +19,13 @@ export const fetchLabFeatures = (): LabsFeature[] => ([
widget from the Controls page.`),
storageKey: BooleanSetting.hideWebcamWidget,
value: false
},
{
name: "Confirm Sequence Step Deletion",
description: trim(`Show a confirmation dialog when the sequence delete step
icon is pressed.`),
storageKey: BooleanSetting.confirmStepDeletion,
value: false
}
].map(fetchRealValue));

View file

@ -0,0 +1,41 @@
const mockStorj: Dictionary<boolean> = {};
jest.mock("../../../session", () => {
return {
Session: {
getBool: (k: string) => {
mockStorj[k] = !!mockStorj[k];
return mockStorj[k];
}
}
};
});
import { Dictionary } from "farmbot";
import { remove } from "../index";
import { fakeSequence } from "../../../__test_support__/fake_state/resources";
import { BooleanSetting } from "../../../session_keys";
describe("remove()", () => {
beforeEach(function () {
jest.clearAllMocks();
});
it("deletes step without confirmation", () => {
const dispatch = jest.fn();
mockStorj[BooleanSetting.confirmStepDeletion] = false;
remove({ index: 0, dispatch, sequence: fakeSequence() });
expect(dispatch).toHaveBeenCalled();
});
it("deletes step with confirmation", () => {
const dispatch = jest.fn();
mockStorj[BooleanSetting.confirmStepDeletion] = true;
remove({ index: 0, dispatch, sequence: fakeSequence() });
expect(dispatch).not.toHaveBeenCalled();
// tslint:disable-next-line:no-any
(global as any).confirm = () => true;
remove({ index: 0, dispatch, sequence: fakeSequence() });
expect(dispatch).toHaveBeenCalled();
});
});

View file

@ -18,6 +18,9 @@ import { CeleryNode, LegalSequenceKind, LegalArgString, If, Execute, Nothing } f
import { TaggedSequence } from "../../resources/tagged_resources";
import { overwrite } from "../../api/crud";
import { TileFindHome } from "./tile_find_home";
import { t } from "i18next";
import { Session } from "../../session";
import { BooleanSetting } from "../../session_keys";
interface MoveParams {
step: Step;
@ -63,12 +66,15 @@ interface RemoveParams {
}
export function remove({ dispatch, index, sequence }: RemoveParams) {
const original = sequence;
const update = defensiveClone(original);
update.body.body = (update.body.body || []);
delete update.body.body[index];
update.body.body = _.compact(update.body.body);
dispatch(overwrite(original, update.body));
if (!Session.getBool(BooleanSetting.confirmStepDeletion) ||
confirm(t("Are you sure you want to delete this step?"))) {
const original = sequence;
const update = defensiveClone(original);
update.body.body = (update.body.body || []);
delete update.body.body[index];
update.body.body = _.compact(update.body.body);
dispatch(overwrite(original, update.body));
}
}
export function updateStep(props: StepInputProps) {

View file

@ -14,6 +14,7 @@ export enum BooleanSetting {
/** "Labs" feature names. */
weedDetector = "weedDetector",
hideWebcamWidget = "hideWebcamWidget",
confirmStepDeletion = "confirmStepDeletion",
}
export enum NumericSetting {