[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.
(cherry picked from commit 5a20d449f6)
This commit is contained in:
Evan Jacobs
2016-07-13 11:39:39 -07:00
committed by Paul O’Shannessy
parent 343033bf06
commit 3b80d4dcd7
2 changed files with 21 additions and 0 deletions
+2
View File
@@ -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(
+19
View File
@@ -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');
});
});
});