-
Notifications
You must be signed in to change notification settings - Fork 69
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
1 parent
db2f2ab
commit 029f3d9
Showing
2 changed files
with
47 additions
and
13 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
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 |
---|---|---|
@@ -1,26 +1,63 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import React, { useState } from 'react'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
|
||
import FormControl from '../FormControl'; | ||
|
||
const ref = { | ||
current: null, | ||
}; | ||
|
||
// eslint-disable-next-line react/prop-types | ||
function Component({ isClearValue }) { | ||
const onChangeFunc = jest.fn(); | ||
const [inputValue, setInputValue] = useState(''); | ||
|
||
return ( | ||
<FormControl | ||
hasInputMask | ||
mask="+{1}(000)000-00-00" | ||
value={inputValue} | ||
onChange={isClearValue ? onChangeFunc : (e) => setInputValue(e.target.value)} | ||
onAccept={isClearValue ? onChangeFunc : (value) => setInputValue(value)} | ||
data-testid="form-control-with-mask" | ||
/> | ||
); | ||
} | ||
|
||
describe('FormControl', () => { | ||
it('textarea changes its height with autoResize prop', () => { | ||
const useReferenceSpy = jest.spyOn(React, 'useRef').mockReturnValue(ref); | ||
const onChangeFunc = jest.fn(); | ||
const wrapper = mount(( | ||
<FormControl as="textarea" autoResize onChange={onChangeFunc} /> | ||
)); | ||
render( | ||
<FormControl as="textarea" autoResize onChange={onChangeFunc} data-testid="form-control-textarea" />, | ||
); | ||
|
||
ref.scrollHeight = 180; | ||
ref.offsetHeight = 90; | ||
ref.clientHeight = 88; | ||
|
||
const textarea = screen.getByTestId('form-control-textarea'); | ||
|
||
expect(useReferenceSpy).toHaveBeenCalledTimes(1); | ||
expect(ref.current.style.height).toBe('0px'); | ||
wrapper.find('textarea').simulate('change'); | ||
|
||
fireEvent.change(textarea, { target: { value: 'new text' } }); | ||
|
||
expect(onChangeFunc).toHaveBeenCalledTimes(1); | ||
expect(ref.current.style.height).toEqual(`${ref.current.scrollHeight + ref.current.offsets}px`); | ||
expect(ref.current.style.height).toEqual(`${ref.current.scrollHeight + ref.current.offsetHeight}px`); | ||
}); | ||
it('should apply and accept input mask for phone numbers', () => { | ||
render(<Component />); | ||
|
||
const input = screen.getByTestId('form-control-with-mask'); | ||
fireEvent.change(input, { target: { value: '1234567890' } }); | ||
expect(input.value).toBe('+1(234)567-89-0'); | ||
}); | ||
it('should be cleared from the mask elements value', () => { | ||
render(<Component isClearValue />); | ||
|
||
const input = screen.getByTestId('form-control-with-mask'); | ||
fireEvent.change(input, { target: { value: '1234567890' } }); | ||
expect(input.value).toBe('1234567890'); | ||
}); | ||
}); |