-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: setup tests and write tests to TextInput component (#71)
test: setup tests and write tests to TextInput component
- Loading branch information
Showing
8 changed files
with
13,655 additions
and
7,809 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"extends": ["@yandex-cloud/eslint-config", "@yandex-cloud/eslint-config/prettier"], | ||
"root": true | ||
"root": true, | ||
"env": { | ||
"node": true | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ node_modules | |
/storybook-static | ||
/build | ||
/styles/**/*.css | ||
/coverage |
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,14 @@ | ||
module.exports = { | ||
verbose: true, | ||
moduleFileExtensions: ['js', 'json', 'ts', 'tsx'], | ||
rootDir: '.', | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest', | ||
}, | ||
coverageDirectory: './coverage', | ||
testEnvironment: 'node', | ||
setupFiles: ['<rootDir>/src/setup-tests.ts'], | ||
moduleNameMapper: { | ||
'\\.(css|less|scss|sass)$': 'jest-transform-css', | ||
}, | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import React from 'react'; | ||
import {shallow} from 'enzyme'; | ||
|
||
import {TextInput} from '../TextInput'; | ||
import {TextAreaControl} from '../TextAreaControl/TextAreaControl'; | ||
import {InputControl} from '../InputControl/InputControl'; | ||
|
||
type TextInputEvent = React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>; | ||
|
||
describe('TextInput', () => { | ||
it('should render text area component when component receives multiline props', () => { | ||
const wrapper = shallow(<TextInput multiline />); | ||
|
||
expect(wrapper.find(TextAreaControl).exists()).toBeTruthy(); | ||
}); | ||
|
||
it("shouldn render text input component when component doesn't receive multiline props", () => { | ||
const wrapper = shallow(<TextInput />); | ||
|
||
expect(wrapper.find(InputControl).exists()).toBeTruthy(); | ||
}); | ||
|
||
it('should render error message in input when component receives error props', () => { | ||
const wrapper = shallow(<TextInput error="FAKE_ERROR" />); | ||
|
||
expect(wrapper.find('.yc-text-input__error').exists()).toBeTruthy(); | ||
}); | ||
|
||
it("shouldn't render error message in input when component doesn't receive error props", () => { | ||
const wrapper = shallow(<TextInput />); | ||
|
||
expect(wrapper.find('.yc-text-input__error').exists()).toBeFalsy(); | ||
}); | ||
|
||
it('should render clear button when component receives hasClear props', () => { | ||
const wrapper = shallow(<TextInput hasClear />); | ||
|
||
expect(wrapper.find('.yc-text-input__clear').exists()).toBeTruthy(); | ||
}); | ||
|
||
it("shouldn't render clear button when component doesn't receive hasClear props", () => { | ||
const wrapper = shallow(<TextInput />); | ||
|
||
expect(wrapper.find('.yc-text-input__clear').exists()).toBeFalsy(); | ||
}); | ||
|
||
it('should call onChange with event function when input changes value', () => { | ||
const FAKE_ON_CHANGE_FN = jest.fn(); | ||
const FAKE_EVENT = {target: {}} as TextInputEvent; | ||
|
||
const wrapper = shallow(<TextInput onChange={FAKE_ON_CHANGE_FN} />); | ||
wrapper.find(InputControl).props().onChange?.(FAKE_EVENT); | ||
|
||
expect(FAKE_ON_CHANGE_FN).toBeCalledWith({...FAKE_EVENT}); | ||
}); | ||
|
||
it('should call onUpdate with "FAKE_VALUE" function when input changes value', () => { | ||
const FAKE_VALUE = 'FAKE_VALUE'; | ||
const FAKE_ON_UPDATE_FN = jest.fn(); | ||
const FAKE_EVENT = {target: {value: FAKE_VALUE}} as TextInputEvent; | ||
|
||
const wrapper = shallow(<TextInput onUpdate={FAKE_ON_UPDATE_FN} />); | ||
wrapper.find(InputControl).props().onChange?.(FAKE_EVENT); | ||
|
||
expect(FAKE_ON_UPDATE_FN).toBeCalledWith(FAKE_VALUE); | ||
}); | ||
|
||
it('should call onChange function when it clickes to clean button and component receives hasClear', () => { | ||
const FAKE_ON_CHANGE_FN = jest.fn(); | ||
const FAKE_EVENT = {target: {}} as TextInputEvent; | ||
const FAKE_CONTROL_REF = { | ||
current: {focus: jest.fn(), value: ''} as any as HTMLInputElement, | ||
}; | ||
|
||
const wrapper = shallow( | ||
<TextInput controlRef={FAKE_CONTROL_REF} hasClear onChange={FAKE_ON_CHANGE_FN} />, | ||
); | ||
(wrapper.find(InputControl).props().controlRef as Function)(FAKE_CONTROL_REF.current); | ||
wrapper.find('.yc-text-input__clear').simulate('click', FAKE_EVENT); | ||
|
||
expect(FAKE_ON_CHANGE_FN).toBeCalled(); | ||
}); | ||
|
||
it('should call onUpdate with "FAKE_VALUE" function when it clickes to clean button and component receives hasClear', () => { | ||
const FAKE_ON_UPDATE_FN = jest.fn(); | ||
const FAKE_EVENT = {target: {}} as TextInputEvent; | ||
const FAKE_CONTROL_REF = { | ||
current: {focus: jest.fn(), value: ''} as any as HTMLInputElement, | ||
}; | ||
|
||
const wrapper = shallow( | ||
<TextInput controlRef={FAKE_CONTROL_REF} hasClear onUpdate={FAKE_ON_UPDATE_FN} />, | ||
); | ||
(wrapper.find(InputControl).props().controlRef as Function)(FAKE_CONTROL_REF.current); | ||
wrapper.find('.yc-text-input__clear').simulate('click', FAKE_EVENT); | ||
|
||
expect(FAKE_ON_UPDATE_FN).toBeCalledWith(''); | ||
}); | ||
|
||
it('should pass autoComplete = on to InputControl component when component recieves autoComplete props = true', () => { | ||
const wrapper = shallow(<TextInput autoComplete />); | ||
|
||
expect(wrapper.find(InputControl).props().autoComplete).toBe('on'); | ||
}); | ||
|
||
it('should pass autoComplete = off to InputControl component when component recieves autoComplete props = false', () => { | ||
const wrapper = shallow(<TextInput autoComplete={false} />); | ||
|
||
expect(wrapper.find(InputControl).props().autoComplete).toBe('off'); | ||
}); | ||
}); |
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,4 @@ | ||
import {configure} from 'enzyme'; | ||
import EnzymeAdapter from 'enzyme-adapter-react-16'; | ||
|
||
configure({adapter: new EnzymeAdapter()}); |