Skip to content

Commit

Permalink
Merge pull request facebook#939 from spicyj/simulate-synthetic
Browse files Browse the repository at this point in the history
Simulate synthetic events using ReactTestUtils
  • Loading branch information
chenglou committed Feb 19, 2014
2 parents 42473b2 + a447d30 commit e13977c
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 56 deletions.
2 changes: 2 additions & 0 deletions src/browser/ReactEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ var ReactEventEmitter = merge(ReactEventEmitterMixin, {
}
},

eventNameDispatchConfigs: EventPluginHub.eventNameDispatchConfigs,

registrationNameModules: EventPluginHub.registrationNameModules,

putListener: EventPluginHub.putListener,
Expand Down
20 changes: 10 additions & 10 deletions src/browser/__tests__/ReactEventEmitter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ describe('ReactEventEmitter', function() {
it('should not invoke handlers if ReactEventEmitter is disabled', function() {
registerSimpleTestHandler();
ReactEventEmitter.setEnabled(false);
ReactTestUtils.Simulate.click(CHILD);
ReactTestUtils.SimulateNative.click(CHILD);
expect(LISTENER.mock.calls.length).toBe(0);
ReactEventEmitter.setEnabled(true);
ReactTestUtils.Simulate.click(CHILD);
ReactTestUtils.SimulateNative.click(CHILD);
expect(LISTENER.mock.calls.length).toBe(1);
});

Expand Down Expand Up @@ -312,11 +312,11 @@ describe('ReactEventEmitter', function() {
ON_TOUCH_TAP_KEY,
recordID.bind(null, getID(CHILD))
);
ReactTestUtils.Simulate.touchStart(
ReactTestUtils.SimulateNative.touchStart(
CHILD,
ReactTestUtils.nativeTouchData(0, 0)
);
ReactTestUtils.Simulate.touchEnd(
ReactTestUtils.SimulateNative.touchEnd(
CHILD,
ReactTestUtils.nativeTouchData(0, 0)
);
Expand All @@ -330,11 +330,11 @@ describe('ReactEventEmitter', function() {
ON_TOUCH_TAP_KEY,
recordID.bind(null, getID(CHILD))
);
ReactTestUtils.Simulate.touchStart(
ReactTestUtils.SimulateNative.touchStart(
CHILD,
ReactTestUtils.nativeTouchData(0, 0)
);
ReactTestUtils.Simulate.touchEnd(
ReactTestUtils.SimulateNative.touchEnd(
CHILD,
ReactTestUtils.nativeTouchData(0, tapMoveThreshold - 1)
);
Expand All @@ -348,11 +348,11 @@ describe('ReactEventEmitter', function() {
ON_TOUCH_TAP_KEY,
recordID.bind(null, getID(CHILD))
);
ReactTestUtils.Simulate.touchStart(
ReactTestUtils.SimulateNative.touchStart(
CHILD,
ReactTestUtils.nativeTouchData(0, 0)
);
ReactTestUtils.Simulate.touchEnd(
ReactTestUtils.SimulateNative.touchEnd(
CHILD,
ReactTestUtils.nativeTouchData(0, tapMoveThreshold + 1)
);
Expand Down Expand Up @@ -415,11 +415,11 @@ describe('ReactEventEmitter', function() {
ON_TOUCH_TAP_KEY,
recordID.bind(null, getID(GRANDPARENT))
);
ReactTestUtils.Simulate.touchStart(
ReactTestUtils.SimulateNative.touchStart(
CHILD,
ReactTestUtils.nativeTouchData(0, 0)
);
ReactTestUtils.Simulate.touchEnd(
ReactTestUtils.SimulateNative.touchEnd(
CHILD,
ReactTestUtils.nativeTouchData(0, 0)
);
Expand Down
11 changes: 5 additions & 6 deletions src/browser/dom/components/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('ReactDOMInput', function() {
var node = renderTextInput(stub);

node.value = 'giraffe';
ReactTestUtils.Simulate.input(node);
ReactTestUtils.Simulate.change(node);
expect(node.value).toBe('0');
});

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

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

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

instance.getDOMNode().value = 'test';
ReactTestUtils.Simulate.input(instance.getDOMNode());
ReactTestUtils.Simulate.change(instance.getDOMNode());

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

instance.getDOMNode().checked = false;
ReactTestUtils.Simulate.click(instance.getDOMNode());
ReactTestUtils.Simulate.change(instance.getDOMNode());

expect(link.requestChange.mock.calls.length).toBe(1);
expect(link.requestChange.mock.calls[0][0]).toEqual(false);
Expand Down
4 changes: 2 additions & 2 deletions src/browser/dom/components/__tests__/ReactDOMTextarea-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('ReactDOMTextarea', function() {
var node = renderTextarea(stub);

node.value = 'giraffe';
ReactTestUtils.Simulate.input(node);
ReactTestUtils.Simulate.change(node);
expect(node.value).toBe('0');
});

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

instance.getDOMNode().value = 'test';
ReactTestUtils.Simulate.input(instance.getDOMNode());
ReactTestUtils.Simulate.change(instance.getDOMNode());

expect(link.requestChange.mock.calls.length).toBe(1);
expect(link.requestChange.mock.calls[0][0]).toEqual('test');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ describe('AnalyticsEventPlugin', function() {

// Simulate some clicks
for (var i = 0; i < numClickEvents; i++) {
ReactTestUtils.Simulate.click(renderedComponent.refs.testDiv);
ReactTestUtils.SimulateNative.click(renderedComponent.refs.testDiv);
}
// Simulate some double clicks
for (i = 0; i < numDoubleClickEvents; i++) {
ReactTestUtils.Simulate.doubleClick(renderedComponent.refs.testDiv);
ReactTestUtils.SimulateNative.doubleClick(renderedComponent.refs.testDiv);
}
// Simulate some other events not being tracked for analytics
ReactTestUtils.Simulate.focus(renderedComponent.refs.testDiv);
ReactTestUtils.SimulateNative.focus(renderedComponent.refs.testDiv);

window.mockRunTimersOnce();
expect(cb).toBeCalled();
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('AnalyticsEventPlugin', function() {

var error = false;
try {
ReactTestUtils.Simulate.click(renderedComponent.refs.testDiv);
ReactTestUtils.SimulateNative.click(renderedComponent.refs.testDiv);
} catch(e) {
error = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/__tests__/ReactBind-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ describe('autobinding', function() {
render: function() {
return (
<div
onMouseEnter={this.onMouseEnter.bind(this)}
onMouseLeave={this.onMouseLeave}
onMouseOver={this.onMouseEnter.bind(this)}
onMouseOut={this.onMouseLeave}
onClick={this.onClick}
/>
);
Expand Down
2 changes: 2 additions & 0 deletions src/event/EventPluginHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ var EventPluginHub = {

},

eventNameDispatchConfigs: EventPluginRegistry.eventNameDispatchConfigs,

registrationNameModules: EventPluginRegistry.registrationNameModules,

/**
Expand Down
23 changes: 22 additions & 1 deletion src/event/EventPluginRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ function recomputePluginOrdering() {
* @private
*/
function publishEventForPlugin(dispatchConfig, PluginModule, eventName) {
invariant(
!EventPluginRegistry.eventNameDispatchConfigs[eventName],
'EventPluginHub: More than one plugin attempted to publish the same ' +
'event name, `%s`.',
eventName
);
EventPluginRegistry.eventNameDispatchConfigs[eventName] = dispatchConfig;

var phasedRegistrationNames = dispatchConfig.phasedRegistrationNames;
if (phasedRegistrationNames) {
for (var phaseName in phasedRegistrationNames) {
Expand Down Expand Up @@ -142,7 +150,12 @@ var EventPluginRegistry = {
plugins: [],

/**
* Mapping from registration names to plugin modules.
* Mapping from event name to dispatch config
*/
eventNameDispatchConfigs: {},

/**
* Mapping from registration name to plugin module
*/
registrationNameModules: {},

Expand Down Expand Up @@ -243,6 +256,14 @@ var EventPluginRegistry = {
}
}
EventPluginRegistry.plugins.length = 0;

var eventNameDispatchConfigs = EventPluginRegistry.eventNameDispatchConfigs;
for (var eventName in eventNameDispatchConfigs) {
if (eventNameDispatchConfigs.hasOwnProperty(eventName)) {
delete eventNameDispatchConfigs[eventName];
}
}

var registrationNameModules = EventPluginRegistry.registrationNameModules;
for (var registrationName in registrationNameModules) {
if (registrationNameModules.hasOwnProperty(registrationName)) {
Expand Down
Loading

0 comments on commit e13977c

Please sign in to comment.