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

[test] Fix broken master branch #35446

Merged
merged 3 commits into from Dec 14, 2022

Conversation

oliviertassinari
Copy link
Member

@oliviertassinari oliviertassinari commented Dec 11, 2022

Something I noticed while I was working on the KPI page. master is broken:

Screenshot 2022-12-11 at 13 58 42

https://www.notion.so/mui-org/KPIs-1ce9658b85ce4628a2a2ed2ae74ff69c#59bb101e5eab4375a6018d6694d036a7

The issue is that since #35150, this test never pass:

Screenshot 2022-12-11 at 13 59 16

https://app.circleci.com/pipelines/github/mui/material-ui/87228/workflows/4504c13e-6d89-49fe-9589-60586b34e23a/jobs/461431

My proposed solution is to remove it.


I was also curious, I saw we had no tests for theme.unstable_sx so I added one. On my end, it doesn't behave exactly like I was exepecting:

  1. Colors aren't supported.
  2. When using createTheme() I would expect it returns the raw value.
  3. When using extendTheme() I would expect it returns the CSS variable.

I even wonder if we shouldn't rename these helpers for clarity:

  • createTheme -> createRawTheme. It's the plain values
  • extendTheme -> createTheme. It's the new one

Live demo https://codesandbox.io/s/red-meadow-eejvie?file=/demo.tsx.

@mui-bot
Copy link

mui-bot commented Dec 11, 2022

Messages
📖 Netlify deploy preview: https://deploy-preview-35446--material-ui.netlify.app/

No bundle size changes

Generated by 🚫 dangerJS against d204d4e

expect(theme.unstable_sx({ bgcolor: 'primary.500' })).to.deep.equal({
// TODO fixme
// backgroundColor: 'var(--joy-palette-primary-500)',
backgroundColor: 'primary.500',
Copy link
Member

Choose a reason for hiding this comment

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

This probably fails because it is not used inside CssVarsProvider. The CssVarsProvider is required when using @mui/joy: https://mui.com/joy-ui/getting-started/usage/#quickstart. cc @siriwatknp is this correct?

Copy link
Member

Choose a reason for hiding this comment

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

yes, you are correct.

The theme output from extendTheme() does not have theme.vars and theme.palette.* yet. That's why theme.unstable_sx returns primary.500 because it could not find the palette.

Using ThemeProvider should be enough because it attaches vars and use light color scheme as a palette.

Copy link
Member

Choose a reason for hiding this comment

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

All other tests already use the CssVarsProvider, I'd suggest adding it to this test too.

Copy link
Member

Choose a reason for hiding this comment

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

I push a change, I moved these tests to extendTheme, as it looked more related to it. I also wrap the tests with CssVarsProvider and the results look as expected now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahhh. I have been confused about this a couple of times. Sometimes .vars was defined sometimes not. I couldn't really pinpoint what was happening. Now I get it, it's about either accessing the theme with the context or accessing the theme with a direct import.

Why is CssVarsProvider mutating the theme to add the vars? I mean, why is this logic not inside extendTheme? Looking at the code, It seems that we could move this logic up in the chain.

Copy link
Member

Choose a reason for hiding this comment

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

I would expect that the theme would add vars and maybe point them to the plain values before the CssVarsProvider is used. This would be safest. The palette could be generated the same way too. I know we have it for defaultTheme, maybe we can just add this logic to extendTheme too. I was thinking about this when creating the theme for Material You, but for now used the same approach in Joy UI.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the code:

const { css, vars } = cssVarsParser(scheme, {
prefix: cssVarPrefix,
shouldSkipGeneratingVar,
});
theme.vars = deepmerge(theme.vars, vars);

It seems that theme.vars is defined with params that aren't specific to the theme provider. This is what put me on the track of, we should be able to do:

export const theme = extendTheme({ /* … */ });
import theme from './myTheme';

{
  color: theme.vars.palette.primary.main,
},

Pretty much like https://vanilla-extract.style/documentation/api/create-theme/.

for now used the same approach in Joy UI

👍 to keep these in sync

Copy link
Member

@mnajdova mnajdova Jan 2, 2023

Choose a reason for hiding this comment

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

I will draft a PR for this to see what the changes would look like.

@oliviertassinari
Copy link
Member Author

The patch looks great, @mnajdova thanks for the fix

@oliviertassinari oliviertassinari merged commit 15998ea into mui:master Dec 14, 2022
@oliviertassinari oliviertassinari deleted the fix-flaky-test branch December 14, 2022 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regression A bug, but worse test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants