|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react'; |
| 3 | + |
| 4 | +import { NetlifyCmsWidgetString } from '../'; |
| 5 | + |
| 6 | +const StringControl = NetlifyCmsWidgetString.controlComponent; |
| 7 | + |
| 8 | +class StringController extends React.Component { |
| 9 | + state = { |
| 10 | + value: this.props.defaultValue, |
| 11 | + }; |
| 12 | + |
| 13 | + handleOnChange = jest.fn(value => { |
| 14 | + this.setState({ value }); |
| 15 | + }); |
| 16 | + |
| 17 | + render() { |
| 18 | + return this.props.children({ |
| 19 | + value: this.state.value, |
| 20 | + handleOnChange: this.handleOnChange, |
| 21 | + }); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function setup({ defaultValue } = {}) { |
| 26 | + let renderArgs; |
| 27 | + const setActiveSpy = jest.fn(); |
| 28 | + const setInactiveSpy = jest.fn(); |
| 29 | + |
| 30 | + const helpers = render( |
| 31 | + <StringController defaultValue={defaultValue}> |
| 32 | + {({ value, handleOnChange }) => { |
| 33 | + renderArgs = { value, onChangeSpy: handleOnChange }; |
| 34 | + return ( |
| 35 | + <StringControl |
| 36 | + value={value} |
| 37 | + onChange={handleOnChange} |
| 38 | + forID="test-string" |
| 39 | + classNameWrapper="test-classname" |
| 40 | + setActiveStyle={setActiveSpy} |
| 41 | + setInactiveStyle={setInactiveSpy} |
| 42 | + /> |
| 43 | + ); |
| 44 | + }} |
| 45 | + </StringController>, |
| 46 | + ); |
| 47 | + |
| 48 | + const input = helpers.container.querySelector('input'); |
| 49 | + |
| 50 | + return { |
| 51 | + ...helpers, |
| 52 | + ...renderArgs, |
| 53 | + setActiveSpy, |
| 54 | + setInactiveSpy, |
| 55 | + input, |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +describe('String widget', () => { |
| 60 | + it('calls setActiveStyle when input focused', () => { |
| 61 | + const { input, setActiveSpy } = setup(); |
| 62 | + |
| 63 | + fireEvent.focus(input); |
| 64 | + |
| 65 | + expect(setActiveSpy).toBeCalledTimes(1); |
| 66 | + }); |
| 67 | + |
| 68 | + it('calls setInactiveSpy when input blurred', () => { |
| 69 | + const { input, setInactiveSpy } = setup(); |
| 70 | + |
| 71 | + fireEvent.focus(input); |
| 72 | + fireEvent.blur(input); |
| 73 | + |
| 74 | + expect(setInactiveSpy).toBeCalledTimes(1); |
| 75 | + }); |
| 76 | + |
| 77 | + it('renders with default value', () => { |
| 78 | + const testValue = 'bar'; |
| 79 | + const { input } = setup({ defaultValue: testValue }); |
| 80 | + expect(input.value).toEqual(testValue); |
| 81 | + }); |
| 82 | + |
| 83 | + it('calls onChange when input changes', () => { |
| 84 | + jest.useFakeTimers(); |
| 85 | + const testValue = 'foo'; |
| 86 | + const { input, onChangeSpy } = setup(); |
| 87 | + |
| 88 | + fireEvent.focus(input); |
| 89 | + fireEvent.change(input, { target: { value: testValue } }); |
| 90 | + |
| 91 | + jest.runAllTimers(); |
| 92 | + |
| 93 | + expect(onChangeSpy).toBeCalledTimes(1); |
| 94 | + expect(onChangeSpy).toBeCalledWith(testValue); |
| 95 | + }); |
| 96 | +}); |
0 commit comments