Skip to content

Commit 0b8d253

Browse files
omgovichVlad Shilov
authored andcommitted
Tests for multitouch feature
1 parent 3ac7276 commit 0b8d253

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

src/components/common/Interactive.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ const InteractiveBase = ({ onMove, onKey, ...rest }: Props) => {
7373

7474
if (isTouch(nativeEvent)) {
7575
hasTouch.current = true;
76-
touchId.current = nativeEvent.changedTouches[0].identifier;
76+
const changedTouches = nativeEvent.changedTouches || [];
77+
if (changedTouches.length) touchId.current = changedTouches[0].identifier;
7778
}
7879

7980
el.focus();

tests/components.test.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ it("Doesn't call `onChange` when user changes a hue of a grayscale color", () =>
9191
const { container } = render(<HexColorPicker color="#000" onChange={handleChange} />);
9292
const hue = container.querySelector(".react-colorful__hue .react-colorful__interactive");
9393

94-
fireEvent.touchStart(hue, {
95-
touches: [{ pageX: 0, pageY: 0 }],
96-
});
94+
fireEvent.touchStart(hue, { touches: [{ pageX: 0, pageY: 0 }] });
9795
fireEvent.touchMove(hue, { touches: [{ pageX: 100, pageY: 0 }] });
9896

9997
expect(handleChange).not.toHaveBeenCalled();
@@ -113,20 +111,51 @@ it("Triggers `onChange` after a mouse interaction", async () => {
113111
});
114112

115113
it("Triggers `onChange` after a touch interaction", async () => {
116-
const handleChange = jest.fn((hex) => hex);
114+
const handleChange = jest.fn((hsv) => hsv);
117115
const initialValue = { h: 0, s: 100, v: 100 };
118116
const result = render(<HsvColorPicker color={initialValue} onChange={handleChange} />);
119117
const hue = result.container.querySelector(".react-colorful__hue .react-colorful__interactive");
120118

121-
fireEvent.touchStart(hue, {
122-
changedTouches: [{ pageX: 0, pageY: 0 }],
123-
touches: [{ pageX: 0, pageY: 0, bubbles: true }],
124-
});
119+
fireEvent.touchStart(hue, { touches: [{ pageX: 0, pageY: 0, bubbles: true }] });
125120
fireEvent.touchMove(hue, { touches: [{ pageX: 55, pageY: 0, bubbles: true }] });
126121

127122
expect(handleChange).toHaveReturnedWith({ h: 180, s: 100, v: 100 });
128123
});
129124

125+
it("Supports multitouch", async () => {
126+
const handleChange = jest.fn((hsva) => hsva);
127+
const initialValue = { h: 0, s: 100, v: 100, a: 0 };
128+
const result = render(<HsvaColorPicker color={initialValue} onChange={handleChange} />);
129+
const hue = result.container.querySelector(".react-colorful__hue .react-colorful__interactive");
130+
const alpha = result.container.querySelector(
131+
".react-colorful__alpha .react-colorful__interactive"
132+
);
133+
134+
const firstFingerBefore = { pageX: 0, pageY: 0, identifier: 0, bubbles: true };
135+
const firstFingerAfter = { pageX: 55, pageY: 0, identifier: 0, bubbles: true };
136+
137+
const secondFingerBefore = { pageX: 0, pageY: 0, identifier: 1, bubbles: true };
138+
const secondFingerAfter = { pageX: 200, pageY: 0, identifier: 1, bubbles: true };
139+
140+
const extraTouch = { pageX: 10, pageY: 10, identifier: 2, bubbles: true };
141+
142+
fireEvent.touchStart(hue, {
143+
changedTouches: [firstFingerBefore],
144+
touches: [firstFingerBefore],
145+
});
146+
147+
fireEvent.touchStart(alpha, {
148+
changedTouches: [secondFingerBefore],
149+
touches: [firstFingerBefore, secondFingerBefore],
150+
});
151+
152+
fireEvent.touchMove(hue, { touches: [firstFingerAfter, secondFingerAfter] });
153+
fireEvent.touchMove(alpha, { touches: [extraTouch] }); // test touch fallback
154+
fireEvent.touchMove(alpha, { touches: [firstFingerAfter, secondFingerAfter] });
155+
156+
expect(handleChange).toHaveReturnedWith({ h: 180, s: 100, v: 100, a: 1 });
157+
});
158+
130159
it("Pointer doesn't follow the mouse if it was released outside of the document bounds", async () => {
131160
const handleChange = jest.fn();
132161
const result = render(<RgbaColorPicker onChange={handleChange} />);
@@ -166,10 +195,7 @@ it("Doesn't react on mouse events after a touch interaction", () => {
166195
const result = render(<HslStringColorPicker color="hsl(100, 0%, 0%)" onChange={handleChange} />);
167196
const hue = result.container.querySelector(".react-colorful__hue .react-colorful__interactive");
168197

169-
fireEvent.touchStart(hue, {
170-
changedTouches: [{ pageX: 0, pageY: 0 }],
171-
touches: [{ pageX: 0, pageY: 0, bubbles: true }],
172-
}); // 1
198+
fireEvent.touchStart(hue, { touches: [{ pageX: 0, pageY: 0, bubbles: true }] }); // 1
173199
fireEvent.touchMove(hue, { touches: [{ pageX: 55, pageY: 0, bubbles: true }] }); // 2
174200

175201
// Should be skipped

0 commit comments

Comments
 (0)