|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file BottomSheetSuspense.test.tsx |
| 5 | + * @input Uses vitest, @testing-library/react, BottomSheet and a Suspense |
| 6 | + * boundary that hides the sheet while it closes |
| 7 | + * @output Tests that a sheet a Suspense boundary hides during its exit, and |
| 8 | + * then shows again, finishes closing: its dialog closes, it is no longer |
| 9 | + * inert or hidden, and the page under it is usable |
| 10 | + * @position Core testing; validates the motion completion in |
| 11 | + * BottomSheetPanel.tsx when React re-runs its layout effects |
| 12 | + */ |
| 13 | + |
| 14 | +import {act, fireEvent, render, screen} from '@testing-library/react'; |
| 15 | +import {Suspense, use, useState} from 'react'; |
| 16 | +import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; |
| 17 | +import {BottomSheet} from './BottomSheet'; |
| 18 | +import {resetBottomSheetStackForTests} from './bottomSheetStack'; |
| 19 | +import {Button} from '../Button/Button'; |
| 20 | +import {__resetInteractionModalityForTest} from '../utils/interactionModality'; |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + resetBottomSheetStackForTests(); |
| 24 | + __resetInteractionModalityForTest(); |
| 25 | + HTMLDialogElement.prototype.showModal = vi.fn(function ( |
| 26 | + this: HTMLDialogElement, |
| 27 | + ) { |
| 28 | + this.setAttribute('open', ''); |
| 29 | + }); |
| 30 | + HTMLDialogElement.prototype.show = vi.fn(function (this: HTMLDialogElement) { |
| 31 | + this.setAttribute('open', ''); |
| 32 | + }); |
| 33 | + HTMLDialogElement.prototype.close = vi.fn(function (this: HTMLDialogElement) { |
| 34 | + this.removeAttribute('open'); |
| 35 | + }); |
| 36 | + if (!Element.prototype.setPointerCapture) { |
| 37 | + Element.prototype.setPointerCapture = vi.fn(); |
| 38 | + Element.prototype.releasePointerCapture = vi.fn(); |
| 39 | + } |
| 40 | + window.scrollTo = vi.fn(); |
| 41 | + vi.stubGlobal( |
| 42 | + 'matchMedia', |
| 43 | + vi.fn().mockReturnValue({ |
| 44 | + matches: false, |
| 45 | + media: '', |
| 46 | + onchange: null, |
| 47 | + addEventListener: vi.fn(), |
| 48 | + removeEventListener: vi.fn(), |
| 49 | + addListener: vi.fn(), |
| 50 | + removeListener: vi.fn(), |
| 51 | + dispatchEvent: vi.fn(), |
| 52 | + }), |
| 53 | + ); |
| 54 | +}); |
| 55 | + |
| 56 | +afterEach(() => { |
| 57 | + vi.unstubAllGlobals(); |
| 58 | +}); |
| 59 | + |
| 60 | +function Page({wait}: {wait: Promise<void> | null}) { |
| 61 | + if (wait != null) { |
| 62 | + use(wait); |
| 63 | + } |
| 64 | + return <p>Page</p>; |
| 65 | +} |
| 66 | + |
| 67 | +function Shell({wait}: {wait: Promise<void> | null}) { |
| 68 | + const [isOpen, setIsOpen] = useState(false); |
| 69 | + return ( |
| 70 | + <Suspense fallback={<p>Loading</p>}> |
| 71 | + <Page wait={wait} /> |
| 72 | + <Button label="Open" onClick={() => setIsOpen(true)} /> |
| 73 | + <BottomSheet isOpen={isOpen} onOpenChange={setIsOpen} label="Sheet"> |
| 74 | + <Button label="Close" onClick={() => setIsOpen(false)} /> |
| 75 | + </BottomSheet> |
| 76 | + </Suspense> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +function sheetState() { |
| 81 | + const dialog = document.querySelector<HTMLDialogElement>( |
| 82 | + 'dialog[aria-label="Sheet"]', |
| 83 | + ); |
| 84 | + if (dialog == null) { |
| 85 | + throw new Error('sheet dialog not found'); |
| 86 | + } |
| 87 | + return { |
| 88 | + open: dialog.hasAttribute('open'), |
| 89 | + inert: dialog.hasAttribute('inert'), |
| 90 | + ariaHidden: dialog.getAttribute('aria-hidden'), |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +/** Gives the exit a real transition, so it ends on a timer, not at once. */ |
| 95 | +function slowExit() { |
| 96 | + const panel = document.querySelector<HTMLElement>( |
| 97 | + 'dialog[aria-label="Sheet"] .astryx-bottom-sheet', |
| 98 | + ); |
| 99 | + if (panel == null) { |
| 100 | + throw new Error('sheet panel not found'); |
| 101 | + } |
| 102 | + panel.style.transitionProperty = 'transform'; |
| 103 | + panel.style.transitionDuration = '200ms'; |
| 104 | + panel.style.transitionDelay = '0s'; |
| 105 | +} |
| 106 | + |
| 107 | +const CLOSED = {open: false, inert: false, ariaHidden: null}; |
| 108 | + |
| 109 | +async function waitPastExit() { |
| 110 | + await act(async () => { |
| 111 | + await new Promise(resolve => setTimeout(resolve, 600)); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +describe('BottomSheet hidden by Suspense while it closes', () => { |
| 116 | + it('closes normally without the hide', async () => { |
| 117 | + render(<Shell wait={null} />); |
| 118 | + fireEvent.click(screen.getByRole('button', {name: 'Open'})); |
| 119 | + slowExit(); |
| 120 | + fireEvent.click(screen.getByRole('button', {name: 'Close'})); |
| 121 | + await waitPastExit(); |
| 122 | + expect(sheetState()).toEqual(CLOSED); |
| 123 | + }); |
| 124 | + |
| 125 | + it('finishes closing when it is shown again', async () => { |
| 126 | + const {rerender} = render(<Shell wait={null} />); |
| 127 | + fireEvent.click(screen.getByRole('button', {name: 'Open'})); |
| 128 | + slowExit(); |
| 129 | + fireEvent.click(screen.getByRole('button', {name: 'Close'})); |
| 130 | + |
| 131 | + let resolve: () => void = () => {}; |
| 132 | + const pending = new Promise<void>(done => { |
| 133 | + resolve = done; |
| 134 | + }); |
| 135 | + rerender(<Shell wait={pending} />); |
| 136 | + expect(screen.getByText('Loading')).toBeInTheDocument(); |
| 137 | + |
| 138 | + await act(async () => { |
| 139 | + resolve(); |
| 140 | + await pending; |
| 141 | + }); |
| 142 | + rerender(<Shell wait={null} />); |
| 143 | + expect(screen.getByText('Page')).toBeInTheDocument(); |
| 144 | + |
| 145 | + await waitPastExit(); |
| 146 | + expect(sheetState()).toEqual(CLOSED); |
| 147 | + }); |
| 148 | +}); |
0 commit comments