Skip to content

Commit f2a927d

Browse files
committed
test: adds tests for color widget
1 parent d90cf36 commit f2a927d

3 files changed

Lines changed: 147 additions & 3 deletions

File tree

packages/netlify-cms-widget-colorstring/src/ColorControl.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import ImmutablePropTypes from 'react-immutable-proptypes';
34
import styled from '@emotion/styled';
45
import ChromePicker from 'react-color';
56
import validateColor from 'validate-color';
@@ -79,6 +80,7 @@ const ClickOutsideDiv = styled.div`
7980

8081
export default class ColorControl extends React.Component {
8182
static propTypes = {
83+
field: ImmutablePropTypes.map.isRequired,
8284
onChange: PropTypes.func.isRequired,
8385
forID: PropTypes.string,
8486
value: PropTypes.node,
@@ -140,7 +142,7 @@ export default class ColorControl extends React.Component {
140142
<>
141143
{' '}
142144
{showClearButton && (
143-
<ClearButtonWrapper>
145+
<ClearButtonWrapper data-testid="clear-btn-wrapper">
144146
<ClearButton onClick={this.handleClear}>
145147
<ClearIcon />
146148
</ClearButton>
@@ -155,8 +157,8 @@ export default class ColorControl extends React.Component {
155157
?
156158
</ColorSwatch>
157159
{this.state.showColorPicker && (
158-
<ColorPickerContainer>
159-
<ClickOutsideDiv onClick={this.handleClose} />
160+
<ColorPickerContainer data-testid="color-picker-container">
161+
<ClickOutsideDiv onClick={this.handleClose} data-testid="picker-bg" />
160162
<ChromePicker
161163
color={value || ''}
162164
onChange={this.handleChange}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Color widget field.allowInput is false renders input as readonly 1`] = `
4+
<input
5+
class="test-classname"
6+
id="test-string"
7+
readonly=""
8+
style="padding-left: 75px; padding-right: 70px; color: rgb(187, 187, 187);"
9+
type="text"
10+
value=""
11+
/>
12+
`;
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import React from 'react';
2+
import { fromJS } from 'immutable';
3+
import { render, fireEvent } from '@testing-library/react';
4+
5+
import { NetlifyCmsWidgetColorString } from '../';
6+
7+
const ColorControl = NetlifyCmsWidgetColorString.controlComponent;
8+
9+
const fieldSettings = {
10+
allowInput: false,
11+
};
12+
13+
function setup({ field, defaultValue } = {}) {
14+
const setActiveSpy = jest.fn();
15+
const setInactiveSpy = jest.fn();
16+
const onChangeSpy = jest.fn();
17+
18+
const helpers = render(
19+
<ColorControl
20+
field={field}
21+
value={defaultValue}
22+
onChange={onChangeSpy}
23+
forID="test-string"
24+
classNameWrapper="test-classname"
25+
setActiveStyle={setActiveSpy}
26+
setInactiveStyle={setInactiveSpy}
27+
/>,
28+
);
29+
30+
const input = helpers.container.querySelector('input');
31+
32+
jest.useFakeTimers();
33+
34+
return {
35+
...helpers,
36+
setActiveSpy,
37+
setInactiveSpy,
38+
onChangeSpy,
39+
input,
40+
};
41+
}
42+
43+
describe('Color widget', () => {
44+
it('renders with default value', () => {
45+
const field = fromJS(fieldSettings);
46+
const testValue = '#fff000';
47+
const { input } = setup({ field, defaultValue: testValue });
48+
49+
expect(input.value).toEqual(testValue);
50+
});
51+
52+
describe('field.allowInput is false', () => {
53+
it('renders input as readonly', () => {
54+
const field = fromJS(fieldSettings);
55+
const { input } = setup({ field });
56+
57+
expect(input).toMatchSnapshot();
58+
});
59+
60+
it('opens picker on input click', () => {
61+
const field = fromJS(fieldSettings);
62+
const { input, queryByTestId } = setup({ field });
63+
64+
fireEvent.click(input);
65+
66+
expect(queryByTestId('color-picker-container')).not.toBeNull();
67+
});
68+
69+
it('displays clear button when input is present', () => {
70+
const field = fromJS(fieldSettings);
71+
const { queryByTestId } = setup({ field, defaultValue: '#fff000' });
72+
73+
expect(queryByTestId('clear-btn-wrapper')).not.toBeNull();
74+
});
75+
});
76+
77+
describe('field.allowInput is true', () => {
78+
const field = fromJS({ ...fieldSettings, allowInput: true });
79+
80+
it('calls onChange when input changes', () => {
81+
const testValue = '#fff000';
82+
const { input, onChangeSpy } = setup({ field });
83+
84+
fireEvent.focus(input);
85+
fireEvent.change(input, { target: { value: testValue } });
86+
87+
jest.runAllTimers();
88+
89+
expect(onChangeSpy).toHaveBeenCalledTimes(1);
90+
expect(onChangeSpy).toHaveBeenCalledWith(testValue);
91+
});
92+
93+
it('sets input value', () => {
94+
const testValue = '#fff000';
95+
const { input } = setup({ field });
96+
97+
fireEvent.focus(input);
98+
fireEvent.change(input, { target: { value: testValue } });
99+
100+
jest.runAllTimers();
101+
102+
expect(input.value).toEqual(testValue);
103+
});
104+
105+
it('does not open picker on input click', () => {
106+
const { input, queryByTestId } = setup({ field });
107+
108+
fireEvent.click(input);
109+
110+
expect(queryByTestId('color-picker-container')).toBeNull();
111+
});
112+
113+
it('calls setActiveStyle when input focused', () => {
114+
const { input, setActiveSpy } = setup({ field });
115+
116+
fireEvent.focus(input);
117+
118+
expect(setActiveSpy).toHaveBeenCalledTimes(1);
119+
});
120+
121+
it('calls setInactiveSpy when input blurred', () => {
122+
const { input, setInactiveSpy } = setup({ field });
123+
124+
fireEvent.focus(input);
125+
fireEvent.blur(input);
126+
127+
expect(setInactiveSpy).toHaveBeenCalledTimes(1);
128+
});
129+
});
130+
});

0 commit comments

Comments
 (0)