Skip to content

Commit

Permalink
Release syntheticEvent.target on the destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Jan 14, 2016
1 parent b60e8c2 commit be0551d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/renderers/dom/client/syntheticEvents/SyntheticEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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];
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down
22 changes: 16 additions & 6 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit be0551d

Please sign in to comment.