|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { Button } from '../'; |
| 4 | + |
| 5 | +describe('Button component', () => { |
| 6 | + it('renders without crashing', () => { |
| 7 | + render(<Button>Test Button</Button>); |
| 8 | + expect(screen.getByRole('button', { name: /test button/i })).toBeInTheDocument(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('applies the "contained" variant classes when variant is "contained"', () => { |
| 12 | + const { container } = render(<Button variant="contained">Test Button</Button>); |
| 13 | + expect(container.firstChild).toHaveClass('deriv-button__variant--contained'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('applies the "ghost" variant classes when variant is "ghost"', () => { |
| 17 | + const { container } = render(<Button variant="ghost">Test Button</Button>); |
| 18 | + expect(container.firstChild).toHaveClass('deriv-button__variant--ghost'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('applies the "outlined" variant classes when variant is "outlined"', () => { |
| 22 | + const { container } = render(<Button variant="outlined">Test Button</Button>); |
| 23 | + expect(container.firstChild).toHaveClass('deriv-button__variant--outlined'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('applies the correct color class based on the "color" prop', () => { |
| 27 | + const { container } = render(<Button color="primary">Test Button</Button>); |
| 28 | + expect(container.firstChild).toHaveClass('deriv-button__color--primary'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('applies full width class when "isFullWidth" prop is true', () => { |
| 32 | + const { container } = render(<Button isFullWidth>Test Button</Button>); |
| 33 | + expect(container.firstChild).toHaveClass('deriv-button__full-width'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('does not apply full width class when "isFullWidth" prop is false', () => { |
| 37 | + const { container } = render(<Button>Test Button</Button>); |
| 38 | + expect(container.firstChild).not.toHaveClass('deriv-button__full-width'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('shows loader when "isLoading" prop is true', () => { |
| 42 | + render(<Button isLoading>Test Button</Button>); |
| 43 | + expect(screen.getByTestId('dt_derivs-loader')).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('does not show loader when "isLoading" prop is false', () => { |
| 47 | + render(<Button>Test Button</Button>); |
| 48 | + expect(screen.queryByTestId('loader')).toBeNull(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('disables the button when "disabled" prop is true', () => { |
| 52 | + render(<Button disabled>Test Button</Button>); |
| 53 | + expect(screen.getByRole('button', { name: /test button/i })).toBeDisabled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('disables the button when "isLoading" prop is true', () => { |
| 57 | + render(<Button isLoading>Test Button</Button>); |
| 58 | + expect(screen.getByRole('button', { name: /test button/i })).toBeDisabled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('applies the correct size class based on the "size" prop', () => { |
| 62 | + const { container } = render(<Button size="lg">Test Button</Button>); |
| 63 | + expect(container.firstChild).toHaveClass('deriv-button__size--lg'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('applies the correct rounded class based on the "rounded" prop', () => { |
| 67 | + const { container } = render(<Button rounded="md">Test Button</Button>); |
| 68 | + expect(container.firstChild).toHaveClass('deriv-button__rounded--md'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('shows the icon when provided and not loading', () => { |
| 72 | + const Icon = () => <span>Icon</span>; |
| 73 | + render(<Button icon={<Icon />}>Test Button</Button>); |
| 74 | + expect(screen.getByText('Icon')).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('does not show the icon when "isLoading" prop is true', () => { |
| 78 | + const Icon = () => <span>Icon</span>; |
| 79 | + render(<Button isLoading icon={<Icon />}>Test Button</Button>); |
| 80 | + expect(screen.queryByText('Icon')).toBeNull(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('renders children text when not loading', () => { |
| 84 | + render(<Button>Test Button</Button>); |
| 85 | + expect(screen.getByRole('button', { name: /test button/i })).toHaveTextContent('Test Button'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('does not render children text when "isLoading" prop is true', () => { |
| 89 | + render(<Button isLoading>Test Button</Button>); |
| 90 | + expect(screen.queryByText('Test Button')).toBeNull(); |
| 91 | + }); |
| 92 | +}); |
0 commit comments