|
| 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