UNPKG

wix-style-react

Version:
68 lines 3.44 kB
import ReactTestUtils from 'react-dom/test-utils'; import { tooltipLegacyDriverFactory } from '../Tooltip/Tooltip.driver'; import { dataHooks } from './constants'; const modalDriverFactory = ({ element, eventTrigger }) => { const getPortal = () => { const dataHook = element && element.getAttribute('data-hook'); return dataHook ? document.body.querySelector(`.portal.portal-${dataHook}`) : document.body.querySelector('.portal'); }; const getOverlay = () => document.body.querySelector('.ReactModal__Overlay'); const getContent = () => document.body.querySelector('.ReactModal__Content'); const isOpen = () => !!getContent(); const getCloseButton = () => document.body.querySelector(`[data-hook="${dataHooks.modalCloseButton}"]`); const getNavigationPrevious = () => document.body.querySelector(`[data-hook="${dataHooks.navigationPrevButton}"]`); const getNavigationNext = () => document.body.querySelector(`[data-hook="${dataHooks.navigationNextButton}"]`); const getTooltipNext = () => tooltipLegacyDriverFactory({ element: document.body.querySelector(`[data-hook="${dataHooks.navigationNextTooltip}"]`), eventTrigger, }); const getTooltipPrev = () => tooltipLegacyDriverFactory({ element: document.body.querySelector(`[data-hook="${dataHooks.navigationPrevTooltip}"]`), eventTrigger, }); return { /** true if the modal is on the DOM */ exists: () => !!getPortal(), element: () => element, /** true when the module is open */ isOpen, getChildBySelector: selector => getPortal().querySelector(selector), /** true if the modal is scrollable */ isScrollable: () => !!getPortal().querySelector('[data-scrollable]'), closeButtonExists: () => !!getCloseButton(), /** click on the modal overlay (helpful for testing if the modal is dismissed) */ clickOnOverlay: () => { const overlay = getOverlay(); ReactTestUtils.Simulate.click(overlay); }, clickOnCloseButton: () => { const button = getCloseButton(); ReactTestUtils.Simulate.click(button); }, /** click on navigation control previous button */ clickOnNavigationPrevious: () => { const button = getNavigationPrevious(); ReactTestUtils.Simulate.click(button); }, /** click on navigation control next button */ clickOnNavigationNext: () => { const button = getNavigationNext(); ReactTestUtils.Simulate.click(button); }, navigationPreviousExists: () => !!getNavigationPrevious(), navigationNextExists: () => !!getNavigationNext(), getNavigationPreviousLabel: () => getTooltipPrev().getTooltipText(), getNavigationNextLabel: () => getTooltipNext().getTooltipText(), /** returns the element of the modal content (helpful to initialize a layout testkit) */ getContent, /** returns the style of the modal content */ getContentStyle: () => getContent().style, /** returns the modal aria-label value as given in contentLabel property */ getContentLabel: () => getContent().getAttribute('aria-label'), getZIndex: () => getOverlay().style['z-index'], }; }; export default modalDriverFactory; //# sourceMappingURL=Modal.driver.js.map