Skip to content

Commit

Permalink
[core] Replace enzyme in describeConformance
Browse files Browse the repository at this point in the history
Co-authored-by: Aarón García Hervás <aaron@mui.com>
  • Loading branch information
DiegoAndai and aarongarciah committed Jul 5, 2024
1 parent 3f2aff4 commit fb6c7d9
Show file tree
Hide file tree
Showing 30 changed files with 129 additions and 183 deletions.
2 changes: 1 addition & 1 deletion docs/pages/joy-ui/api/tooltip.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
"isGlobal": false
}
],
"spread": true,
"spread": false,
"themeDefaultProps": true,
"muiName": "JoyTooltip",
"forwardsRefTo": "HTMLButtonElement",
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/material-ui/api/switch.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"isGlobal": false
}
],
"spread": false,
"spread": true,
"themeDefaultProps": false,
"muiName": "MuiSwitch",
"forwardsRefTo": "HTMLSpanElement",
Expand Down
20 changes: 19 additions & 1 deletion packages/mui-base/test/describeConformanceUnstyled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
SlotTestingOptions,
describeRef,
randomStringValue,
testClassName,
testComponentProp,
testReactTestRenderer,
} from '@mui-internal/test-utils';
Expand Down Expand Up @@ -266,6 +265,25 @@ function testSlotPropsProp(
});
}

function testClassName(element: React.ReactElement, getOptions: () => ConformanceOptions) {
it('applies the className to the root component', async () => {
const { render } = getOptions();

if (!render) {
throwMissingPropError('render');
}

const className = randomStringValue();
const testId = randomStringValue();

const { getByTestId } = await render(
React.cloneElement(element, { className, 'data-testid': testId }),
);

expect(getByTestId(testId)).to.have.class(className);
});
}

interface TestOwnerState {
'data-testid'?: string;
}
Expand Down
11 changes: 8 additions & 3 deletions packages/mui-joy/src/Autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,14 @@ const Autocomplete = React.forwardRef(function Autocomplete(
},
});

const defaultRenderOption = (optionProps: any, option: unknown) => (
<SlotOption {...optionProps}>{getOptionLabel(option)}</SlotOption>
);
const defaultRenderOption = (optionProps: any, option: unknown) => {
const { key, ...rest } = optionProps;
return (
<SlotOption key={key} {...rest}>
{getOptionLabel(option)}
</SlotOption>
);
};

const renderOption = renderOptionProp || defaultRenderOption;

Expand Down
6 changes: 0 additions & 6 deletions packages/mui-joy/src/Menu/Menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ describe('Joy <Menu />', () => {
<DropdownContext.Provider value={testContext}>{node}</DropdownContext.Provider>,
);
},
wrapMount: (mount) => (node: React.ReactNode) => {
const wrapper = mount(
<DropdownContext.Provider value={testContext}>{node}</DropdownContext.Provider>,
);
return wrapper.childAt(0);
},
ThemeProvider,
muiName: 'JoyMenu',
refInstanceof: window.HTMLUListElement,
Expand Down
6 changes: 0 additions & 6 deletions packages/mui-joy/src/MenuButton/MenuButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ describe('<MenuButton />', () => {
describeConformance(<MenuButton />, () => ({
classes,
inheritComponent: 'button',
wrapMount: (mount) => (node: React.ReactNode) => {
const wrapper = mount(
<DropdownContext.Provider value={testContext}>{node}</DropdownContext.Provider>,
);
return wrapper.childAt(0);
},
muiName: 'JoyMenuButton',
refInstanceof: window.HTMLButtonElement,
render: (node) => {
Expand Down
4 changes: 0 additions & 4 deletions packages/mui-joy/src/MenuItem/MenuItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ describe('Joy <MenuItem />', () => {
classes,
inheritComponent: ListItemButton,
render: (node) => render(<MenuProvider value={testContext}>{node}</MenuProvider>),
wrapMount: (mount) => (node) => {
const wrapper = mount(<MenuProvider value={testContext}>{node}</MenuProvider>);
return wrapper.childAt(0);
},
ThemeProvider,
refInstanceof: window.HTMLLIElement,
testComponentPropWith: 'a',
Expand Down
1 change: 0 additions & 1 deletion packages/mui-joy/src/Tab/Tab.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ describe('Joy <Tab />', () => {
classes,
inheritComponent: 'button',
render: (node) => render(<TabsProvider defaultValue={0}>{node}</TabsProvider>),
wrapMount: (mount) => (node) => mount(<TabsProvider defaultValue={0}>{node}</TabsProvider>),
ThemeProvider,
muiName: 'JoyTab',
refInstanceof: window.HTMLButtonElement,
Expand Down
1 change: 0 additions & 1 deletion packages/mui-joy/src/TabList/TabList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe('Joy <TabList />', () => {
classes,
inheritComponent: 'div',
render: (node) => render(<TabsProvider defaultValue={0}>{node}</TabsProvider>),
wrapMount: (mount) => (node) => mount(<TabsProvider defaultValue={0}>{node}</TabsProvider>),
ThemeProvider,
muiName: 'JoyTabList',
refInstanceof: window.HTMLDivElement,
Expand Down
1 change: 0 additions & 1 deletion packages/mui-joy/src/TabPanel/TabPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('Joy <TabPanel />', () => {
classes,
inheritComponent: 'div',
render: (node) => render(<TabsProvider defaultValue={0}>{node}</TabsProvider>),
wrapMount: (mount) => (node) => mount(<TabsProvider defaultValue={0}>{node}</TabsProvider>),
ThemeProvider,
muiName: 'JoyTabPanel',
refInstanceof: window.HTMLDivElement,
Expand Down
6 changes: 6 additions & 0 deletions packages/mui-joy/src/Tooltip/Tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ describe('<Tooltip />', () => {
'themeVariants',
// react-transition-group issue
'reactTestRenderer',
// Props are spread to root and children
// We cannot use the standard propsSpread test which relies on data-testid only on the root
'propsSpread',
// Props are spread to root and children
// We cannot use the standard mergeClassName test which relies on data-testid only on the root
'mergeClassName',
],
}),
);
Expand Down
4 changes: 0 additions & 4 deletions packages/mui-lab/src/TabList/TabList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ describe('<TabList />', () => {
* @param {React.ReactNode} node
*/
render: (node) => render(<TabContext value="0">{node}</TabContext>),
wrapMount: (mount) => (node) => {
const wrapper = mount(<TabContext value="0">{node}</TabContext>);
return wrapper.childAt(0);
},
refInstanceof: window.HTMLDivElement,
// TODO: no idea why reactTestRenderer fails
skip: [
Expand Down
1 change: 0 additions & 1 deletion packages/mui-lab/src/TabPanel/TabPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe('<TabPanel />', () => {
classes,
inheritComponent: 'div',
render: (node) => render(<TabContext value="0">{node}</TabContext>),
wrapMount: (mount) => (node) => mount(<TabContext value="0">{node}</TabContext>),
refInstanceof: window.HTMLDivElement,
muiName: 'MuiTabPanel',
skip: [
Expand Down
1 change: 1 addition & 0 deletions packages/mui-material/src/Fade/Fade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('<Fade />', () => {
};

describeConformance(<Fade {...defaultProps} />, () => ({
render,
classes: {},
inheritComponent: Transition,
refInstanceof: window.HTMLDivElement,
Expand Down
3 changes: 2 additions & 1 deletion packages/mui-material/src/Grow/Grow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ describe('<Grow />', () => {
};

describeConformance(
<Grow in>
<Grow in appear={false}>
<div />
</Grow>,
() => ({
render,
classes: {},
inheritComponent: Transition,
refInstanceof: window.HTMLDivElement,
Expand Down
1 change: 1 addition & 0 deletions packages/mui-material/src/MenuList/MenuList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('<MenuList />', () => {
const { render } = createRenderer();

describeConformance(<MenuList />, () => ({
render,
classes: {},
inheritComponent: List,
refInstanceof: window.HTMLUListElement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('<NativeSelectInput />', () => {
const { render } = createRenderer();

describeConformance(<NativeSelectInput IconComponent="div" />, () => ({
render,
only: ['refForwarding'],
refInstanceof: window.HTMLSelectElement,
muiName: 'MuiNativeSelectInput',
Expand Down
1 change: 1 addition & 0 deletions packages/mui-material/src/RadioGroup/RadioGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('<RadioGroup />', () => {
const { render } = createRenderer();

describeConformance(<RadioGroup value="" />, () => ({
render,
classes: {},
inheritComponent: FormGroup,
refInstanceof: window.HTMLDivElement,
Expand Down
1 change: 1 addition & 0 deletions packages/mui-material/src/Slide/Slide.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('<Slide />', () => {
<div />
</Slide>,
() => ({
render,
classes: {},
inheritComponent: Transition,
refInstanceof: window.HTMLDivElement,
Expand Down
16 changes: 0 additions & 16 deletions packages/mui-material/src/StepContent/StepContent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ describe('<StepContent />', () => {
describeConformance(<StepContent />, () => ({
classes,
inheritComponent: 'div',
wrapMount: (mount) => (node) => {
const wrapper = mount(
<Stepper orientation="vertical">
<Step>{node}</Step>
</Stepper>,
);
// `wrapper.find(Step)` tree.
// "->" indicates the path we want
// "n:" indicates the index
// <ForwardRef(Step)>
// -> 0: <MuiStepRoot>
// 0: <Noop /> // from Emotion
// -> 1: <div className="MuiStep-root">
// -> 0: <MuiStepContentRoot />
return wrapper.find(Step).childAt(0).childAt(1).childAt(0);
},
muiName: 'MuiStepContent',
refInstanceof: window.HTMLDivElement,
render: (node) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('<SwipeableDrawer />', () => {
const { render } = createRenderer({ clock: 'fake' });

describeConformance(<SwipeableDrawer onOpen={() => {}} onClose={() => {}} open />, () => ({
render,
classes: {},
inheritComponent: Drawer,
refInstanceof: window.HTMLDivElement,
Expand Down
19 changes: 18 additions & 1 deletion packages/mui-material/src/Switch/Switch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ describe('<Switch />', () => {
{ slotName: 'input', slotClassName: classes.input },
],
refInstanceof: window.HTMLSpanElement,
skip: ['componentProp', 'componentsProp', 'propsSpread', 'themeDefaultProps', 'themeVariants'],
skip: [
'componentProp',
'componentsProp',
'themeDefaultProps',
'themeVariants',
// Props are spread to the root's child but className is added to the root
// We cannot use the standard mergeClassName test which relies on data-testid on the root
// We should fix this when refactoring with Base UI
'mergeClassName',
],
}));

describe('styleSheet', () => {
Expand Down Expand Up @@ -142,4 +151,12 @@ describe('<Switch />', () => {
});
});
});

describe('mergeClassName', () => {
it('should merge the className', () => {
const { container } = render(<Switch className="test-class-name" />);

expect(container.firstChild).to.have.class('test-class-name');
});
});
});
4 changes: 0 additions & 4 deletions packages/mui-material/src/TableBody/TableBody.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ describe('<TableBody />', () => {
describeConformance(<TableBody />, () => ({
classes,
inheritComponent: 'tbody',
wrapMount: (mount) => (node) => {
const wrapper = mount(<table>{node}</table>);
return wrapper.find('table').childAt(0);
},
render: (node) => {
const { container, ...other } = render(<table>{node}</table>);
return { container: container.firstChild, ...other };
Expand Down
10 changes: 0 additions & 10 deletions packages/mui-material/src/TableCell/TableCell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ describe('<TableCell />', () => {
);
return { container: container.firstChild.firstChild.firstChild, ...other };
},
wrapMount: (mount) => (node) => {
const wrapper = mount(
<table>
<tbody>
<tr>{node}</tr>
</tbody>
</table>,
);
return wrapper.find('tr').childAt(0);
},
muiName: 'MuiTableCell',
testVariantProps: { variant: 'body' },
refInstanceof: window.HTMLTableCellElement,
Expand Down
4 changes: 0 additions & 4 deletions packages/mui-material/src/TableFooter/TableFooter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ describe('<TableFooter />', () => {
const { container, ...other } = render(<table>{node}</table>);
return { container: container.firstChild, ...other };
},
wrapMount: (mount) => (node) => {
const wrapper = mount(<table>{node}</table>);
return wrapper.find('table').childAt(0);
},
muiName: 'MuiTableFooter',
testVariantProps: { variant: 'foo' },
refInstanceof: window.HTMLTableSectionElement,
Expand Down
4 changes: 0 additions & 4 deletions packages/mui-material/src/TableHead/TableHead.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ describe('<TableHead />', () => {
describeConformance(<TableHead />, () => ({
classes,
inheritComponent: 'thead',
wrapMount: (mount) => (node) => {
const wrapper = mount(<table>{node}</table>);
return wrapper.find('table').childAt(0);
},
render: (node) => {
const { container, ...other } = render(<table>{node}</table>);
return { container: container.firstChild, ...other };
Expand Down
10 changes: 0 additions & 10 deletions packages/mui-material/src/TablePagination/TablePagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ describe('<TablePagination />', () => {
);
return { container: container.firstChild.firstChild.firstChild, ...other };
},
wrapMount: (mount) => (node) => {
const wrapper = mount(
<table>
<tbody>
<tr>{node}</tr>
</tbody>
</table>,
);
return wrapper.find('tr').childAt(0);
},
muiName: 'MuiTablePagination',
refInstanceof: window.HTMLTableCellElement,
testComponentPropWith: 'td',
Expand Down
8 changes: 0 additions & 8 deletions packages/mui-material/src/TableRow/TableRow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ describe('<TableRow />', () => {
);
return { container: container.firstChild.firstChild, ...other };
},
wrapMount: (mount) => (node) => {
const wrapper = mount(
<table>
<tbody>{node}</tbody>
</table>,
);
return wrapper.find('tbody').childAt(0);
},
muiName: 'MuiTableRow',
testVariantProps: { variant: 'foo' },
refInstanceof: window.HTMLTableRowElement,
Expand Down
1 change: 1 addition & 0 deletions packages/mui-material/src/Zoom/Zoom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('<Zoom />', () => {
<div />
</Zoom>,
() => ({
render,
classes: {},
inheritComponent: Transition,
refInstanceof: window.HTMLDivElement,
Expand Down
Loading

0 comments on commit fb6c7d9

Please sign in to comment.