Farmbot-Web-App/frontend/farm_designer/plants/__tests__/crop_catalog_test.tsx

76 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-12-03 20:05:45 -07:00
jest.mock("lodash", () => ({
debounce: jest.fn(x => x),
trim: jest.fn(x => x),
last: () => "fake",
2019-04-09 19:29:25 -06:00
times: jest.fn(),
set: jest.fn(),
throttle: jest.fn(),
2018-12-03 20:05:45 -07:00
}));
2018-11-28 15:29:46 -07:00
2018-08-01 20:03:16 -06:00
jest.mock("../../../history", () => ({ history: { push: jest.fn() } }));
2017-09-15 23:51:12 -06:00
import * as React from "react";
2019-09-23 12:56:35 -06:00
import { RawCropCatalog as CropCatalog } from "../crop_catalog";
2018-08-01 20:03:16 -06:00
import { mount, shallow } from "enzyme";
import { CropCatalogProps } from "../../interfaces";
import { Actions } from "../../../constants";
import { history } from "../../../history";
2018-11-28 15:29:46 -07:00
import {
fakeCropLiveSearchResult
} from "../../../__test_support__/fake_crop_search_result";
2017-09-15 23:51:12 -06:00
describe("<CropCatalog />", () => {
2018-08-01 20:03:16 -06:00
const fakeProps = (): CropCatalogProps => {
return {
dispatch: jest.fn(),
openfarmSearch: jest.fn(() => jest.fn()),
2018-08-01 20:03:16 -06:00
cropSearchResults: [],
cropSearchQuery: "",
2018-11-28 15:29:46 -07:00
cropSearchInProgress: false,
2018-08-01 20:03:16 -06:00
};
};
2017-09-15 23:51:12 -06:00
it("renders", () => {
2018-08-01 20:03:16 -06:00
const wrapper = mount(<CropCatalog {...fakeProps()} />);
2017-09-15 23:51:12 -06:00
expect(wrapper.text()).toContain("Choose a crop");
expect(wrapper.find("input").props().placeholder)
.toEqual("Search OpenFarm...");
});
2018-08-01 20:03:16 -06:00
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
});
2018-11-28 15:29:46 -07:00
// Requires lodash.debouce to be mocked
expect(p.openfarmSearch).toHaveBeenCalledWith("apple");
2018-08-01 20:03:16 -06:00
});
it("goes back", () => {
const wrapper = mount(<CropCatalog {...fakeProps()} />);
wrapper.find("i").first().simulate("click");
expect(history.push).toHaveBeenCalledWith("/app/designer/plants");
});
2018-11-28 15:29:46 -07:00
it("search term is too short", () => {
const p = fakeProps();
p.cropSearchQuery = "ab";
const wrapper = mount(<CropCatalog {...p} />);
expect(wrapper.text().toLowerCase()).toContain("too short");
});
it("shows result update spinner", () => {
const p = fakeProps();
p.cropSearchQuery = "abc";
p.cropSearchInProgress = true;
p.cropSearchResults = [fakeCropLiveSearchResult()];
const wrapper = shallow(<CropCatalog {...p} />);
expect(wrapper.find("Spinner").length).toEqual(1);
});
2017-09-15 23:51:12 -06:00
});