Merge pull request #5840 from koba04/release-event-target-on-destructor

Release syntheticEvent.target on the destructor
This commit is contained in:
Paul O’Shannessy
2016-01-15 16:38:42 -08:00
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,
@@ -56,10 +57,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) {
@@ -70,7 +68,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
@@ -404,15 +404,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() {
@@ -426,15 +431,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() {