Skip to content
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

[system] Remove dependency on overridesResolver for the variants #27859

Merged
merged 6 commits into from Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/material-ui-system/src/createStyled.js
Expand Up @@ -136,7 +136,7 @@ export default function createStyled(input = {}) {
});
}

if (componentName && overridesResolver && !skipVariantsResolver) {
if (componentName && !skipVariantsResolver) {
expressionsWithDefaultTheme.push((props) => {
const theme = isEmpty(props.theme) ? defaultTheme : props.theme;
return variantsResolver(
Expand Down
24 changes: 24 additions & 0 deletions packages/material-ui-system/src/styled.test.js
Expand Up @@ -308,6 +308,30 @@ describe('styled', () => {
});
});

it('variants should not be skipped if overridesResolver is not defined', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "skipped" here. The test looks like it's not using variant in any way and is also passing without the change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you maybe use a theme that has styles for this specific variant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is used on line 322 https://github.com/mui-org/material-ui/pull/27859/files#diff-57eedb98ff493d66eb3e22d9e9ef1b1540504af969aa62accbb30b3a2a780044R322 The same theme is used in all tests. It is defined in https://github.com/mui-org/material-ui/blob/5585e100feec585f40f42ba2db605a56d1594718/packages/material-ui-system/src/styled.test.js#L140

Previously the variants from the theme were not applied, if when creating the component, the overridesResolver option was not applied. That's what I mean by should not be skipped.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the variants from the theme were not applied, if when creating the component, the overridesResolver option was not applied.

But that's not related to the change in implementation in this PR. I ran the test with yarn test:karma and in JSDOM locally on next and it is passing.

This is confusing long-term because usually tests are coupled with the change in implementation. But here the change in implementation and test are unrelated.

Maybe you need to assert on different CSS properties to showcase what behavior changed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. I've fixed the tests. I was adding tests for a non root slot, but then variants are never applied :)

const TestSlot = styled('div', {
shouldForwardProp: (prop) => prop !== 'variant' && prop !== 'size' && prop !== 'sx',
name: 'MuiTest',
slot: 'Root',
})`
width: 800px;
height: 300px;
`;

const { container } = render(
<ThemeProvider theme={theme}>
<TestSlot variant="rect" size="large">
Test
</TestSlot>
</ThemeProvider>,
);

expect(container.firstChild).toHaveComputedStyle({
width: '400px',
height: '400px',
});
});

it('variants should respect skipVariantsResolver if defined', () => {
const TestSlot = styled('div', {
shouldForwardProp: (prop) => prop !== 'variant' && prop !== 'size' && prop !== 'sx',
Expand Down