-
-
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
3 changed files
with
147 additions
and
3 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
12 changes: 12 additions & 0 deletions
12
packages/netlify-cms-widget-colorstring/src/__tests__/__snapshots__/color.spec.js.snap
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,12 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Color widget field.allowInput is false renders input as readonly 1`] = ` | ||
<input | ||
class="test-classname" | ||
id="test-string" | ||
readonly="" | ||
style="padding-left: 75px; padding-right: 70px; color: rgb(187, 187, 187);" | ||
type="text" | ||
value="" | ||
/> | ||
`; |
130 changes: 130 additions & 0 deletions
130
packages/netlify-cms-widget-colorstring/src/__tests__/color.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,130 @@ | ||
import React from 'react'; | ||
import { fromJS } from 'immutable'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
|
||
import { NetlifyCmsWidgetColorString } from '../'; | ||
|
||
const ColorControl = NetlifyCmsWidgetColorString.controlComponent; | ||
|
||
const fieldSettings = { | ||
allowInput: false, | ||
}; | ||
|
||
function setup({ field, defaultValue } = {}) { | ||
const setActiveSpy = jest.fn(); | ||
const setInactiveSpy = jest.fn(); | ||
const onChangeSpy = jest.fn(); | ||
|
||
const helpers = render( | ||
<ColorControl | ||
field={field} | ||
value={defaultValue} | ||
onChange={onChangeSpy} | ||
forID="test-string" | ||
classNameWrapper="test-classname" | ||
setActiveStyle={setActiveSpy} | ||
setInactiveStyle={setInactiveSpy} | ||
/>, | ||
); | ||
|
||
const input = helpers.container.querySelector('input'); | ||
|
||
jest.useFakeTimers(); | ||
|
||
return { | ||
...helpers, | ||
setActiveSpy, | ||
setInactiveSpy, | ||
onChangeSpy, | ||
input, | ||
}; | ||
} | ||
|
||
describe('Color widget', () => { | ||
it('renders with default value', () => { | ||
const field = fromJS(fieldSettings); | ||
const testValue = '#fff000'; | ||
const { input } = setup({ field, defaultValue: testValue }); | ||
|
||
expect(input.value).toEqual(testValue); | ||
}); | ||
|
||
describe('field.allowInput is false', () => { | ||
it('renders input as readonly', () => { | ||
const field = fromJS(fieldSettings); | ||
const { input } = setup({ field }); | ||
|
||
expect(input).toMatchSnapshot(); | ||
}); | ||
|
||
it('opens picker on input click', () => { | ||
const field = fromJS(fieldSettings); | ||
const { input, queryByTestId } = setup({ field }); | ||
|
||
fireEvent.click(input); | ||
|
||
expect(queryByTestId('color-picker-container')).not.toBeNull(); | ||
}); | ||
|
||
it('displays clear button when input is present', () => { | ||
const field = fromJS(fieldSettings); | ||
const { queryByTestId } = setup({ field, defaultValue: '#fff000' }); | ||
|
||
expect(queryByTestId('clear-btn-wrapper')).not.toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('field.allowInput is true', () => { | ||
const field = fromJS({ ...fieldSettings, allowInput: true }); | ||
|
||
it('calls onChange when input changes', () => { | ||
const testValue = '#fff000'; | ||
const { input, onChangeSpy } = setup({ field }); | ||
|
||
fireEvent.focus(input); | ||
fireEvent.change(input, { target: { value: testValue } }); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(onChangeSpy).toHaveBeenCalledTimes(1); | ||
expect(onChangeSpy).toHaveBeenCalledWith(testValue); | ||
}); | ||
|
||
it('sets input value', () => { | ||
const testValue = '#fff000'; | ||
const { input } = setup({ field }); | ||
|
||
fireEvent.focus(input); | ||
fireEvent.change(input, { target: { value: testValue } }); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(input.value).toEqual(testValue); | ||
}); | ||
|
||
it('does not open picker on input click', () => { | ||
const { input, queryByTestId } = setup({ field }); | ||
|
||
fireEvent.click(input); | ||
|
||
expect(queryByTestId('color-picker-container')).toBeNull(); | ||
}); | ||
|
||
it('calls setActiveStyle when input focused', () => { | ||
const { input, setActiveSpy } = setup({ field }); | ||
|
||
fireEvent.focus(input); | ||
|
||
expect(setActiveSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('calls setInactiveSpy when input blurred', () => { | ||
const { input, setInactiveSpy } = setup({ field }); | ||
|
||
fireEvent.focus(input); | ||
fireEvent.blur(input); | ||
|
||
expect(setInactiveSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |