Skip to content

Commit

Permalink
Automatically set data-testid
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongarciah committed Jun 14, 2024
1 parent 81f9574 commit 5fdc832
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
18 changes: 12 additions & 6 deletions packages-internal/test-utils/src/describeConformance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export interface ConformanceOptions {
*
* It must either:
* - Be a string that is a valid HTML tag, or
* - Contain at least one DOM which has `data-testid="custom"`
* - A component that spread props to the underlying rendered element.
*
* If not provided, the default 'em' element is used
* If not provided, the default 'em' element is used.
*/
testComponentPropWith?: string | React.ElementType;
testDeepOverrides?: SlotTestOverride | SlotTestOverride[];
Expand Down Expand Up @@ -128,17 +128,23 @@ export function testComponentProp(
throwMissingPropError('render');
}

if (typeof component === 'string') {
const testId = randomStringValue();
const testId = randomStringValue();

if (typeof component === 'string') {
const { getByTestId } = render(
React.cloneElement(element, { component, 'data-testid': testId }),
);
expect(getByTestId(testId)).not.to.equal(null);
expect(getByTestId(testId).nodeName.toLowerCase()).to.eq(component);
} else {
const { getByTestId } = render(React.cloneElement(element, { component }));
expect(getByTestId('custom')).not.to.equal(null);
const componentWithTestId = (props: {}) =>
React.createElement(component, { ...props, 'data-testid': testId });
const { getByTestId } = render(
React.cloneElement(element, {
component: componentWithTestId,
}),
);
expect(getByTestId(testId)).not.to.equal(null);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/SvgIcon/SvgIcon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('<SvgIcon />', () => {
muiName: 'JoySvgIcon',
refInstanceof: window.SVGSVGElement,
testComponentPropWith: (props: SvgIconProps) => (
<svg {...props} data-testid="custom">
<svg {...props}>
<defs>
<linearGradient id="gradient1">
<stop offset="20%" stopColor="#39F" />
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/SvgIcon/SvgIcon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('<SvgIcon />', () => {
muiName: 'MuiSvgIcon',
refInstanceof: window.SVGSVGElement,
testComponentPropWith: (props) => (
<svg {...props} data-testid="custom">
<svg {...props}>
<defs>
<linearGradient id="gradient1">
<stop offset="20%" stopColor="#39F" />
Expand Down

0 comments on commit 5fdc832

Please sign in to comment.