-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathCell.test.tsx
More file actions
189 lines (185 loc) · 6.47 KB
/
Copy pathCell.test.tsx
File metadata and controls
189 lines (185 loc) · 6.47 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* @jest-environment jsdom
*/
import React from "react";
import { fireEvent, render } from "@testing-library/react";
import { Cell } from "./Cell";
import { Parser as FormulaParser } from "hot-formula-parser";
import * as Types from "./types";
import * as Point from "./point";
import { getOffsetRect } from "./util";
const MOCK_DATA_VIEWER = jest.fn(() => null);
const MOCK_CUSTOM_DATA_VIEWER = jest.fn(() => null);
const MOCK_FORMULA_PARSER = {} as FormulaParser;
const MOCK_SELECT = jest.fn();
const MOCK_ACTIVATE = jest.fn();
const MOCK_SET_CELL_DIMENSIONS = jest.fn();
const EXAMPLE_ROW = 0;
const EXAMPLE_COLUMN = 0;
const EXAMPLE_PROPS: Types.CellComponentProps = {
row: EXAMPLE_ROW,
column: EXAMPLE_COLUMN,
DataViewer: MOCK_DATA_VIEWER,
formulaParser: MOCK_FORMULA_PARSER,
selected: false,
active: false,
copied: false,
dragging: false,
mode: "view",
data: { value: null },
select: MOCK_SELECT,
activate: MOCK_ACTIVATE,
setCellDimensions: MOCK_SET_CELL_DIMENSIONS,
width: 100,
};
const EXAMPLE_DATA_VIEWER_PROPS: Types.DataViewerProps = {
row: EXAMPLE_ROW,
column: EXAMPLE_COLUMN,
cell: EXAMPLE_PROPS.data,
formulaParser: MOCK_FORMULA_PARSER,
};
const EXAMPLE_READ_ONLY_DATA: Types.CellBase = { value: null, readOnly: true };
const EXAMPLE_DATA_WITH_CLASS_NAME: Types.CellBase = {
value: null,
className: "example",
};
const EXAMPLE_DATA_WITH_CUSTOM_DATA_VIEWER: Types.CellBase = {
value: null,
DataViewer: MOCK_CUSTOM_DATA_VIEWER,
};
const EXAMPLE_CUSTOM_DATA_VIEWER_PROPS: Types.DataViewerProps = {
row: EXAMPLE_ROW,
column: EXAMPLE_COLUMN,
cell: EXAMPLE_DATA_WITH_CUSTOM_DATA_VIEWER,
formulaParser: MOCK_FORMULA_PARSER,
};
const EXAMPLE_POINT: Point.Point = { row: EXAMPLE_ROW, column: EXAMPLE_COLUMN };
beforeEach(() => {
jest.clearAllMocks();
});
describe("<Cell />", () => {
test("renders", () => {
render(<Cell {...EXAMPLE_PROPS} />);
const element = document.querySelector(".Spreadsheet__cell");
expect(element).not.toBeNull();
expect(MOCK_DATA_VIEWER).toBeCalledTimes(1);
expect(MOCK_DATA_VIEWER).toBeCalledWith(EXAMPLE_DATA_VIEWER_PROPS, {});
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(0);
});
test("renders read only", () => {
render(<Cell {...EXAMPLE_PROPS} data={EXAMPLE_READ_ONLY_DATA} />);
const element = document.querySelector(
".Spreadsheet__cell.Spreadsheet__cell--readonly"
);
expect(element).not.toBeNull();
expect(MOCK_DATA_VIEWER).toBeCalledTimes(1);
expect(MOCK_DATA_VIEWER).toBeCalledWith(
{ ...EXAMPLE_DATA_VIEWER_PROPS, cell: EXAMPLE_READ_ONLY_DATA },
{}
);
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(0);
});
test("renders with given class name", () => {
render(<Cell {...EXAMPLE_PROPS} data={EXAMPLE_DATA_WITH_CLASS_NAME} />);
const element = document.querySelector(
`.Spreadsheet__cell.${EXAMPLE_DATA_WITH_CLASS_NAME.className}`
);
expect(element).not.toBeNull();
expect(MOCK_DATA_VIEWER).toBeCalledTimes(1);
expect(MOCK_DATA_VIEWER).toBeCalledWith(
{ ...EXAMPLE_DATA_VIEWER_PROPS, cell: EXAMPLE_DATA_WITH_CLASS_NAME },
{}
);
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(0);
});
test("renders selected", () => {
render(<Cell {...EXAMPLE_PROPS} selected />);
const element = document.querySelector<HTMLElement>(".Spreadsheet__cell");
expect(element).not.toBeNull();
if (!element) {
throw new Error("element must be defined");
}
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(1);
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledWith(
EXAMPLE_POINT,
getOffsetRect(element)
);
});
test("renders active", () => {
render(<Cell {...EXAMPLE_PROPS} active />);
const element = document.querySelector<HTMLElement>(".Spreadsheet__cell");
expect(element).not.toBeNull();
if (!element) {
throw new Error("element must be defined");
}
expect(document.activeElement === element);
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(0);
});
test("handles mouse down", () => {
render(<Cell {...EXAMPLE_PROPS} />);
const element = document.querySelector<HTMLElement>(".Spreadsheet__cell");
expect(element).not.toBeNull();
if (!element) {
throw new Error("element must be defined");
}
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(0);
fireEvent.mouseDown(element);
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(1);
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledWith(
EXAMPLE_POINT,
getOffsetRect(element)
);
expect(MOCK_ACTIVATE).toBeCalledTimes(1);
expect(MOCK_ACTIVATE).toBeCalledWith(EXAMPLE_POINT);
expect(MOCK_SELECT).toBeCalledTimes(0);
});
test("handles mouse down + shift", () => {
render(<Cell {...EXAMPLE_PROPS} />);
const element = document.querySelector<HTMLElement>(".Spreadsheet__cell");
expect(element).not.toBeNull();
if (!element) {
throw new Error("element must be defined");
}
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(0);
fireEvent.mouseDown(element, { shiftKey: true });
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(1);
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledWith(
EXAMPLE_POINT,
getOffsetRect(element)
);
expect(MOCK_ACTIVATE).toBeCalledTimes(0);
expect(MOCK_SELECT).toBeCalledTimes(1);
expect(MOCK_SELECT).toBeCalledWith(EXAMPLE_POINT);
});
test("handles mouse over with dragging", () => {
render(<Cell {...EXAMPLE_PROPS} dragging />);
const element = document.querySelector<HTMLElement>(".Spreadsheet__cell");
expect(element).not.toBeNull();
if (!element) {
throw new Error("element must be defined");
}
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(0);
fireEvent.mouseOver(element);
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(1);
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledWith(
EXAMPLE_POINT,
getOffsetRect(element)
);
expect(MOCK_ACTIVATE).toBeCalledTimes(0);
expect(MOCK_SELECT).toBeCalledTimes(1);
expect(MOCK_SELECT).toBeCalledWith(EXAMPLE_POINT);
});
test("custom cell DataViewer", () => {
render(
<Cell {...EXAMPLE_PROPS} data={EXAMPLE_DATA_WITH_CUSTOM_DATA_VIEWER} />
);
const element = document.querySelector(".Spreadsheet__cell");
expect(element).not.toBeNull();
expect(MOCK_CUSTOM_DATA_VIEWER).toBeCalledTimes(1);
expect(MOCK_CUSTOM_DATA_VIEWER).toBeCalledWith(
EXAMPLE_CUSTOM_DATA_VIEWER_PROPS,
{}
);
expect(MOCK_SET_CELL_DIMENSIONS).toBeCalledTimes(0);
});
});