Add test for <EmptyEditor/>

This commit is contained in:
Rick Carlino 2017-09-28 10:12:05 -05:00
parent 73756b42c1
commit 5d41fafce4
4 changed files with 115 additions and 1 deletions

View file

@ -0,0 +1,73 @@
const mockStorj: Dictionary<boolean> = {};
jest.mock("../../../session", () => {
return {
Session: {
getBool: (k: string) => {
mockStorj[k] = !!mockStorj[k];
return mockStorj[k];
},
invertBool: (k: string) => {
console.log(`${k} will go from ${mockStorj[k]} to ${!mockStorj[k]}`);
mockStorj[k] = !mockStorj[k];
return mockStorj[k];
}
}
};
});
import { Dictionary } from "farmbot";
import { fetchLabFeatures, maybeToggleFeature, LabsFeature } from "../labs_features_list_data";
import { BooleanSetting } from "../../../session_keys";
describe("maybeToggleFeature()", () => {
it("returns `undefined` without consent", () => {
Object.defineProperty(global, "confirm", () => false);
const data: LabsFeature = {
name: "Example",
value: false,
description: "I stub this.",
storageKey: BooleanSetting.weedDetector
};
const out = maybeToggleFeature(data);
expect(data.value).toBeFalsy();
expect(out).toBeUndefined();
});
it("updates a `LabsFeature` with consent", () => {
// tslint:disable-next-line:no-any
(global as any).confirm = () => true;
const data: LabsFeature = {
name: "Example1",
value: (mockStorj[BooleanSetting.weedDetector] = false),
description: "I stub this.",
storageKey: BooleanSetting.weedDetector
};
const out = maybeToggleFeature(data);
out ?
expect(out.value).toBeTruthy() : fail("out === undefined. Thats bad");
expect(out).toBeTruthy();
});
it("Does not require consent when going from true to false", () => {
const conf = jest.fn(() => true);
Object.defineProperty(global, "confirm", conf);
const output = maybeToggleFeature({
name: "Example",
value: (mockStorj[BooleanSetting.weedDetector] = true),
description: "I stub this.",
storageKey: BooleanSetting.weedDetector
});
expect(conf).not.toHaveBeenCalled();
output ?
expect(output.value).toBeFalsy() : fail("`output` should be defined.");
});
});
describe("fetchLabFeatures", () => {
it("basically just initializes stuff", () => {
const val = fetchLabFeatures();
expect(val.length).toBe(1);
expect(val[0].value).toBeFalsy();
});
});

View file

@ -0,0 +1,26 @@
const mocks = {
"maybeToggleFeature": jest.fn(),
"fetchLabFeatures": jest.fn(() => ([
{
name: "Weed Detection",
description: "Plots points of weeds in the garden.",
storageKey: "weedDetector",
value: false
}
]))
};
jest.mock("../labs_features_list_data", () => mocks);
import * as React from "react";
import { mount } from "enzyme";
import { LabsFeatures } from "../labs_features";
describe("<LabsFeatures/>", () => {
it("triggers the correct callback on click", () => {
const el = mount(<LabsFeatures />);
expect(mocks.fetchLabFeatures.mock.calls.length).toBeGreaterThan(0);
el.find("button").simulate("click");
expect(mocks.maybeToggleFeature.mock.calls.length).toBeGreaterThan(0);
});
});

View file

@ -0,0 +1,14 @@
import * as React from "react";
import { shallow } from "enzyme";
import { EmptyEditor, IMAGE_PATH } from "../empty_editor";
import { Content } from "../../../constants";
describe("EmptyEditor", () => {
it("renders an SVG image on an empty page.", () => {
const el = shallow(<EmptyEditor />);
const img = el.find("img");
expect(img.length).toBe(1);
expect(img.props().src).toBe(IMAGE_PATH);
expect(el.text()).toContain(Content.NO_REGIMEN_SELECTED);
});
});

View file

@ -2,6 +2,7 @@ import * as React from "react";
import { t } from "i18next";
import { Content } from "../../constants";
export const IMAGE_PATH = "/app-resources/img/empty-state-regimen.svg";
/** The bottom half of the regimen editor panel (when no Regimen is selected). */
export function EmptyEditor({ }: {}) {
return <div>
@ -10,6 +11,6 @@ export function EmptyEditor({ }: {}) {
</p>
<img
className="empty-state-graphic"
src="/app-resources/img/empty-state-regimen.svg" />
src={IMAGE_PATH} />
</div>;
}