Skip to content

Commit edf5356

Browse files
committed
add tests
1 parent dd10464 commit edf5356

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from 'react';
2+
3+
import { renderWithTheme } from '../../../test/utils';
4+
import ColorInput from './ColorInput';
5+
6+
function rgb2hex(str) {
7+
const rgb = str.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
8+
function hex(x) {
9+
return `0${parseInt(x, 10).toString(16)}`.slice(-2);
10+
}
11+
return `#${hex(rgb[1])}${hex(rgb[2])}${hex(rgb[3])}`;
12+
}
13+
14+
describe('<ColorInput />', () => {
15+
// TODO
16+
it('should call handlers', () => {});
17+
18+
it('should properly pass value to input element', () => {
19+
const color = '#f0f0dd';
20+
const { container } = renderWithTheme(<ColorInput value={color} />);
21+
const input = container.querySelector(`[type="color"]`);
22+
23+
expect(input.value).toBe(color);
24+
});
25+
it('should display current color', () => {
26+
const color = '#f0f0dd';
27+
const { getByRole } = renderWithTheme(<ColorInput value={color} />);
28+
const colorPreview = getByRole('presentation');
29+
const displayedColor = window.getComputedStyle(colorPreview, null)
30+
.background;
31+
32+
const displayedColorHex = rgb2hex(displayedColor);
33+
expect(displayedColorHex).toBe(color);
34+
});
35+
36+
describe('prop: disabled', () => {
37+
it('should render disabled input', () => {
38+
const { container } = renderWithTheme(<ColorInput disabled />);
39+
const input = container.querySelector(`[type="color"]`);
40+
41+
expect(input).toHaveAttribute('disabled');
42+
});
43+
44+
it('should be overridden by props', () => {
45+
const { container, rerender } = renderWithTheme(<ColorInput disabled />);
46+
rerender(<ColorInput disabled={false} />);
47+
const input = container.querySelector(`[type="color"]`);
48+
49+
expect(input).not.toHaveAttribute('disabled');
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)