Skip to content

Commit b780f4a

Browse files
Merge pull request #543 from rebeccaalpert/data-test-id
fix(ResponseActions): Allow passing unspecified props
2 parents c1d1a22 + 3144bd3 commit b780f4a

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

packages/module/src/ResponseActions/ResponseActionButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
2-
import { Button, Icon, Tooltip, TooltipProps } from '@patternfly/react-core';
2+
import { Button, ButtonProps, Icon, Tooltip, TooltipProps } from '@patternfly/react-core';
33

4-
export interface ResponseActionButtonProps {
4+
export interface ResponseActionButtonProps extends ButtonProps {
55
/** Aria-label for the button. Defaults to the value of the tooltipContent if none provided */
66
ariaLabel?: string;
77
/** Aria-label for the button, shown when the button is clicked. Defaults to the value of ariaLabel or tooltipContent if not provided. */

packages/module/src/ResponseActions/ResponseActions.test.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ const CUSTOM_ACTIONS = [
4141
}
4242
];
4343

44+
const ALL_ACTIONS_DATA_TEST = [
45+
{ type: 'positive', label: 'Good response', dataTestId: 'positive' },
46+
{ type: 'negative', label: 'Bad response', dataTestId: 'negative' },
47+
{ type: 'copy', label: 'Copy', dataTestId: 'copy' },
48+
{ type: 'share', label: 'Share', dataTestId: 'share' },
49+
{ type: 'listen', label: 'Listen', dataTestId: 'listen' }
50+
];
51+
4452
describe('ResponseActions', () => {
4553
afterEach(() => {
4654
jest.clearAllMocks();
@@ -181,6 +189,13 @@ describe('ResponseActions', () => {
181189
});
182190
});
183191

192+
it('should be able to add custom attributes to buttons', () => {
193+
ALL_ACTIONS_DATA_TEST.forEach(({ type, dataTestId }) => {
194+
render(<ResponseActions actions={{ [type]: { onClick: jest.fn(), 'data-testid': dataTestId } }} />);
195+
expect(screen.getByTestId(dataTestId)).toBeTruthy();
196+
});
197+
});
198+
184199
it('should be able to add custom actions', () => {
185200
CUSTOM_ACTIONS.forEach((action) => {
186201
const key = Object.keys(action)[0];
@@ -192,12 +207,14 @@ describe('ResponseActions', () => {
192207
onClick: action[key].onClick,
193208
// doing this just because it's easier to test without a regex for the button name
194209
ariaLabel: action[key].ariaLabel.toLowerCase(),
195-
icon: action[key].icon
210+
icon: action[key].icon,
211+
'data-testid': action[key]
196212
}
197213
}}
198214
/>
199215
);
200216
expect(screen.getByRole('button', { name: key })).toBeTruthy();
217+
expect(screen.getByTestId(action[key])).toBeTruthy();
201218
});
202219
});
203220
});

packages/module/src/ResponseActions/ResponseActions.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ export interface ActionProps extends Omit<ButtonProps, 'ref'> {
3636
'aria-controls'?: string;
3737
}
3838

39+
type ExtendedActionProps = ActionProps & {
40+
[key: string]: any;
41+
};
3942
export interface ResponseActionProps {
4043
/** Props for message actions, such as feedback (positive or negative), copy button, share, and listen */
41-
actions: Record<string, ActionProps | undefined> & {
44+
actions: Record<string, ExtendedActionProps | undefined> & {
4245
positive?: ActionProps;
4346
negative?: ActionProps;
4447
copy?: ActionProps;
@@ -78,6 +81,7 @@ export const ResponseActions: React.FunctionComponent<ResponseActionProps> = ({
7881
<div ref={responseActions} className="pf-chatbot__response-actions">
7982
{positive && (
8083
<ResponseActionButton
84+
{...positive}
8185
ariaLabel={positive.ariaLabel ?? 'Good response'}
8286
clickedAriaLabel={positive.ariaLabel ?? 'Response recorded'}
8387
onClick={(e) => handleClick(e, 'positive', positive.onClick)}
@@ -95,6 +99,7 @@ export const ResponseActions: React.FunctionComponent<ResponseActionProps> = ({
9599
)}
96100
{negative && (
97101
<ResponseActionButton
102+
{...negative}
98103
ariaLabel={negative.ariaLabel ?? 'Bad response'}
99104
clickedAriaLabel={negative.ariaLabel ?? 'Response recorded'}
100105
onClick={(e) => handleClick(e, 'negative', negative.onClick)}
@@ -112,6 +117,7 @@ export const ResponseActions: React.FunctionComponent<ResponseActionProps> = ({
112117
)}
113118
{copy && (
114119
<ResponseActionButton
120+
{...copy}
115121
ariaLabel={copy.ariaLabel ?? 'Copy'}
116122
clickedAriaLabel={copy.ariaLabel ?? 'Copied'}
117123
onClick={(e) => handleClick(e, 'copy', copy.onClick)}
@@ -129,6 +135,7 @@ export const ResponseActions: React.FunctionComponent<ResponseActionProps> = ({
129135
)}
130136
{share && (
131137
<ResponseActionButton
138+
{...share}
132139
ariaLabel={share.ariaLabel ?? 'Share'}
133140
clickedAriaLabel={share.ariaLabel ?? 'Shared'}
134141
onClick={(e) => handleClick(e, 'share', share.onClick)}
@@ -146,6 +153,7 @@ export const ResponseActions: React.FunctionComponent<ResponseActionProps> = ({
146153
)}
147154
{listen && (
148155
<ResponseActionButton
156+
{...listen}
149157
ariaLabel={listen.ariaLabel ?? 'Listen'}
150158
clickedAriaLabel={listen.ariaLabel ?? 'Listening'}
151159
onClick={(e) => handleClick(e, 'listen', listen.onClick)}
@@ -163,6 +171,7 @@ export const ResponseActions: React.FunctionComponent<ResponseActionProps> = ({
163171
)}
164172
{Object.keys(additionalActions).map((action) => (
165173
<ResponseActionButton
174+
{...additionalActions[action]}
166175
key={action}
167176
ariaLabel={additionalActions[action]?.ariaLabel}
168177
clickedAriaLabel={additionalActions[action]?.clickedAriaLabel}

0 commit comments

Comments
 (0)