Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correct event type into SimulateNative methods #11563

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/react-dom/src/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,34 @@ describe('ReactTestUtils', () => {
jasmine.objectContaining({target: input}),
);
});

it('should set the correct event type when simulating synthetic events', () => {
expect.assertions(1);

let handler = function(event) {
expect(event.nativeEvent.type).toEqual('click');
};

let button = ReactTestUtils.renderIntoDocument(
<button onClick={handler} />,
);

ReactTestUtils.Simulate.click(button);
});

it('should set the correct event type when simulating native events', () => {
expect.assertions(1);

let handler = function(event) {
expect(event.nativeEvent.type).toEqual('click');
};

let button = ReactTestUtils.renderIntoDocument(
<button onClick={handler} />,
);

ReactTestUtils.SimulateNative.click(button);
});
});

it('should call setState callback with no arguments', () => {
Expand Down
16 changes: 9 additions & 7 deletions packages/react-dom/src/test-utils/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ var {

var topLevelTypes = BrowserEventConstants.topLevelTypes;

function Event(suffix) {}
function FakeNativeEvent(type) {
this.type = type;
}

/**
* @class ReactTestUtils
Expand Down Expand Up @@ -359,9 +361,8 @@ function makeSimulator(eventType) {
var dispatchConfig =
EventPluginRegistry.eventNameDispatchConfigs[eventType];

var fakeNativeEvent = new Event();
var fakeNativeEvent = new FakeNativeEvent(eventType.toLowerCase());
fakeNativeEvent.target = domNode;
fakeNativeEvent.type = eventType.toLowerCase();

// We don't use SyntheticEvent.getPooled in order to not have to worry about
// properly destroying any properties assigned from `eventData` upon release
Expand Down Expand Up @@ -438,20 +439,21 @@ buildSimulators();
* to dispatch synthetic events.
*/

function makeNativeSimulator(eventType) {
function makeNativeSimulator(topLevelType) {
return function(domComponentOrNode, nativeEventData) {
var fakeNativeEvent = new Event(eventType);
var eventType = topLevelTypes[topLevelType];
var fakeNativeEvent = new FakeNativeEvent(eventType);
Object.assign(fakeNativeEvent, nativeEventData);
if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
ReactTestUtils.simulateNativeEventOnDOMComponent(
eventType,
topLevelType,
domComponentOrNode,
fakeNativeEvent,
);
} else if (domComponentOrNode.tagName) {
// Will allow on actual dom nodes.
ReactTestUtils.simulateNativeEventOnNode(
eventType,
topLevelType,
domComponentOrNode,
fakeNativeEvent,
);
Expand Down