Skip to content

Commit

Permalink
[TSVB] Visualization blows up when invalid color is passed (elastic#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra and elasticmachine authored Aug 19, 2024
1 parent d1c4bb3 commit f8e873f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,76 @@

import React from 'react';
import { ColorPicker, ColorPickerProps } from './color_picker';
import { mount } from 'enzyme';
import { ReactWrapper } from 'enzyme';
import { EuiColorPicker, EuiIconTip } from '@elastic/eui';
import { findTestSubject } from '@elastic/eui/lib/test';
import { fireEvent, render, screen } from '@testing-library/react';

describe('ColorPicker', () => {
const onChange = jest.fn();
const defaultProps: ColorPickerProps = {
name: 'color',
value: null,
onChange: jest.fn(),
onChange,
disableTrash: true,
};
let component: ReactWrapper<ColorPickerProps>;

const renderColorPicker = (props?: Partial<ColorPickerProps>) =>
render(<ColorPicker {...defaultProps} {...props} />);

afterEach(() => {
jest.clearAllMocks();
});

it('should render the EuiColorPicker', () => {
component = mount(<ColorPicker {...defaultProps} />);
expect(component.find(EuiColorPicker).length).toBe(1);
renderColorPicker();
expect(screen.getByTestId('tvbColorPicker')).toBeInTheDocument();
});

it('should not render the clear button', () => {
component = mount(<ColorPicker {...defaultProps} />);
expect(findTestSubject(component, 'tvbColorPickerClear').length).toBe(0);
renderColorPicker();
expect(screen.queryByTestId('tvbColorPickerClear')).toBeNull();
});

it('should render the correct value to the input text if the prop value is hex', () => {
const props = { ...defaultProps, value: '#68BC00' };
component = mount(<ColorPicker {...props} />);
findTestSubject(component, 'tvbColorPicker').find('button').simulate('click');
const input = findTestSubject(component, 'euiColorPickerInput_top');
expect(input.props().value).toBe('#68BC00');
it('should render incorrect value to the input text but not call onChange prop', () => {
renderColorPicker({ value: '#68BC00' });
fireEvent.click(screen.getByRole('button'));
fireEvent.change(screen.getAllByTestId('euiColorPickerInput_top')[0], {
target: { value: 'INVALID' },
});
expect(onChange).not.toHaveBeenCalled();
expect(screen.getAllByTestId('euiColorPickerInput_top')[0]).toHaveValue('INVALID');
});

it('should render the correct value to the input text if the prop value is rgba', () => {
const props = { ...defaultProps, value: 'rgba(85,66,177,1)' };
component = mount(<ColorPicker {...props} />);
findTestSubject(component, 'tvbColorPicker').find('button').simulate('click');
const input = findTestSubject(component, 'euiColorPickerInput_top');
expect(input.props().value).toBe('85,66,177,1');
it('should render correct value to the input text and call onChange prop', () => {
renderColorPicker({ value: '#68BC00' });
fireEvent.click(screen.getByRole('button'));
fireEvent.change(screen.getAllByTestId('euiColorPickerInput_top')[0], {
target: { value: '#FFF' },
});
expect(onChange).toHaveBeenCalled();
expect(screen.getAllByTestId('euiColorPickerInput_top')[0]).toHaveValue('#FFF');
});

it('should render the correct aria label to the color swatch button', () => {
const props = { ...defaultProps, value: 'rgba(85,66,177,0.59)' };
component = mount(<ColorPicker {...props} />);
const button = findTestSubject(component, 'tvbColorPicker').find('button');
expect(button.prop('aria-label')).toBe('Color picker (rgba(85,66,177,0.59)), not accessible');
renderColorPicker({ value: 'rgba(85,66,177,0.59)' });
expect(
screen.getByLabelText('Color picker (rgba(85,66,177,0.59)), not accessible')
).toBeInTheDocument();
});

it('should call clear function if the disableTrash prop is false', () => {
const props = { ...defaultProps, disableTrash: false, value: 'rgba(85,66,177,1)' };
component = mount(<ColorPicker {...props} />);
const { container } = renderColorPicker({ disableTrash: false, value: 'rgba(85,66,177,1)' });
fireEvent.click(screen.getByTestId('tvbColorPickerClear'));
expect(onChange).toHaveBeenCalled();
expect(container.querySelector('[data-euiicon-type="cross"]')).toBeInTheDocument();
});

findTestSubject(component, 'tvbColorPickerClear').simulate('click');
it('should render the correct value to the input text if the prop value is hex', () => {
renderColorPicker({ value: '#68BC00' });
fireEvent.click(screen.getByRole('button'));
expect(screen.getAllByTestId('euiColorPickerInput_top')[0]).toHaveValue('#68BC00');
});

expect(component.find(EuiIconTip).length).toBe(1);
expect(defaultProps.onChange).toHaveBeenCalled();
it('should render the correct value to the input text if the prop value is rgba', () => {
renderColorPicker({ value: 'rgba(85,66,177,1)' });
fireEvent.click(screen.getByRole('button'));
expect(screen.getAllByTestId('euiColorPickerInput_top')[0]).toHaveValue('85,66,177,1');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ export function ColorPicker({ name, value, disableTrash = false, onChange }: Col

const { euiTheme } = useEuiTheme();

const handleColorChange: EuiColorPickerProps['onChange'] = (text: string, { rgba, hex }) => {
const handleColorChange: EuiColorPickerProps['onChange'] = (
text: string,
{ rgba, hex, isValid }
) => {
setColor(text);
if (!isValid) {
return;
}
onChange({ [name]: hex ? `rgba(${rgba.join(',')})` : '' });
};

Expand Down
4 changes: 1 addition & 3 deletions test/functional/apps/visualize/group5/_tsvb_time_series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
expect(actualCountMin).to.be('3 hours');
});

// FLAKY: https://github.com/elastic/kibana/issues/182136
describe.skip('Dark mode', () => {
describe('Dark mode', () => {
before(async () => {
await kibanaServer.uiSettings.update({
'theme:darkMode': true,
Expand All @@ -156,7 +155,6 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
it(`viz should have light class when background color is white`, async () => {
await visualBuilder.clickPanelOptions('timeSeries');
await visualBuilder.setBackgroundColor('#FFFFFF');

await retry.try(async () => {
expect(await visualBuilder.checkTimeSeriesIsLight()).to.be(true);
});
Expand Down

0 comments on commit f8e873f

Please sign in to comment.