From 17f4c15433d1902586a963b5d109c2a075b911d7 Mon Sep 17 00:00:00 2001 From: Evan Jacobs Date: Mon, 29 Feb 2016 21:40:48 -0500 Subject: [PATCH] [TestUtils] Implement type property in Simulate. 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. --- src/test/ReactTestUtils.js | 2 ++ src/test/__tests__/ReactTestUtils-test.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/test/ReactTestUtils.js b/src/test/ReactTestUtils.js index 4f8e4ebb38c2b..a113da08be7e2 100644 --- a/src/test/ReactTestUtils.js +++ b/src/test/ReactTestUtils.js @@ -491,6 +491,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( diff --git a/src/test/__tests__/ReactTestUtils-test.js b/src/test/__tests__/ReactTestUtils-test.js index 0e08a97b39b55..976d2bcbcf25e 100644 --- a/src/test/__tests__/ReactTestUtils-test.js +++ b/src/test/__tests__/ReactTestUtils-test.js @@ -486,4 +486,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(
, container); + const node = ReactDOM.findDOMNode(instance); + + ReactTestUtils.Simulate.keyDown(node); + + expect(event.type).toBe('keydown'); + expect(event.nativeEvent.type).toBe('keydown'); + }); + }); + });