Skip to content

Commit

Permalink
[palette] Better defensive logic (#12402)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored Aug 3, 2018
1 parent 72716a5 commit f6036ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/material-ui/src/styles/createPalette.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ export default function createPalette(palette: Object) {
if (!color.main && color[mainShade]) {
color.main = color[mainShade];
}

if (process.env.NODE_ENV !== 'production' && !color.main) {
throw new Error(
[
'Material-UI: the color provided to augmentColor(color) is invalid.',
`The color object needs to have a \`main\` property or a \`${mainShade}\` property.`,
].join('\n'),
);
}

addLightOrDark(color, 'light', lightShade, tonalOffset);
addLightOrDark(color, 'dark', darkShade, tonalOffset);
if (!color.contrastText) {
Expand Down
9 changes: 9 additions & 0 deletions packages/material-ui/src/styles/createPalette.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,13 @@ describe('createPalette()', () => {
/Material-UI: the palette type `foo` is not supported/,
);
});

describe('augmentColor', () => {
it('should throw when the input is invalid', () => {
const palette = createPalette({});
assert.throws(() => {
palette.augmentColor({});
}, /The color object needs to have a/);
});
});
});

0 comments on commit f6036ed

Please sign in to comment.