Skip to content

Commit

Permalink
[TestUtils] Copy type to nativeEvent in Simulate.<eventName> (#6154)
Browse files Browse the repository at this point in the history
Although it is unreasonable to set every possible property for
simulated events, `type` is useful for event handlers that are shared
between types and potentially have different behaviors.
(cherry picked from commit 5a20d44)
  • Loading branch information
Evan Jacobs authored and zpao committed Jul 13, 2016
1 parent 343033b commit 3b80d4d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ function makeSimulator(eventType) {

var fakeNativeEvent = new Event();
fakeNativeEvent.target = node;
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
var event = new SyntheticEvent(
Expand Down
19 changes: 19 additions & 0 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,23 @@ describe('ReactTestUtils', function() {
expect(hrs.length).toBe(2);
});

describe('Simulate', () => {
it('should set the type of the event', () => {
let event;
const stub = jest.genMockFn().mockImpl((e) => {
e.persist();
event = e;
});

const container = document.createElement('div');
const instance = ReactDOM.render(<div onKeyDown={stub} />, container);
const node = ReactDOM.findDOMNode(instance);

ReactTestUtils.Simulate.keyDown(node);

expect(event.type).toBe('keydown');
expect(event.nativeEvent.type).toBe('keydown');
});
});

});

0 comments on commit 3b80d4d

Please sign in to comment.