From 55289acca243673ff6b4bcdc3a61ee611b791bf6 Mon Sep 17 00:00:00 2001 From: Philipp Spiess Date: Mon, 3 Sep 2018 21:22:37 +0200 Subject: [PATCH] Document event bubble order This is documenting the current order in which events are dispatched when interacting with native document listeners and other React apps. For more context, check out #12919. --- .../src/__tests__/ReactDOMComponent-test.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/packages/react-dom/src/__tests__/ReactDOMComponent-test.js b/packages/react-dom/src/__tests__/ReactDOMComponent-test.js index b83ce5d5e1f36..4dc613805c94f 100644 --- a/packages/react-dom/src/__tests__/ReactDOMComponent-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMComponent-test.js @@ -2603,4 +2603,60 @@ describe('ReactDOMComponent', () => { expect(node.getAttribute('onx')).toBe('bar'); }); }); + + it('receives events in specific order', () => { + let eventOrder = []; + let track = tag => () => eventOrder.push(tag); + let outerRef = React.createRef(); + let innerRef = React.createRef(); + + function OuterReactApp() { + return ( +
+ ); + } + + function InnerReactApp() { + return ( +
+ ); + } + + const container = document.createElement('div'); + document.body.appendChild(container); + + try { + ReactDOM.render(, container); + ReactDOM.render(, outerRef.current); + + document.addEventListener('click', track('document bubble')); + document.addEventListener('click', track('document capture'), true); + + innerRef.current.click(); + + // The order we receive here is not ideal since it is expected that the + // capture listener fire before all bubble listeners. Other React apps + // might depend on this. + // + // @see https://github.com/facebook/react/pull/12919#issuecomment-395224674 + expect(eventOrder).toEqual([ + 'document capture', + 'inner capture', + 'inner bubble', + 'outer capture', + 'outer bubble', + 'document bubble', + ]); + } finally { + document.body.removeChild(container); + } + }); });