Farmbot-Web-App/frontend/farm_designer/point_groups/__tests__/group_list_panel_test.tsx

94 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-08-23 15:18:28 -06:00
jest.mock("../../../history", () => ({
getPathArray: jest.fn(() => ["L", "O", "L"]),
history: { push: jest.fn() }
}));
2019-08-20 13:55:30 -06:00
2020-04-13 13:24:38 -06:00
jest.mock("../actions", () => ({
createGroup: jest.fn(),
}));
2019-08-19 14:52:50 -06:00
import React from "react";
2019-08-23 15:18:28 -06:00
import { mount, shallow } from "enzyme";
2019-09-23 12:56:35 -06:00
import {
2020-02-28 09:35:32 -07:00
RawGroupListPanel as GroupListPanel, GroupListPanelProps, mapStateToProps,
2019-09-23 12:56:35 -06:00
} from "../group_list_panel";
2020-02-07 16:05:16 -07:00
import {
2020-02-28 09:35:32 -07:00
fakePointGroup, fakePlant,
2020-02-07 16:05:16 -07:00
} from "../../../__test_support__/fake_state/resources";
2019-08-20 13:55:30 -06:00
import { history } from "../../../history";
2019-09-09 07:23:28 -06:00
import { fakeState } from "../../../__test_support__/fake_state";
2019-09-23 12:56:35 -06:00
import {
2020-02-28 09:35:32 -07:00
buildResourceIndex,
2019-09-23 12:56:35 -06:00
} from "../../../__test_support__/resource_index_builder";
2020-04-13 13:24:38 -06:00
import { createGroup } from "../actions";
import { DesignerPanelTop } from "../../designer_panel";
2020-04-13 19:15:11 -06:00
import { SearchField } from "../../../ui/search_field";
2019-08-19 14:52:50 -06:00
describe("<GroupListPanel />", () => {
2019-08-23 15:18:28 -06:00
const fakeProps = (): GroupListPanelProps => {
2020-02-07 16:05:16 -07:00
const group1 = fakePointGroup();
group1.body.name = "one";
group1.body.id = 9;
group1.body.point_ids = [1, 2, 3];
const group2 = fakePointGroup();
group2.body.name = "two";
2020-03-13 15:21:44 -06:00
group2.body.criteria.day.days_ago = -1;
2020-02-07 16:05:16 -07:00
const point1 = fakePlant();
point1.body.id = 1;
const point2 = fakePlant();
point2.body.id = 2;
const point3 = fakePlant();
point3.body.id = 3;
return {
dispatch: jest.fn(),
groups: [group1, group2],
allPoints: [point1, point2, point3],
};
};
2020-04-13 13:24:38 -06:00
it("creates new group", () => {
const p = fakeProps();
const wrapper = shallow(<GroupListPanel {...p} />);
wrapper.find(DesignerPanelTop).simulate("click");
expect(createGroup).toHaveBeenCalledWith({ pointUuids: [] });
});
2019-08-23 15:18:28 -06:00
it("changes search term", () => {
const p = fakeProps();
const wrapper = shallow<GroupListPanel>(<GroupListPanel {...p} />);
2020-04-13 19:15:11 -06:00
wrapper.find(SearchField).simulate("change", "one");
2019-08-23 15:18:28 -06:00
expect(wrapper.state().searchTerm).toEqual("one");
});
it("renders relevant group data as a list", () => {
2019-08-23 15:18:28 -06:00
const p = fakeProps();
const wrapper = mount(<GroupListPanel {...p} />);
2019-10-09 06:55:30 -06:00
wrapper.find(".group-search-item").first().simulate("click");
2019-08-20 13:55:30 -06:00
expect(history.push).toHaveBeenCalledWith("/app/designer/groups/9");
2019-08-19 14:52:50 -06:00
2019-08-23 15:18:28 -06:00
["3 items",
"0 items",
p.groups[0].body.name,
p.groups[1].body.name].map(string =>
expect(wrapper.text()).toContain(string));
});
it("renders no groups", () => {
const p = fakeProps();
p.groups = [];
const wrapper = mount(<GroupListPanel {...p} />);
expect(wrapper.text().toLowerCase()).toContain("no groups yet");
2019-08-19 14:52:50 -06:00
});
2020-04-13 13:24:38 -06:00
});
2019-09-09 07:23:28 -06:00
2020-04-13 13:24:38 -06:00
describe("mapStateToProps()", () => {
2019-09-09 07:23:28 -06:00
it("maps state to props", () => {
const state = fakeState();
const group = fakePointGroup();
const resources = buildResourceIndex([group]);
state.resources = resources;
const x = mapStateToProps(state);
expect(x.groups).toContain(group);
});
2019-08-19 14:52:50 -06:00
});