-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved Jest config, Github Action, added Button UI test
- Loading branch information
Showing
5 changed files
with
101 additions
and
5 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
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
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,96 @@ | ||
import React from 'react' | ||
|
||
import '@testing-library/jest-dom/jest-globals' | ||
import '@testing-library/jest-dom' | ||
|
||
import Button, { ButtonProps } from './Button' | ||
|
||
import { fireEvent, screen } from '@testing-library/dom' | ||
import { render } from '@testing-library/react' | ||
|
||
describe('Button Component', () => { | ||
const defaultProps: ButtonProps = { | ||
label: 'Click Me', | ||
mode: 'primary', | ||
size: 'medium' | ||
} | ||
|
||
it('renders the button with label', () => { | ||
render(<Button {...defaultProps} />) | ||
const buttonElement = screen.getByText(/Click Me/i) | ||
expect(buttonElement).toBeInTheDocument() | ||
}) | ||
|
||
it('applies the correct className based on mode and size', () => { | ||
render( | ||
<Button | ||
{...defaultProps} | ||
className='custom-class' | ||
/> | ||
) | ||
const buttonElement = screen.getByText(/Click Me/i) | ||
expect(buttonElement).toHaveClass('custom-class') | ||
expect(buttonElement).toHaveClass('button') | ||
expect(buttonElement).toHaveClass('primary') | ||
expect(buttonElement).toHaveClass('medium') | ||
}) | ||
|
||
it('displays the spinner when loading is true', () => { | ||
const { container } = render( | ||
<Button | ||
{...defaultProps} | ||
loading={true} | ||
/> | ||
) | ||
|
||
const iconElement = container.querySelector('svg') | ||
expect(iconElement).toBeInTheDocument() | ||
}) | ||
|
||
it('renders an icon if icon prop is provided', () => { | ||
const { container } = render( | ||
<Button | ||
{...defaultProps} | ||
icon='CheckCircle' | ||
/> | ||
) | ||
const iconElement = container.querySelector('svg') | ||
expect(iconElement).toBeInTheDocument() | ||
}) | ||
|
||
it('handles click events', () => { | ||
const handleClick = jest.fn() | ||
render( | ||
<Button | ||
{...defaultProps} | ||
onClick={handleClick} | ||
/> | ||
) | ||
const buttonElement = screen.getByText(/Click Me/i) | ||
fireEvent.click(buttonElement) | ||
expect(handleClick).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('renders a link if the link prop is provided', () => { | ||
render( | ||
<Button | ||
{...defaultProps} | ||
link='https://example.com' | ||
/> | ||
) | ||
const linkElement = screen.getByRole('link') | ||
expect(linkElement).toHaveAttribute('href', 'https://example.com') | ||
}) | ||
|
||
it('renders noindex attribute when noIndex is true', () => { | ||
render( | ||
<Button | ||
{...defaultProps} | ||
link='https://example.com' | ||
noIndex={true} | ||
/> | ||
) | ||
const linkElement = screen.getByRole('link') | ||
expect(linkElement).toHaveAttribute('rel', 'noindex nofollow') | ||
}) | ||
}) |