Skip to content

Commit

Permalink
Refactor to use getLgIds instead of constant LGIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
stephl3 committed Feb 13, 2025
1 parent 7d56942 commit 6be89a0
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .changeset/perfect-badgers-relate.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
Stub for Drawer changeset

- Exports `getTestUtils`, a util to reliably interact with LG `Drawer` component instances in a product test suite. For more details, check out the [README](https://github.com/mongodb/leafygreen-ui/tree/main/packages/drawer#test-harnesses)
- Exports the constant, `LGIDs`, which stores `data-lgid` values.
- Exports `getLgIds`, a util to instantiate an object of `data-lgid` values for a given LG `Drawer` component instance.
7 changes: 0 additions & 7 deletions packages/drawer/src/Drawer/Drawer.constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
const LGID_ROOT = 'lg-drawer';

export const LGIDs = {
root: LGID_ROOT,
closeButton: `${LGID_ROOT}-close_button`,
} as const;

export const PANEL_WIDTH = 432;
7 changes: 4 additions & 3 deletions packages/drawer/src/Drawer/Drawer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { axe } from 'jest-axe';

import { getTestUtils } from '../utils';
import { getLgIds, getTestUtils } from '../utils';

import { Drawer, DrawerProps, LGIDs } from '.';
import { Drawer, DrawerProps } from '.';

const drawerContent = 'Drawer content';

Expand Down Expand Up @@ -52,8 +52,9 @@ describe('packages/drawer', () => {
});

test('close button is not rendered when onClose is not provided', () => {
const lgIds = getLgIds();
const { queryByTestId } = renderDrawer({ open: true });
expect(queryByTestId(LGIDs.closeButton)).toBeNull();
expect(queryByTestId(lgIds.closeButton)).not.toBeInTheDocument();
});

test('calls onClose when close button is clicked', () => {
Expand Down
12 changes: 7 additions & 5 deletions packages/drawer/src/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import LeafyGreenProvider, {
import { BaseFontSize } from '@leafygreen-ui/tokens';
import { Body } from '@leafygreen-ui/typography';

import { LGIDs } from './Drawer.constants';
import { DEFAULT_LGID_ROOT, getLgIds } from '../utils';

import { getDrawerStyles, getHeaderStyles } from './Drawer.styles';
import { DrawerProps } from './Drawer.types';

Expand All @@ -18,7 +19,7 @@ export const Drawer = forwardRef<HTMLDivElement, DrawerProps>(
{
children,
className,
'data-lgid': dataLgId = LGIDs.root,
'data-lgid': dataLgId = DEFAULT_LGID_ROOT,
id: idProp,
onClose,
open = false,
Expand All @@ -29,6 +30,7 @@ export const Drawer = forwardRef<HTMLDivElement, DrawerProps>(
) => {
const { darkMode, theme } = useDarkMode();

const lgIds = getLgIds(dataLgId);
const id = useIdAllocator({ prefix: 'drawer', id: idProp });
const titleId = useIdAllocator({ prefix: 'drawer' });

Expand All @@ -40,7 +42,7 @@ export const Drawer = forwardRef<HTMLDivElement, DrawerProps>(
aria-hidden={!open}
aria-labelledby={titleId}
className={getDrawerStyles({ className, open, theme })}
data-lgid={dataLgId}
data-lgid={lgIds.root}
id={id}
ref={fwdRef}
role="dialog"
Expand All @@ -58,8 +60,8 @@ export const Drawer = forwardRef<HTMLDivElement, DrawerProps>(
{showCloseButton && (
<IconButton
aria-label="Close drawer"
data-lgid={LGIDs.closeButton}
data-testid={LGIDs.closeButton}
data-lgid={lgIds.closeButton}
data-testid={lgIds.closeButton}
onClick={onClose}
>
<XIcon />
Expand Down
1 change: 0 additions & 1 deletion packages/drawer/src/Drawer/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { Drawer } from './Drawer';
export { LGIDs } from './Drawer.constants';
export { type DrawerProps } from './Drawer.types';
4 changes: 2 additions & 2 deletions packages/drawer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { Drawer, type DrawerProps, LGIDs } from './Drawer';
export { getTestUtils } from './utils';
export { Drawer, type DrawerProps } from './Drawer';
export { getLgIds, getTestUtils, type GetTestUtilsReturnType } from './utils';
6 changes: 6 additions & 0 deletions packages/drawer/src/utils/getLgIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const DEFAULT_LGID_ROOT = 'drawer';

export const getLgIds = (root = DEFAULT_LGID_ROOT) => ({
root,
closeButton: `${root}-close_button`,
});
11 changes: 6 additions & 5 deletions packages/drawer/src/utils/getTestUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ import { getByLgId } from '@lg-tools/test-harnesses';

import { getTestUtils as getButtonTestUtils } from '@leafygreen-ui/button';

import { LGIDs } from '../Drawer';

import { DEFAULT_LGID_ROOT, getLgIds } from './getLgIds';
import { GetTestUtilsReturnType } from './getTestUtils.types';

export const getTestUtils = <T extends HTMLElement = HTMLElement>(
lgId: string = LGIDs.root,
lgId: string = DEFAULT_LGID_ROOT,
): GetTestUtilsReturnType<T> => {
const lgIds = getLgIds(lgId);

/**
* Queries the DOM for the element using the `data-lgid` data attribute.
* Will throw if no element is found.
*/
const element: T = getByLgId!(lgId);
const element: T = getByLgId!(lgIds.root);

/**
* Returns the button test utils for the close button.
*/
const getCloseButtonUtils = () =>
getButtonTestUtils<HTMLButtonElement>(LGIDs.closeButton);
getButtonTestUtils<HTMLButtonElement>(lgIds.closeButton);

/**
* Returns the aria-hidden attribute on the drawer.
Expand Down
2 changes: 2 additions & 0 deletions packages/drawer/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { DEFAULT_LGID_ROOT, getLgIds } from './getLgIds';
export { getTestUtils } from './getTestUtils';
export type { GetTestUtilsReturnType } from './getTestUtils.types';

0 comments on commit 6be89a0

Please sign in to comment.