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

[material-ui][Typography] Color prop check for primitive type #39071

Merged
merged 8 commits into from
Oct 5, 2023
Merged
20 changes: 20 additions & 0 deletions packages/mui-material/src/Typography/Typography.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as React from 'react';
import { expect } from 'chai';
import { createRenderer, describeConformance } from '@mui-internal/test-utils';
import Typography, { typographyClasses as classes } from '@mui/material/Typography';
import sinon from 'sinon';

describe('<Typography />', () => {
const { render } = createRenderer();
Expand Down Expand Up @@ -112,6 +113,25 @@ describe('<Typography />', () => {
});
});

describe('prop: color', () => {
it('should check for invalid color value', () => {
const consoleWarnStub = sinon.stub(console, 'warn');

render(
<Typography variant="h6" color="background">
Hello
</Typography>,
);

sinon.assert.calledWith(
consoleWarnStub,
'MUI: The value found in theme for prop: "background" is an [Object] instead of string or number. Check if you forgot to add the correct dotted notation, eg, "background.paper" instead of "background".',
);

consoleWarnStub.restore();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
render(
<Typography variant="h6" color="background">
Hello
</Typography>,
);
sinon.assert.calledWith(
consoleWarnStub,
'MUI: The value found in theme for prop: "background" is an [Object] instead of string or number. Check if you forgot to add the correct dotted notation, eg, "background.paper" instead of "background".',
);
consoleWarnStub.restore();
expect(() => render(
<Typography variant="h6" color="background">
Hello
</Typography>,
)).toWarnDev('MUI: The value found in theme for prop: "background" is an [Object] instead of string or number. Check if you forgot to add the correct dotted notation, eg, "background.paper" instead of "background".')

No need to use sinon. Same change in other place as well.

Copy link
Contributor Author

@DarhkVoyd DarhkVoyd Sep 28, 2023

Choose a reason for hiding this comment

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

I am unable to pass any of the warn tests this way, I get similar errors for each one of them:

<Typography /> prop: color should check for invalid color value FAILED
        AssertionError: Recorded unexpected console.warn calls: 

          - Expected no more error messages but got:
        "MUI: The value found in theme for prop: "background" is an [Object] instead of string or number. Check if you forgot to add the correct dotted notation, eg, "background.paper" instead of "background"."

I am unable to debug this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here'e the changes I did - https://github.com/mui/material-ui/compare/master...brijeshb42:material-ui:issue-38478?expand=1

Basically, you add to call toWarnDev([msg, msg]) expecting the warn to be called twice. That's why it was failing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how can I merge these changes? kindly tell if anything else remaining to work on this.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can just copy-paste the toWarnDev related changes to your branch. Nothing else after that.

});
});

it('combines system properties with the sx prop', () => {
const { container } = render(<Typography mt={2} mr={1} sx={{ marginRight: 5, mb: 2 }} />);

Expand Down
11 changes: 11 additions & 0 deletions packages/mui-system/src/palette.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import sinon from 'sinon';
import palette from './palette';

const theme = {
Expand All @@ -9,10 +10,20 @@ const theme = {

describe('palette', () => {
it('should treat grey as CSS color', () => {
const consoleWarnStub = sinon.stub(console, 'warn');

const output = palette({
theme,
backgroundColor: 'grey',
});

sinon.assert.calledWith(
consoleWarnStub,
'MUI: The value found in theme for prop: "grey" is an [Object] instead of string or number. Check if you forgot to add the correct dotted notation, eg, "background.paper" instead of "background".',
);

consoleWarnStub.restore();

expect(output).to.deep.equal({
backgroundColor: 'grey',
});
Expand Down
8 changes: 8 additions & 0 deletions packages/mui-system/src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export function getStyleValue(themeMapping, transform, propValueFinal, userValue
value = getPath(themeMapping, propValueFinal) || userValue;
}

if (typeof value === 'object') {
if (process.env.NODE_ENV !== 'production') {
console.warn(
`MUI: The value found in theme for prop: "${propValueFinal}" is an [Object] instead of string or number. Check if you forgot to add the correct dotted notation, eg, "background.paper" instead of "background".`,
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please also set the userValue to value here like you were doing last time. If a proper value is not found in theme, it should just use whatever user provided in prop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, it was removed on purpose because there are cases when value could be an object such as Typography theme key body1 or h1 which is assigned as an object through handlebreakpoints. So, I think just warning the user for possible mistake is enough. If my point is unclear kindly refer to this test case

}

if (transform) {
value = transform(value, userValue, themeMapping);
}
Expand Down
65 changes: 64 additions & 1 deletion packages/mui-system/src/style.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import style from './style';
import sinon from 'sinon';
import style, { getStyleValue } from './style';

describe('style', () => {
const bgcolor = style({
Expand Down Expand Up @@ -258,4 +259,66 @@ describe('style', () => {
});
});
});
describe('getStyleValue', () => {
it('should warn on acceptable object', () => {
const round = (value) => Math.round(value * 1e5) / 1e5;
const consoleWarnStub = sinon.stub(console, 'warn');

const output = getStyleValue(
{
body1: {
fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
fontSize: '1rem',
letterSpacing: `${round(0.15 / 16)}em`,
fontWeight: 400,
lineHeight: 1.5,
},
},
null,
'body1',
);

sinon.assert.calledWith(
consoleWarnStub,
'MUI: The value found in theme for prop: "body1" is an [Object] instead of string or number. Check if you forgot to add the correct dotted notation, eg, "background.paper" instead of "background".',
);

consoleWarnStub.restore();

expect(output).to.deep.equal({
fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
fontSize: '1rem',
letterSpacing: `${round(0.15 / 16)}em`,
fontWeight: 400,
lineHeight: 1.5,
});
});

it('should warn on unacceptable object', () => {
const theme = {
palette: {
grey: { 100: '#f5f5f5' },
},
};

const paletteTransform = (value, userValue) => {
if (userValue === 'grey') {
return userValue;
}
return value;
};
const consoleWarnStub = sinon.stub(console, 'warn');

const output = getStyleValue(theme.palette, paletteTransform, 'grey');

sinon.assert.calledWith(
consoleWarnStub,
'MUI: The value found in theme for prop: "grey" is an [Object] instead of string or number. Check if you forgot to add the correct dotted notation, eg, "background.paper" instead of "background".',
);

consoleWarnStub.restore();

expect(output).to.be.equal('grey');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import createMixins from '@mui/material/styles/createMixins';
import createTypography from '@mui/material/styles/createTypography';
import sinon from 'sinon';
import createBreakpoints from '../createTheme/createBreakpoints';
import styleFunctionSx from './styleFunctionSx';

Expand Down Expand Up @@ -93,11 +94,16 @@ describe('styleFunctionSx', () => {
});

it('resolves system typography', () => {
const consoleWarnStub = sinon.stub(console, 'warn');

const result = styleFunctionSx({
theme,
sx: { typography: ['body2', 'body1'] },
});

sinon.assert.calledTwice(consoleWarnStub);
consoleWarnStub.restore();

expect(result).to.deep.equal({
'@media (min-width:0px)': {
fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
Expand Down
Loading