-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from deriv-com/markwylde/add-unit-tests-for-b…
…utton chore: add unit tests for button
- Loading branch information
Showing
2 changed files
with
94 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { Button } from '../'; | ||
|
||
describe('Button component', () => { | ||
it('renders without crashing', () => { | ||
render(<Button>Test Button</Button>); | ||
expect(screen.getByRole('button', { name: /test button/i })).toBeInTheDocument(); | ||
}); | ||
|
||
it('applies the "contained" variant classes when variant is "contained"', () => { | ||
const { container } = render(<Button variant="contained">Test Button</Button>); | ||
expect(container.firstChild).toHaveClass('deriv-button__variant--contained'); | ||
}); | ||
|
||
it('applies the "ghost" variant classes when variant is "ghost"', () => { | ||
const { container } = render(<Button variant="ghost">Test Button</Button>); | ||
expect(container.firstChild).toHaveClass('deriv-button__variant--ghost'); | ||
}); | ||
|
||
it('applies the "outlined" variant classes when variant is "outlined"', () => { | ||
const { container } = render(<Button variant="outlined">Test Button</Button>); | ||
expect(container.firstChild).toHaveClass('deriv-button__variant--outlined'); | ||
}); | ||
|
||
it('applies the correct color class based on the "color" prop', () => { | ||
const { container } = render(<Button color="primary">Test Button</Button>); | ||
expect(container.firstChild).toHaveClass('deriv-button__color--primary'); | ||
}); | ||
|
||
it('applies full width class when "isFullWidth" prop is true', () => { | ||
const { container } = render(<Button isFullWidth>Test Button</Button>); | ||
expect(container.firstChild).toHaveClass('deriv-button__full-width'); | ||
}); | ||
|
||
it('does not apply full width class when "isFullWidth" prop is false', () => { | ||
const { container } = render(<Button>Test Button</Button>); | ||
expect(container.firstChild).not.toHaveClass('deriv-button__full-width'); | ||
}); | ||
|
||
it('shows loader when "isLoading" prop is true', () => { | ||
render(<Button isLoading>Test Button</Button>); | ||
expect(screen.getByTestId('dt_derivs-loader')).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not show loader when "isLoading" prop is false', () => { | ||
render(<Button>Test Button</Button>); | ||
expect(screen.queryByTestId('loader')).toBeNull(); | ||
}); | ||
|
||
it('disables the button when "disabled" prop is true', () => { | ||
render(<Button disabled>Test Button</Button>); | ||
expect(screen.getByRole('button', { name: /test button/i })).toBeDisabled(); | ||
}); | ||
|
||
it('disables the button when "isLoading" prop is true', () => { | ||
render(<Button isLoading>Test Button</Button>); | ||
expect(screen.getByRole('button', { name: /test button/i })).toBeDisabled(); | ||
}); | ||
|
||
it('applies the correct size class based on the "size" prop', () => { | ||
const { container } = render(<Button size="lg">Test Button</Button>); | ||
expect(container.firstChild).toHaveClass('deriv-button__size--lg'); | ||
}); | ||
|
||
it('applies the correct rounded class based on the "rounded" prop', () => { | ||
const { container } = render(<Button rounded="md">Test Button</Button>); | ||
expect(container.firstChild).toHaveClass('deriv-button__rounded--md'); | ||
}); | ||
|
||
it('shows the icon when provided and not loading', () => { | ||
const Icon = () => <span>Icon</span>; | ||
render(<Button icon={<Icon />}>Test Button</Button>); | ||
expect(screen.getByText('Icon')).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not show the icon when "isLoading" prop is true', () => { | ||
const Icon = () => <span>Icon</span>; | ||
render(<Button isLoading icon={<Icon />}>Test Button</Button>); | ||
expect(screen.queryByText('Icon')).toBeNull(); | ||
}); | ||
|
||
it('renders children text when not loading', () => { | ||
render(<Button>Test Button</Button>); | ||
expect(screen.getByRole('button', { name: /test button/i })).toHaveTextContent('Test Button'); | ||
}); | ||
|
||
it('does not render children text when "isLoading" prop is true', () => { | ||
render(<Button isLoading>Test Button</Button>); | ||
expect(screen.queryByText('Test Button')).toBeNull(); | ||
}); | ||
}); |
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