aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/design-system/src/sonar-aligned/components/__tests__/FlagMessageV2-test.tsx
blob: 5b2dfe95a961e72365a92696b041929629ee053d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { screen } from '@testing-library/react';
import { ComponentProps } from 'react';
import { IntlShape } from 'react-intl';
import { render } from '../../../helpers/testUtils';
import { FlagMessageV2, FlagMessageV2Variant } from '../FlagMessageV2';

jest.mock(
  'react-intl',
  () =>
    ({
      ...jest.requireActual('react-intl'),
      useIntl: () => ({
        formatMessage: ({ id }: { id: string }, values = {}) =>
          [id, ...Object.values(values)].join('.'),
      }),
    }) as IntlShape,
);

it.each([
  ['error', '1px solid rgb(249,112,102)'],
  ['warning', '1px solid rgb(248,205,92)'],
  ['success', '1px solid rgb(166,208,91)'],
  ['info', '1px solid rgb(143,202,234)'],
  ['recommended', '1px solid rgb(93,108,208)'],
])('should render properly for "%s" variant', (variant: FlagMessageV2Variant, color) => {
  renderFlagMessage({ variant });

  const item = screen.getByRole('status');
  expect(item).toBeInTheDocument();
  expect(item).toHaveStyle({ border: color });
});

it('should render correctly with optional props', async () => {
  const onDismiss = jest.fn();
  const { user } = renderFlagMessage({
    title: 'This is a title',
    hasIcon: false,
    onDismiss,
  });
  expect(screen.getByText('This is a title')).toBeInTheDocument();
  expect(screen.queryByRole('img')).not.toBeInTheDocument();
  await user.click(screen.getByRole('button'));
  expect(onDismiss).toHaveBeenCalled();
});

function renderFlagMessage(props: Partial<ComponentProps<typeof FlagMessageV2>> = {}) {
  return render(
    <FlagMessageV2 role="status" variant="error" {...props}>
      This is an error!
    </FlagMessageV2>,
  );
}