Skip to content

Commit e225bf5

Browse files
committed
Refactor unit tests
1 parent b020ea2 commit e225bf5

File tree

2 files changed

+22
-58
lines changed

2 files changed

+22
-58
lines changed

packages/components/src/color-palette/test/index.tsx

-58
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ const EXAMPLE_COLORS = [
1616
];
1717
const INITIAL_COLOR = EXAMPLE_COLORS[ 0 ].color;
1818

19-
const CSS_VARIABLE_COLORS = [
20-
{ name: 'red', color: 'var(--red)' },
21-
{ name: 'blue', color: 'var(--blue)' },
22-
];
23-
24-
const CSS_VARS_STYLE_PROPS = {
25-
'--red': '#f00',
26-
'--blue': '#00f',
27-
} as React.CSSProperties;
28-
2919
function getWrappingPopoverElement( element: HTMLElement ) {
3020
return element.closest( '.components-popover' );
3121
}
@@ -211,52 +201,4 @@ describe( 'ColorPalette', () => {
211201
screen.getByRole( 'button', { name: 'Clear' } )
212202
).toBeInTheDocument();
213203
} );
214-
215-
it( 'should show the readable text label even when the background color has a CSS variable.', async () => {
216-
// const user = userEvent.setup();
217-
const onChange = jest.fn();
218-
219-
render(
220-
<div style={ CSS_VARS_STYLE_PROPS }>
221-
<ColorPalette
222-
colors={ CSS_VARIABLE_COLORS }
223-
value={ CSS_VARIABLE_COLORS[ 0 ].color }
224-
onChange={ onChange }
225-
/>
226-
</div>
227-
);
228-
229-
const dropdownButton = screen.getByRole( 'button', {
230-
name: /^Custom color picker/,
231-
expanded: false,
232-
} );
233-
234-
const dropdownButtonStyles = window.getComputedStyle( dropdownButton );
235-
expect( dropdownButtonStyles.color ).toBe( 'rgb(0, 0, 0)' );
236-
} );
237-
238-
it( 'should render dropdown with Hex value transformed from CSS a variable to actual color', async () => {
239-
const user = userEvent.setup();
240-
const onChange = jest.fn();
241-
242-
render(
243-
<div style={ CSS_VARS_STYLE_PROPS }>
244-
<ColorPalette
245-
colors={ CSS_VARIABLE_COLORS }
246-
value={ CSS_VARIABLE_COLORS[ 0 ].color }
247-
onChange={ onChange }
248-
/>
249-
</div>
250-
);
251-
252-
const dropdownButton = screen.getByRole( 'button', {
253-
name: /^Custom color picker/,
254-
expanded: false,
255-
} );
256-
257-
await user.click( dropdownButton );
258-
259-
const dropdownColorInput = screen.getByLabelText( 'Hex color' );
260-
expect( dropdownColorInput ).toHaveValue( 'FF0000' );
261-
} );
262204
} );

packages/components/src/color-palette/test/utils.ts

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import {
55
extractColorNameFromCurrentValue,
66
showTransparentBackground,
7+
normalizeColorValue,
78
} from '../utils';
89

910
describe( 'ColorPalette: Utils', () => {
@@ -38,4 +39,25 @@ describe( 'ColorPalette: Utils', () => {
3839
expect( showTransparentBackground( '#f5f5f524' ) ).toBe( false ); // 0.14 alpha.
3940
} );
4041
} );
42+
43+
describe( 'normalizeColorValue', () => {
44+
test( 'should return the value as is if the color value is not a CSS variable', () => {
45+
const element = document.createElement( 'div' );
46+
expect( normalizeColorValue( '#ff0000', element ) ).toBe(
47+
'#ff0000'
48+
);
49+
} );
50+
test( 'should return the background color computed from a element if the color value is a CSS variable', () => {
51+
const element = document.createElement( 'div' );
52+
const { ownerDocument } = element;
53+
const { defaultView } = ownerDocument;
54+
// @ts-ignore
55+
defaultView.getComputedStyle = () => ( {
56+
backgroundColor: '#ff0000',
57+
} );
58+
expect( normalizeColorValue( 'var(--red)', element ) ).toBe(
59+
'#ff0000'
60+
);
61+
} );
62+
} );
4163
} );

0 commit comments

Comments
 (0)