Skip to content

Commit 2b4c449

Browse files
mshrivercursoragent
andcommitted
chore(ExpandableSection): Include OUIAProps for ExpandableSection
Add OUIA attribute support to ExpandableSection for better test automation. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 8c89c01 commit 2b4c449

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-mi
66
import { PickOptional } from '../../helpers/typeUtils';
77
import { debounce } from '../../helpers/util';
88
import { getResizeObserver } from '../../helpers/resizeObserver';
9-
import { GenerateId } from '../../helpers';
9+
import { GenerateId, getOUIAProps, OUIAProps } from '../../helpers';
1010
import { Button } from '../Button';
1111

1212
export enum ExpandableSectionVariant {
@@ -16,7 +16,7 @@ export enum ExpandableSectionVariant {
1616

1717
/** The main expandable section component. */
1818

19-
export interface ExpandableSectionProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onToggle'> {
19+
export interface ExpandableSectionProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onToggle'>, OUIAProps {
2020
/** Content rendered inside the expandable section. */
2121
children?: React.ReactNode;
2222
/** Additional classes added to the expandable section. */
@@ -84,6 +84,10 @@ export interface ExpandableSectionProps extends Omit<React.HTMLProps<HTMLDivElem
8484
* This is useful when the toggle text should function as a heading in the document structure.
8585
*/
8686
toggleWrapper?: 'div' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
87+
/** Value to overwrite the randomly generated data-ouia-component-id.*/
88+
ouiaId?: number | string;
89+
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
90+
ouiaSafe?: boolean;
8791
}
8892

8993
interface ExpandableSectionState {
@@ -134,7 +138,8 @@ class ExpandableSection extends Component<ExpandableSectionProps, ExpandableSect
134138
displaySize: 'default',
135139
isWidthLimited: false,
136140
isIndented: false,
137-
variant: 'default'
141+
variant: 'default',
142+
ouiaSafe: true
138143
};
139144

140145
private calculateToggleText(
@@ -232,6 +237,8 @@ class ExpandableSection extends Component<ExpandableSectionProps, ExpandableSect
232237
truncateMaxLines,
233238
direction,
234239
toggleWrapper = 'div',
240+
ouiaId,
241+
ouiaSafe,
235242
...props
236243
} = this.props;
237244

@@ -307,6 +314,7 @@ class ExpandableSection extends Component<ExpandableSectionProps, ExpandableSect
307314
className
308315
)}
309316
{...props}
317+
{...getOUIAProps(ExpandableSection.displayName, ouiaId, ouiaSafe)}
310318
>
311319
{variant === ExpandableSectionVariant.default && expandableToggle}
312320
<div

packages/react-core/src/components/ExpandableSection/__tests__/ExpandableSection.test.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import RhUiNotificationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-
88
const props = { contentId: 'content-id', toggleId: 'toggle-id' };
99

1010
test('ExpandableSection', () => {
11-
const { asFragment } = render(<ExpandableSection {...props}>test </ExpandableSection>);
11+
const { asFragment } = render(
12+
<ExpandableSection {...props} ouiaId="ouia-id">
13+
test{' '}
14+
</ExpandableSection>
15+
);
1216
expect(asFragment()).toMatchSnapshot();
1317
});
1418

@@ -306,3 +310,27 @@ test('Renders toggle icon by default when hasToggleIcon is true', () => {
306310
const button = screen.getByRole('button');
307311
expect(button.querySelector('.pf-v6-c-expandable-section__toggle-icon')).toBeInTheDocument();
308312
});
313+
314+
test('Renders with custom ouiaId', () => {
315+
const { container } = render(<ExpandableSection ouiaId="test-id">Test content</ExpandableSection>);
316+
expect(container.firstChild).toHaveAttribute('data-ouia-component-id', 'test-id');
317+
});
318+
319+
test('Renders with expected ouia component type', () => {
320+
const { container } = render(<ExpandableSection ouiaId="test-id">Test content</ExpandableSection>);
321+
expect(container.firstChild).toHaveAttribute('data-ouia-component-type', 'PF6/ExpandableSection');
322+
});
323+
324+
test('Renders with ouiaSafe defaulting to true', () => {
325+
const { container } = render(<ExpandableSection ouiaId="test-id">Test content</ExpandableSection>);
326+
expect(container.firstChild).toHaveAttribute('data-ouia-safe', 'true');
327+
});
328+
329+
test('Renders with ouiaSafe=false when specified', () => {
330+
const { container } = render(
331+
<ExpandableSection ouiaId="test-id" ouiaSafe={false}>
332+
Test content
333+
</ExpandableSection>
334+
);
335+
expect(container.firstChild).toHaveAttribute('data-ouia-safe', 'false');
336+
});

packages/react-core/src/components/ExpandableSection/__tests__/__snapshots__/ExpandableSection.test.tsx.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ exports[`Disclosure ExpandableSection 1`] = `
44
<DocumentFragment>
55
<div
66
class="pf-v6-c-expandable-section pf-m-display-lg pf-m-limit-width"
7+
data-ouia-component-type="PF6/ExpandableSection"
8+
data-ouia-safe="true"
79
>
810
<div
911
class="pf-v6-c-expandable-section__toggle"
@@ -57,6 +59,9 @@ exports[`ExpandableSection 1`] = `
5759
<DocumentFragment>
5860
<div
5961
class="pf-v6-c-expandable-section"
62+
data-ouia-component-id="ouia-id"
63+
data-ouia-component-type="PF6/ExpandableSection"
64+
data-ouia-safe="true"
6065
>
6166
<div
6267
class="pf-v6-c-expandable-section__toggle"
@@ -110,6 +115,8 @@ exports[`Renders ExpandableSection expanded 1`] = `
110115
<DocumentFragment>
111116
<div
112117
class="pf-v6-c-expandable-section pf-m-expanded"
118+
data-ouia-component-type="PF6/ExpandableSection"
119+
data-ouia-safe="true"
113120
>
114121
<div
115122
class="pf-v6-c-expandable-section__toggle"
@@ -163,6 +170,8 @@ exports[`Renders ExpandableSection indented 1`] = `
163170
<DocumentFragment>
164171
<div
165172
class="pf-v6-c-expandable-section pf-m-expanded pf-m-indented"
173+
data-ouia-component-type="PF6/ExpandableSection"
174+
data-ouia-safe="true"
166175
>
167176
<div
168177
class="pf-v6-c-expandable-section__toggle"
@@ -216,6 +225,8 @@ exports[`Renders Uncontrolled ExpandableSection 1`] = `
216225
<DocumentFragment>
217226
<div
218227
class="pf-v6-c-expandable-section"
228+
data-ouia-component-type="PF6/ExpandableSection"
229+
data-ouia-safe="true"
219230
>
220231
<div
221232
class="pf-v6-c-expandable-section__toggle"

packages/react-core/src/helpers/OUIA/OUIA.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ component.
5757
* [Content](/components/content)
5858
* [Dropdown](/components/menus/dropdown)
5959
* [DropdownItem](/components/menus/dropdown)
60+
* [ExpandableSection](/components/expandable-section)
6061
* [Form](/components/forms/form)
6162
* [FormGroup](/components/forms/form)
6263
* [FormSelect](/components/forms/form-select)

0 commit comments

Comments
 (0)