test readability refactoring

pull/579/head
gabrielburnworth 2017-12-14 15:03:50 -08:00
parent b54ea7c1bb
commit a6842ac1d3
24 changed files with 166 additions and 117 deletions

View File

@ -38,8 +38,8 @@ describe("refresh()", () => {
const { mock } = dispatch;
thunk(dispatch);
setImmediate(() => {
expect(mock.calls.length).toEqual(2);
// Test call to refesh();
expect(dispatch).toHaveBeenCalledTimes(2);
// Test call to refresh();
const firstCall = mock.calls[0][0];
const dispatchAction1 = get(firstCall, "type", "NO TYPE FOUND");
expect(dispatchAction1).toBe(Actions.REFRESH_RESOURCE_START);

View File

@ -45,7 +45,9 @@ describe("handleCreateOrUpdate", () => {
const result = handleCreateOrUpdate(dispatch, getState, myPayload);
expect(result).toBe(undefined);
expect(dispatch).toHaveBeenCalled();
expect(dispatch.mock.calls[0][0].type).toBe(Actions.INIT_RESOURCE);
expect(dispatch).toHaveBeenCalledWith(expect.objectContaining({
type: Actions.INIT_RESOURCE
}));
});
it("ignores local echo", () => {
@ -71,7 +73,9 @@ describe("handleCreateOrUpdate", () => {
myPayload.kind = "Sequence";
handleCreateOrUpdate(dispatch, getState, myPayload);
expect(dispatch).toHaveBeenCalled();
expect(dispatch.mock.calls[0][0].type).toBe(Actions.OVERWRITE_RESOURCE);
expect(dispatch).toHaveBeenCalledWith(expect.objectContaining({
type: Actions.OVERWRITE_RESOURCE
}));
});
});

View File

@ -149,10 +149,9 @@ describe("changeLastClientConnected", () => {
const setUserEnv = jest.fn();
const fakeFarmbot = { setUserEnv: setUserEnv as any } as Farmbot;
changeLastClientConnected(fakeFarmbot)();
expect(fakeFarmbot.setUserEnv)
.toHaveBeenCalled();
expect(Object.keys(setUserEnv.mock.calls[0][0]))
.toContain("LAST_CLIENT_CONNECTED");
expect(setUserEnv).toHaveBeenCalledWith(expect.objectContaining({
"LAST_CLIENT_CONNECTED": expect.any(String)
}));
});
});

View File

@ -13,7 +13,7 @@ describe("<ToggleButton/>", function () {
toggleValue={0}
toggleAction={() => toggle()} />);
toggleButton.simulate("click");
expect(toggle.mock.calls.length).toEqual(1);
expect(toggle).toHaveBeenCalledTimes(1);
});
it("displays no", () => {

View File

@ -43,6 +43,6 @@ describe("<PeripheralForm/>", function () {
expect(inputs.at(3).props().value).toEqual("13");
inputs.at(3).simulate("change");
buttons.at(1).simulate("click");
expect(dispatch.mock.calls.length).toEqual(6);
expect(dispatch).toHaveBeenCalledTimes(6);
});
});

View File

@ -7,12 +7,12 @@ describe("<LockableButton/>", () => {
const fakeCB = jest.fn();
const btn = mount(<LockableButton disabled={true} onClick={fakeCB} />);
btn.simulate("click");
expect(fakeCB.mock.calls.length).toEqual(0);
expect(fakeCB).not.toHaveBeenCalled();
});
it("does trigger callback when clicked and enabled", () => {
const fakeCB = jest.fn();
const btn = mount(<LockableButton disabled={false} onClick={fakeCB} />);
btn.simulate("click");
expect(fakeCB.mock.calls.length).toEqual(1);
expect(fakeCB).toHaveBeenCalled();
});
});

View File

@ -15,7 +15,6 @@ jest.mock("farmbot-toastr", () => ({
import * as React from "react";
import { mount, shallow } from "enzyme";
import { BoardType } from "../board_type";
import { getDevice } from "../../../../device";
describe("<BoardType/>", () => {
it("Farmduino", () => {
@ -49,11 +48,11 @@ describe("<BoardType/>", () => {
});
it("calls updateConfig", () => {
const updateConfig = getDevice().updateConfig as jest.Mock<{}>;
const wrapper = shallow(<BoardType
firmwareVersion={"Arduino Disconnected!"} />);
wrapper.find("FBSelect").simulate("change",
{ label: "firmware_hardware", value: "farmduino" });
expect(updateConfig).toBeCalledWith({ firmware_hardware: "farmduino" });
expect(mockDevice.updateConfig)
.toBeCalledWith({ firmware_hardware: "farmduino" });
});
});

View File

@ -24,13 +24,18 @@ describe("movePlant", () => {
gridSize: { x: 3000, y: 1500 }
};
movePlant(payload);
const [argList] = (edit as jest.Mock<{}>).mock.calls;
const oldPlant = argList[0];
expect(oldPlant.body.x).toBe(100);
expect(oldPlant.body.y).toBe(200);
const update = argList[1];
expect(update.x).toBe(expected.x);
expect(update.y).toBe(expected.y);
expect(edit).toHaveBeenCalledWith(
// Old plant
expect.objectContaining({
body: expect.objectContaining({
x: 100, y: 200
})
}),
// Update
expect.objectContaining({
x: expected.x, y: expected.y
})
);
});
}
movePlantTest("within bounds", { x: 1, y: 2 }, { x: 101, y: 202 });

View File

@ -43,7 +43,7 @@ describe("<FarmEventForm/>", () => {
const i = instance(p);
expect(i.dispatch).toBe(p.dispatch);
i.dispatch();
expect((p.dispatch as jest.Mock<{}>).mock.calls.length).toBe(1);
expect(p.dispatch).toHaveBeenCalledTimes(1);
});
it("has a view model", () => {

View File

@ -21,7 +21,7 @@ describe("<PlantPanel/>", () => {
expect(txt).toContain("1 days old");
expect(txt).toContain("(10, 30)");
el.find("button").first().simulate("click");
expect(onDestroy.mock.calls.length).toEqual(1);
expect(onDestroy).toHaveBeenCalledTimes(1);
});
it("renders", () => {

View File

@ -10,7 +10,6 @@ jest.mock("../../device", () => ({
import * as React from "react";
import { mount, shallow } from "enzyme";
import { FarmwareForms } from "../farmware_forms";
import { getDevice } from "../../device";
import { fakeFarmwares } from "../../__test_support__/fake_farmwares";
describe("<FarmwareForms/>", () => {
@ -35,29 +34,27 @@ describe("<FarmwareForms/>", () => {
});
it("runs", () => {
const runFarmware = getDevice().execScript as jest.Mock<{}>;
const wrapper = mount(<FarmwareForms
farmwares={fakeFarmwares()}
user_env={{}} />);
const run = wrapper.find("button").first();
run.simulate("click");
expect(run.text()).toEqual("Run");
const argsList = runFarmware.mock.calls[0];
expect(argsList[0]).toEqual("My Farmware");
const pairs = argsList[1][0];
expect(pairs.kind).toEqual("pair");
expect(pairs.args)
.toEqual({ "label": "my_farmware_config_1", "value": "4" });
expect(mockDevice.execScript).toHaveBeenCalledWith(
"My Farmware", [{
kind: "pair",
args: { label: "my_farmware_config_1", value: "4" }
}]
);
});
it("sets env", () => {
const setUserEnv = getDevice().setUserEnv;
const wrapper = shallow(<FarmwareForms
farmwares={fakeFarmwares()}
user_env={{}} />);
const input = wrapper.find("BlurableInput").first();
input.simulate("commit", { currentTarget: { value: "changed value" } });
expect(setUserEnv).toBeCalledWith({
expect(mockDevice.setUserEnv).toBeCalledWith({
"my_farmware_config_1": "changed value"
});
});

View File

@ -27,7 +27,7 @@ describe("resend_verification.tsx - failure case", () => {
el.find("button").last().simulate("click");
const { calls } = props.no.mock;
setImmediate(() => {
expect(props.ok.mock.calls.length).toEqual(0);
expect(props.ok).not.toHaveBeenCalled();
expect(calls.length).toEqual(1);
expect(get(calls[0][0], "err", "NOT FOUND")).toEqual("hi");
done();

View File

@ -14,7 +14,7 @@ import { API } from "../../api/index";
describe("resend_verification.tsx - base case", () => {
API.setBaseUrl("http://localhost:3000");
let props = () => ({
const props = () => ({
ok: jest.fn(),
no: jest.fn(),
onGoBack: jest.fn(),
@ -25,10 +25,9 @@ describe("resend_verification.tsx - base case", () => {
const p = props();
const el = mount(<ResendVerification {...p } />);
el.find("button").first().simulate("click");
const { calls } = p.onGoBack.mock;
expect(p.no.mock.calls.length).toEqual(0);
expect(p.ok.mock.calls.length).toEqual(0);
expect(calls.length).toEqual(1);
expect(p.no).not.toHaveBeenCalled();
expect(p.ok).not.toHaveBeenCalled();
expect(p.onGoBack).toHaveBeenCalledTimes(1);
});
it("fires the `ok()` callback", (done) => {
@ -38,7 +37,7 @@ describe("resend_verification.tsx - base case", () => {
el.find("button").last().simulate("click");
const { calls } = p.ok.mock;
setImmediate(() => {
expect(p.no.mock.calls.length).toEqual(0);
expect(p.no).not.toHaveBeenCalled();
expect(calls.length).toEqual(1);
expect(get(calls[0][0], "data", "NOT FOUND")).toEqual("whatever");
done();

View File

@ -63,15 +63,15 @@ describe("commitBulkEditor()", () => {
state.resources.consumers.regimens.dailyOffsetMs = 2000;
state.resources.consumers.regimens.weeks = [{
days:
{
day1: true,
day2: false,
day3: false,
day4: false,
day5: false,
day6: false,
day7: false
}
{
day1: true,
day2: false,
day3: false,
day4: false,
day5: false,
day6: false,
day7: false
}
}];
return state;
}
@ -113,10 +113,16 @@ describe("commitBulkEditor()", () => {
const getState = () => state;
const dispatch = jest.fn();
commitBulkEditor()(dispatch, getState);
const argsList = dispatch.mock.calls[0][0];
expect(argsList.type).toEqual(Actions.OVERWRITE_RESOURCE);
expect(argsList.payload.update.regimen_items[1])
.toEqual({ sequence_id: 1, time_offset: 2000 });
expect(dispatch).toHaveBeenCalledWith({
payload: expect.objectContaining({
update: expect.objectContaining({
regimen_items: [
{ id: 1, regimen_id: 1, sequence_id: 1, time_offset: 1000 },
{ sequence_id: 1, time_offset: 2000 }]
}),
}),
type: Actions.OVERWRITE_RESOURCE
});
expect(mockErr).not.toHaveBeenCalled();
});
});

View File

@ -4,6 +4,7 @@ import { ActiveEditor } from "../active_editor";
import { fakeRegimen } from "../../../__test_support__/fake_state/resources";
import { ActiveEditorProps } from "../interfaces";
import { Actions } from "../../../constants";
import { SpecialStatus } from "../../../resources/tagged_resources";
describe("<ActiveEditor />", () => {
const props: ActiveEditorProps = {
@ -37,11 +38,9 @@ describe("<ActiveEditor />", () => {
wrapper.find("i").simulate("click");
expect(props.dispatch).toHaveBeenCalledWith({
payload: {
update: {
color: "red", name: "Foo", regimen_items: []
},
uuid: "Regimen.1.18",
specialStatus: "DIRTY"
update: expect.objectContaining({ regimen_items: [] }),
uuid: expect.stringContaining("Regimen"),
specialStatus: SpecialStatus.DIRTY
},
type: Actions.OVERWRITE_RESOURCE
});

View File

@ -9,6 +9,7 @@ import { mount } from "enzyme";
import { CopyButton } from "../copy_button";
import { fakeRegimen } from "../../../__test_support__/fake_state/resources";
import { SpecialStatus } from "../../../resources/tagged_resources";
import { Actions } from "../../../constants";
describe("Copy button", () => {
@ -18,13 +19,17 @@ describe("Copy button", () => {
const el = mount(<CopyButton dispatch={dispatch} regimen={regimen} />);
expect(el.find("button").length).toBe(1);
el.simulate("click");
expect(dispatch.mock.calls.length).toBe(1);
const action = dispatch.mock.calls[0][0];
expect(typeof action).toEqual("object");
expect(action.type).toEqual("INIT_RESOURCE");
const reg = action.payload.body;
expect(action.payload.specialStatus).toBe(SpecialStatus.DIRTY);
expect(reg.name).toContain("Foo copy");
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({
payload: expect.objectContaining({
body: expect.objectContaining({
name: expect.stringContaining("Foo copy")
}),
specialStatus: SpecialStatus.DIRTY,
kind: "Regimen"
}),
type: Actions.INIT_RESOURCE
});
expect(mockPush).toHaveBeenCalledWith("/app/regimens/foo_copy_1");
});
@ -34,7 +39,7 @@ describe("Copy button", () => {
const el = mount(<CopyButton dispatch={dispatch} regimen={regimen} />);
expect(el.find("button").length).toBe(1);
el.simulate("click");
expect(dispatch.mock.calls.length).toBe(1);
expect(dispatch).toHaveBeenCalledTimes(1);
});
it("renders nothing if not given a regimen", () => {
@ -42,7 +47,7 @@ describe("Copy button", () => {
const el = mount(<CopyButton dispatch={dispatch} />);
expect(el.find("button").length).toBe(0);
el.simulate("click");
expect(dispatch.mock.calls.length).toBe(0);
expect(dispatch).not.toHaveBeenCalled();
});
});

View File

@ -5,6 +5,7 @@ import * as React from "react";
import { AddRegimen } from "../add_button";
import { AddRegimenProps } from "../../interfaces";
import { shallow } from "enzyme";
import { Actions } from "../../../constants";
describe("<AddRegimen/>", () => {
function btn(props: AddRegimenProps) {
@ -21,13 +22,15 @@ describe("<AddRegimen/>", () => {
it("dispatches a new regimen onclick", () => {
const dispatch = jest.fn();
const b = btn({ dispatch, length });
expect(mockPush.mock.calls.length).toBe(0);
expect(mockPush).not.toHaveBeenCalled();
b.find("button").simulate("click");
expect(dispatch.mock.calls.length).toEqual(1);
const action = dispatch.mock.calls[0][0];
expect(action.type).toEqual("INIT_RESOURCE");
expect(action.payload).toBeTruthy();
expect(action.payload.kind).toEqual("Regimen");
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({
type: Actions.INIT_RESOURCE,
payload: expect.objectContaining({
kind: "Regimen"
})
});
});
it("has children (or defaults)");

View File

@ -21,8 +21,11 @@ describe("copySequence()", () => {
const sequence = fakeSequence();
const copy = copySequence(sequence);
copy(jest.fn(), fakeState);
const args = mockInit.mock.calls[0][0];
expect(args.body.name).toEqual("fake copy 1");
expect(mockInit).toHaveBeenCalledWith(expect.objectContaining({
body: expect.objectContaining({
name: "fake copy 1"
})
}));
});
it("updates current path", () => {

View File

@ -47,12 +47,14 @@ describe("<SequenceEditorMiddleActive/>", () => {
it("deletes", () => {
clickButton(2, "Delete");
expect(destroy).toHaveBeenCalledWith("Sequence.0.17");
expect(destroy).toHaveBeenCalledWith(expect.stringContaining("Sequence"));
});
it("copies", () => {
clickButton(3, "Copy");
expect(mockCopy.mock.calls[0][0].uuid).toEqual("Sequence.1.35");
expect(mockCopy).toHaveBeenCalledWith(expect.objectContaining({
uuid: expect.stringContaining("Sequence")
}));
});
it("has drag area", () => {
@ -72,9 +74,10 @@ describe("onDrop()", () => {
dispatch.mock.calls[0][0](() => {
return { value: 1, intent: "step_splice", draggerId: 2 };
});
const argsList = mockSplice.mock.calls[0][0];
expect(argsList.step).toEqual(1);
expect(argsList.index).toEqual(0);
expect(mockSplice).toHaveBeenCalledWith(expect.objectContaining({
step: 1,
index: 0
}));
});
it("step_move", () => {
@ -83,10 +86,11 @@ describe("onDrop()", () => {
dispatch.mock.calls[0][0](() => {
return { value: 4, intent: "step_move", draggerId: 5 };
});
const argsList = mockMove.mock.calls[0][0];
expect(argsList.step).toEqual(4);
expect(argsList.to).toEqual(3);
expect(argsList.from).toEqual(5);
expect(mockMove).toHaveBeenCalledWith(expect.objectContaining({
step: 4,
to: 3,
from: 5
}));
});
it("not a valid step object", () => {

View File

@ -5,6 +5,7 @@ import { mount } from "enzyme";
import { TaggedSequence, SpecialStatus } from "../../../resources/tagged_resources";
import { MoveAbsolute } from "farmbot/dist";
import { Wrapper } from "../../../__test_support__/wrapper";
import { Actions } from "../../../constants";
describe("<InputDefault/>", () => {
it("updates the step", () => {
@ -59,11 +60,16 @@ describe("<InputDefault/>", () => {
const input = c.find("input").first();
input.simulate("change");
input.simulate("blur");
expect(dispatcher.mock.calls.length).toEqual(1);
const action = dispatcher.mock.calls[0][0];
const { payload } = action;
expect(action.type).toEqual("OVERWRITE_RESOURCE");
expect(payload.uuid).toContain("Sequence");
expect(payload.update.name).toEqual(tr.body.name);
expect(dispatcher).toHaveBeenCalledTimes(1);
expect(dispatcher).toHaveBeenCalledWith({
type: Actions.OVERWRITE_RESOURCE,
payload: expect.objectContaining({
uuid: expect.stringContaining("Sequence"),
update: expect.objectContaining({
name: tr.body.name
})
})
});
});
});

View File

@ -3,8 +3,7 @@ import { StepButtonParams } from "../../interfaces";
import { StepButton } from "../index";
import { shallow } from "enzyme";
import { fakeSequence } from "../../../__test_support__/fake_state/resources";
import { ReduxAction } from "../../../redux/interfaces";
import { EditResourceParams } from "../../../api/interfaces";
import { Actions } from "../../../constants";
describe("<StepButton/>", () => {
function props(): StepButtonParams {
@ -25,15 +24,13 @@ describe("<StepButton/>", () => {
const p = props();
const el = shallow<StepButtonParams>(<StepButton {...p } />);
el.find("button").simulate("click");
let action: ReduxAction<EditResourceParams>;
action = (p.dispatch as jest.Mock<{}>).mock.calls[0][0];
expect(action).toBeTruthy();
expect(action.type).toBe("OVERWRITE_RESOURCE");
if (p.current && p.current.body.body) {
// tslint:disable-next-line:no-any
expect((action.payload.update as any).body[0]).toMatchObject(p.step);
} else {
fail("No");
}
expect(p.dispatch).toHaveBeenCalledWith({
payload: expect.objectContaining({
update: expect.objectContaining({
body: [p.step]
}),
}),
type: Actions.OVERWRITE_RESOURCE
});
});
});

View File

@ -44,10 +44,13 @@ describe("<RefactoredExecuteBlock />", () => {
// tslint:disable-next-line:no-any
const instance = block.instance() as any;
instance.changeSelection({ label: "", value: 10 });
expect(dispatch).toHaveBeenCalledWith(expect.objectContaining({
type: Actions.OVERWRITE_RESOURCE
}));
const { payload } = dispatch.mock.calls[0][0];
expect(payload.update.body[0].args.sequence_id).toEqual(10);
expect(dispatch).toHaveBeenCalledWith({
type: Actions.OVERWRITE_RESOURCE,
payload: expect.objectContaining({
update: expect.objectContaining({
body: [{ kind: "execute", args: { sequence_id: 10 } }]
})
})
});
});
});

View File

@ -75,14 +75,30 @@ describe("<InnerIf />", () => {
describe("IfBlockDropDownHandler()", () => {
it("onChange()", () => {
const { onChange } = IfBlockDropDownHandler(fakeProps(), "_else");
onChange(expectedItem);
const [argsList1] = (overwrite as jest.Mock).mock.calls;
expect(argsList1[1].body[0].args._else).toEqual(execute);
expect(overwrite).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
body: [{
kind: "_if",
args: expect.objectContaining({ _else: execute })
}]
}));
jest.clearAllMocks();
onChange({ label: "None", value: "" });
const [argsList2] = (overwrite as jest.Mock).mock.calls;
expect(argsList2[1].body[0].args._else)
.toEqual({ kind: "nothing", args: {} });
expect(overwrite).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
body: [{
kind: "_if",
args: expect.objectContaining({
_else: { kind: "nothing", args: {} }
})
}]
}));
});
it("selectedItem()", () => {

View File

@ -3,6 +3,8 @@ import { ToolBayForm } from "../toolbay_form";
import { mount } from "enzyme";
import { mapStateToProps } from "../../state_to_props";
import { fakeState } from "../../../__test_support__/fake_state";
import { Actions } from "../../../constants";
import { SpecialStatus } from "../../../resources/tagged_resources";
describe("<ToolBayForm/>", () => {
function bootstrapTest() {
@ -40,10 +42,12 @@ describe("<ToolBayForm/>", () => {
const buttons = test.component.find("button");
expect(buttons.length).toEqual(6);
buttons.at(3).simulate("click");
const argList = test.dispatch.mock.calls[0][0].payload.update;
expect(argList.x).toEqual(1);
expect(argList.y).toEqual(2);
expect(argList.z).toEqual(3);
expect(test.dispatch).toHaveBeenCalledWith({
type: Actions.EDIT_RESOURCE,
payload: expect.objectContaining({
update: { x: 1, y: 2, z: 3 }
})
});
});
});