2022-11-09 22:55:13 +01:00
|
|
|
/* eslint-disable jest/require-top-level-describe */
|
|
|
|
import { render } from '@testing-library/react';
|
|
|
|
import React from 'react';
|
2023-04-05 18:11:10 +02:00
|
|
|
import { IconName } from '..';
|
2023-04-26 18:17:25 +02:00
|
|
|
import { BUTTON_SIZES, BUTTON_VARIANT } from './button.constants';
|
2022-11-09 22:55:13 +01:00
|
|
|
import { Button } from './button';
|
|
|
|
|
|
|
|
describe('Button', () => {
|
|
|
|
it('should render button element correctly', () => {
|
|
|
|
const { getByTestId, getByText, container } = render(
|
|
|
|
<Button data-testid="button">Button</Button>,
|
|
|
|
);
|
|
|
|
expect(getByText('Button')).toBeDefined();
|
|
|
|
expect(container.querySelector('button')).toBeDefined();
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('button')).toHaveClass('mm-button-base');
|
2022-11-09 22:55:13 +01:00
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render anchor element correctly', () => {
|
|
|
|
const { getByTestId, container } = render(
|
|
|
|
<Button as="a" data-testid="button">
|
|
|
|
Button
|
|
|
|
</Button>,
|
|
|
|
);
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('button')).toHaveClass('mm-button-base');
|
2022-11-09 22:55:13 +01:00
|
|
|
const anchor = container.getElementsByTagName('a').length;
|
|
|
|
expect(anchor).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render anchor element correctly by href only being passed', () => {
|
|
|
|
const { getByTestId, container } = render(
|
|
|
|
<Button href="/metamask" data-testid="button">
|
|
|
|
Visit Site
|
|
|
|
</Button>,
|
|
|
|
);
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('button')).toHaveClass('mm-button-base');
|
2022-11-09 22:55:13 +01:00
|
|
|
const anchor = container.getElementsByTagName('a').length;
|
|
|
|
expect(anchor).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render button as block', () => {
|
|
|
|
const { getByTestId } = render(<Button block data-testid="block" />);
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('block')).toHaveClass(`mm-button-base--block`);
|
2022-11-09 22:55:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render with different button types', () => {
|
|
|
|
const { getByTestId, container } = render(
|
|
|
|
<>
|
2023-04-26 18:17:25 +02:00
|
|
|
<Button
|
|
|
|
variant={BUTTON_VARIANT.PRIMARY}
|
|
|
|
data-testid={BUTTON_VARIANT.PRIMARY}
|
|
|
|
>
|
2022-11-09 22:55:13 +01:00
|
|
|
Button
|
|
|
|
</Button>
|
|
|
|
<Button
|
2023-04-26 18:17:25 +02:00
|
|
|
variant={BUTTON_VARIANT.SECONDARY}
|
|
|
|
data-testid={BUTTON_VARIANT.SECONDARY}
|
2022-11-09 22:55:13 +01:00
|
|
|
>
|
|
|
|
Button
|
|
|
|
</Button>
|
2023-04-26 18:17:25 +02:00
|
|
|
<Button variant={BUTTON_VARIANT.LINK} data-testid={BUTTON_VARIANT.LINK}>
|
2022-11-09 22:55:13 +01:00
|
|
|
Button
|
|
|
|
</Button>
|
|
|
|
</>,
|
|
|
|
);
|
2023-04-26 18:17:25 +02:00
|
|
|
expect(getByTestId(BUTTON_VARIANT.PRIMARY)).toHaveClass(
|
|
|
|
`mm-button-${BUTTON_VARIANT.PRIMARY}`,
|
2022-11-09 22:55:13 +01:00
|
|
|
);
|
2023-04-26 18:17:25 +02:00
|
|
|
expect(getByTestId(BUTTON_VARIANT.SECONDARY)).toHaveClass(
|
|
|
|
`mm-button-${BUTTON_VARIANT.SECONDARY}`,
|
2022-11-09 22:55:13 +01:00
|
|
|
);
|
2023-04-26 18:17:25 +02:00
|
|
|
expect(getByTestId(BUTTON_VARIANT.LINK)).toHaveClass(
|
|
|
|
`mm-button-${BUTTON_VARIANT.LINK}`,
|
2022-11-09 22:55:13 +01:00
|
|
|
);
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render with different size classes', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<>
|
|
|
|
<Button
|
2023-01-26 19:32:11 +01:00
|
|
|
size={BUTTON_SIZES.INHERIT}
|
2023-04-26 18:17:25 +02:00
|
|
|
variant={BUTTON_VARIANT.LINK}
|
2023-01-26 19:32:11 +01:00
|
|
|
data-testid={BUTTON_SIZES.INHERIT}
|
2022-11-09 22:55:13 +01:00
|
|
|
>
|
2023-01-26 19:32:11 +01:00
|
|
|
Button {BUTTON_SIZES.INHERIT}
|
2022-11-09 22:55:13 +01:00
|
|
|
</Button>
|
|
|
|
<Button size={BUTTON_SIZES.SM} data-testid={BUTTON_SIZES.SM}>
|
|
|
|
Button {BUTTON_SIZES.SM}
|
|
|
|
</Button>
|
|
|
|
<Button size={BUTTON_SIZES.MD} data-testid={BUTTON_SIZES.MD}>
|
|
|
|
Button {BUTTON_SIZES.MD}
|
|
|
|
</Button>
|
|
|
|
<Button size={BUTTON_SIZES.LG} data-testid={BUTTON_SIZES.LG}>
|
|
|
|
Button {BUTTON_SIZES.LG}
|
|
|
|
</Button>
|
|
|
|
</>,
|
|
|
|
);
|
2023-01-26 19:32:11 +01:00
|
|
|
expect(getByTestId(BUTTON_SIZES.INHERIT)).toHaveClass(
|
|
|
|
`mm-button-link--size-${BUTTON_SIZES.INHERIT}`,
|
2022-11-09 22:55:13 +01:00
|
|
|
);
|
|
|
|
expect(getByTestId(BUTTON_SIZES.SM)).toHaveClass(
|
2023-01-13 22:58:09 +01:00
|
|
|
`mm-button-base--size-${BUTTON_SIZES.SM}`,
|
2022-11-09 22:55:13 +01:00
|
|
|
);
|
|
|
|
expect(getByTestId(BUTTON_SIZES.MD)).toHaveClass(
|
2023-01-13 22:58:09 +01:00
|
|
|
`mm-button-base--size-${BUTTON_SIZES.MD}`,
|
2022-11-09 22:55:13 +01:00
|
|
|
);
|
|
|
|
expect(getByTestId(BUTTON_SIZES.LG)).toHaveClass(
|
2023-01-13 22:58:09 +01:00
|
|
|
`mm-button-base--size-${BUTTON_SIZES.LG}`,
|
2022-11-09 22:55:13 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render with added classname', () => {
|
|
|
|
const { getByTestId } = render(
|
2023-01-13 22:58:09 +01:00
|
|
|
<Button data-testid="classname" className="mm-button-base--test">
|
2022-11-09 22:55:13 +01:00
|
|
|
Button
|
|
|
|
</Button>,
|
|
|
|
);
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('classname')).toHaveClass('mm-button-base--test');
|
2022-11-09 22:55:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render with different button states', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<>
|
|
|
|
<Button loading data-testid="loading">
|
|
|
|
Button
|
|
|
|
</Button>
|
|
|
|
<Button disabled data-testid="disabled">
|
|
|
|
Button
|
|
|
|
</Button>
|
|
|
|
</>,
|
|
|
|
);
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('loading')).toHaveClass(`mm-button-base--loading`);
|
|
|
|
expect(getByTestId('disabled')).toHaveClass(`mm-button-base--disabled`);
|
2022-11-09 22:55:13 +01:00
|
|
|
});
|
|
|
|
it('should render with icon', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<Button
|
|
|
|
data-testid="icon"
|
2023-04-05 18:11:10 +02:00
|
|
|
startIconName={IconName.AddSquare}
|
2023-02-22 18:42:06 +01:00
|
|
|
startIconProps={{ 'data-testid': 'start-button-icon' }}
|
2022-11-09 22:55:13 +01:00
|
|
|
>
|
|
|
|
Button
|
|
|
|
</Button>,
|
|
|
|
);
|
|
|
|
|
2023-02-22 18:42:06 +01:00
|
|
|
expect(getByTestId('start-button-icon')).toBeDefined();
|
2022-11-09 22:55:13 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render as danger', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<>
|
|
|
|
<Button danger data-testid="danger">
|
|
|
|
Button Danger
|
|
|
|
</Button>
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(getByTestId('danger')).toHaveClass('mm-button-primary--type-danger');
|
|
|
|
});
|