-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
MixedResponders-test-internal.js
331 lines (282 loc) · 9 KB
/
MixedResponders-test-internal.js
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
import {createEventTarget} from '../testing-library';
let React;
let ReactFeatureFlags;
let ReactDOM;
let Scheduler;
describe('mixing responders with the heritage event system', () => {
let container;
beforeEach(() => {
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableFlareAPI = true;
React = require('react');
ReactDOM = require('react-dom');
Scheduler = require('scheduler');
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
container = null;
});
it('should properly only flush sync once when the event systems are mixed', () => {
const useTap = require('react-interactions/events/tap').useTap;
const ref = React.createRef();
let renderCounts = 0;
function MyComponent() {
const [, updateCounter] = React.useState(0);
renderCounts++;
function handleTap() {
updateCounter(count => count + 1);
}
const listener = useTap({
onTapEnd: handleTap,
});
return (
<div>
<button
ref={ref}
listeners={listener}
onClick={() => {
updateCounter(count => count + 1);
}}>
Press me
</button>
</div>
);
}
const newContainer = document.createElement('div');
const root = ReactDOM.unstable_createRoot(newContainer);
document.body.appendChild(newContainer);
root.render(<MyComponent />);
Scheduler.unstable_flushAll();
const target = createEventTarget(ref.current);
target.pointerdown({timeStamp: 100});
target.pointerup({timeStamp: 100});
target.click({timeStamp: 100});
if (__DEV__) {
expect(renderCounts).toBe(2);
} else {
expect(renderCounts).toBe(1);
}
Scheduler.unstable_flushAll();
if (__DEV__) {
expect(renderCounts).toBe(4);
} else {
expect(renderCounts).toBe(2);
}
target.pointerdown({timeStamp: 100});
target.pointerup({timeStamp: 100});
// Ensure the timeStamp logic works
target.click({timeStamp: 101});
if (__DEV__) {
expect(renderCounts).toBe(6);
} else {
expect(renderCounts).toBe(3);
}
Scheduler.unstable_flushAll();
document.body.removeChild(newContainer);
});
it('should properly flush sync when the event systems are mixed with unstable_flushDiscreteUpdates', () => {
const useTap = require('react-interactions/events/tap').useTap;
const ref = React.createRef();
let renderCounts = 0;
function MyComponent() {
const [, updateCounter] = React.useState(0);
renderCounts++;
function handleTap() {
updateCounter(count => count + 1);
}
const listener = useTap({
onTapEnd: handleTap,
});
return (
<div>
<button
ref={ref}
listeners={listener}
onClick={() => {
// This should flush synchronously
ReactDOM.unstable_flushDiscreteUpdates();
updateCounter(count => count + 1);
}}>
Press me
</button>
</div>
);
}
const newContainer = document.createElement('div');
const root = ReactDOM.unstable_createRoot(newContainer);
document.body.appendChild(newContainer);
root.render(<MyComponent />);
Scheduler.unstable_flushAll();
const target = createEventTarget(ref.current);
target.pointerdown({timeStamp: 100});
target.pointerup({timeStamp: 100});
if (__DEV__) {
expect(renderCounts).toBe(4);
} else {
expect(renderCounts).toBe(2);
}
Scheduler.unstable_flushAll();
if (__DEV__) {
expect(renderCounts).toBe(6);
} else {
expect(renderCounts).toBe(3);
}
target.pointerdown({timeStamp: 100});
// Ensure the timeStamp logic works
target.pointerup({timeStamp: 101});
if (__DEV__) {
expect(renderCounts).toBe(8);
} else {
expect(renderCounts).toBe(4);
}
Scheduler.unstable_flushAll();
document.body.removeChild(newContainer);
});
it(
'should only flush before outermost discrete event handler when mixing ' +
'event systems',
async () => {
const {useState} = React;
const useTap = require('react-interactions/events/tap').useTap;
const button = React.createRef();
const ops = [];
function MyComponent() {
const [pressesCount, updatePressesCount] = useState(0);
const [clicksCount, updateClicksCount] = useState(0);
function handleTap() {
// This dispatches a synchronous, discrete event in the legacy event
// system. However, because it's nested inside the new event system,
// its updates should not flush until the end of the outer handler.
const target = createEventTarget(button.current);
target.click();
// Text context should not have changed
ops.push(newContainer.textContent);
updatePressesCount(pressesCount + 1);
}
const tap = useTap({
onTapEnd: handleTap,
});
return (
<div>
<button
listeners={tap}
ref={button}
onClick={() => updateClicksCount(clicksCount + 1)}>
Presses: {pressesCount}, Clicks: {clicksCount}
</button>
</div>
);
}
const newContainer = document.createElement('div');
document.body.appendChild(newContainer);
const root = ReactDOM.unstable_createRoot(newContainer);
root.render(<MyComponent />);
Scheduler.unstable_flushAll();
expect(newContainer.textContent).toEqual('Presses: 0, Clicks: 0');
const target = createEventTarget(button.current);
target.pointerdown({timeStamp: 100});
target.pointerup({timeStamp: 100});
Scheduler.unstable_flushAll();
expect(newContainer.textContent).toEqual('Presses: 1, Clicks: 1');
expect(ops).toEqual(['Presses: 0, Clicks: 0']);
},
);
describe('mixing the Input and Press repsonders', () => {
it('is async for non-input events', () => {
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
const useTap = require('react-interactions/events/tap').useTap;
const useInput = require('react-interactions/events/input').useInput;
const root = ReactDOM.unstable_createRoot(container);
let input;
let ops = [];
function Component({innerRef, onChange, controlledValue, listeners}) {
const inputListener = useInput({onChange});
return (
<input
type="text"
ref={innerRef}
value={controlledValue}
listeners={[inputListener, listeners]}
/>
);
}
function PressWrapper({innerRef, onTap, onChange, controlledValue}) {
const tap = useTap({
onTapEnd: onTap,
});
return (
<Component
onChange={onChange}
innerRef={el => (input = el)}
controlledValue={controlledValue}
listeners={tap}
/>
);
}
class ControlledInput extends React.Component {
state = {value: 'initial'};
onChange = event => this.setState({value: event.target.value});
reset = () => {
this.setState({value: ''});
};
render() {
ops.push(`render: ${this.state.value}`);
const controlledValue =
this.state.value === 'changed' ? 'changed [!]' : this.state.value;
return (
<PressWrapper
onTap={this.reset}
onChange={this.onChange}
innerRef={el => (input = el)}
controlledValue={controlledValue}
/>
);
}
}
// Initial mount. Test that this is async.
root.render(<ControlledInput />);
// Should not have flushed yet.
expect(ops).toEqual([]);
expect(input).toBe(undefined);
// Flush callbacks.
Scheduler.unstable_flushAll();
expect(ops).toEqual(['render: initial']);
expect(input.value).toBe('initial');
ops = [];
// Trigger a click event
input.dispatchEvent(
new MouseEvent('mousedown', {
bubbles: true,
cancelable: true,
buttons: 1,
}),
);
input.dispatchEvent(
new MouseEvent('mouseup', {
bubbles: true,
cancelable: true,
buttons: 0,
}),
);
// Nothing should have changed
expect(ops).toEqual([]);
expect(input.value).toBe('initial');
// Flush callbacks.
Scheduler.unstable_flushAll();
// Now the click update has flushed.
expect(ops).toEqual(['render: ']);
expect(input.value).toBe('');
});
});
});