-
Notifications
You must be signed in to change notification settings - Fork 92
Тест для кнопки #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Тест для кнопки #95
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
912c304
chore(config): добавление .editorconfig
fixmylie 0d308a0
chore(tests): добавление инфраструктуры для тестов
fixmylie a31a758
test(components): добавление теста Button
fixmylie 3f37624
chore(package): удаление enzmy
fixmylie f8cf0fc
chore(package): добавление jest-dom
fixmylie 300ab6d
chore(jest): добавление jest-dom в jest.setup.js
fixmylie 3dfb06d
test(Button): рефакторинг теста
fixmylie 9af52a1
refactor(jest): замена identity-obj-proxy на styleMock
fixmylie 0a038fe
refactor(button): добавление типа
fixmylie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,13 @@ | ||
# editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_size = 2 | ||
indent_style = space | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains hidden or 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 @@ | ||
module.exports = {}; |
This file contains hidden or 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,11 @@ | ||
module.exports = { | ||
verbose: true, | ||
preset: 'ts-jest', | ||
moduleNameMapper: { | ||
'\\.css$': '<rootDir>/__mocks__/styleMock.js', | ||
}, | ||
coveragePathIgnorePatterns: ['/node_modules/', '/coverage/', '/types/'], | ||
testMatch: ['**/*.test.{ts,tsx}'], | ||
collectCoverageFrom: ['**/*.{ts,tsx}', '!**/*.stories.tsx'], | ||
setupFilesAfterEnv: ['./jest.setup.ts'], | ||
}; |
This file contains hidden or 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 @@ | ||
import '@testing-library/jest-dom'; |
This file contains hidden or 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 hidden or 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,209 @@ | ||
import * as React from 'react'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
|
||
import { Button, ButtonProps } from './Button'; | ||
|
||
const testId = 'button'; | ||
|
||
const renderComponent = (props: ButtonProps = {}) => { | ||
const { label = 'Текст кнопки', ...rest } = props; | ||
return render(<Button data-testid={testId} label={label} {...rest} />); | ||
}; | ||
|
||
describe('Компонент Button', () => { | ||
it('должен рендериться без ошибок', () => { | ||
expect(renderComponent).not.toThrow(); | ||
}); | ||
|
||
describe('проверка props', () => { | ||
describe('проверка size', () => { | ||
const sizes = ['xs', 's', 'm', 'l'] as const; | ||
sizes.forEach((size) => { | ||
it(`присваивает класс для size=${size}`, () => { | ||
renderComponent({ size }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button).toHaveClass(`Button_size_${size}`); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('проверка view', () => { | ||
const views = ['clear', 'ghost', 'primary', 'secondary'] as const; | ||
views.forEach((view) => { | ||
it(`присваивает класс для view=${view}`, () => { | ||
renderComponent({ view }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button).toHaveClass(`Button_view_${view}`); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('проверка width', () => { | ||
const widths = ['full', 'default'] as const; | ||
widths.forEach((width) => { | ||
it(`присваивает класс для width=${width}`, () => { | ||
renderComponent({ width }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button).toHaveClass(`Button_width_${width}`); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('проверка form', () => { | ||
const forms = [ | ||
'default', | ||
'brick', | ||
'round', | ||
'brickRound', | ||
'roundBrick', | ||
'brickDefault', | ||
'defaultBrick', | ||
] as const; | ||
|
||
forms.forEach((form) => { | ||
it(`присваивает класс для form=${form}`, () => { | ||
renderComponent({ form }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button).toHaveClass(`Button_form_${form}`); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('проверка тэга', () => { | ||
const tags = ['a', 'div', 'span'] as const; | ||
tags.forEach((el) => { | ||
it(`должен рендериться как <${el}>`, () => { | ||
renderComponent({ as: el }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button.tagName).toEqual(el.toUpperCase()); | ||
}); | ||
}); | ||
|
||
it(`должен рендериться как функциональный компонент`, () => { | ||
const Component = (props: ButtonProps = {}) => <span {...props} />; | ||
|
||
renderComponent({ as: Component }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button.tagName).toEqual('SPAN'); | ||
}); | ||
}); | ||
|
||
describe('проверка disabled', () => { | ||
it.skip('должен отключать <button>', () => { | ||
const handleClick = jest.fn(); | ||
|
||
renderComponent({ disabled: true, onClick: handleClick }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
fireEvent.click(button); | ||
|
||
expect(handleClick).toHaveBeenCalledTimes(0); | ||
|
||
expect(button).toBeDisabled(); | ||
expect(button).toHaveClass('Button_disabled'); | ||
}); | ||
|
||
it('должен вешать класс disabled на <a> элемент', () => { | ||
const handleClick = jest.fn(); | ||
|
||
renderComponent({ disabled: true, as: 'a', onClick: handleClick }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
fireEvent.click(button); | ||
|
||
expect(handleClick).toHaveBeenCalledTimes(0); | ||
expect(button).toHaveClass('Button_disabled'); | ||
}); | ||
}); | ||
}); | ||
|
||
it('должен отображать текст в кнопке', () => { | ||
const label = 'Это кнопка'; | ||
|
||
renderComponent({ label }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button.textContent).toEqual(label); | ||
}); | ||
|
||
it('должен работать onClick, если кнопка не отключена', () => { | ||
const handleClick = jest.fn(); | ||
|
||
renderComponent({ onClick: handleClick }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
fireEvent.click(button); | ||
|
||
expect(handleClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
describe('проверка иконки', () => { | ||
it('должен отображать иконку слева', () => { | ||
const label = 'Текст кнопки'; | ||
const iconLeftText = 'Иконка слева'; | ||
const IconLeft = () => <span>{iconLeftText}</span>; | ||
|
||
renderComponent({ label, iconLeft: IconLeft }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button).toHaveTextContent(iconLeftText + label); | ||
}); | ||
|
||
it('должен отображать иконку справа', () => { | ||
const label = 'Текст кнопки'; | ||
const iconRightText = 'Иконка справа'; | ||
const IconRight = () => <span>{iconRightText}</span>; | ||
|
||
renderComponent({ label, iconRight: IconRight }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button).toHaveTextContent(label + iconRightText); | ||
}); | ||
|
||
it('должен отображать иконки слева и справа', () => { | ||
const label = 'Текст кнопки'; | ||
const iconRightText = 'Иконка справа'; | ||
const iconLeftText = 'Иконка слева'; | ||
|
||
const IconLeft = () => <span>{iconLeftText}</span>; | ||
const IconRight = () => <span>{iconRightText}</span>; | ||
|
||
renderComponent({ label, iconRight: IconRight, iconLeft: IconLeft }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button).toHaveTextContent(iconLeftText + label + iconRightText); | ||
}); | ||
|
||
it('должен отображать только иконку', () => { | ||
const label = 'Текст кнопки'; | ||
const iconLeftText = 'Иконка слева'; | ||
|
||
const IconLeft = () => <span>{iconLeftText}</span>; | ||
|
||
renderComponent({ label, iconLeft: IconLeft, onlyIcon: true }); | ||
|
||
const button = screen.getByTestId(testId); | ||
|
||
expect(button).toHaveTextContent(iconLeftText); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.