|
| 1 | +import * as React from 'react'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { spy } from 'sinon'; |
| 4 | +import { act, fireEvent, flushMicrotasks } from '@mui/internal-test-utils'; |
| 5 | +import { Collapsible } from '@base-ui-components/react/collapsible'; |
| 6 | +import { createRenderer, describeConformance, isJSDOM } from '#test-utils'; |
| 7 | + |
| 8 | +const PANEL_CONTENT = 'This is panel content'; |
| 9 | + |
| 10 | +describe('<Collapsible.Panel />', () => { |
| 11 | + const { render } = createRenderer(); |
| 12 | + |
| 13 | + describeConformance(<Collapsible.Panel />, () => ({ |
| 14 | + refInstanceof: window.HTMLDivElement, |
| 15 | + render: (node) => { |
| 16 | + return render(<Collapsible.Root defaultOpen>{node}</Collapsible.Root>); |
| 17 | + }, |
| 18 | + })); |
| 19 | + |
| 20 | + describe('prop: keepMounted', () => { |
| 21 | + it('does not unmount the panel when true', async () => { |
| 22 | + function App() { |
| 23 | + const [open, setOpen] = React.useState(false); |
| 24 | + return ( |
| 25 | + <Collapsible.Root open={open} onOpenChange={setOpen}> |
| 26 | + <Collapsible.Trigger /> |
| 27 | + <Collapsible.Panel keepMounted>{PANEL_CONTENT}</Collapsible.Panel> |
| 28 | + </Collapsible.Root> |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + const { queryByText, getByRole } = await render(<App />); |
| 33 | + |
| 34 | + const trigger = getByRole('button'); |
| 35 | + |
| 36 | + expect(trigger).to.have.attribute('aria-expanded', 'false'); |
| 37 | + expect(trigger.getAttribute('aria-controls')).to.equal( |
| 38 | + queryByText(PANEL_CONTENT)?.getAttribute('id'), |
| 39 | + ); |
| 40 | + expect(queryByText(PANEL_CONTENT)).to.not.equal(null); |
| 41 | + expect(queryByText(PANEL_CONTENT)).not.toBeVisible(); |
| 42 | + expect(queryByText(PANEL_CONTENT)).to.have.attribute('data-closed'); |
| 43 | + |
| 44 | + fireEvent.click(trigger); |
| 45 | + await flushMicrotasks(); |
| 46 | + |
| 47 | + expect(trigger).to.have.attribute('aria-expanded', 'true'); |
| 48 | + |
| 49 | + expect(queryByText(PANEL_CONTENT)).toBeVisible(); |
| 50 | + expect(queryByText(PANEL_CONTENT)).to.have.attribute('data-open'); |
| 51 | + expect(trigger).to.have.attribute('data-panel-open'); |
| 52 | + |
| 53 | + fireEvent.click(trigger); |
| 54 | + await flushMicrotasks(); |
| 55 | + |
| 56 | + expect(trigger).to.have.attribute('aria-expanded', 'false'); |
| 57 | + expect(trigger.getAttribute('aria-controls')).to.equal( |
| 58 | + queryByText(PANEL_CONTENT)?.getAttribute('id'), |
| 59 | + ); |
| 60 | + expect(queryByText(PANEL_CONTENT)).not.toBeVisible(); |
| 61 | + expect(queryByText(PANEL_CONTENT)).to.have.attribute('data-closed'); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + // we test firefox in browserstack which does not support this yet |
| 66 | + describe.skipIf(!('onbeforematch' in window) || isJSDOM)('prop: hiddenUntilFound', () => { |
| 67 | + it('uses `hidden="until-found" to hide panel when true', async () => { |
| 68 | + const handleOpenChange = spy(); |
| 69 | + |
| 70 | + const { queryByText } = await render( |
| 71 | + <Collapsible.Root defaultOpen={false} onOpenChange={handleOpenChange}> |
| 72 | + <Collapsible.Trigger /> |
| 73 | + <Collapsible.Panel hiddenUntilFound keepMounted> |
| 74 | + {PANEL_CONTENT} |
| 75 | + </Collapsible.Panel> |
| 76 | + </Collapsible.Root>, |
| 77 | + ); |
| 78 | + |
| 79 | + const panel = queryByText(PANEL_CONTENT); |
| 80 | + |
| 81 | + act(() => { |
| 82 | + const event = new window.Event('beforematch', { |
| 83 | + bubbles: true, |
| 84 | + cancelable: false, |
| 85 | + }); |
| 86 | + panel?.dispatchEvent(event); |
| 87 | + }); |
| 88 | + |
| 89 | + expect(handleOpenChange.callCount).to.equal(1); |
| 90 | + expect(panel).to.have.attribute('data-open'); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments