Skip to content

Commit 5c5fb21

Browse files
authored
Merge pull request #111 from deriv-com/markwylde/add-unit-tests-for-button
chore: add unit tests for button
2 parents 01e6529 + d94d8d8 commit 5c5fb21

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
});

lib/components/Button/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface ButtonProps extends ComponentProps<"button"> {
1313
icon?: ReactElement;
1414
isFullWidth?: boolean;
1515
isLoading?: boolean;
16-
rounded?: Extract<TGenericSizes, "md" | "sm">;
16+
rounded?: Extract<TGenericSizes, "lg" | "md" | "sm">;
1717
size?: Extract<TGenericSizes, "lg" | "md" | "sm">;
1818
textSize?: ComponentProps<typeof Text>["size"];
1919
variant?: TVariant;
@@ -84,6 +84,7 @@ export const Button = ({
8484
className,
8585
)}
8686
disabled={rest.disabled || isLoading}
87+
aria-label={rest.children && typeof rest.children === 'string' ? rest.children : ''}
8788
{...rest}
8889
>
8990
{isLoading && (

0 commit comments

Comments
 (0)