Skip to content

Commit f0d7731

Browse files
committed
Add test to ensure that the input text of the picker is set up correctly
1 parent eca8143 commit f0d7731

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/plugins/vis_type_timeseries/public/application/components/color_picker.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { ColorPicker, ColorPickerProps } from './color_picker';
2222
import { mount } from 'enzyme';
2323
import { ReactWrapper } from 'enzyme';
2424
import { EuiColorPicker, EuiIconTip } from '@elastic/eui';
25+
// @ts-ignore
26+
import { findTestSubject } from '@elastic/eui/lib/test';
2527

2628
describe('ColorPicker', () => {
2729
const defaultProps: ColorPickerProps = {
@@ -42,6 +44,22 @@ describe('ColorPicker', () => {
4244
expect(component.find('.tvbColorPicker__clear').length).toBe(0);
4345
});
4446

47+
it('should render the correct value to the input text if the prop value is hex', () => {
48+
const props = { ...defaultProps, value: '#68BC00' };
49+
component = mount(<ColorPicker {...props} />);
50+
component.find('.tvbColorPicker button').simulate('click');
51+
const input = findTestSubject(component, 'topColorPickerInput');
52+
expect(input.props().value).toBe('#68BC00');
53+
});
54+
55+
it('should render the correct value to the input text if the prop value is rgba', () => {
56+
const props = { ...defaultProps, value: 'rgba(85,66,177,1)' };
57+
component = mount(<ColorPicker {...props} />);
58+
component.find('.tvbColorPicker button').simulate('click');
59+
const input = findTestSubject(component, 'topColorPickerInput');
60+
expect(input.props().value).toBe('85,66,177,1');
61+
});
62+
4563
it('should render the correct aria label to the color swatch button', () => {
4664
const props = { ...defaultProps, value: 'rgba(85,66,177,0.59)' };
4765
component = mount(<ColorPicker {...props} />);

src/plugins/vis_type_timeseries/public/application/components/color_picker.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function ColorPicker({ name, value, disableTrash = false, onChange }: Col
4646
const initialColorValue = value?.includes('rgba')
4747
? value.replace(COMMAS_NUMS_ONLY_RE, '')
4848
: value;
49-
const [color, setColor] = useState(initialColorValue);
49+
const [color, setColor] = useState(initialColorValue || '');
5050

5151
const handleColorChange: EuiColorPickerProps['onChange'] = (text: string, { rgba, hex }) => {
5252
setColor(text);

0 commit comments

Comments
 (0)