Skip to content

Commit

Permalink
Add test coverage for doubly nested event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
nhunzaker committed Dec 14, 2017
1 parent 0fe6469 commit c8ff4ae
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2705,5 +2705,78 @@ describe('ReactDOMComponent', () => {
'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';
});
});

it('triggers events local captured events from children without listeners', () => {
const callback = jest.fn();
const container = document.createElement('div');

ReactDOM.render(
<div id="top" onTouchEnd={callback}>
<div id="middle">
<div id="bottom" />
</div>
</div>,
container,
);

const event = document.createEvent('Event');

event.initEvent('touchend', true, true);
container.querySelector('#bottom').dispatchEvent(event);

expect(callback).toHaveBeenCalledTimes(1);
});

it('does not double dispatch captured events at the deepest leaf', () => {
const top = jest.fn();
const middle = jest.fn();
const bottom = jest.fn();
const container = document.createElement('div');

ReactDOM.render(
<div id="top" onTouchEnd={top}>
<div id="middle" onTouchEnd={middle}>
<div id="bottom" onTouchEnd={bottom} />
</div>
</div>,
container,
);

const target = container.querySelector('#bottom');
const event = document.createEvent('Event');

event.initEvent('touchend', true, true);
target.dispatchEvent(event);

expect(top).toHaveBeenCalledTimes(1);
expect(middle).toHaveBeenCalledTimes(1);
expect(bottom).toHaveBeenCalledTimes(1);
});

it('does not double dispatch captured events at the middle leaf', () => {
const top = jest.fn();
const middle = jest.fn();
const bottom = jest.fn();
const container = document.createElement('div');

ReactDOM.render(
<div id="top" onTouchEnd={top}>
<div id="middle" onTouchEnd={middle}>
<div id="bottom" onTouchEnd={bottom} />
</div>
</div>,
container,
);

const target = container.querySelector('#middle');
const event = document.createEvent('Event');

event.initEvent('touchend', true, true);
target.dispatchEvent(event);

expect(top).toHaveBeenCalledTimes(1);
expect(middle).toHaveBeenCalledTimes(1);
expect(bottom).toHaveBeenCalledTimes(0);
});
});
});

0 comments on commit c8ff4ae

Please sign in to comment.