misc tests

This commit is contained in:
gabrielburnworth 2018-08-01 19:03:16 -07:00
parent 75a780a033
commit 639d82d7c0
5 changed files with 104 additions and 14 deletions

View file

@ -25,6 +25,7 @@ describe("<CreatePoints />", () => {
currentPoint: undefined
};
};
it("renders", () => {
const wrapper = mount(<CreatePoints {...fakeProps()} />);
["create point", "cancel", "delete", "x", "y", "radius", "color"]
@ -93,4 +94,22 @@ describe("<CreatePoints />", () => {
type: Actions.SET_CURRENT_POINT_DATA
});
});
it("fills the state with point data", () => {
const p = fakeProps();
p.currentPoint = { cx: 1, cy: 2, r: 3, color: "blue" };
const wrapper = shallow(<CreatePoints {...p} />);
// tslint:disable-next-line:no-any
const instance = wrapper.instance() as any;
instance.getPointData();
expect(instance.state).toEqual({ color: "blue", cx: 1, cy: 2, r: 3 });
});
it("fills the state with default data", () => {
const wrapper = shallow(<CreatePoints {...fakeProps()} />);
// tslint:disable-next-line:no-any
const instance = wrapper.instance() as any;
instance.getPointData();
expect(instance.state).toEqual({ color: "green", cx: 0, cy: 0, r: 1 });
});
});

View file

@ -2,20 +2,47 @@ jest.mock("react-redux", () => ({
connect: jest.fn()
}));
jest.mock("../../../history", () => ({ history: { push: jest.fn() } }));
import * as React from "react";
import { CropCatalog } from "../crop_catalog";
import { mount } from "enzyme";
import { mount, shallow } from "enzyme";
import { CropCatalogProps } from "../../interfaces";
import { Actions } from "../../../constants";
import { history } from "../../../history";
describe("<CropCatalog />", () => {
const fakeProps = (): CropCatalogProps => {
return {
dispatch: jest.fn(),
OFSearch: jest.fn(),
cropSearchResults: [],
cropSearchQuery: "",
};
};
it("renders", () => {
const wrapper = mount(
<CropCatalog
OFSearch={jest.fn()}
dispatch={jest.fn()}
cropSearchResults={[]}
cropSearchQuery={""} />);
const wrapper = mount(<CropCatalog {...fakeProps()} />);
expect(wrapper.text()).toContain("Choose a crop");
expect(wrapper.find("input").props().placeholder)
.toEqual("Search OpenFarm...");
});
it("handles search term change", () => {
const p = fakeProps();
const wrapper = shallow(<CropCatalog {...p} />);
wrapper.find("input").first().simulate("change", {
currentTarget: { value: "apple" }
});
expect(p.dispatch).toHaveBeenCalledWith({
payload: "apple",
type: Actions.SEARCH_QUERY_CHANGE
});
});
it("goes back", () => {
const wrapper = mount(<CropCatalog {...fakeProps()} />);
wrapper.find("i").first().simulate("click");
expect(history.push).toHaveBeenCalledWith("/app/designer/plants");
});
});

View file

@ -0,0 +1,18 @@
import { mapStateToProps } from "../map_state_to_props";
import { fakeState } from "../../../__test_support__/fake_state";
import { buildResourceIndex } from "../../../__test_support__/resource_index_builder";
import { fakePlant } from "../../../__test_support__/fake_state/resources";
describe("mapStateToProps()", () => {
it("returns findPlant()", () => {
const state = fakeState();
const plant = fakePlant();
plant.body.id = 10;
state.resources = buildResourceIndex([plant]);
const uuid = state.resources.index.all[0];
const result = mapStateToProps(state);
expect(result.findPlant(undefined)).toEqual(undefined);
expect(result.findPlant("10")).toEqual(
expect.objectContaining({ uuid }));
});
});

View file

@ -12,12 +12,14 @@ jest.mock("../../../device", () => ({
let mockPath = "";
jest.mock("../../../history", () => ({
getPathArray: jest.fn(() => { return mockPath.split("/"); })
getPathArray: jest.fn(() => { return mockPath.split("/"); }),
history: { push: jest.fn() }
}));
import * as React from "react";
import { mount } from "enzyme";
import { MoveTo, MoveToProps, MoveToForm, MoveToFormProps } from "../move_to";
import { history } from "../../../history";
describe("<MoveTo />", () => {
beforeEach(function () {
@ -37,6 +39,12 @@ describe("<MoveTo />", () => {
wrapper.find("button").simulate("click");
expect(mockDevice.moveAbsolute).toHaveBeenCalledWith({ x: 1, y: 2, z: 30 });
});
it("goes back", () => {
const wrapper = mount(<MoveTo {...fakeProps()} />);
wrapper.find("i").first().simulate("click");
expect(history.push).toHaveBeenCalledWith("/app/designer/plants");
});
});
describe("<MoveToForm />", () => {

View file

@ -2,19 +2,29 @@ jest.mock("react-redux", () => ({
connect: jest.fn()
}));
jest.mock("../../../history", () => ({
getPathArray: jest.fn(() => { return []; }),
history: { push: jest.fn() }
}));
import * as React from "react";
import { PlantInfo } from "../plant_info";
import { mount } from "enzyme";
import { fakePlant } from "../../../__test_support__/fake_state/resources";
import { EditPlantInfoProps } from "../../interfaces";
import { history } from "../../../history";
describe("<PlantInfo />", () => {
function fakeProps(): EditPlantInfoProps {
return {
push: jest.fn(),
findPlant: fakePlant,
dispatch: jest.fn()
};
}
it("renders", () => {
const dispatch = jest.fn();
const wrapper = mount(
<PlantInfo
push={jest.fn()}
dispatch={() => dispatch}
findPlant={fakePlant} />);
const wrapper = mount(<PlantInfo {...fakeProps()} />);
expect(wrapper.text()).toContain("Strawberry Plant 1");
expect(wrapper.text().replace(/\s+/g, " "))
.toContain("Plant Type: Strawberry");
@ -22,4 +32,12 @@ describe("<PlantInfo />", () => {
expect(buttons.first().props().hidden).toBeTruthy();
expect(buttons.last().props().hidden).toBeTruthy();
});
it("renders: no plant", () => {
const p = fakeProps();
p.findPlant = () => undefined;
const wrapper = mount(<PlantInfo {...p} />);
expect(wrapper.text().toLowerCase()).toContain("redirecting...");
expect(history.push).toHaveBeenCalledWith("/app/designer/plants");
});
});