Skip to content

Commit

Permalink
[palette] Better defensive logic
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Aug 3, 2018
1 parent 1d6ba9a commit a8040a0
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 a8040a0

Please sign in to comment.