-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
packages/netlify-cms-widget-string/src/__tests__/string.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
|
||
import { NetlifyCmsWidgetString } from '../'; | ||
|
||
const StringControl = NetlifyCmsWidgetString.controlComponent; | ||
|
||
class StringController extends React.Component { | ||
state = { | ||
value: this.props.defaultValue, | ||
}; | ||
|
||
handleOnChange = jest.fn(value => { | ||
this.setState({ value }); | ||
}); | ||
|
||
render() { | ||
return this.props.children({ | ||
value: this.state.value, | ||
handleOnChange: this.handleOnChange, | ||
}); | ||
} | ||
} | ||
|
||
function setup({ defaultValue } = {}) { | ||
let renderArgs; | ||
const setActiveSpy = jest.fn(); | ||
const setInactiveSpy = jest.fn(); | ||
|
||
const helpers = render( | ||
<StringController defaultValue={defaultValue}> | ||
{({ value, handleOnChange }) => { | ||
renderArgs = { value, onChangeSpy: handleOnChange }; | ||
return ( | ||
<StringControl | ||
value={value} | ||
onChange={handleOnChange} | ||
forID="test-string" | ||
classNameWrapper="test-classname" | ||
setActiveStyle={setActiveSpy} | ||
setInactiveStyle={setInactiveSpy} | ||
/> | ||
); | ||
}} | ||
</StringController>, | ||
); | ||
|
||
const input = helpers.container.querySelector('input'); | ||
|
||
return { | ||
...helpers, | ||
...renderArgs, | ||
setActiveSpy, | ||
setInactiveSpy, | ||
input, | ||
}; | ||
} | ||
|
||
describe('String widget', () => { | ||
it('calls setActiveStyle when input focused', () => { | ||
const { input, setActiveSpy } = setup(); | ||
|
||
fireEvent.focus(input); | ||
|
||
expect(setActiveSpy).toBeCalledTimes(1); | ||
}); | ||
|
||
it('calls setInactiveSpy when input blurred', () => { | ||
const { input, setInactiveSpy } = setup(); | ||
|
||
fireEvent.focus(input); | ||
fireEvent.blur(input); | ||
|
||
expect(setInactiveSpy).toBeCalledTimes(1); | ||
}); | ||
|
||
it('renders with default value', () => { | ||
const testValue = 'bar'; | ||
const { input } = setup({ defaultValue: testValue }); | ||
expect(input.value).toEqual(testValue); | ||
}); | ||
|
||
it('calls onChange when input changes', () => { | ||
jest.useFakeTimers(); | ||
const testValue = 'foo'; | ||
const { input, onChangeSpy } = setup(); | ||
|
||
fireEvent.focus(input); | ||
fireEvent.change(input, { target: { value: testValue } }); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(onChangeSpy).toBeCalledTimes(1); | ||
expect(onChangeSpy).toBeCalledWith(testValue); | ||
}); | ||
}); |