From f6036ed612ea9aede2880f8669144614985a9e25 Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Fri, 3 Aug 2018 22:47:06 +0200 Subject: [PATCH] [palette] Better defensive logic (#12402) --- packages/material-ui/src/styles/createPalette.js | 10 ++++++++++ packages/material-ui/src/styles/createPalette.test.js | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/material-ui/src/styles/createPalette.js b/packages/material-ui/src/styles/createPalette.js index b15e7c354446c2..9441749a86f691 100644 --- a/packages/material-ui/src/styles/createPalette.js +++ b/packages/material-ui/src/styles/createPalette.js @@ -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) { diff --git a/packages/material-ui/src/styles/createPalette.test.js b/packages/material-ui/src/styles/createPalette.test.js index ee90d0d3d38ac5..55c229d08c73b2 100644 --- a/packages/material-ui/src/styles/createPalette.test.js +++ b/packages/material-ui/src/styles/createPalette.test.js @@ -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/); + }); + }); });