Skip to content

Commit

Permalink
test(button): add test (#2047)
Browse files Browse the repository at this point in the history
Co-authored-by: maxin <[email protected]>
  • Loading branch information
nnmax and maxin authored May 31, 2022
1 parent eaf157f commit fd0d351
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 16 deletions.
8 changes: 0 additions & 8 deletions src/button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,4 @@ export const BUTTON_DISPLAY_NAME = 'Button';

Button.displayName = BUTTON_DISPLAY_NAME;

Button.defaultProps = {
type: 'primary',
size: 'normal',
loading: false,
disabled: false,
htmlType: 'button',
};

export default Button;
8 changes: 0 additions & 8 deletions src/button/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,4 @@ export const ICON_BUTTON_DISPLAY_NAME = 'IconButton';

IconButton.displayName = ICON_BUTTON_DISPLAY_NAME;

IconButton.defaultProps = {
type: 'primary',
size: 'normal',
loading: false,
disabled: false,
htmlType: 'button',
};

export default IconButton;
86 changes: 86 additions & 0 deletions src/button/__test__/button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React from 'react';
import { render } from '@testing-library/react';
import { PlusOutlined } from '@gio-design/icons';
import Button from '../Button';
import IconButton from '../IconButton';

describe('<Button />', () => {
test('Should render different types of buttons', () => {
const { getAllByTestId } = render(
<>
<Button type="primary">Primary</Button>
<Button type="secondary">Secondary</Button>
<Button type="text">Text</Button>
</>
);
const buttons = getAllByTestId('button');
expect(buttons[0]).toHaveClass('gio-button_primary');
expect(buttons[1]).toHaveClass('gio-button_secondary');
expect(buttons[2]).toHaveClass('gio-button_text');
});

test('Should render different sizes of buttons', () => {
const { getAllByRole } = render(
<>
<Button size="normal">Normal</Button>
<Button size="small">Small</Button>
</>
);
const buttons = getAllByRole('button');
expect(buttons[0]).toHaveClass('gio-button_normal');
expect(buttons[1]).toHaveClass('gio-button_small');
});

test('Should render a disabled button', () => {
const { getByRole } = render(<Button disabled>Disabled</Button>);
expect(getByRole('button')).toBeDisabled();
expect(getByRole('button')).toHaveClass('gio-button_disabled');
});

test('Should render a loading button with disabled', () => {
const { getByRole } = render(<Button loading>Loading</Button>);
expect(getByRole('button')).toBeDisabled();
expect(getByRole('button')).toHaveClass('gio-button_loading');
expect(getByRole('img')).toHaveAttribute('aria-label', 'loading-two-tone');
});

test('Should render buttons with a prefix or suffix icon', () => {
const { getAllByRole } = render(
<>
<Button prefix={<PlusOutlined />}>Prefix</Button>
<Button suffix={<PlusOutlined />}>Suffix</Button>
</>
);
const buttons = getAllByRole('button');
expect(buttons[0].firstElementChild).toHaveClass('gio-button-prefix-icon');
expect(buttons[1].firstElementChild).toHaveClass('gio-button-suffix-icon');
});

test('Should render a button with a active status', () => {
const { getByRole } = render(<Button active>Active</Button>);
expect(getByRole('button')).toHaveClass('gio-button_active');
});

test('Should render a submit button or reset button', () => {
const { getByText } = render(
<>
<Button htmlType="submit">Submit</Button>
<Button htmlType="reset">Reset</Button>
</>
);
expect(getByText('Submit')).toHaveAttribute('type', 'submit');
expect(getByText('Reset')).toHaveAttribute('type', 'reset');
});
});

describe('<Button.IconButton />', () => {
test('Should render a icon button', () => {
const { getByRole } = render(
<IconButton>
<PlusOutlined />
</IconButton>
);

expect(getByRole('button')).toHaveClass('gio-icon-button');
});
});

1 comment on commit fd0d351

@vercel
Copy link

@vercel vercel bot commented on fd0d351 May 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

gio-design – ./

gio-design-growingio.vercel.app
gio-design.vercel.app
gio-design-git-master-growingio.vercel.app

Please sign in to comment.