Skip to content

Commit

Permalink
Include rootEventTypes in DOMEventResponderSystem stopPropagation tes…
Browse files Browse the repository at this point in the history
…ts (#15433)
  • Loading branch information
necolas authored Apr 17, 2019
1 parent 1ae409d commit 0b50fb2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/react-dom/src/events/DOMEventResponderSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,8 @@ function traverseAndTriggerEventResponderInstances(
let shouldStopPropagation = false;
let responderEvent;

// Capture target phase
if (length > 0) {
// Capture target phase
responderEvent = createResponderEvent(
((topLevelType: any): string),
nativeEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ function phaseToString(phase) {
: 'capture';
}

function dispatchEvent(element, type) {
const event = document.createEvent('Event');
event.initEvent(type, true, true);
element.dispatchEvent(event);
}

function dispatchClickEvent(element) {
const clickEvent = document.createEvent('Event');
clickEvent.initEvent('click', true, true);
element.dispatchEvent(clickEvent);
dispatchEvent(element, 'click');
}

function createReactEventTarget(type) {
Expand Down Expand Up @@ -308,15 +312,23 @@ describe('DOMEventResponderSystem', () => {
['click'],
undefined,
(event, context, props) => {
eventLog.push(`A [${phaseToString(event.phase)}]`);
context.addRootEventTypes(event.target.ownerDocument, [
'click',
'pointermove',
]);
eventLog.push(`A [${event.type}, ${phaseToString(event.phase)}]`);
},
);

const ClickEventComponentB = createReactEventComponent(
['click'],
undefined,
(event, context, props) => {
eventLog.push(`B [${phaseToString(event.phase)}]`);
context.addRootEventTypes(event.target.ownerDocument, [
'click',
'pointermove',
]);
eventLog.push(`B [${event.type}, ${phaseToString(event.phase)}]`);
if (event.phase === stopPropagationOnPhase) {
return true;
}
Expand All @@ -337,12 +349,26 @@ describe('DOMEventResponderSystem', () => {
ReactDOM.render(<Test />, container);
let buttonElement = buttonRef.current;
dispatchClickEvent(buttonElement);
dispatchEvent(buttonElement, 'pointermove');
}

runTestWithPhase(BUBBLE_PHASE);
expect(eventLog).toEqual(['A [capture]', 'B [capture]', 'B [bubble]']);
// Root phase should not be skipped for different event type
expect(eventLog).toEqual([
'A [click, capture]',
'B [click, capture]',
'B [click, bubble]',
'A [pointermove, root]',
'B [pointermove, root]',
]);
runTestWithPhase(CAPTURE_PHASE);
expect(eventLog).toEqual(['A [capture]', 'B [capture]']);
// Root phase should not be skipped for different event type
expect(eventLog).toEqual([
'A [click, capture]',
'B [click, capture]',
'A [pointermove, root]',
'B [pointermove, root]',
]);
});

it('nested event responders and their onEvent() should fire in the correct order with stopPropagation #2', () => {
Expand All @@ -354,7 +380,11 @@ describe('DOMEventResponderSystem', () => {
['click'],
undefined,
(event, context, props) => {
eventLog.push(`A [${phaseToString(event.phase)}]`);
context.addRootEventTypes(event.target.ownerDocument, [
'click',
'pointermove',
]);
eventLog.push(`A [${event.type}, ${phaseToString(event.phase)}]`);
if (event.phase === stopPropagationOnPhase) {
return true;
}
Expand All @@ -365,7 +395,11 @@ describe('DOMEventResponderSystem', () => {
['click'],
undefined,
(event, context, props) => {
eventLog.push(`B [${phaseToString(event.phase)}]`);
context.addRootEventTypes(event.target.ownerDocument, [
'click',
'pointermove',
]);
eventLog.push(`B [${event.type}, ${phaseToString(event.phase)}]`);
},
);

Expand All @@ -383,17 +417,26 @@ describe('DOMEventResponderSystem', () => {
ReactDOM.render(<Test />, container);
let buttonElement = buttonRef.current;
dispatchClickEvent(buttonElement);
dispatchEvent(buttonElement, 'pointermove');
}

runTestWithPhase(BUBBLE_PHASE);
// Root phase should not be skipped for different event type
expect(eventLog).toEqual([
'A [capture]',
'B [capture]',
'B [bubble]',
'A [bubble]',
'A [click, capture]',
'B [click, capture]',
'B [click, bubble]',
'A [click, bubble]',
'A [pointermove, root]',
'B [pointermove, root]',
]);
runTestWithPhase(CAPTURE_PHASE);
expect(eventLog).toEqual(['A [capture]']);
// Root phase should not be skipped for different event type
expect(eventLog).toEqual([
'A [click, capture]',
'A [pointermove, root]',
'B [pointermove, root]',
]);
});

it('custom event dispatching for click -> magicClick works', () => {
Expand Down

0 comments on commit 0b50fb2

Please sign in to comment.