mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[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:
committed by
Paul O’Shannessy
parent
343033bf06
commit
3b80d4dcd7
@@ -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(
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user