forked from mitmproxy/mitmproxy
/
SplitterSpec.tsx
172 lines (160 loc) · 5.14 KB
/
SplitterSpec.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
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
import * as React from "react";
import ReactDOM from "react-dom";
import renderer from "react-test-renderer";
import Splitter from "../../../components/common/Splitter";
import TestUtils from "react-dom/test-utils";
describe.each([
["", ""],
["x", "X"],
["y", "Y"],
])("Splitter Component", (axisLower, axisUpper) => {
if (axisLower === "") {
it("should render correctly with default (x) axis", () => {
const splitter = renderer.create(<Splitter />),
tree = splitter.toJSON();
expect(tree).toMatchInlineSnapshot(`
<div
className="splitter splitter-x"
>
<div
onLostPointerCapture={[Function]}
onPointerDown={[Function]}
onPointerMove={[Function]}
/>
</div>
`);
});
return;
}
it("should render correctly with specified axis", () => {
const splitter = renderer.create(<Splitter axis={axisLower} />),
tree = splitter.toJSON();
expect(tree).toMatchInlineSnapshot(`
<div
className="splitter splitter-${axisLower}"
>
<div
onLostPointerCapture={[Function]}
onPointerDown={[Function]}
onPointerMove={[Function]}
/>
</div>
`);
});
const splitter = TestUtils.renderIntoDocument(
<Splitter axis={axisLower} />
),
dom = ReactDOM.findDOMNode(splitter),
previousElementSibling = {
offsetWidth: 300,
offsetHeight: 500,
style: { flex: "" },
},
nextElementSibling = {
style: { flex: "" },
};
Object.defineProperties(dom, {
previousElementSibling: { value: previousElementSibling },
nextElementSibling: { value: nextElementSibling },
});
dom.firstElementChild.setPointerCapture = jest.fn();
it("should handle pointerdown", () => {
const e = {
pageX: 13,
pageY: 22,
pointerId: -4618,
target: dom.firstElementChild,
};
expect(splitter.state.dragPointer).toEqual(0.1);
splitter.onPointerDown(e);
expect(e.target.setPointerCapture).toBeCalledWith(-4618);
expect(splitter.state.dragPointer).toEqual(-4618);
expect(splitter.state.startPos).toEqual(e[`page${axisUpper}`]);
});
it("should handle pointermove", () => {
const e = {
pageX: 62,
pageY: 21,
pointerId: -4618,
target: dom.firstElementChild,
};
splitter.onPointerMove(e);
expect(dom.style.transform).toEqual(
axisLower === "x"
? `translateX(${62 - 13}px)`
: `translateY(${21 - 22}px)`
);
});
it("should handle lostpointercapture", () => {
const e = {
pageX: 56,
pageY: 82,
pointerId: -4618,
target: dom.firstElementChild,
};
splitter.onLostPointerCapture(e);
expect(splitter.state.dragPointer).toEqual(0.1);
expect(dom.style.transform).toEqual("");
expect(previousElementSibling.style.flex).toEqual(
`0 0 ${axisLower === "x" ? 300 + 56 - 13 : 500 + 82 - 22}px`
);
expect(nextElementSibling.style.flex).toEqual("1 1 auto");
});
it("should not resize previousElementSibling negative", () => {
const e = {
pageX: 56,
pageY: 82,
pointerId: 47,
target: dom.firstElementChild,
};
splitter.onPointerDown(e);
e[`page${axisUpper}`] = -1234;
splitter.onLostPointerCapture(e);
expect(previousElementSibling.style.flex).toEqual("0 0 0px");
});
it("should ignore other pointers", () => {
splitter.onPointerDown({
pageX: 70,
pageY: 60,
pointerId: 47,
target: dom.firstElementChild,
});
splitter.onPointerDown({
pageX: 70,
pageY: 60,
pointerId: 46,
target: dom.firstElementChild,
});
expect(splitter.state.dragPointer).toEqual(47);
splitter.onPointerMove({ pageX: 75, pageY: 55, pointerId: 46 });
expect(dom.style.transform).toEqual("");
splitter.onPointerMove({
pageX: 74,
pageY: 54,
pointerId: 47,
target: dom.firstElementChild,
});
splitter.onLostPointerCapture({ pageX: 76, pageY: 56, pointerId: 46 });
expect(dom.style.transform).toEqual(
axisLower === "x"
? `translateX(${74 - 70}px)`
: `translateY(${54 - 60}px)`
);
});
it("should handle resize", () => {
const x = jest.spyOn(window, "setTimeout");
splitter.onResize();
expect(x).toHaveBeenCalled();
});
it("should handle componentWillUnmount", () => {
splitter.componentWillUnmount();
expect(previousElementSibling.style.flex).toEqual("");
expect(nextElementSibling.style.flex).toEqual("");
expect(splitter.state.applied).toBeTruthy();
});
it("should handle reset", () => {
splitter.reset(false);
expect(splitter.state.applied).toBeFalsy();
expect(splitter.reset(true)).toEqual(undefined);
});
});