|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +import GleapBannerManager, { bodyClipsInlineBanner } from './GleapBannerManager'; |
| 5 | + |
| 6 | +jest.mock('./Gleap', () => ({ |
| 7 | + __esModule: true, |
| 8 | + default: {}, |
| 9 | + GleapFrameManager: { getInstance: jest.fn(() => ({ urlHandler: jest.fn() })) }, |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('./GleapHelper', () => ({ |
| 13 | + bootstrapGleapFrame: jest.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +const style = (overflowX, overflowY = overflowX) => ({ overflowX, overflowY }); |
| 17 | + |
| 18 | +describe('bodyClipsInlineBanner', () => { |
| 19 | + test('body overflow is propagated to the viewport while the root is visible', () => { |
| 20 | + expect(bodyClipsInlineBanner(style('visible'), style('hidden'))).toBe(false); |
| 21 | + expect(bodyClipsInlineBanner(style('visible'), style('visible'))).toBe(false); |
| 22 | + }); |
| 23 | + |
| 24 | + test('a visible body never clips', () => { |
| 25 | + expect(bodyClipsInlineBanner(style('hidden'), style('visible'))).toBe(false); |
| 26 | + }); |
| 27 | + |
| 28 | + test('body clips when both root and body overflow are not visible', () => { |
| 29 | + expect(bodyClipsInlineBanner(style('hidden'), style('hidden'))).toBe(true); |
| 30 | + expect(bodyClipsInlineBanner(style('auto'), style('clip'))).toBe(true); |
| 31 | + expect(bodyClipsInlineBanner(style('hidden'), style('visible', 'auto'))).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + test('missing styles do not clip', () => { |
| 35 | + expect(bodyClipsInlineBanner(null, style('hidden'))).toBe(false); |
| 36 | + expect(bodyClipsInlineBanner(style('hidden'), {})).toBe(false); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('GleapBannerManager inline clip layout', () => { |
| 41 | + const manager = GleapBannerManager.getInstance(); |
| 42 | + let styleEl; |
| 43 | + |
| 44 | + const show = (format) => { |
| 45 | + manager.injectBannerUI({ format }); |
| 46 | + const frame = document.querySelector('.gleap-b-frame'); |
| 47 | + window.dispatchEvent( |
| 48 | + new MessageEvent('message', { |
| 49 | + data: JSON.stringify({ type: 'BANNER', name: 'banner-data-set' }), |
| 50 | + source: frame.contentWindow, |
| 51 | + }) |
| 52 | + ); |
| 53 | + }; |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + manager.removeBannerUI(); |
| 57 | + styleEl?.remove(); |
| 58 | + styleEl = null; |
| 59 | + document.documentElement.style.removeProperty('--gleap-b-body-padding-top'); |
| 60 | + }); |
| 61 | + |
| 62 | + const setPageCss = (css) => { |
| 63 | + styleEl = document.createElement('style'); |
| 64 | + styleEl.textContent = css; |
| 65 | + document.head.appendChild(styleEl); |
| 66 | + }; |
| 67 | + |
| 68 | + test('app shells that clip body get the padding layout, keeping their own padding', () => { |
| 69 | + setPageCss('html, body { height: 100%; overflow: hidden; } body { padding-top: 12px; }'); |
| 70 | + show('inline'); |
| 71 | + expect(document.body.classList.contains('gleap-b-shown')).toBe(true); |
| 72 | + expect(document.body.classList.contains('gleap-b-clip')).toBe(true); |
| 73 | + expect(document.documentElement.style.getPropertyValue('--gleap-b-body-padding-top')).toBe('12px'); |
| 74 | + |
| 75 | + manager.removeBannerUI(); |
| 76 | + expect(document.body.classList.contains('gleap-b-clip')).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + test('scrolling pages keep the margin layout', () => { |
| 80 | + setPageCss('body { overflow: hidden; }'); |
| 81 | + show('inline'); |
| 82 | + expect(document.body.classList.contains('gleap-b-shown')).toBe(true); |
| 83 | + expect(document.body.classList.contains('gleap-b-clip')).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + test('floating banners are not affected', () => { |
| 87 | + setPageCss('html, body { overflow: hidden; }'); |
| 88 | + show('floating'); |
| 89 | + expect(document.body.classList.contains('gleap-b-f')).toBe(true); |
| 90 | + expect(document.body.classList.contains('gleap-b-clip')).toBe(false); |
| 91 | + }); |
| 92 | +}); |
0 commit comments