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

[getContrastText] Throw descriptive exception when passing falsy argument #18383

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions packages/material-ui/src/styles/createPalette.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,10 @@ export default function createPalette(palette) {
// Bootstrap: https://github.com/twbs/bootstrap/blob/1d6e3710dd447de1a200f29e8fa521f8a0908f70/scss/_functions.scss#L59
// and material-components-web https://github.com/material-components/material-components-web/blob/ac46b8863c4dab9fc22c4c662dc6bd1b65dd652f/packages/mdc-theme/_functions.scss#L54
function getContrastText(background) {
if (process.env.NODE_ENV !== 'production') {
if (!background) {
console.error(
`Material-UI: missing background argument in getContrastText(${background}).`,
);
}
if (!background) {
throw new TypeError(
`Material-UI: missing background argument in getContrastText(${background}).`,
);
}

const contrastText =
Expand Down
38 changes: 34 additions & 4 deletions packages/material-ui/src/styles/createPalette.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { darken, lighten } from './colorManipulator';
import createPalette, { dark, light } from './createPalette';

describe('createPalette()', () => {
before(() => {
beforeEach(() => {
consoleErrorMock.spy();
});

after(() => {
afterEach(() => {
consoleErrorMock.reset();
});

Expand Down Expand Up @@ -361,15 +361,14 @@ describe('createPalette()', () => {
assert.strictEqual(consoleErrorMock.callCount(), 0);
});

it('should throw an exception when an invalid type is specified', () => {
it('logs an error when an invalid type is specified', () => {
createPalette({ type: 'foo' });
assert.strictEqual(consoleErrorMock.callCount(), 1);
assert.match(
consoleErrorMock.args()[0][0],
/Material-UI: the palette type `foo` is not supported/,
);
});

describe('augmentColor', () => {
const palette = createPalette({});

Expand Down Expand Up @@ -433,6 +432,37 @@ describe('createPalette()', () => {
});
});

describe('getContrastText', () => {
it('throws an exception with a falsy argument', () => {
const { getContrastText } = createPalette({});

[
[undefined, 'missing background argument in getContrastText(undefined)'],
[null, 'missing background argument in getContrastText(null)'],
['', 'missing background argument in getContrastText()'],
[0, 'missing background argument in getContrastText(0)'],
].forEach(testEntry => {
const [argument, errorMessage] = testEntry;

assert.throws(() => getContrastText(argument), errorMessage);
});
});

it('logs an error when the contrast ratio does not reach AA', () => {
const { getContrastText } = createPalette({
contrastThreshold: 0,
});

getContrastText('#fefefe');

assert.strictEqual(consoleErrorMock.callCount(), 1);
assert.include(
consoleErrorMock.args()[0][0],
'falls below the WACG recommended absolute minimum contrast ratio of 3:1',
);
});
});

it('should create a palette with unique object references', () => {
const redPalette = createPalette({ background: { paper: 'red' } });
const bluePalette = createPalette({ background: { paper: 'blue' } });
Expand Down