Skip to content

Commit 88e25a1

Browse files
kraenhansenstephl3
andauthored
[LG-5593] fix(modal) revert change in order of data-testid and ...rest in ModalView (#3183)
* Add explicit type for "data-testid" * Add tests for data-testid propagation * Revert order of data-testid and ...rest in ModalView * Fix propagation of data-testid to buttons * chore: changesets --------- Co-authored-by: Stephen Lee <[email protected]>
1 parent 9b2d156 commit 88e25a1

File tree

7 files changed

+58
-4
lines changed

7 files changed

+58
-4
lines changed

.changeset/eager-mails-lie.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@leafygreen-ui/confirmation-modal': patch
3+
'@leafygreen-ui/modal': patch
4+
---
5+
6+
Fix data-testid attributes to allow consumer override

.changeset/great-ears-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@leafygreen-ui/lib': patch
3+
---
4+
5+
Add data-testid documentation in `LgIdProps`

packages/confirmation-modal/src/ConfirmationModal/ConfirmationModal.spec.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,32 @@ describe('packages/confirmation-modal', () => {
497497
});
498498
});
499499

500+
describe('testid attribute', () => {
501+
it('propagates to the dom element', () => {
502+
const { getByTestId } = renderModal({
503+
open: true,
504+
'data-testid': 'my-modal',
505+
});
506+
507+
const modal = getByTestId('my-modal');
508+
expect(modal).toBeInTheDocument();
509+
});
510+
511+
it('propagates to the buttons', () => {
512+
const { getByTestId } = renderModal({
513+
open: true,
514+
confirmButtonProps: { 'data-testid': 'my-confirm-btn' },
515+
cancelButtonProps: { 'data-testid': 'my-cancel-btn' },
516+
});
517+
518+
const confirmButton = getByTestId('my-confirm-btn');
519+
expect(confirmButton).toBeInTheDocument();
520+
521+
const cancelButton = getByTestId('my-cancel-btn');
522+
expect(cancelButton).toBeInTheDocument();
523+
});
524+
});
525+
500526
// eslint-disable-next-line jest/no-disabled-tests
501527
test.skip('types behave as expected', () => {
502528
<>

packages/confirmation-modal/src/ConfirmationModal/ConfirmationModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ export const ConfirmationModal = forwardRef<
134134
</div>
135135
<Footer>
136136
<Button
137-
{...confirmButtonProps}
138137
data-testid={lgIds.confirm}
138+
{...confirmButtonProps}
139139
disabled={!confirmEnabled || isConfirmDisabled}
140140
className={cx(buttonStyle, confirmButtonProps?.className)}
141141
variant={variant}
@@ -145,10 +145,10 @@ export const ConfirmationModal = forwardRef<
145145
{buttonText || confirmButtonProps?.children || 'Confirm'}
146146
</Button>
147147
<Button
148+
data-testid={lgIds.cancel}
148149
{...cancelButtonProps}
149150
onClick={handleCancel}
150151
className={cx(buttonStyle, cancelButtonProps?.className)}
151-
data-testid={lgIds.cancel}
152152
>
153153
Cancel
154154
</Button>

packages/lib/src/LgIdProps/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ export interface LgIdProps {
1010
* LG test id passed to the component wrapper.
1111
*/
1212
['data-lgid']?: LgIdString;
13+
14+
/**
15+
* An additional test id passed to the component wrapper, meant for use by consumers of the library.
16+
*/
17+
['data-testid']?: string;
1318
}

packages/modal/src/Modal/Modal.spec.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,18 @@ describe('packages/modal', () => {
260260
expect(modal).not.toBeVisible();
261261
});
262262
});
263+
264+
describe('testid attribute', () => {
265+
it('propagates to the dom element', () => {
266+
const { getByTestId } = renderModal({
267+
open: true,
268+
'data-testid': 'my-modal',
269+
});
270+
271+
const modal = getByTestId('my-modal');
272+
expect(modal).toBeInTheDocument();
273+
});
274+
});
263275
});
264276

265277
async function expectElementToNotBeRemoved(element: HTMLElement) {

packages/modal/src/Modal/ModalView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ const ModalView = React.forwardRef<HTMLDialogElement, ModalProps>(
9090
}}
9191
>
9292
<dialog
93+
data-testid={lgIds.root}
94+
data-lgid={lgIds.root}
9395
{...rest}
9496
ref={dialogRef}
9597
id={id}
96-
data-testid={lgIds.root}
97-
data-lgid={lgIds.root}
9898
className={getDialogStyles({
9999
backdropClassName,
100100
className,

0 commit comments

Comments
 (0)