diff --git a/change/@fluentui-react-utilities-638b8284-f061-4808-853e-5987365c9c52.json b/change/@fluentui-react-utilities-638b8284-f061-4808-853e-5987365c9c52.json new file mode 100644 index 0000000000000..265db3c214ce7 --- /dev/null +++ b/change/@fluentui-react-utilities-638b8284-f061-4808-853e-5987365c9c52.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "chore: add e2e test for useOnScrollOutside", + "packageName": "@fluentui/react-utilities", + "email": "yuanboxue@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/packages/react-components/react-utilities/src/hooks/useOnScrollOutside.cy.tsx b/packages/react-components/react-utilities/src/hooks/useOnScrollOutside.cy.tsx new file mode 100644 index 0000000000000..df682c1ef6702 --- /dev/null +++ b/packages/react-components/react-utilities/src/hooks/useOnScrollOutside.cy.tsx @@ -0,0 +1,115 @@ +import { mount } from '@cypress/react'; +import * as React from 'react'; + +import { useOnScrollOutside } from './useOnScrollOutside'; + +describe('useOnScrollOutside', () => { + it('should work', () => { + const onOutsideScroll = cy.spy(); + + const OutsideScrollExample: React.FC<{ onOutsideScroll: () => void }> = props => { + const innerRef = React.useRef(null); + + useOnScrollOutside({ + element: document, + callback: props.onOutsideScroll, + refs: [innerRef], + }); + + return ( +
+
+
+
+ +
+
+
+
+ ); + }; + + mount(); + + cy.get('#inside-scrollable-area') + .trigger('wheel', { deltaY: 10 }) + .then(() => { + expect(onOutsideScroll).not.to.be.called; + }); + + cy.get('#outside-scrollable-area') + .trigger('wheel', { deltaY: 10 }) + .then(() => { + expect(onOutsideScroll).to.be.called; + }); + }); + + it('should not trigger callback on browser-initiated scroll due to focus change', () => { + const onOutsideScroll = cy.spy(); + + const OutsideScrollExample: React.FC<{ onOutsideScroll: () => void }> = props => { + const innerRef = React.useRef(null); + + useOnScrollOutside({ + element: document, + callback: props.onOutsideScroll, + refs: [innerRef], + }); + + return ( +
+
+ +
+ +
+ +
+ ); + }; + + mount(); + + cy.get('#inside-button') + .click() + .realPress('Tab') + .get('#outside-button') + .should('be.focused') + .then(() => { + expect(onOutsideScroll).to.not.be.called; + }); + }); +});