Skip to content

Commit

Permalink
Remove isTemporaryElement function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t-hamano committed Dec 20, 2023
1 parent 54b2e9a commit 7f3524e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 102 deletions.
28 changes: 1 addition & 27 deletions packages/components/src/palette-edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import type {
PaletteElement,
} from './types';

export const DEFAULT_COLOR = '#000';
const DEFAULT_COLOR = '#000';

function NameInput( { value, onChange, label }: NameInputProps ) {
return (
Expand Down Expand Up @@ -261,32 +261,6 @@ function Option< T extends Color | Gradient >( {
);
}

/**
* Checks if a color or gradient is a temporary element by testing against default values.
*/
export function isTemporaryElement(
slugPrefix: string,
{ slug, color, gradient }: Color | Gradient
): Boolean {
const regex = new RegExp( `^${ slugPrefix }color-([\\d]+)$` );

// If the slug matches the temporary name regex,
// check if the color or gradient matches the default value.
if ( regex.test( slug ) ) {
// The order is important as gradient elements
// contain a color property.
if ( !! gradient ) {
return gradient === DEFAULT_GRADIENT;
}

if ( !! color ) {
return color === DEFAULT_COLOR;
}
}

return false;
}

function PaletteEditListView< T extends Color | Gradient >( {
elements,
onChange,
Expand Down
76 changes: 1 addition & 75 deletions packages/components/src/palette-edit/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ import { render, fireEvent, screen } from '@testing-library/react';
/**
* Internal dependencies
*/
import PaletteEdit, {
getNameForPosition,
isTemporaryElement,
DEFAULT_COLOR,
} from '..';
import PaletteEdit, { getNameForPosition } from '..';
import type { PaletteElement } from '../types';
import { DEFAULT_GRADIENT } from '../../custom-gradient-picker/constants';

describe( 'getNameForPosition', () => {
test( 'should return 1 by default', () => {
Expand Down Expand Up @@ -85,75 +80,6 @@ describe( 'getNameForPosition', () => {
} );
} );

describe( 'isTemporaryElement', () => {
[
{
message: 'identifies temporary color',
slug: 'test-',
obj: {
name: '',
slug: 'test-color-1',
color: DEFAULT_COLOR,
},
expected: true,
},
{
message: 'identifies temporary gradient',
slug: 'test-',
obj: {
name: '',
slug: 'test-color-1',
gradient: DEFAULT_GRADIENT,
},
expected: true,
},
{
message: 'identifies custom color slug',
slug: 'test-',
obj: {
name: '',
slug: 'test-color-happy',
color: DEFAULT_COLOR,
},
expected: false,
},
{
message: 'identifies custom color value',
slug: 'test-',
obj: {
name: '',
slug: 'test-color-1',
color: '#ccc',
},
expected: false,
},
{
message: 'identifies custom gradient slug',
slug: 'test-',
obj: {
name: '',
slug: 'test-gradient-joy',
color: DEFAULT_GRADIENT,
},
expected: false,
},
{
message: 'identifies custom gradient value',
slug: 'test-',
obj: {
name: '',
slug: 'test-color-3',
color: 'linear-gradient(90deg, rgba(22, 22, 22, 1) 0%, rgb(155, 81, 224) 100%)',
},
expected: false,
},
].forEach( ( { message, slug, obj, expected } ) => {
it( `should ${ message }`, () => {
expect( isTemporaryElement( slug, obj ) ).toBe( expected );
} );
} );
} );

describe( 'PaletteEdit', () => {
const defaultProps = {
colors: [ { color: '#ffffff', name: 'Base', slug: 'base' } ],
Expand Down

0 comments on commit 7f3524e

Please sign in to comment.