2022-10-04 18:55:51 +02: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-07-25 18:05:15 +02:00
|
|
|
import { ButtonBaseSize } from './button-base.types';
|
2022-10-04 18:55:51 +02:00
|
|
|
import { ButtonBase } from './button-base';
|
|
|
|
|
|
|
|
describe('ButtonBase', () => {
|
2022-11-03 19:41:16 +01:00
|
|
|
it('should render button element correctly and match snapshot', () => {
|
2022-10-04 18:55:51 +02:00
|
|
|
const { getByTestId, getByText, container } = render(
|
|
|
|
<ButtonBase data-testid="button-base">Button base</ButtonBase>,
|
|
|
|
);
|
|
|
|
expect(getByText('Button base')).toBeDefined();
|
|
|
|
expect(container.querySelector('button')).toBeDefined();
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('button-base')).toHaveClass('mm-button-base');
|
2022-11-03 19:41:16 +01:00
|
|
|
expect(container).toMatchSnapshot();
|
2022-10-04 18:55:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render anchor element correctly', () => {
|
|
|
|
const { getByTestId, container } = render(
|
|
|
|
<ButtonBase as="a" data-testid="button-base" />,
|
|
|
|
);
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('button-base')).toHaveClass('mm-button-base');
|
2022-10-06 06:51:02 +02:00
|
|
|
const anchor = container.getElementsByTagName('a').length;
|
2022-10-14 00:39:22 +02:00
|
|
|
expect(anchor).toBe(1);
|
|
|
|
});
|
|
|
|
|
2022-11-22 21:25:49 +01:00
|
|
|
it('should render anchor element correctly by href only being passed and href exists', () => {
|
2022-10-14 00:39:22 +02:00
|
|
|
const { getByTestId, container } = render(
|
2023-03-10 18:47:01 +01:00
|
|
|
<ButtonBase href="/metamask" data-testid="button-base">
|
|
|
|
Button Base
|
|
|
|
</ButtonBase>,
|
|
|
|
);
|
|
|
|
expect(getByTestId('button-base')).toHaveClass('mm-button-base');
|
|
|
|
expect(getByTestId('button-base')).toHaveAttribute('href', '/metamask');
|
|
|
|
const anchor = container.getElementsByTagName('a').length;
|
|
|
|
expect(anchor).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render anchor element correctly by href and externalLink, href target and rel exist', () => {
|
|
|
|
const { getByTestId, container } = render(
|
|
|
|
<ButtonBase
|
|
|
|
href="https://www.test.com/"
|
|
|
|
externalLink
|
|
|
|
data-testid="button-base"
|
|
|
|
>
|
2022-11-22 21:25:49 +01:00
|
|
|
Button Base
|
|
|
|
</ButtonBase>,
|
2022-10-14 00:39:22 +02:00
|
|
|
);
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('button-base')).toHaveClass('mm-button-base');
|
2022-11-22 21:25:49 +01:00
|
|
|
expect(getByTestId('button-base')).toHaveAttribute(
|
|
|
|
'href',
|
|
|
|
'https://www.test.com/',
|
2023-03-10 18:47:01 +01:00
|
|
|
);
|
2023-07-25 18:05:15 +02:00
|
|
|
expect(getByTestId('button-base')).toHaveAttribute('target', '_blank');
|
2023-03-10 18:47:01 +01:00
|
|
|
expect(getByTestId('button-base')).toHaveAttribute(
|
|
|
|
'rel',
|
|
|
|
'noopener noreferrer',
|
2022-11-22 21:25:49 +01:00
|
|
|
);
|
2022-10-14 00:39:22 +02:00
|
|
|
const anchor = container.getElementsByTagName('a').length;
|
2022-10-06 06:51:02 +02:00
|
|
|
expect(anchor).toBe(1);
|
2023-03-10 18:47:01 +01:00
|
|
|
expect(container).toMatchSnapshot();
|
2022-10-04 18:55:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render button as block', () => {
|
|
|
|
const { getByTestId } = render(<ButtonBase block data-testid="block" />);
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('block')).toHaveClass(`mm-button-base--block`);
|
2022-10-04 18:55:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render with different size classes', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<>
|
2023-07-25 18:05:15 +02:00
|
|
|
<ButtonBase size={ButtonBaseSize.Sm} data-testid={ButtonBaseSize.Sm} />
|
|
|
|
<ButtonBase size={ButtonBaseSize.Md} data-testid={ButtonBaseSize.Md} />
|
|
|
|
<ButtonBase size={ButtonBaseSize.Lg} data-testid={ButtonBaseSize.Lg} />
|
2022-10-04 18:55:51 +02:00
|
|
|
</>,
|
|
|
|
);
|
2023-07-25 18:05:15 +02:00
|
|
|
expect(getByTestId(ButtonBaseSize.Sm)).toHaveClass(
|
|
|
|
`mm-button-base--size-${ButtonBaseSize.Sm}`,
|
2022-10-04 18:55:51 +02:00
|
|
|
);
|
2023-07-25 18:05:15 +02:00
|
|
|
expect(getByTestId(ButtonBaseSize.Md)).toHaveClass(
|
|
|
|
`mm-button-base--size-${ButtonBaseSize.Md}`,
|
2022-10-04 18:55:51 +02:00
|
|
|
);
|
2023-07-25 18:05:15 +02:00
|
|
|
expect(getByTestId(ButtonBaseSize.Lg)).toHaveClass(
|
|
|
|
`mm-button-base--size-${ButtonBaseSize.Lg}`,
|
2022-10-04 18:55:51 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-10-06 06:51:02 +02:00
|
|
|
it('should render with added classname', () => {
|
|
|
|
const { getByTestId } = render(
|
2023-01-13 22:58:09 +01:00
|
|
|
<ButtonBase data-testid="classname" className="mm-button-base--test" />,
|
2022-10-06 06:51:02 +02:00
|
|
|
);
|
2023-01-13 22:58:09 +01:00
|
|
|
expect(getByTestId('classname')).toHaveClass('mm-button-base--test');
|
2022-10-06 06:51:02 +02:00
|
|
|
});
|
|
|
|
|
2022-10-04 18:55:51 +02:00
|
|
|
it('should render with different button states', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<>
|
|
|
|
<ButtonBase loading data-testid="loading" />
|
|
|
|
<ButtonBase disabled data-testid="disabled" />
|
|
|
|
</>,
|
|
|
|
);
|
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-10-04 18:55:51 +02:00
|
|
|
});
|
|
|
|
it('should render with icon', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<ButtonBase
|
|
|
|
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' }}
|
2023-04-05 18:11:10 +02:00
|
|
|
endIconName={IconName.AddSquare}
|
2023-02-22 18:42:06 +01:00
|
|
|
endIconProps={{ 'data-testid': 'end-button-icon' }}
|
2022-10-04 18:55:51 +02:00
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
2023-02-22 18:42:06 +01:00
|
|
|
expect(getByTestId('start-button-icon')).toBeDefined();
|
|
|
|
expect(getByTestId('end-button-icon')).toBeDefined();
|
2022-10-04 18:55:51 +02:00
|
|
|
});
|
|
|
|
});
|