Skip to content

Commit 6b4f52c

Browse files
second review
1 parent afaa5de commit 6b4f52c

File tree

2 files changed

+30
-58
lines changed

2 files changed

+30
-58
lines changed

packages/material-ui/src/styles/createPalette.js

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -160,41 +160,35 @@ export default function createPalette(palette) {
160160
}
161161

162162
if (!color.main) {
163-
if (process.env.NODE_ENV !== 'production') {
164-
console.error(
165-
[
166-
'Material-UI: the color provided to augmentColor(color) is invalid.',
167-
`The color object needs to have a \`main\` property or a \`${mainShade}\` property.`,
168-
].join('\n'),
169-
);
170-
}
171-
color.main = indigo[500];
163+
throw new Error(
164+
[
165+
'Material-UI: the color provided to augmentColor(color) is invalid.',
166+
`The color object needs to have a \`main\` property or a \`${mainShade}\` property.`,
167+
].join('\n'),
168+
);
172169
}
173170

174171
if (typeof color.main !== 'string') {
175-
if (process.env.NODE_ENV !== 'production') {
176-
console.error(
177-
[
178-
'Material-UI: the color provided to augmentColor(color) is invalid.',
179-
`\`color.main\` should be a string, but \`${JSON.stringify(
180-
color.main,
181-
)}\` was provided instead.`,
182-
'',
183-
'Did you intent to do one of the followings?',
184-
'',
185-
'import { green } from "@material-ui/core/colors";',
186-
'',
187-
'const theme1 = createMuiTheme({ palette: {',
188-
' primary: green,',
189-
'} });',
190-
'',
191-
'const theme2 = createMuiTheme({ palette: {',
192-
' primary: { main: green[500] },',
193-
'} });',
194-
].join('\n'),
195-
);
196-
}
197-
color.main = indigo[500];
172+
throw new Error(
173+
[
174+
'Material-UI: the color provided to augmentColor(color) is invalid.',
175+
`\`color.main\` should be a string, but \`${JSON.stringify(
176+
color.main,
177+
)}\` was provided instead.`,
178+
'',
179+
'Did you intend to use one of the following approaches?',
180+
'',
181+
'import { green } from "@material-ui/core/colors";',
182+
'',
183+
'const theme1 = createMuiTheme({ palette: {',
184+
' primary: green,',
185+
'} });',
186+
'',
187+
'const theme2 = createMuiTheme({ palette: {',
188+
' primary: { main: green[500] },',
189+
'} });',
190+
].join('\n'),
191+
);
198192
}
199193

200194
addLightOrDark(color, 'light', lightShade, tonalOffset);

packages/material-ui/src/styles/createPalette.test.js

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,6 @@ describe('createPalette()', () => {
112112
});
113113
});
114114

115-
describe('getContrastText', () => {
116-
it('throws an exception with a falsy argument', () => {
117-
const { getContrastText } = createPalette({});
118-
119-
[
120-
[undefined, 'missing background argument in getContrastText(undefined)'],
121-
[null, 'missing background argument in getContrastText(null)'],
122-
['', 'missing background argument in getContrastText()'],
123-
[0, 'missing background argument in getContrastText(0)'],
124-
].forEach((testEntry) => {
125-
const [argument, errorMessage] = testEntry;
126-
127-
expect(() => getContrastText(argument), errorMessage).to.throw();
128-
});
129-
});
130-
});
131-
132115
it('should create a palette with unique object references', () => {
133116
const redPalette = createPalette({ background: { paper: 'red' } });
134117
const bluePalette = createPalette({ background: { paper: 'blue' } });
@@ -145,24 +128,19 @@ describe('createPalette()', () => {
145128
consoleErrorMock.reset();
146129
});
147130

148-
it('logs an error when an invalid type is specified', () => {
131+
it('throws an exception when an invalid type is specified', () => {
149132
createPalette({ type: 'foo' });
150133
expect(consoleErrorMock.callCount()).to.equal(1);
151134
expect(consoleErrorMock.messages()[0]).to.include(
152135
'Material-UI: the palette type `foo` is not supported',
153136
);
154137
});
155138

156-
it('logs an error when a wrong color is provided', () => {
157-
createPalette({ primary: '#fff' });
158-
expect(consoleErrorMock.callCount()).to.equal(1);
159-
expect(consoleErrorMock.messages()[0]).to.include(
139+
it('throws an exception when a wrong color is provided', () => {
140+
expect(() => createPalette({ primary: '#fff' })).to.throw(
160141
'The color object needs to have a `main` property or a `500` property.',
161142
);
162-
163-
createPalette({ primary: { main: { foo: 'bar' }} });
164-
expect(consoleErrorMock.callCount()).to.equal(2);
165-
expect(consoleErrorMock.messages()[1]).to.include(
143+
expect(() => createPalette({ primary: { main: { foo: 'bar' } } })).to.throw(
166144
'`color.main` should be a string, but `{"foo":"bar"}` was provided instead.',
167145
);
168146
});

0 commit comments

Comments
 (0)