Skip to content

Commit 18c3e3c

Browse files
authored
feat: allow dom props on alert dialog. add test ids to alert dialog buttons (#8370)
* 8369: allow dom props on alert dialog and buttons in it * add tests * add aria props * lint * naming * naming * add testid directly to button * add testid directly to button * add testid directly to button
1 parent 6173beb commit 18c3e3c

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

packages/@react-spectrum/dialog/src/AlertDialog.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import AlertMedium from '@spectrum-icons/ui/AlertMedium';
1414
import {Button} from '@react-spectrum/button';
1515
import {ButtonGroup} from '@react-spectrum/buttongroup';
16-
import {chain} from '@react-aria/utils';
16+
import {chain, filterDOMProps} from '@react-aria/utils';
1717
import {classNames, useStyleProps} from '@react-spectrum/utils';
1818
import {Content} from '@react-spectrum/view';
1919
import {Dialog} from './Dialog';
@@ -71,7 +71,8 @@ export const AlertDialog = forwardRef(function AlertDialog(props: SpectrumAlertD
7171
isHidden={styleProps.hidden}
7272
size="M"
7373
role="alertdialog"
74-
ref={ref}>
74+
ref={ref}
75+
{...filterDOMProps(props)}>
7576
<Heading>{title}</Heading>
7677
{(variant === 'error' || variant === 'warning') &&
7778
<AlertMedium
@@ -85,7 +86,8 @@ export const AlertDialog = forwardRef(function AlertDialog(props: SpectrumAlertD
8586
<Button
8687
variant="secondary"
8788
onPress={() => chain(onClose(), onCancel())}
88-
autoFocus={autoFocusButton === 'cancel'}>
89+
autoFocus={autoFocusButton === 'cancel'}
90+
data-testid="rsp-alertDialog-cancelButton">
8991
{cancelLabel}
9092
</Button>
9193
}
@@ -94,15 +96,17 @@ export const AlertDialog = forwardRef(function AlertDialog(props: SpectrumAlertD
9496
variant="secondary"
9597
onPress={() => chain(onClose(), onSecondaryAction())}
9698
isDisabled={isSecondaryActionDisabled}
97-
autoFocus={autoFocusButton === 'secondary'}>
99+
autoFocus={autoFocusButton === 'secondary'}
100+
data-testid="rsp-alertDialog-secondaryButton">
98101
{secondaryActionLabel}
99102
</Button>
100103
}
101104
<Button
102105
variant={confirmVariant}
103106
onPress={() => chain(onClose(), onPrimaryAction())}
104107
isDisabled={isPrimaryActionDisabled}
105-
autoFocus={autoFocusButton === 'primary'}>
108+
autoFocus={autoFocusButton === 'primary'}
109+
data-testid="rsp-alertDialog-confirmButton">
106110
{primaryActionLabel}
107111
</Button>
108112
</ButtonGroup>

packages/@react-spectrum/dialog/stories/AlertDialog.stories.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ export const AutoFocusCancel = () => renderAlert({
172172
onPrimaryAction: action('primary'),
173173
onSecondaryAction: action('secondary'),
174174
onCancel: action('cancel'),
175-
autoFocusButton: 'cancel'
176-
});
175+
autoFocusButton: 'cancel',
176+
'data-testid': 'alert-dialog'
177+
} as SpectrumAlertDialogProps);
177178

178179
AutoFocusCancel.story = {
179180
name: 'autoFocus cancel'

packages/@react-spectrum/dialog/test/AlertDialog.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,30 @@ describe('AlertDialog', function () {
177177
let button = getByText('secondary').closest('button');
178178
expect(document.activeElement).toBe(button);
179179
});
180+
181+
it('can add test ids', function () {
182+
let {getByTestId} = render(
183+
<Provider theme={theme}>
184+
<AlertDialog
185+
variant="confirmation"
186+
title="the title"
187+
cancelLabel="cancel"
188+
primaryActionLabel="confirm"
189+
secondaryActionLabel="secondary"
190+
autoFocusButton="secondary"
191+
data-testid="alert-dialog">
192+
Content body
193+
</AlertDialog>
194+
</Provider>
195+
);
196+
197+
let dialog = getByTestId('alert-dialog');
198+
expect(dialog).toBeDefined();
199+
let cancelBtn = getByTestId('rsp-alertDialog-cancelButton');
200+
expect(cancelBtn).toBeDefined();
201+
let secondaryBtn = getByTestId('rsp-alertDialog-secondaryButton');
202+
expect(secondaryBtn).toBeDefined();
203+
let primaryBtn = getByTestId('rsp-alertDialog-confirmButton');
204+
expect(primaryBtn).toBeDefined();
205+
});
180206
});

0 commit comments

Comments
 (0)