-
-
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
2 changed files
with
82 additions
and
34 deletions.
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
packages/netlify-cms-widget-text/src/__tests__/text.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,72 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
|
||
import { NetlifyCmsWidgetText } from '../'; | ||
|
||
const TextControl = NetlifyCmsWidgetText.controlComponent; | ||
|
||
function setup({ defaultValue } = {}) { | ||
const setActiveSpy = jest.fn(); | ||
const setInactiveSpy = jest.fn(); | ||
const onChangeSpy = jest.fn(); | ||
|
||
const helpers = render( | ||
<TextControl | ||
value={defaultValue} | ||
onChange={onChangeSpy} | ||
forID="test-string" | ||
classNameWrapper="test-classname" | ||
setActiveStyle={setActiveSpy} | ||
setInactiveStyle={setInactiveSpy} | ||
/>, | ||
); | ||
|
||
const textarea = helpers.container.querySelector('textarea'); | ||
|
||
return { | ||
...helpers, | ||
setActiveSpy, | ||
setInactiveSpy, | ||
onChangeSpy, | ||
textarea, | ||
}; | ||
} | ||
|
||
describe('String widget', () => { | ||
it('calls setActiveStyle when textarea focused', () => { | ||
const { textarea, setActiveSpy } = setup(); | ||
|
||
fireEvent.focus(textarea); | ||
|
||
expect(setActiveSpy).toBeCalledTimes(1); | ||
}); | ||
|
||
it('calls setInactiveSpy when textarea blurred', () => { | ||
const { textarea, setInactiveSpy } = setup(); | ||
|
||
fireEvent.focus(textarea); | ||
fireEvent.blur(textarea); | ||
|
||
expect(setInactiveSpy).toBeCalledTimes(1); | ||
}); | ||
|
||
it('renders with default value', () => { | ||
const testValue = 'bar'; | ||
const { textarea } = setup({ defaultValue: testValue }); | ||
expect(textarea.value).toEqual(testValue); | ||
}); | ||
|
||
it('calls onChange when textarea changes', () => { | ||
jest.useFakeTimers(); | ||
const testValue = 'foo'; | ||
const { textarea, onChangeSpy } = setup(); | ||
|
||
fireEvent.focus(textarea); | ||
fireEvent.change(textarea, { target: { value: testValue } }); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(onChangeSpy).toBeCalledTimes(1); | ||
expect(onChangeSpy).toBeCalledWith(testValue); | ||
}); | ||
}); |