<PlantGrid/> tests, part I

pull/1575/head
Rick Carlino 2019-11-19 08:25:03 -06:00
parent dbb330efcf
commit 60fc32fd7e
2 changed files with 69 additions and 2 deletions

View File

@ -0,0 +1,67 @@
jest.mock("../thunks", () => ({
saveGrid: jest.fn(),
stashGrid: jest.fn()
}));
import * as React from "react";
import { mount } from "enzyme";
import { PlantGrid } from "../plant_grid";
import { saveGrid, stashGrid } from "../thunks";
import { error } from "../../../../toast/toast";
describe("PlantGrid", () => {
function fakeProps() {
return {
xy_swap: true,
openfarm_slug: "beets",
cropName: "Beets",
dispatch: jest.fn(() => Promise.resolve({})),
};
}
it("renders", () => {
const p = fakeProps();
const el = mount<PlantGrid>(<PlantGrid {...p} />);
// Upon load, there should be one button.
const previewButton = el.find("a.clear-button");
expect(previewButton.text()).toContain("Preview");
previewButton.simulate("click");
// After clicking PREVIEW, there should be two buttons.
const saveAndCancelBtns = el.find("a.clear-button");
const cancel = saveAndCancelBtns.at(0);
const save = saveAndCancelBtns.at(1);
expect(cancel.text()).toContain("Cancel");
expect(save.text()).toContain("Save");
expect(el.state().status).toEqual("dirty");
});
it("saves a grid", async () => {
const props = fakeProps();
const pg = mount<PlantGrid>(<PlantGrid {...props} />).instance();
await pg.saveGrid();
expect(saveGrid).toHaveBeenCalledWith(pg.state.gridId);
});
it("stashes a grid", async () => {
const props = fakeProps();
const pg = mount<PlantGrid>(<PlantGrid {...props} />).instance();
await pg.revertPreview();
expect(stashGrid).toHaveBeenCalledWith(pg.state.gridId);
});
it("prevents creation of grids with > 100 plants", () => {
const props = fakeProps();
const pg = mount<PlantGrid>(<PlantGrid {...props} />).instance();
pg.setState({
...pg.state,
grid: {
...pg.state.grid,
numPlantsH: 10,
numPlantsV: 11
}
});
pg.performPreview();
expect(error).toHaveBeenCalledWith("Please make a grid with less than 100 plants");
});
});

View File

@ -52,12 +52,12 @@ export class PlantGrid extends React.Component<PlantGridProps, PlantGridState> {
revertPreview = () => {
const p: Promise<{}> = this.props.dispatch(stashGrid(this.state.gridId));
p.then(() => this.setState({ status: "clean" }));
return p.then(() => this.setState({ status: "clean" }));
}
saveGrid = () => {
const p: Promise<{}> = this.props.dispatch(saveGrid(this.state.gridId));
p.then(() => this.setState(EMPTY_PLANT_GRID));
return p.then(() => this.setState(EMPTY_PLANT_GRID));
}
inputs = () => {