tag.spec.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import Tag from 'sentry/components/tag';
  3. import {IconFire} from 'sentry/icons';
  4. describe('Tag', () => {
  5. it('basic', () => {
  6. render(<Tag>Text</Tag>);
  7. expect(screen.getByText('Text')).toBeInTheDocument();
  8. });
  9. it('with icon', () => {
  10. render(
  11. <Tag icon={<IconFire data-test-id="icon-fire" />} type="error">
  12. Error
  13. </Tag>
  14. );
  15. expect(screen.getByText('Error')).toBeInTheDocument();
  16. expect(screen.getByTestId('icon-fire')).toBeInTheDocument();
  17. });
  18. it('with tooltip', async () => {
  19. render(
  20. <Tag type="highlight" tooltipText="lorem ipsum">
  21. Tooltip
  22. </Tag>
  23. );
  24. expect(screen.getByText('Tooltip')).toBeInTheDocument();
  25. expect(screen.queryByText('lorem ipsum')).not.toBeInTheDocument();
  26. userEvent.hover(screen.getByText('Tooltip'));
  27. expect(await screen.findByText('lorem ipsum')).toBeInTheDocument();
  28. });
  29. it('with dismiss', () => {
  30. const mockCallback = jest.fn();
  31. render(
  32. <Tag type="highlight" onDismiss={mockCallback}>
  33. Dismissable
  34. </Tag>
  35. );
  36. expect(screen.getByText('Dismissable')).toBeInTheDocument();
  37. expect(screen.getByRole('button', {name: 'Dismiss'})).toBeInTheDocument();
  38. expect(mockCallback).toHaveBeenCalledTimes(0);
  39. userEvent.click(screen.getByRole('button', {name: 'Dismiss'}));
  40. expect(mockCallback).toHaveBeenCalledTimes(1);
  41. });
  42. it('with internal link', () => {
  43. const to = '/organizations/sentry/issues/';
  44. render(
  45. <Tag type="highlight" to={to}>
  46. Internal link
  47. </Tag>
  48. );
  49. expect(screen.getByText('Internal link')).toBeInTheDocument();
  50. expect(screen.getByRole('link', {name: 'Internal link'})).toBeInTheDocument();
  51. expect(screen.getByRole('link', {name: 'Internal link'})).toHaveAttribute('href', to);
  52. });
  53. it('with external link', () => {
  54. const href = 'https://sentry.io/';
  55. render(
  56. <Tag type="highlight" href={href}>
  57. External link
  58. </Tag>
  59. );
  60. expect(screen.getByText('External link')).toBeInTheDocument();
  61. const link = screen.getByRole('link', {name: 'External link'});
  62. expect(link).toBeInTheDocument();
  63. expect(link).toHaveAttribute('href', href);
  64. expect(link).toHaveAttribute('target', '_blank');
  65. });
  66. it('overrides a link default icon', () => {
  67. render(
  68. <Tag href="#" icon={<IconFire data-test-id="icon-fire" />}>
  69. 3
  70. </Tag>
  71. );
  72. expect(screen.getByTestId('icon-fire')).toBeInTheDocument();
  73. });
  74. });