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

[color] Add a warning when an invalid value is provided #9783

Merged
merged 1 commit into from
Jan 8, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/styles/colorManipulator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @flow weak
/* eslint-disable no-use-before-define */

import warning from 'warning';

/**
* Returns a number whose value is limited to the given range.
*
Expand All @@ -10,6 +12,11 @@
* @returns {number} A number in the range [min, max]
*/
function clamp(value, min, max) {
warning(
value >= min && value <= max,
`Material-UI: the value provided ${value} is out of range [${min}, ${max}].`,
);

if (value < min) {
return min;
}
Expand Down
13 changes: 13 additions & 0 deletions src/styles/colorManipulator.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from 'chai';
import consoleErrorMock from '../../test/utils/consoleErrorMock';
import {
convertColorToString,
convertHexToRGB,
Expand All @@ -12,6 +13,14 @@ import {
} from './colorManipulator';

describe('utils/colorManipulator', () => {
beforeEach(() => {
consoleErrorMock.spy();
});

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

describe('convertColorToString', () => {
it('converts a decomposed rgb color object to a string` ', () => {
assert.strictEqual(
Expand Down Expand Up @@ -183,10 +192,12 @@ describe('utils/colorManipulator', () => {

it("doesn't overshoot if an above-range coefficient is supplied", () => {
assert.strictEqual(darken('rgb(0, 127, 255)', 1.5), 'rgb(0, 0, 0)');
assert.strictEqual(consoleErrorMock.callCount(), 1);
});

it("doesn't overshoot if a below-range coefficient is supplied", () => {
assert.strictEqual(darken('rgb(0, 127, 255)', -0.1), 'rgb(0, 127, 255)');
assert.strictEqual(consoleErrorMock.callCount(), 1);
});

it('darkens rgb white to black when coefficient is 1', () => {
Expand Down Expand Up @@ -233,10 +244,12 @@ describe('utils/colorManipulator', () => {

it("doesn't overshoot if an above-range coefficient is supplied", () => {
assert.strictEqual(lighten('rgb(0, 127, 255)', 1.5), 'rgb(255, 255, 255)');
assert.strictEqual(consoleErrorMock.callCount(), 1);
});

it("doesn't overshoot if a below-range coefficient is supplied", () => {
assert.strictEqual(lighten('rgb(0, 127, 255)', -0.1), 'rgb(0, 127, 255)');
assert.strictEqual(consoleErrorMock.callCount(), 1);
});

it('lightens rgb black to white when coefficient is 1', () => {
Expand Down