@samitouri / QOS-React-2 / commits / be0551d01e

Release syntheticEvent.target on the destructor

Toru Kobayashi committed Jan 13, 2016 at 02:03 UTC be0551d01e1e1de2fa0899703c3c217c9123dc21
3 files changed +31 -10
src/renderers/dom/client/syntheticEvents/SyntheticEvent.js
+6 -4
@@ -23,6 +23,7 @@ var warning = require('warning');
23 */
24 var EventInterface = {
25 type: null,
26 + target: null,
27 // currentTarget is set when dispatching; no use in copying it here
28 currentTarget: emptyFunction.thatReturnsNull,
29 eventPhase: null,
@@ -55,10 +56,7 @@ var EventInterface = {
56 function SyntheticEvent(dispatchConfig, targetInst, nativeEvent, nativeEventTarget) {
57 this.dispatchConfig = dispatchConfig;
58 this._targetInst = targetInst;
58 -
59 this.nativeEvent = nativeEvent;
60 - this.target = nativeEventTarget;
61 - this.currentTarget = nativeEventTarget;
60
61 var Interface = this.constructor.Interface;
62 for (var propName in Interface) {
@@ -69,7 +67,11 @@ function SyntheticEvent(dispatchConfig, targetInst, nativeEvent, nativeEventTarg
67 if (normalize) {
68 this[propName] = normalize(nativeEvent);
69 } else {
72 - this[propName] = nativeEvent[propName];
70 + if (propName === 'target') {
71 + this.target = nativeEventTarget;
72 + } else {
73 + this[propName] = nativeEvent[propName];
74 + }
75 }
76 }
77
src/renderers/dom/client/syntheticEvents/__tests__/SyntheticEvent-test.js
+9
@@ -72,6 +72,15 @@ describe('SyntheticEvent', function() {
72 expect(syntheticEvent.isPersistent()).toBe(true);
73 });
74
75 + it('should be nullified if the synthetic event has called destructor', function() {
76 + var target = document.createElement('div');
77 + var syntheticEvent = createEvent({srcElement: target});
78 + syntheticEvent.destructor();
79 + expect(syntheticEvent.type).toBe(null);
80 + expect(syntheticEvent.nativeEvent).toBe(null);
81 + expect(syntheticEvent.target).toBe(null);
82 + });
83 +
84 it('should warn if the synthetic event has been released when calling `preventDefault`', function() {
85 spyOn(console, 'error');
86 var syntheticEvent = createEvent({});
src/test/__tests__/ReactTestUtils-test.js
+16 -6
@@ -431,15 +431,20 @@ describe('ReactTestUtils', function() {
431 });
432
433 it('should change the value of an input field', function() {
434 - var handler = jasmine.createSpy('spy');
434 + var obj = {
435 + handler: function(e) {
436 + e.persist();
437 + },
438 + };
439 + spyOn(obj, 'handler').andCallThrough();
440 var container = document.createElement('div');
436 - var instance = ReactDOM.render(<input type="text" onChange={handler} />, container);
441 + var instance = ReactDOM.render(<input type="text" onChange={obj.handler} />, container);
442
443 var node = ReactDOM.findDOMNode(instance);
444 node.value = 'giraffe';
445 ReactTestUtils.Simulate.change(node);
446
442 - expect(handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
447 + expect(obj.handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
448 });
449
450 it('should change the value of an input field in a component', function() {
@@ -453,15 +458,20 @@ describe('ReactTestUtils', function() {
458 },
459 });
460
456 - var handler = jasmine.createSpy('spy');
461 + var obj = {
462 + handler: function(e) {
463 + e.persist();
464 + },
465 + };
466 + spyOn(obj, 'handler').andCallThrough();
467 var container = document.createElement('div');
458 - var instance = ReactDOM.render(<SomeComponent handleChange={handler} />, container);
468 + var instance = ReactDOM.render(<SomeComponent handleChange={obj.handler} />, container);
469
470 var node = ReactDOM.findDOMNode(instance.refs.input);
471 node.value = 'zebra';
472 ReactTestUtils.Simulate.change(node);
473
464 - expect(handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
474 + expect(obj.handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
475 });
476
477 it('should throw when attempting to use ReactTestUtils.Simulate with shallow rendering', function() {