2022-09-20 19:15:14 +02:00
|
|
|
/* eslint-disable jest/require-top-level-describe */
|
|
|
|
import { render } from '@testing-library/react';
|
|
|
|
import React from 'react';
|
2023-04-04 18:48:04 +02:00
|
|
|
import { IconColor } from '../../../helpers/constants/design-system';
|
|
|
|
import { IconName, IconSize } from './icon.types';
|
2022-09-20 19:15:14 +02:00
|
|
|
import { Icon } from './icon';
|
|
|
|
|
|
|
|
describe('Icon', () => {
|
|
|
|
it('should render correctly', () => {
|
|
|
|
const { getByTestId, container } = render(
|
2023-04-04 18:48:04 +02:00
|
|
|
<Icon name={IconName.AddSquare} data-testid="icon" />,
|
2022-09-20 19:15:14 +02:00
|
|
|
);
|
|
|
|
expect(getByTestId('icon')).toBeDefined();
|
|
|
|
expect(container.querySelector('svg')).toBeDefined();
|
2022-11-23 18:58:43 +01:00
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('should render with a custom class', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
2022-11-23 18:58:43 +01:00
|
|
|
data-testid="icon"
|
|
|
|
className="test-class"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(getByTestId('icon')).toHaveClass('test-class');
|
|
|
|
});
|
|
|
|
it('should render with an aria-label attribute', () => {
|
|
|
|
/**
|
|
|
|
* We aren't specifically adding an ariaLabel prop because in most cases
|
|
|
|
* the icon should be decorative or be accompanied by text. Also if the icon
|
|
|
|
* is to be used as a button in most cases ButtonIcon should be used. However
|
|
|
|
* we should test if it's possible to pass an aria-label attribute to the
|
|
|
|
* root html element.
|
|
|
|
*/
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
2022-11-23 18:58:43 +01:00
|
|
|
data-testid="icon"
|
|
|
|
aria-label="test aria label"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(getByTestId('icon')).toHaveAttribute(
|
|
|
|
'aria-label',
|
|
|
|
'test aria label',
|
|
|
|
);
|
2022-09-20 19:15:14 +02:00
|
|
|
});
|
|
|
|
it('should render with different icons using mask-image and image urls', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<>
|
2023-04-04 18:48:04 +02:00
|
|
|
<Icon name={IconName.AddSquare} data-testid="add-square" />
|
|
|
|
<Icon name={IconName.Bank} data-testid="bank" />
|
|
|
|
<Icon name={IconName.Bookmark} data-testid="bookmark" />
|
|
|
|
<Icon name={IconName.Calculator} data-testid="calculator" />
|
2022-09-20 19:15:14 +02:00
|
|
|
</>,
|
|
|
|
);
|
2023-01-24 18:39:46 +01:00
|
|
|
expect(window.getComputedStyle(getByTestId('add-square')).maskImage).toBe(
|
|
|
|
`url('./images/icons/add-square.svg')`,
|
|
|
|
);
|
|
|
|
expect(window.getComputedStyle(getByTestId('bank')).maskImage).toBe(
|
|
|
|
`url('./images/icons/bank.svg')`,
|
|
|
|
);
|
|
|
|
expect(window.getComputedStyle(getByTestId('bookmark')).maskImage).toBe(
|
|
|
|
`url('./images/icons/bookmark.svg')`,
|
|
|
|
);
|
|
|
|
expect(window.getComputedStyle(getByTestId('calculator')).maskImage).toBe(
|
|
|
|
`url('./images/icons/calculator.svg')`,
|
|
|
|
);
|
2022-09-20 19:15:14 +02:00
|
|
|
});
|
|
|
|
it('should render with different size classes', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<>
|
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
|
|
|
size={IconSize.Xs}
|
2022-09-20 19:15:14 +02:00
|
|
|
data-testid="icon-xs"
|
|
|
|
/>
|
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
|
|
|
size={IconSize.Sm}
|
2022-09-20 19:15:14 +02:00
|
|
|
data-testid="icon-sm"
|
|
|
|
/>
|
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
|
|
|
size={IconSize.Md}
|
2022-09-20 19:15:14 +02:00
|
|
|
data-testid="icon-md"
|
|
|
|
/>
|
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
|
|
|
size={IconSize.Lg}
|
2022-09-20 19:15:14 +02:00
|
|
|
data-testid="icon-lg"
|
|
|
|
/>
|
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
|
|
|
size={IconSize.Xl}
|
2022-09-20 19:15:14 +02:00
|
|
|
data-testid="icon-xl"
|
|
|
|
/>
|
2022-09-29 17:33:34 +02:00
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
|
|
|
size={IconSize.Inherit}
|
|
|
|
data-testid="icon-inherit"
|
2022-09-29 17:33:34 +02:00
|
|
|
/>
|
2022-09-20 19:15:14 +02:00
|
|
|
</>,
|
|
|
|
);
|
2022-11-23 18:58:43 +01:00
|
|
|
expect(getByTestId('icon-xs')).toHaveClass('mm-icon--size-xs');
|
|
|
|
expect(getByTestId('icon-sm')).toHaveClass('mm-icon--size-sm');
|
|
|
|
expect(getByTestId('icon-md')).toHaveClass('mm-icon--size-md');
|
|
|
|
expect(getByTestId('icon-lg')).toHaveClass('mm-icon--size-lg');
|
|
|
|
expect(getByTestId('icon-xl')).toHaveClass('mm-icon--size-xl');
|
2023-04-04 18:48:04 +02:00
|
|
|
expect(getByTestId('icon-inherit')).toHaveClass('mm-icon--size-inherit');
|
2022-09-20 19:15:14 +02:00
|
|
|
});
|
|
|
|
it('should render with icon colors', () => {
|
|
|
|
const { getByTestId } = render(
|
|
|
|
<>
|
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
2023-02-02 21:15:26 +01:00
|
|
|
color={IconColor.iconDefault}
|
2022-09-20 19:15:14 +02:00
|
|
|
data-testid="icon-color-default"
|
|
|
|
/>
|
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
2023-02-02 21:15:26 +01:00
|
|
|
color={IconColor.iconAlternative}
|
2022-09-20 19:15:14 +02:00
|
|
|
data-testid="icon-color-alternative"
|
|
|
|
/>
|
|
|
|
<Icon
|
2023-04-04 18:48:04 +02:00
|
|
|
name={IconName.AddSquare}
|
2023-02-02 21:15:26 +01:00
|
|
|
color={IconColor.iconMuted}
|
2022-09-20 19:15:14 +02:00
|
|
|
data-testid="icon-color-muted"
|
|
|
|
/>
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
expect(getByTestId('icon-color-default')).toHaveClass(
|
2023-07-25 18:05:37 +02:00
|
|
|
'mm-box--color-icon-default',
|
2022-09-20 19:15:14 +02:00
|
|
|
);
|
|
|
|
expect(getByTestId('icon-color-alternative')).toHaveClass(
|
2023-07-25 18:05:37 +02:00
|
|
|
'mm-box--color-icon-alternative',
|
2022-09-20 19:15:14 +02:00
|
|
|
);
|
|
|
|
expect(getByTestId('icon-color-muted')).toHaveClass(
|
2023-07-25 18:05:37 +02:00
|
|
|
'mm-box--color-icon-muted',
|
2022-09-20 19:15:14 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|