mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Implemented tests for ColorPickerPopover and NamedColorsPalette
This commit is contained in:
@@ -0,0 +1,72 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { mount, ReactWrapper } from 'enzyme';
|
||||||
|
import { ColorPickerPopover } from './ColorPickerPopover';
|
||||||
|
import { ColorsPalette, BasicGreen, BasicBlue } from '../../utils/namedColorsPalette';
|
||||||
|
import { ColorSwatch } from './NamedColorsGroup';
|
||||||
|
import { flatten } from 'lodash';
|
||||||
|
import { GrafanaTheme } from '../../types';
|
||||||
|
|
||||||
|
const allColors = flatten(Array.from(ColorsPalette.values()));
|
||||||
|
|
||||||
|
describe('ColorPickerPopover', () => {
|
||||||
|
describe('rendering', () => {
|
||||||
|
it('should render provided color as selected if color provided by name', () => {
|
||||||
|
const wrapper = mount(<ColorPickerPopover color={BasicGreen.name} onChange={() => {}} />);
|
||||||
|
const selectedSwatch = wrapper.find(ColorSwatch).findWhere(node => node.key() === BasicGreen.name);
|
||||||
|
const notSelectedSwatches = wrapper.find(ColorSwatch).filterWhere(node => node.prop('isSelected') === false);
|
||||||
|
|
||||||
|
expect(selectedSwatch.length).toBe(1);
|
||||||
|
expect(notSelectedSwatches.length).toBe(allColors.length - 1);
|
||||||
|
expect(selectedSwatch.prop('isSelected')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should render provided color as selected if color provided by hex', () => {
|
||||||
|
const wrapper = mount(<ColorPickerPopover color={BasicGreen.variants.dark} onChange={() => {}} />);
|
||||||
|
const selectedSwatch = wrapper.find(ColorSwatch).findWhere(node => node.key() === BasicGreen.name);
|
||||||
|
const notSelectedSwatches = wrapper.find(ColorSwatch).filterWhere(node => node.prop('isSelected') === false);
|
||||||
|
|
||||||
|
expect(selectedSwatch.length).toBe(1);
|
||||||
|
expect(notSelectedSwatches.length).toBe(allColors.length - 1);
|
||||||
|
expect(selectedSwatch.prop('isSelected')).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('named colors support', () => {
|
||||||
|
const onChangeSpy = jest.fn();
|
||||||
|
let wrapper: ReactWrapper;
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
wrapper.unmount();
|
||||||
|
onChangeSpy.mockClear();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should pass hex color value to onChange prop by default', () => {
|
||||||
|
wrapper = mount(
|
||||||
|
<ColorPickerPopover color={BasicGreen.variants.dark} onChange={onChangeSpy} theme={GrafanaTheme.Light} />
|
||||||
|
);
|
||||||
|
const basicBlueSwatch = wrapper.find(ColorSwatch).findWhere(node => node.key() === BasicBlue.name);
|
||||||
|
|
||||||
|
basicBlueSwatch.simulate('click');
|
||||||
|
|
||||||
|
expect(onChangeSpy).toBeCalledTimes(1);
|
||||||
|
expect(onChangeSpy).toBeCalledWith(BasicBlue.variants.light);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should pass color name to onChange prop when named colors enabled', () => {
|
||||||
|
wrapper = mount(
|
||||||
|
<ColorPickerPopover
|
||||||
|
enableNamedColors
|
||||||
|
color={BasicGreen.variants.dark}
|
||||||
|
onChange={onChangeSpy}
|
||||||
|
theme={GrafanaTheme.Light}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
const basicBlueSwatch = wrapper.find(ColorSwatch).findWhere(node => node.key() === BasicBlue.name);
|
||||||
|
|
||||||
|
basicBlueSwatch.simulate('click');
|
||||||
|
|
||||||
|
expect(onChangeSpy).toBeCalledTimes(1);
|
||||||
|
expect(onChangeSpy).toBeCalledWith(BasicBlue.name);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -18,7 +18,7 @@ interface ColorSwatchProps extends Themeable, React.DOMAttributes<HTMLDivElement
|
|||||||
isSelected?: boolean;
|
isSelected?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ColorSwatch: FunctionComponent<ColorSwatchProps> = ({
|
export const ColorSwatch: FunctionComponent<ColorSwatchProps> = ({
|
||||||
color,
|
color,
|
||||||
label,
|
label,
|
||||||
variant = ColorSwatchVariant.Small,
|
variant = ColorSwatchVariant.Small,
|
||||||
@@ -73,6 +73,7 @@ const NamedColorsGroup: FunctionComponent<NamedColorsGroupProps> = ({
|
|||||||
<div {...otherProps} style={{ display: 'flex', flexDirection: 'column' }}>
|
<div {...otherProps} style={{ display: 'flex', flexDirection: 'column' }}>
|
||||||
{primaryColor && (
|
{primaryColor && (
|
||||||
<ColorSwatch
|
<ColorSwatch
|
||||||
|
key={primaryColor.name}
|
||||||
isSelected={primaryColor.name === selectedColor}
|
isSelected={primaryColor.name === selectedColor}
|
||||||
variant={ColorSwatchVariant.Large}
|
variant={ColorSwatchVariant.Large}
|
||||||
color={getColorForTheme(primaryColor, theme)}
|
color={getColorForTheme(primaryColor, theme)}
|
||||||
@@ -92,6 +93,7 @@ const NamedColorsGroup: FunctionComponent<NamedColorsGroupProps> = ({
|
|||||||
!color.isPrimary && (
|
!color.isPrimary && (
|
||||||
<div key={color.name} style={{ marginRight: '4px' }}>
|
<div key={color.name} style={{ marginRight: '4px' }}>
|
||||||
<ColorSwatch
|
<ColorSwatch
|
||||||
|
key={color.name}
|
||||||
isSelected={color.name === selectedColor}
|
isSelected={color.name === selectedColor}
|
||||||
color={getColorForTheme(color, theme)}
|
color={getColorForTheme(color, theme)}
|
||||||
onClick={() => onColorSelect(color)}
|
onClick={() => onColorSelect(color)}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { mount, ReactWrapper } from 'enzyme';
|
||||||
|
import { NamedColorsPalette } from './NamedColorsPalette';
|
||||||
|
import { ColorSwatch } from './NamedColorsGroup';
|
||||||
|
import { BasicGreen } from '../../utils';
|
||||||
|
import { GrafanaTheme } from '../../types';
|
||||||
|
|
||||||
|
describe('NamedColorsPalette', () => {
|
||||||
|
describe('theme support for named colors', () => {
|
||||||
|
let wrapper: ReactWrapper, selectedSwatch;
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should render provided color variant specific for theme', () => {
|
||||||
|
wrapper = mount(<NamedColorsPalette color={BasicGreen.name} theme={GrafanaTheme.Dark} onChange={() => {}} />);
|
||||||
|
selectedSwatch = wrapper.find(ColorSwatch).findWhere(node => node.key() === BasicGreen.name);
|
||||||
|
expect(selectedSwatch.prop('color')).toBe(BasicGreen.variants.dark);
|
||||||
|
|
||||||
|
wrapper.unmount();
|
||||||
|
wrapper = mount(<NamedColorsPalette color={BasicGreen.name} theme={GrafanaTheme.Light} onChange={() => {}} />);
|
||||||
|
selectedSwatch = wrapper.find(ColorSwatch).findWhere(node => node.key() === BasicGreen.name);
|
||||||
|
expect(selectedSwatch.prop('color')).toBe(BasicGreen.variants.light);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should render dar variant of provided color when theme not provided', () => {
|
||||||
|
wrapper = mount(<NamedColorsPalette color={BasicGreen.name} onChange={() => {}} />);
|
||||||
|
selectedSwatch = wrapper.find(ColorSwatch).findWhere(node => node.key() === BasicGreen.name);
|
||||||
|
expect(selectedSwatch.prop('color')).toBe(BasicGreen.variants.dark);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user