Skip to content

Commit

Permalink
Refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t-hamano committed Feb 6, 2023
1 parent b020ea2 commit e225bf5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 58 deletions.
58 changes: 0 additions & 58 deletions packages/components/src/color-palette/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ const EXAMPLE_COLORS = [
];
const INITIAL_COLOR = EXAMPLE_COLORS[ 0 ].color;

const CSS_VARIABLE_COLORS = [
{ name: 'red', color: 'var(--red)' },
{ name: 'blue', color: 'var(--blue)' },
];

const CSS_VARS_STYLE_PROPS = {
'--red': '#f00',
'--blue': '#00f',
} as React.CSSProperties;

function getWrappingPopoverElement( element: HTMLElement ) {
return element.closest( '.components-popover' );
}
Expand Down Expand Up @@ -211,52 +201,4 @@ describe( 'ColorPalette', () => {
screen.getByRole( 'button', { name: 'Clear' } )
).toBeInTheDocument();
} );

it( 'should show the readable text label even when the background color has a CSS variable.', async () => {
// const user = userEvent.setup();
const onChange = jest.fn();

render(
<div style={ CSS_VARS_STYLE_PROPS }>
<ColorPalette
colors={ CSS_VARIABLE_COLORS }
value={ CSS_VARIABLE_COLORS[ 0 ].color }
onChange={ onChange }
/>
</div>
);

const dropdownButton = screen.getByRole( 'button', {
name: /^Custom color picker/,
expanded: false,
} );

const dropdownButtonStyles = window.getComputedStyle( dropdownButton );
expect( dropdownButtonStyles.color ).toBe( 'rgb(0, 0, 0)' );
} );

it( 'should render dropdown with Hex value transformed from CSS a variable to actual color', async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(
<div style={ CSS_VARS_STYLE_PROPS }>
<ColorPalette
colors={ CSS_VARIABLE_COLORS }
value={ CSS_VARIABLE_COLORS[ 0 ].color }
onChange={ onChange }
/>
</div>
);

const dropdownButton = screen.getByRole( 'button', {
name: /^Custom color picker/,
expanded: false,
} );

await user.click( dropdownButton );

const dropdownColorInput = screen.getByLabelText( 'Hex color' );
expect( dropdownColorInput ).toHaveValue( 'FF0000' );
} );
} );
22 changes: 22 additions & 0 deletions packages/components/src/color-palette/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {
extractColorNameFromCurrentValue,
showTransparentBackground,
normalizeColorValue,
} from '../utils';

describe( 'ColorPalette: Utils', () => {
Expand Down Expand Up @@ -38,4 +39,25 @@ describe( 'ColorPalette: Utils', () => {
expect( showTransparentBackground( '#f5f5f524' ) ).toBe( false ); // 0.14 alpha.
} );
} );

describe( 'normalizeColorValue', () => {
test( 'should return the value as is if the color value is not a CSS variable', () => {
const element = document.createElement( 'div' );
expect( normalizeColorValue( '#ff0000', element ) ).toBe(
'#ff0000'
);
} );
test( 'should return the background color computed from a element if the color value is a CSS variable', () => {
const element = document.createElement( 'div' );
const { ownerDocument } = element;
const { defaultView } = ownerDocument;
// @ts-ignore
defaultView.getComputedStyle = () => ( {
backgroundColor: '#ff0000',
} );
expect( normalizeColorValue( 'var(--red)', element ) ).toBe(
'#ff0000'
);
} );
} );
} );

0 comments on commit e225bf5

Please sign in to comment.