Skip to content

Commit e13977c

Browse files
committed
Merge pull request #939 from spicyj/simulate-synthetic
Simulate synthetic events using ReactTestUtils
2 parents 42473b2 + a447d30 commit e13977c

File tree

9 files changed

+142
-56
lines changed

9 files changed

+142
-56
lines changed

src/browser/ReactEventEmitter.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ var ReactEventEmitter = merge(ReactEventEmitterMixin, {
318318
}
319319
},
320320

321+
eventNameDispatchConfigs: EventPluginHub.eventNameDispatchConfigs,
322+
321323
registrationNameModules: EventPluginHub.registrationNameModules,
322324

323325
putListener: EventPluginHub.putListener,

src/browser/__tests__/ReactEventEmitter-test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ describe('ReactEventEmitter', function() {
142142
it('should not invoke handlers if ReactEventEmitter is disabled', function() {
143143
registerSimpleTestHandler();
144144
ReactEventEmitter.setEnabled(false);
145-
ReactTestUtils.Simulate.click(CHILD);
145+
ReactTestUtils.SimulateNative.click(CHILD);
146146
expect(LISTENER.mock.calls.length).toBe(0);
147147
ReactEventEmitter.setEnabled(true);
148-
ReactTestUtils.Simulate.click(CHILD);
148+
ReactTestUtils.SimulateNative.click(CHILD);
149149
expect(LISTENER.mock.calls.length).toBe(1);
150150
});
151151

@@ -312,11 +312,11 @@ describe('ReactEventEmitter', function() {
312312
ON_TOUCH_TAP_KEY,
313313
recordID.bind(null, getID(CHILD))
314314
);
315-
ReactTestUtils.Simulate.touchStart(
315+
ReactTestUtils.SimulateNative.touchStart(
316316
CHILD,
317317
ReactTestUtils.nativeTouchData(0, 0)
318318
);
319-
ReactTestUtils.Simulate.touchEnd(
319+
ReactTestUtils.SimulateNative.touchEnd(
320320
CHILD,
321321
ReactTestUtils.nativeTouchData(0, 0)
322322
);
@@ -330,11 +330,11 @@ describe('ReactEventEmitter', function() {
330330
ON_TOUCH_TAP_KEY,
331331
recordID.bind(null, getID(CHILD))
332332
);
333-
ReactTestUtils.Simulate.touchStart(
333+
ReactTestUtils.SimulateNative.touchStart(
334334
CHILD,
335335
ReactTestUtils.nativeTouchData(0, 0)
336336
);
337-
ReactTestUtils.Simulate.touchEnd(
337+
ReactTestUtils.SimulateNative.touchEnd(
338338
CHILD,
339339
ReactTestUtils.nativeTouchData(0, tapMoveThreshold - 1)
340340
);
@@ -348,11 +348,11 @@ describe('ReactEventEmitter', function() {
348348
ON_TOUCH_TAP_KEY,
349349
recordID.bind(null, getID(CHILD))
350350
);
351-
ReactTestUtils.Simulate.touchStart(
351+
ReactTestUtils.SimulateNative.touchStart(
352352
CHILD,
353353
ReactTestUtils.nativeTouchData(0, 0)
354354
);
355-
ReactTestUtils.Simulate.touchEnd(
355+
ReactTestUtils.SimulateNative.touchEnd(
356356
CHILD,
357357
ReactTestUtils.nativeTouchData(0, tapMoveThreshold + 1)
358358
);
@@ -415,11 +415,11 @@ describe('ReactEventEmitter', function() {
415415
ON_TOUCH_TAP_KEY,
416416
recordID.bind(null, getID(GRANDPARENT))
417417
);
418-
ReactTestUtils.Simulate.touchStart(
418+
ReactTestUtils.SimulateNative.touchStart(
419419
CHILD,
420420
ReactTestUtils.nativeTouchData(0, 0)
421421
);
422-
ReactTestUtils.Simulate.touchEnd(
422+
ReactTestUtils.SimulateNative.touchEnd(
423423
CHILD,
424424
ReactTestUtils.nativeTouchData(0, 0)
425425
);

src/browser/dom/components/__tests__/ReactDOMInput-test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ describe('ReactDOMInput', function() {
125125
var node = renderTextInput(stub);
126126

127127
node.value = 'giraffe';
128-
ReactTestUtils.Simulate.input(node);
128+
ReactTestUtils.Simulate.change(node);
129129
expect(node.value).toBe('0');
130130
});
131131

@@ -171,9 +171,8 @@ describe('ReactDOMInput', function() {
171171
aNode.checked = false;
172172
expect(cNode.checked).toBe(true);
173173

174-
// Now let's run the actual ReactDOMInput change event handler (on radio
175-
// inputs, ChangeEventPlugin listens for the `click` event so trigger that)
176-
ReactTestUtils.Simulate.click(bNode);
174+
// Now let's run the actual ReactDOMInput change event handler
175+
ReactTestUtils.Simulate.change(bNode);
177176

178177
// The original state should have been restored
179178
expect(aNode.checked).toBe(true);
@@ -192,7 +191,7 @@ describe('ReactDOMInput', function() {
192191
expect(link.requestChange.mock.calls.length).toBe(0);
193192

194193
instance.getDOMNode().value = 'test';
195-
ReactTestUtils.Simulate.input(instance.getDOMNode());
194+
ReactTestUtils.Simulate.change(instance.getDOMNode());
196195

197196
expect(link.requestChange.mock.calls.length).toBe(1);
198197
expect(link.requestChange.mock.calls[0][0]).toEqual('test');
@@ -265,7 +264,7 @@ describe('ReactDOMInput', function() {
265264
expect(link.requestChange.mock.calls.length).toBe(0);
266265

267266
instance.getDOMNode().checked = false;
268-
ReactTestUtils.Simulate.click(instance.getDOMNode());
267+
ReactTestUtils.Simulate.change(instance.getDOMNode());
269268

270269
expect(link.requestChange.mock.calls.length).toBe(1);
271270
expect(link.requestChange.mock.calls[0][0]).toEqual(false);

src/browser/dom/components/__tests__/ReactDOMTextarea-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('ReactDOMTextarea', function() {
139139
var node = renderTextarea(stub);
140140

141141
node.value = 'giraffe';
142-
ReactTestUtils.Simulate.input(node);
142+
ReactTestUtils.Simulate.change(node);
143143
expect(node.value).toBe('0');
144144
});
145145

@@ -216,7 +216,7 @@ describe('ReactDOMTextarea', function() {
216216
expect(link.requestChange.mock.calls.length).toBe(0);
217217

218218
instance.getDOMNode().value = 'test';
219-
ReactTestUtils.Simulate.input(instance.getDOMNode());
219+
ReactTestUtils.Simulate.change(instance.getDOMNode());
220220

221221
expect(link.requestChange.mock.calls.length).toBe(1);
222222
expect(link.requestChange.mock.calls[0][0]).toEqual('test');

src/browser/eventPlugins/__tests__/AnalyticsEventPlugin-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ describe('AnalyticsEventPlugin', function() {
9999

100100
// Simulate some clicks
101101
for (var i = 0; i < numClickEvents; i++) {
102-
ReactTestUtils.Simulate.click(renderedComponent.refs.testDiv);
102+
ReactTestUtils.SimulateNative.click(renderedComponent.refs.testDiv);
103103
}
104104
// Simulate some double clicks
105105
for (i = 0; i < numDoubleClickEvents; i++) {
106-
ReactTestUtils.Simulate.doubleClick(renderedComponent.refs.testDiv);
106+
ReactTestUtils.SimulateNative.doubleClick(renderedComponent.refs.testDiv);
107107
}
108108
// Simulate some other events not being tracked for analytics
109-
ReactTestUtils.Simulate.focus(renderedComponent.refs.testDiv);
109+
ReactTestUtils.SimulateNative.focus(renderedComponent.refs.testDiv);
110110

111111
window.mockRunTimersOnce();
112112
expect(cb).toBeCalled();
@@ -143,7 +143,7 @@ describe('AnalyticsEventPlugin', function() {
143143

144144
var error = false;
145145
try {
146-
ReactTestUtils.Simulate.click(renderedComponent.refs.testDiv);
146+
ReactTestUtils.SimulateNative.click(renderedComponent.refs.testDiv);
147147
} catch(e) {
148148
error = true;
149149
}

src/core/__tests__/ReactBind-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ describe('autobinding', function() {
5252
render: function() {
5353
return (
5454
<div
55-
onMouseEnter={this.onMouseEnter.bind(this)}
56-
onMouseLeave={this.onMouseLeave}
55+
onMouseOver={this.onMouseEnter.bind(this)}
56+
onMouseOut={this.onMouseLeave}
5757
onClick={this.onClick}
5858
/>
5959
);

src/event/EventPluginHub.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ var EventPluginHub = {
141141

142142
},
143143

144+
eventNameDispatchConfigs: EventPluginRegistry.eventNameDispatchConfigs,
145+
144146
registrationNameModules: EventPluginRegistry.registrationNameModules,
145147

146148
/**

src/event/EventPluginRegistry.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ function recomputePluginOrdering() {
8585
* @private
8686
*/
8787
function publishEventForPlugin(dispatchConfig, PluginModule, eventName) {
88+
invariant(
89+
!EventPluginRegistry.eventNameDispatchConfigs[eventName],
90+
'EventPluginHub: More than one plugin attempted to publish the same ' +
91+
'event name, `%s`.',
92+
eventName
93+
);
94+
EventPluginRegistry.eventNameDispatchConfigs[eventName] = dispatchConfig;
95+
8896
var phasedRegistrationNames = dispatchConfig.phasedRegistrationNames;
8997
if (phasedRegistrationNames) {
9098
for (var phaseName in phasedRegistrationNames) {
@@ -142,7 +150,12 @@ var EventPluginRegistry = {
142150
plugins: [],
143151

144152
/**
145-
* Mapping from registration names to plugin modules.
153+
* Mapping from event name to dispatch config
154+
*/
155+
eventNameDispatchConfigs: {},
156+
157+
/**
158+
* Mapping from registration name to plugin module
146159
*/
147160
registrationNameModules: {},
148161

@@ -243,6 +256,14 @@ var EventPluginRegistry = {
243256
}
244257
}
245258
EventPluginRegistry.plugins.length = 0;
259+
260+
var eventNameDispatchConfigs = EventPluginRegistry.eventNameDispatchConfigs;
261+
for (var eventName in eventNameDispatchConfigs) {
262+
if (eventNameDispatchConfigs.hasOwnProperty(eventName)) {
263+
delete eventNameDispatchConfigs[eventName];
264+
}
265+
}
266+
246267
var registrationNameModules = EventPluginRegistry.registrationNameModules;
247268
for (var registrationName in registrationNameModules) {
248269
if (registrationNameModules.hasOwnProperty(registrationName)) {

0 commit comments

Comments
 (0)