-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[test] Add tests for the custom slots of TreeItem2
#13314
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, | ||
}, | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works great for super basic slots that are always rendered, but
describeSlotsConformance
will need a lot more work to support more complex slots likedragAndDropOverlay
or components likeDatePicker
.But it's a 1st draft and for now I think it does the job.