@remit/ui 0.0.140 → 0.0.141
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/lib/use-long-press.test.ts +37 -16
package/package.json
CHANGED
|
@@ -12,17 +12,25 @@
|
|
|
12
12
|
* need a real `document`/`window`/`PointerEvent`, which `renderToString`
|
|
13
13
|
* (the pattern used elsewhere in this repo for presentational components)
|
|
14
14
|
* cannot exercise.
|
|
15
|
+
*
|
|
16
|
+
* The clock is mocked. Every timing assertion here is about one boundary —
|
|
17
|
+
* the press crossed the threshold, or it ended first — and racing that
|
|
18
|
+
* boundary against a wall clock on a loaded runner turns a passing test red
|
|
19
|
+
* (#645). Time only moves when `advance` moves it.
|
|
15
20
|
*/
|
|
16
21
|
|
|
17
22
|
import "@remit/test-dom";
|
|
18
23
|
import assert from "node:assert/strict";
|
|
19
|
-
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
24
|
+
import { afterEach, beforeEach, describe, it, mock } from "node:test";
|
|
20
25
|
import { act, createElement } from "react";
|
|
21
26
|
import { createRoot, type Root } from "react-dom/client";
|
|
22
27
|
import { useLongPress } from "./use-long-press.js";
|
|
23
28
|
|
|
24
29
|
const THRESHOLD = 40;
|
|
25
30
|
|
|
31
|
+
/** Past react-aria's own teardown of its transient post-pointerup contextmenu listener. */
|
|
32
|
+
const AFTER_ARIA_CONTEXTMENU_TEARDOWN = 100;
|
|
33
|
+
|
|
26
34
|
let container: HTMLElement;
|
|
27
35
|
let root: Root;
|
|
28
36
|
|
|
@@ -106,17 +114,27 @@ function pointerCancel(row: Element) {
|
|
|
106
114
|
row.dispatchEvent(new PointerEvent("pointercancel", { bubbles: true }));
|
|
107
115
|
}
|
|
108
116
|
|
|
109
|
-
function
|
|
110
|
-
return act(() =>
|
|
117
|
+
function advance(ms: number) {
|
|
118
|
+
return act(() => {
|
|
119
|
+
mock.timers.tick(ms);
|
|
120
|
+
});
|
|
111
121
|
}
|
|
112
122
|
|
|
113
123
|
beforeEach(() => {
|
|
114
124
|
container = document.getElementById("root") as unknown as HTMLElement;
|
|
115
125
|
container.innerHTML = "";
|
|
116
126
|
root = createRoot(container);
|
|
127
|
+
mock.timers.enable({ apis: ["setTimeout"] });
|
|
117
128
|
});
|
|
118
129
|
|
|
119
130
|
afterEach(() => {
|
|
131
|
+
// The hook's contextmenu arming is module state bounded by a timer, and a
|
|
132
|
+
// test that leaves the finger down leaves it armed. Release the pointer so
|
|
133
|
+
// the next test starts disarmed rather than inheriting a dead timer id.
|
|
134
|
+
act(() => {
|
|
135
|
+
pointerUp();
|
|
136
|
+
});
|
|
137
|
+
mock.timers.reset();
|
|
120
138
|
act(() => {
|
|
121
139
|
root.unmount();
|
|
122
140
|
});
|
|
@@ -128,7 +146,10 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
128
146
|
const row = mount({ onLongPress: () => fired++ });
|
|
129
147
|
|
|
130
148
|
pointerDown(row);
|
|
131
|
-
await
|
|
149
|
+
await advance(THRESHOLD - 1);
|
|
150
|
+
assert.equal(fired, 0, "the threshold had not elapsed yet");
|
|
151
|
+
|
|
152
|
+
await advance(1);
|
|
132
153
|
|
|
133
154
|
assert.equal(fired, 1);
|
|
134
155
|
});
|
|
@@ -138,9 +159,9 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
138
159
|
const row = mount({ onLongPress: () => fired++ });
|
|
139
160
|
|
|
140
161
|
pointerDown(row);
|
|
141
|
-
await
|
|
162
|
+
await advance(THRESHOLD - 1);
|
|
142
163
|
pointerUp();
|
|
143
|
-
await
|
|
164
|
+
await advance(THRESHOLD);
|
|
144
165
|
|
|
145
166
|
assert.equal(fired, 0);
|
|
146
167
|
});
|
|
@@ -153,9 +174,9 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
153
174
|
const row = mount({ onLongPress: () => fired++ });
|
|
154
175
|
|
|
155
176
|
pointerDown(row);
|
|
156
|
-
await
|
|
177
|
+
await advance(THRESHOLD - 1);
|
|
157
178
|
pointerCancel(row);
|
|
158
|
-
await
|
|
179
|
+
await advance(THRESHOLD);
|
|
159
180
|
|
|
160
181
|
assert.equal(fired, 0);
|
|
161
182
|
});
|
|
@@ -165,7 +186,7 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
165
186
|
const row = mount({ onLongPress: () => fired++, isDisabled: true });
|
|
166
187
|
|
|
167
188
|
pointerDown(row);
|
|
168
|
-
await
|
|
189
|
+
await advance(THRESHOLD);
|
|
169
190
|
|
|
170
191
|
assert.equal(fired, 0);
|
|
171
192
|
});
|
|
@@ -175,7 +196,7 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
175
196
|
const row = mount({ onLongPress: () => fired++ });
|
|
176
197
|
|
|
177
198
|
pointerDown(row);
|
|
178
|
-
await
|
|
199
|
+
await advance(THRESHOLD);
|
|
179
200
|
assert.equal(
|
|
180
201
|
fired,
|
|
181
202
|
1,
|
|
@@ -228,7 +249,7 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
228
249
|
"the touch long-press menu is still suppressed",
|
|
229
250
|
);
|
|
230
251
|
pointerUpOn(row);
|
|
231
|
-
await
|
|
252
|
+
await advance(AFTER_ARIA_CONTEXTMENU_TEARDOWN);
|
|
232
253
|
|
|
233
254
|
assert.equal(
|
|
234
255
|
dispatchContextMenu(row).defaultPrevented,
|
|
@@ -238,15 +259,15 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
238
259
|
});
|
|
239
260
|
|
|
240
261
|
it("does not suppress the keyboard menu after a touch tap that raised no menu", async () => {
|
|
241
|
-
// A tap that lifts without a menu must still disarm suppression. The
|
|
242
|
-
// clears react-aria's own transient post-touch contextmenu
|
|
243
|
-
// which it removes shortly after pointerup — in a browser a
|
|
244
|
-
// arrives long after that
|
|
262
|
+
// A tap that lifts without a menu must still disarm suppression. The
|
|
263
|
+
// advance clears react-aria's own transient post-touch contextmenu
|
|
264
|
+
// listener, which it removes shortly after pointerup — in a browser a
|
|
265
|
+
// keyboard menu arrives long after that, so only this hook's ref decides.
|
|
245
266
|
const row = mount({ onLongPress: () => undefined });
|
|
246
267
|
|
|
247
268
|
pointerDown(row, "touch");
|
|
248
269
|
pointerUpOn(row);
|
|
249
|
-
await
|
|
270
|
+
await advance(AFTER_ARIA_CONTEXTMENU_TEARDOWN);
|
|
250
271
|
|
|
251
272
|
assert.equal(dispatchContextMenu(row).defaultPrevented, false);
|
|
252
273
|
});
|