diff --git a/packages/compass-components/src/components/drawer-portal.spec.tsx b/packages/compass-components/src/components/drawer-portal.spec.tsx index 0e56758bc10..130f51a772a 100644 --- a/packages/compass-components/src/components/drawer-portal.spec.tsx +++ b/packages/compass-components/src/components/drawer-portal.spec.tsx @@ -1,5 +1,10 @@ import React, { useState } from 'react'; -import { render, screen, waitFor } from '@mongodb-js/testing-library-compass'; +import { + render, + screen, + userEvent, + waitFor, +} from '@mongodb-js/testing-library-compass'; import { DrawerContentProvider, DrawerSection, @@ -47,4 +52,114 @@ describe('DrawerSection', function () { .be.visible; }); }); + + // Doesn't really matter, but leafygreen uses these as keys when rendering and + // this produces a ton of warnings in the logs if they are not unique + const icons = ['ArrowDown', 'CaretDown', 'ChevronDown'] as const; + + it('switches drawer content when selecting a different section in the toolbar', async function () { + render( + + + {[1, 2, 3].map((n, idx) => { + return ( + + This is section {n} + + ); + })} + + + ); + + userEvent.click(screen.getByRole('button', { name: 'Section 1' })); + await waitFor(() => { + expect(screen.getByText('This is section 1')).to.be.visible; + }); + + userEvent.click(screen.getByRole('button', { name: 'Section 2' })); + await waitFor(() => { + expect(screen.getByText('This is section 2')).to.be.visible; + }); + + userEvent.click(screen.getByRole('button', { name: 'Section 3' })); + await waitFor(() => { + expect(screen.getByText('This is section 3')).to.be.visible; + }); + + userEvent.click(screen.getByRole('button', { name: 'Section 1' })); + await waitFor(() => { + expect(screen.getByText('This is section 1')).to.be.visible; + }); + + userEvent.click(screen.getByRole('button', { name: 'Close drawer' })); + await waitFor(() => { + expect(screen.queryByText('This is section 1')).not.to.exist; + expect(screen.queryByText('This is section 2')).not.to.exist; + expect(screen.queryByText('This is section 3')).not.to.exist; + }); + }); + + it('closes drawer when opened section is removed from toolbar data', async function () { + // Render two sections, auto-open first one + const { rerender } = render( + + + + This is a test section + + + This is another test section + + + + ); + + await waitFor(() => { + expect(screen.getByText('This is a test section')).to.be.visible; + }); + + // Now render without opened section + rerender( + + + + This is another test section + + + + ); + + await waitFor(() => { + expect(screen.queryByText('This is a test section')).not.to.exist; + }); + + expect( + // Specifically a selector for the drawer content section, not the whole + // drawer with toolbar + screen.getByTestId('lg-drawer') + ).to.have.attribute('aria-hidden', 'true'); + }); }); diff --git a/packages/compass-components/src/components/drawer-portal.tsx b/packages/compass-components/src/components/drawer-portal.tsx index b81b416c05a..631179d46c4 100644 --- a/packages/compass-components/src/components/drawer-portal.tsx +++ b/packages/compass-components/src/components/drawer-portal.tsx @@ -221,6 +221,7 @@ export const DrawerAnchor: React.FunctionComponent<{ ...data, content: (
diff --git a/packages/compass-components/src/components/drawer/drawer-toolbar-layout/drawer-toolbar-layout-container.tsx b/packages/compass-components/src/components/drawer/drawer-toolbar-layout/drawer-toolbar-layout-container.tsx index ee215f274eb..056e911d8a7 100644 --- a/packages/compass-components/src/components/drawer/drawer-toolbar-layout/drawer-toolbar-layout-container.tsx +++ b/packages/compass-components/src/components/drawer/drawer-toolbar-layout/drawer-toolbar-layout-container.tsx @@ -37,8 +37,12 @@ export const DrawerToolbarLayoutContainer = forwardRef< }: DrawerToolbarLayoutContainerProps, forwardRef ) => { - const { openDrawer, closeDrawer, getActiveDrawerContent, isDrawerOpen } = - useDrawerToolbarContext(); + const { + openDrawer, + closeDrawer, + getActiveDrawerContent, + isDrawerOpen: _isDrawerOpen, + } = useDrawerToolbarContext(); const { id } = getActiveDrawerContent() || {}; const lgIds = getLgIds(dataLgId); const hasData = toolbarData && toolbarData.length > 0; @@ -61,6 +65,8 @@ export const DrawerToolbarLayoutContainer = forwardRef< openDrawer(id); }; + const isDrawerOpen = Boolean(title && content && _isDrawerOpen); + return (