|
| 1 | +import {ThemeProvider} from '@gravity-ui/uikit'; |
| 2 | +import {fireEvent, render, screen} from '@testing-library/react'; |
| 3 | + |
| 4 | +import {ContentWithPopup} from '../ContentWithPopup/ContentWithPopup'; |
| 5 | + |
| 6 | +import {getPopupScrollContainer} from './getPopupScrollContainer'; |
| 7 | + |
| 8 | +function makeScrollable(element: HTMLElement) { |
| 9 | + element.style.setProperty('overflow-y', 'auto'); |
| 10 | + Object.defineProperties(element, { |
| 11 | + clientHeight: {value: 100}, |
| 12 | + scrollHeight: {value: 200}, |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +test('preserves document portals and keeps fullscreen portals inside the fullscreen boundary', () => { |
| 17 | + const outer = document.createElement('div'); |
| 18 | + const fullscreen = document.createElement('div'); |
| 19 | + const anchor = document.createElement('span'); |
| 20 | + document.body.append(outer); |
| 21 | + outer.append(fullscreen); |
| 22 | + fullscreen.append(anchor); |
| 23 | + const original = Object.getOwnPropertyDescriptors(document); |
| 24 | + try { |
| 25 | + expect(getPopupScrollContainer(null)).toBeUndefined(); |
| 26 | + expect(getPopupScrollContainer(anchor)).toBeUndefined(); |
| 27 | + makeScrollable(outer); |
| 28 | + expect(getPopupScrollContainer(anchor)).toBe(outer); |
| 29 | + Object.defineProperty(document, 'scrollingElement', {configurable: true, value: outer}); |
| 30 | + expect(getPopupScrollContainer(anchor)).toBeUndefined(); |
| 31 | + Object.defineProperty(document, 'fullscreenElement', { |
| 32 | + configurable: true, |
| 33 | + value: fullscreen, |
| 34 | + }); |
| 35 | + expect(getPopupScrollContainer(anchor)).toBe(fullscreen); |
| 36 | + } finally { |
| 37 | + outer.remove(); |
| 38 | + for (const key of ['scrollingElement', 'fullscreenElement']) { |
| 39 | + if (original[key]) { |
| 40 | + Object.defineProperty(document, key, original[key]); |
| 41 | + } else { |
| 42 | + Reflect.deleteProperty(document, key); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +test.each([false, true])( |
| 49 | + 'ContentWithPopup renders in the selected portal (override: %s)', |
| 50 | + async (override) => { |
| 51 | + const host = document.createElement('div'); |
| 52 | + makeScrollable(host); |
| 53 | + document.body.append(host); |
| 54 | + const explicitContainer = document.createElement('div'); |
| 55 | + document.body.append(explicitContainer); |
| 56 | + const view = render( |
| 57 | + <ThemeProvider theme="light"> |
| 58 | + <ContentWithPopup |
| 59 | + content="Popup details" |
| 60 | + {...(override ? {container: explicitContainer} : {})} |
| 61 | + > |
| 62 | + Open popup |
| 63 | + </ContentWithPopup> |
| 64 | + </ThemeProvider>, |
| 65 | + {container: host}, |
| 66 | + ); |
| 67 | + try { |
| 68 | + fireEvent.mouseEnter(screen.getByText('Open popup')); |
| 69 | + const popup = await screen.findByText('Popup details'); |
| 70 | + expect((override ? explicitContainer : host).contains(popup)).toBe(true); |
| 71 | + } finally { |
| 72 | + view.unmount(); |
| 73 | + host.remove(); |
| 74 | + explicitContainer.remove(); |
| 75 | + } |
| 76 | + }, |
| 77 | +); |
0 commit comments