-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] Add tests for the custom slots of
TreeItem2
(#13314)
- Loading branch information
1 parent
804f882
commit 220d83d
Showing
5 changed files
with
172 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as React from 'react'; | ||
import { createRenderer } from '@mui/internal-test-utils'; | ||
import { TreeItem2 } from '@mui/x-tree-view/TreeItem2'; | ||
import { treeItemClasses as classes } from '@mui/x-tree-view/TreeItem'; | ||
import { TreeViewContext } from '@mui/x-tree-view/internals/TreeViewProvider/TreeViewContext'; | ||
import { describeConformance } from 'test/utils/describeConformance'; | ||
import { getFakeContextValue } from 'test/utils/tree-view/fakeContextValue'; | ||
import { describeSlotsConformance } from 'test/utils/describeSlotsConformance'; | ||
|
||
describe('<TreeItem2 />', () => { | ||
const { render } = createRenderer(); | ||
|
||
describeConformance(<TreeItem2 itemId="one" label="one" />, () => ({ | ||
classes, | ||
inheritComponent: 'li', | ||
wrapMount: (mount) => (item: React.ReactNode) => { | ||
const wrapper = mount( | ||
<TreeViewContext.Provider value={getFakeContextValue()}>{item}</TreeViewContext.Provider>, | ||
); | ||
return wrapper.childAt(0); | ||
}, | ||
render: (item) => { | ||
return render( | ||
<TreeViewContext.Provider value={getFakeContextValue()}>{item}</TreeViewContext.Provider>, | ||
); | ||
}, | ||
muiName: 'MuiTreeItem2', | ||
refInstanceof: window.HTMLLIElement, | ||
skip: ['reactTestRenderer', 'componentProp', 'componentsProp', 'themeVariants'], | ||
})); | ||
|
||
describeSlotsConformance({ | ||
render, | ||
getElement: ({ props, slotName }) => ( | ||
<TreeViewContext.Provider | ||
value={getFakeContextValue({ checkboxSelection: slotName === 'checkbox' })} | ||
> | ||
<TreeItem2 itemId="one" label="one" {...props} /> | ||
</TreeViewContext.Provider> | ||
), | ||
slots: { | ||
label: { className: classes.label }, | ||
iconContainer: { className: classes.iconContainer }, | ||
content: { className: classes.content }, | ||
checkbox: { className: classes.checkbox }, | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import createDescribe from '@mui/internal-test-utils/createDescribe'; | ||
import { MuiRenderResult } from '@mui/internal-test-utils/createRenderer'; | ||
|
||
interface DescribeSlotsConformanceParams { | ||
getElement: (params: { slotName: string; props: any }) => React.ReactElement<any>; | ||
render: (node: React.ReactElement) => MuiRenderResult; | ||
slots: { [slotName: string]: { className: string } }; | ||
} | ||
|
||
export function innerDescribeSlotsConformance(params: DescribeSlotsConformanceParams) { | ||
const { getElement, render, slots } = params; | ||
|
||
Object.keys(slots).forEach((slotName) => { | ||
describe(`Slot: ${slotName}`, () => { | ||
it('should replace the default slot when defined', () => { | ||
const slotConfig = slots[slotName]; | ||
const response = render( | ||
getElement({ | ||
slotName, | ||
props: { | ||
slots: { | ||
[slotName]: React.forwardRef((props, ref: React.Ref<HTMLDivElement>) => ( | ||
<div ref={ref} data-testid="custom-slot" /> | ||
)), | ||
}, | ||
}, | ||
}), | ||
); | ||
|
||
// Check if the default slot is being rendered | ||
expect(response.container.querySelector(`.${slotConfig.className}`)).to.equal(null); | ||
|
||
// Check if the custom slot is being rendered | ||
expect(response.getByTestId('custom-slot')).to.not.equal(null); | ||
}); | ||
|
||
it('should pass props to the default slot', () => { | ||
const slotConfig = slots[slotName]; | ||
const response = render( | ||
getElement({ | ||
slotName, | ||
props: { | ||
slotProps: { | ||
[slotName]: { 'data-testid': 'default-slot', className: 'default-slot' }, | ||
}, | ||
}, | ||
}), | ||
); | ||
|
||
const slotElement = response.container.querySelector(`.${slotConfig.className}`); | ||
|
||
// Check if the default slot is being rendered | ||
expect(slotElement).not.to.equal(null); | ||
|
||
// Check if the default slot receives the `data-testid` | ||
expect(slotElement).to.have.attribute('data-testid', 'default-slot'); | ||
|
||
// Check if the custom class is being applied | ||
expect(slotElement).to.have.class('default-slot'); | ||
|
||
// Make sure that the default class has not been removed | ||
expect(slotElement).to.have.class(slotConfig.className); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Test the slots of the component. | ||
*/ | ||
export const describeSlotsConformance = createDescribe( | ||
'Slots conformance', | ||
innerDescribeSlotsConformance, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { TreeViewContextValue } from '@mui/x-tree-view/internals/TreeViewProvider'; | ||
import { SimpleTreeViewPluginSignatures } from '@mui/x-tree-view/SimpleTreeView/SimpleTreeView.plugins'; | ||
|
||
export const getFakeContextValue = ( | ||
features: { checkboxSelection?: boolean } = {}, | ||
): TreeViewContextValue<SimpleTreeViewPluginSignatures> => ({ | ||
instance: { | ||
isItemExpandable: () => false, | ||
isItemExpanded: () => false, | ||
isItemFocused: () => false, | ||
isItemSelected: () => false, | ||
isItemDisabled: (itemId: string | null): itemId is string => !!itemId, | ||
getTreeItemIdAttribute: () => '', | ||
mapFirstCharFromJSX: () => () => {}, | ||
canItemBeTabbed: () => false, | ||
} as any, | ||
publicAPI: { | ||
focusItem: () => {}, | ||
getItem: () => ({}), | ||
setItemExpansion: () => {}, | ||
}, | ||
runItemPlugins: () => ({ | ||
rootRef: null, | ||
contentRef: null, | ||
}), | ||
wrapItem: ({ children }) => children, | ||
wrapRoot: ({ children }) => children, | ||
disabledItemsFocusable: false, | ||
indentationAtItemLevel: false, | ||
icons: { | ||
slots: {}, | ||
slotProps: {}, | ||
}, | ||
selection: { | ||
multiSelect: false, | ||
checkboxSelection: features.checkboxSelection ?? false, | ||
disableSelection: false, | ||
}, | ||
rootRef: { | ||
current: null, | ||
}, | ||
}); |