@samitouri / QOS-React / commits / 5a20d449f6

[TestUtils] Copy type to nativeEvent in Simulate.<eventName> (#6154)

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.

Evan Jacobs committed Jun 26, 2016 at 15:11 UTC 5a20d449f64ff2d9ef3d4346655244f435880327
2 files changed +21
src/test/ReactTestUtils.js
+2
@@ -523,6 +523,8 @@ function makeSimulator(eventType) {
523
524 var fakeNativeEvent = new Event();
525 fakeNativeEvent.target = node;
526 + fakeNativeEvent.type = eventType.toLowerCase();
527 +
528 // We don't use SyntheticEvent.getPooled in order to not have to worry about
529 // properly destroying any properties assigned from `eventData` upon release
530 var event = new SyntheticEvent(
src/test/__tests__/ReactTestUtils-test.js
+19
@@ -558,4 +558,23 @@ describe('ReactTestUtils', function() {
558 expect(hrs.length).toBe(2);
559 });
560
561 + describe('Simulate', () => {
562 + it('should set the type of the event', () => {
563 + let event;
564 + const stub = jest.genMockFn().mockImpl((e) => {
565 + e.persist();
566 + event = e;
567 + });
568 +
569 + const container = document.createElement('div');
570 + const instance = ReactDOM.render(<div onKeyDown={stub} />, container);
571 + const node = ReactDOM.findDOMNode(instance);
572 +
573 + ReactTestUtils.Simulate.keyDown(node);
574 +
575 + expect(event.type).toBe('keydown');
576 + expect(event.nativeEvent.type).toBe('keydown');
577 + });
578 + });
579 +
580 });