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

Fix inline docs and add tests for color utils #15861

Merged
merged 2 commits into from Jun 4, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/block-editor/README.md
Expand Up @@ -208,7 +208,7 @@ _Parameters_

_Returns_

- `string`: String with the class corresponding to the color in the provided context.
- `?string`: String with the class corresponding to the color in the provided context. Returns undefined if either colorContextName or colorSlug are not provided.

<a name="getColorObjectByAttributeValues" href="#getColorObjectByAttributeValues">#</a> **getColorObjectByAttributeValues**

Expand All @@ -223,7 +223,7 @@ _Parameters_

_Returns_

- `?string`: If definedColor is passed and the name is found in colors, the color object exactly as set by the theme or editor defaults is returned. Otherwise, an object that just sets the color is defined.
- `?Object`: If definedColor is passed and the name is found in colors, the color object exactly as set by the theme or editor defaults is returned. Otherwise, an object that just sets the color is defined.

<a name="getColorObjectByColorValue" href="#getColorObjectByColorValue">#</a> **getColorObjectByColorValue**

Expand All @@ -236,7 +236,7 @@ _Parameters_

_Returns_

- `?string`: Returns the color object included in the colors array whose color property equals colorValue. Returns undefined if no color object matches this requirement.
- `?Object`: Color object included in the colors array whose color property equals colorValue. Returns undefined if no color object matches this requirement.

<a name="getFontSize" href="#getFontSize">#</a> **getFontSize**

Expand Down
83 changes: 83 additions & 0 deletions packages/block-editor/src/components/colors/test/utils.js
@@ -0,0 +1,83 @@
/**
* Internal dependencies
*/
import {
getColorObjectByAttributeValues,
getColorObjectByColorValue,
getColorClassName,
} from '../utils';

describe( 'color utils', () => {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
describe( 'getColorObjectByAttributeValues', () => {
it( 'should return the custom color object when there is no definedColor', () => {
const colors = [
{ slug: 'red' },
{ slug: 'green' },
{ slug: 'blue' },
];
const customColor = '#ffffff';

expect( getColorObjectByAttributeValues( colors, undefined, customColor ) ).toEqual( { color: customColor } );
} );

it( 'should return the custom color object when definedColor was not found', () => {
const colors = [
{ slug: 'red' },
{ slug: 'green' },
{ slug: 'blue' },
];
const definedColor = 'purple';
const customColor = '#ffffff';

expect( getColorObjectByAttributeValues( colors, definedColor, customColor ) ).toEqual( { color: customColor } );
} );

it( 'should return the found color object', () => {
const colors = [
{ slug: 'red' },
{ slug: 'green' },
{ slug: 'blue' },
];
const definedColor = 'blue';
const customColor = '#ffffff';

expect( getColorObjectByAttributeValues( colors, definedColor, customColor ) ).toEqual( { slug: 'blue' } );
} );
} );

describe( 'getColorObjectByColorValue', () => {
it( 'should return undefined if the given color was not found', () => {
const colors = [
{ slug: 'red', color: '#ff0000' },
{ slug: 'green', color: '#00ff00' },
{ slug: 'blue', color: '#0000ff' },
];

expect( getColorObjectByColorValue( colors, '#ffffff' ) ).toBeUndefined();
} );

it( 'should return a color object for the given color value', () => {
const colors = [
{ slug: 'red', color: '#ff0000' },
{ slug: 'green', color: '#00ff00' },
{ slug: 'blue', color: '#0000ff' },
];

expect( getColorObjectByColorValue( colors, '#00ff00' ) ).toEqual( { slug: 'green', color: '#00ff00' } );
} );
} );

describe( 'getColorClassName', () => {
it( 'should return undefined if colorContextName is missing', () => {
expect( getColorClassName( undefined, 'Light Purple' ) ).toBeUndefined();
} );

it( 'should return undefined if colorSlug is missing', () => {
expect( getColorClassName( 'background', undefined ) ).toBeUndefined();
} );

it( 'should return a class name with the color slug in kebab case', () => {
expect( getColorClassName( 'background', 'Light Purple' ) ).toBe( 'has-light-purple-background' );
} );
} );
} );
9 changes: 5 additions & 4 deletions packages/block-editor/src/components/colors/utils.js
Expand Up @@ -12,7 +12,7 @@ import tinycolor from 'tinycolor2';
* @param {?string} definedColor A string containing the color slug.
* @param {?string} customColor A string containing the customColor value.
*
* @return {?string} If definedColor is passed and the name is found in colors,
* @return {?Object} If definedColor is passed and the name is found in colors,
* the color object exactly as set by the theme or editor defaults is returned.
* Otherwise, an object that just sets the color is defined.
*/
Expand All @@ -35,7 +35,7 @@ export const getColorObjectByAttributeValues = ( colors, definedColor, customCol
* @param {Array} colors Array of color objects as set by the theme or by the editor defaults.
* @param {?string} colorValue A string containing the color value.
*
* @return {?string} Returns the color object included in the colors array whose color property equals colorValue.
* @return {?Object} Color object included in the colors array whose color property equals colorValue.
* Returns undefined if no color object matches this requirement.
*/
export const getColorObjectByColorValue = ( colors, colorValue ) => {
Expand All @@ -48,11 +48,12 @@ export const getColorObjectByColorValue = ( colors, colorValue ) => {
* @param {string} colorContextName Context/place where color is being used e.g: background, text etc...
* @param {string} colorSlug Slug of the color.
*
* @return {string} String with the class corresponding to the color in the provided context.
* @return {?string} String with the class corresponding to the color in the provided context.
* Returns undefined if either colorContextName or colorSlug are not provided.
*/
export function getColorClassName( colorContextName, colorSlug ) {
if ( ! colorContextName || ! colorSlug ) {
return;
return undefined;
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
}

return `has-${ kebabCase( colorSlug ) }-${ colorContextName }`;
Expand Down