Release syntheticEvent.target on the destructor

This commit is contained in:
Toru Kobayashi
2016-01-14 15:09:56 +09:00
parent b60e8c255d
commit be0551d01e
3 changed files with 31 additions and 10 deletions
@@ -23,6 +23,7 @@ var warning = require('warning');
*/
var EventInterface = {
type: null,
target: null,
// currentTarget is set when dispatching; no use in copying it here
currentTarget: emptyFunction.thatReturnsNull,
eventPhase: null,
@@ -55,10 +56,7 @@ var EventInterface = {
function SyntheticEvent(dispatchConfig, targetInst, nativeEvent, nativeEventTarget) {
this.dispatchConfig = dispatchConfig;
this._targetInst = targetInst;
this.nativeEvent = nativeEvent;
this.target = nativeEventTarget;
this.currentTarget = nativeEventTarget;
var Interface = this.constructor.Interface;
for (var propName in Interface) {
@@ -69,7 +67,11 @@ function SyntheticEvent(dispatchConfig, targetInst, nativeEvent, nativeEventTarg
if (normalize) {
this[propName] = normalize(nativeEvent);
} else {
this[propName] = nativeEvent[propName];
if (propName === 'target') {
this.target = nativeEventTarget;
} else {
this[propName] = nativeEvent[propName];
}
}
}
@@ -72,6 +72,15 @@ describe('SyntheticEvent', function() {
expect(syntheticEvent.isPersistent()).toBe(true);
});
it('should be nullified if the synthetic event has called destructor', function() {
var target = document.createElement('div');
var syntheticEvent = createEvent({srcElement: target});
syntheticEvent.destructor();
expect(syntheticEvent.type).toBe(null);
expect(syntheticEvent.nativeEvent).toBe(null);
expect(syntheticEvent.target).toBe(null);
});
it('should warn if the synthetic event has been released when calling `preventDefault`', function() {
spyOn(console, 'error');
var syntheticEvent = createEvent({});
+16 -6
View File
@@ -431,15 +431,20 @@ describe('ReactTestUtils', function() {
});
it('should change the value of an input field', function() {
var handler = jasmine.createSpy('spy');
var obj = {
handler: function(e) {
e.persist();
},
};
spyOn(obj, 'handler').andCallThrough();
var container = document.createElement('div');
var instance = ReactDOM.render(<input type="text" onChange={handler} />, container);
var instance = ReactDOM.render(<input type="text" onChange={obj.handler} />, container);
var node = ReactDOM.findDOMNode(instance);
node.value = 'giraffe';
ReactTestUtils.Simulate.change(node);
expect(handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
expect(obj.handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
});
it('should change the value of an input field in a component', function() {
@@ -453,15 +458,20 @@ describe('ReactTestUtils', function() {
},
});
var handler = jasmine.createSpy('spy');
var obj = {
handler: function(e) {
e.persist();
},
};
spyOn(obj, 'handler').andCallThrough();
var container = document.createElement('div');
var instance = ReactDOM.render(<SomeComponent handleChange={handler} />, container);
var instance = ReactDOM.render(<SomeComponent handleChange={obj.handler} />, container);
var node = ReactDOM.findDOMNode(instance.refs.input);
node.value = 'zebra';
ReactTestUtils.Simulate.change(node);
expect(handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
expect(obj.handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
});
it('should throw when attempting to use ReactTestUtils.Simulate with shallow rendering', function() {