-
Notifications
You must be signed in to change notification settings - Fork 324
/
plant_inventory_item_test.tsx
145 lines (129 loc) · 4.86 KB
/
plant_inventory_item_test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
jest.mock("../../open_farm/cached_crop", () => ({
maybeGetCachedPlantIcon: jest.fn(),
}));
import { Path } from "../../internal_urls";
let mockPath = Path.mock(Path.plants());
jest.mock("../../history", () => ({
push: jest.fn(),
getPathArray: () => mockPath.split("/"),
}));
jest.mock("../../farm_designer/map/actions", () => ({
mapPointClickAction: jest.fn(() => jest.fn()),
setHoveredPlant: jest.fn(),
selectPoint: jest.fn(),
}));
import React from "react";
import {
daysOldText,
PlantInventoryItem, PlantInventoryItemProps,
} from "../plant_inventory_item";
import { shallow, mount } from "enzyme";
import {
fakePlant, fakePlantTemplate,
} from "../../__test_support__/fake_state/resources";
import { push } from "../../history";
import { maybeGetCachedPlantIcon } from "../../open_farm/cached_crop";
import {
mapPointClickAction, setHoveredPlant, selectPoint,
} from "../../farm_designer/map/actions";
import moment from "moment";
describe("<PlantInventoryItem />", () => {
const fakeProps = (): PlantInventoryItemProps => ({
plant: fakePlant(),
dispatch: jest.fn(),
hovered: false,
});
it("renders", () => {
const wrapper = shallow(<PlantInventoryItem {...fakeProps()} />);
expect(wrapper.text()).toEqual("Strawberry Plant 1planned");
expect(wrapper.find("div").first().hasClass("hovered")).toBeFalsy();
});
it("handles missing plant name", () => {
const p = fakeProps();
const plant = fakePlant();
plant.body.name = "";
plant.body.planted_at = "" + moment().toISOString();
p.plant = plant;
const wrapper = shallow(<PlantInventoryItem {...p} />);
expect(wrapper.text()).toEqual("Unknown plant1 day old");
expect(wrapper.find("div").first().hasClass("hovered")).toBeFalsy();
});
it("renders hovered", () => {
const p = fakeProps();
p.hovered = true;
const wrapper = shallow(<PlantInventoryItem {...p} />);
expect(wrapper.find("div").first().hasClass("hovered")).toBeTruthy();
});
it("hover begin", () => {
const p = fakeProps();
const wrapper = shallow(<PlantInventoryItem {...p} />);
wrapper.simulate("mouseEnter");
expect(setHoveredPlant).toHaveBeenCalledWith(p.plant.uuid, "");
});
it("hover end", () => {
const wrapper = shallow(<PlantInventoryItem {...fakeProps()} />);
wrapper.simulate("mouseLeave");
expect(setHoveredPlant).toHaveBeenCalledWith(undefined, "");
});
it("selects plant", () => {
mockPath = Path.mock(Path.plants());
const p = fakeProps();
const wrapper = shallow(<PlantInventoryItem {...p} />);
wrapper.simulate("click");
expect(mapPointClickAction).not.toHaveBeenCalled();
expect(selectPoint).toHaveBeenCalledWith([p.plant.uuid]);
expect(push).toHaveBeenCalledWith(Path.plants(p.plant.body.id));
});
it("handles missing plant id", () => {
const p = fakeProps();
p.plant.body.id = 0;
const wrapper = shallow(<PlantInventoryItem {...p} />);
wrapper.simulate("click");
expect(selectPoint).toHaveBeenCalledWith([p.plant.uuid]);
expect(push).toHaveBeenCalledWith(Path.plants("ERR_NO_PLANT_ID"));
});
it("removes item in box select mode", () => {
mockPath = Path.mock(Path.plants("select"));
const p = fakeProps();
const wrapper = shallow(<PlantInventoryItem {...p} />);
wrapper.simulate("click");
expect(mapPointClickAction).toHaveBeenCalledWith(expect.any(Function),
p.plant.uuid);
expect(push).not.toHaveBeenCalled();
expect(setHoveredPlant).toHaveBeenCalledWith(undefined, "");
});
it("selects plant template", () => {
mockPath = Path.mock(Path.plants());
const p = fakeProps();
p.plant = fakePlantTemplate();
const wrapper = shallow(<PlantInventoryItem {...p} />);
wrapper.simulate("click");
expect(selectPoint).toHaveBeenCalledWith([p.plant.uuid]);
expect(push).toHaveBeenCalledWith(
Path.savedGardens(`templates/${p.plant.body.id}`));
});
it("gets cached icon", () => {
const wrapper =
mount<PlantInventoryItem>(<PlantInventoryItem {...fakeProps()} />);
const img = wrapper.find("img");
img.simulate("load");
expect(maybeGetCachedPlantIcon).toHaveBeenCalledWith("strawberry",
img.instance(), expect.any(Function));
});
it("sets icon", () => {
const wrapper =
mount<PlantInventoryItem>(<PlantInventoryItem {...fakeProps()} />);
expect(wrapper.state().icon).toEqual("");
wrapper.instance().updateStateIcon("fake icon");
expect(wrapper.state().icon).toEqual("fake icon");
});
});
describe("daysOldText()", () => {
it("returns correct text", () => {
expect(daysOldText({ age: 1 })).toEqual("1 day old");
expect(daysOldText({ age: 0 })).toEqual("0 days old");
expect(daysOldText({ age: 2 })).toEqual("2 days old");
expect(daysOldText({ age: 2, stage: "planted" })).toEqual("2 days old");
expect(daysOldText({ age: undefined, stage: "planned" })).toEqual("planned");
});
});