Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@react-aria/overlays): prevent exception on Cypress #2341

Merged
merged 7 commits into from Oct 28, 2021
4 changes: 2 additions & 2 deletions packages/@react-aria/overlays/src/useCloseOnScroll.ts
Expand Up @@ -36,8 +36,8 @@ export function useCloseOnScroll(opts: CloseOnScrollOptions) {

let onScroll = (e: MouseEvent) => {
// Ignore if scrolling an scrollable region outside the trigger's tree.
let target = e.target as HTMLElement;
if (!triggerRef.current || !target.contains(triggerRef.current)) {
let target = e.target;
if (!triggerRef.current || !(target instanceof Node) || !target.contains(triggerRef.current)) {
return;
}

Expand Down
19 changes: 19 additions & 0 deletions packages/@react-aria/overlays/test/useOverlayPosition.test.js
Expand Up @@ -163,6 +163,25 @@ describe('useOverlayPosition', function () {
fireEvent.scroll(document.body);
expect(onClose).toHaveBeenCalledTimes(1);
});

it('should close the overlay when the document scrolls', function () {
let onClose = jest.fn();
render(<Example isOpen onClose={onClose} />);

fireEvent.scroll(document);
expect(onClose).toHaveBeenCalledTimes(1);
});

it('should not throw or close when target is window in a scroll event', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we'll want this test to be the same as the document scroll test, still good to have both tests

// Cypress triggers an artificial scroll event in `screenshot` command:
// https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/screenshot.ts#L106
// More info: https://github.com/adobe/react-spectrum/issues/2340
let onClose = jest.fn();
render(<Example isOpen onClose={onClose} />);

fireEvent.scroll(window);
expect(onClose).toHaveBeenCalledTimes(0);
});
});

describe('useOverlayPosition with positioned container', () => {
Expand Down