You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectBadges-test.tsx 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { fireEvent, screen, waitFor } from '@testing-library/react';
  21. import userEvent from '@testing-library/user-event';
  22. import * as React from 'react';
  23. import selectEvent from 'react-select-event';
  24. import { getProjectBadgesToken } from '../../../../api/project-badges';
  25. import { mockBranch } from '../../../../helpers/mocks/branch-like';
  26. import { mockComponent } from '../../../../helpers/mocks/component';
  27. import { renderComponent } from '../../../../helpers/testReactTestingUtils';
  28. import { Location } from '../../../../helpers/urls';
  29. import { ComponentQualifier } from '../../../../types/component';
  30. import { MetricKey } from '../../../../types/metrics';
  31. import ProjectBadges, { ProjectBadgesProps } from '../ProjectBadges';
  32. import { BadgeType } from '../utils';
  33. jest.mock('../../../../helpers/urls', () => ({
  34. getHostUrl: () => 'host',
  35. getPathUrlAsString: (l: Location) => l.pathname,
  36. getProjectUrl: () => ({ pathname: '/dashboard' }) as Location,
  37. }));
  38. jest.mock('../../../../api/project-badges', () => ({
  39. getProjectBadgesToken: jest.fn().mockResolvedValue('foo'),
  40. renewProjectBadgesToken: jest.fn().mockResolvedValue({}),
  41. }));
  42. jest.mock('../../../../api/web-api', () => ({
  43. fetchWebApi: jest.fn().mockResolvedValue([
  44. {
  45. path: 'api/project_badges',
  46. actions: [
  47. {
  48. key: 'measure',
  49. // eslint-disable-next-line local-rules/use-metrickey-enum
  50. params: [{ key: 'metric', possibleValues: ['alert_status', 'coverage', 'bugs'] }],
  51. },
  52. ],
  53. },
  54. ]),
  55. }));
  56. it('should renew token', async () => {
  57. const user = userEvent.setup();
  58. jest.mocked(getProjectBadgesToken).mockResolvedValueOnce('foo').mockResolvedValueOnce('bar');
  59. renderProjectBadges();
  60. await appLoaded();
  61. expect(screen.getByAltText(`overview.badges.${BadgeType.measure}.alt`)).toHaveAttribute(
  62. 'src',
  63. `host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=${MetricKey.alert_status}&token=foo`,
  64. );
  65. await user.click(screen.getByText('overview.badges.renew'));
  66. expect(
  67. await screen.findByAltText(`overview.badges.${BadgeType.qualityGate}.alt`),
  68. ).toHaveAttribute(
  69. 'src',
  70. 'host/api/project_badges/quality_gate?branch=branch-6.7&project=my-project&token=bar',
  71. );
  72. expect(screen.getByAltText(`overview.badges.${BadgeType.measure}.alt`)).toHaveAttribute(
  73. 'src',
  74. `host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=${MetricKey.alert_status}&token=bar`,
  75. );
  76. });
  77. it('should update params', async () => {
  78. renderProjectBadges();
  79. await appLoaded();
  80. expect(
  81. screen.getByText(
  82. `[![${MetricKey.alert_status}](host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=${MetricKey.alert_status}&token=foo)](/dashboard)`,
  83. ),
  84. ).toBeInTheDocument();
  85. await selectEvent.select(
  86. screen.getByLabelText('overview.badges.format'),
  87. 'overview.badges.options.formats.url',
  88. );
  89. expect(
  90. screen.getByText(
  91. `host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=${MetricKey.alert_status}&token=foo`,
  92. ),
  93. ).toBeInTheDocument();
  94. await selectEvent.openMenu(screen.getByLabelText('overview.badges.metric'));
  95. fireEvent.click(screen.getByText(`metric.${MetricKey.coverage}.name`));
  96. expect(
  97. screen.getByText(
  98. `host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=${MetricKey.coverage}&token=foo`,
  99. ),
  100. ).toBeInTheDocument();
  101. fireEvent.click(
  102. screen.getByRole('button', {
  103. name: `overview.badges.${BadgeType.qualityGate}.alt overview.badges.${BadgeType.qualityGate}.description.${ComponentQualifier.Project}`,
  104. }),
  105. );
  106. expect(
  107. screen.getByText(
  108. `host/api/project_badges/quality_gate?branch=branch-6.7&project=my-project&token=foo`,
  109. ),
  110. ).toBeInTheDocument();
  111. fireEvent.click(
  112. screen.getByRole('button', {
  113. name: `overview.badges.${BadgeType.measure}.alt overview.badges.${BadgeType.measure}.description.${ComponentQualifier.Project}`,
  114. }),
  115. );
  116. expect(
  117. screen.getByText(
  118. `host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=${MetricKey.coverage}&token=foo`,
  119. ),
  120. ).toBeInTheDocument();
  121. });
  122. it('should warn about deprecated metrics', async () => {
  123. renderProjectBadges();
  124. await appLoaded();
  125. await selectEvent.openMenu(screen.getByLabelText('overview.badges.metric'));
  126. fireEvent.click(screen.getByText(`metric.${MetricKey.bugs}.name (deprecated)`));
  127. expect(
  128. screen.getByText(
  129. `overview.badges.deprecated_badge_x_y.metric.${MetricKey.bugs}.name.qualifier.${ComponentQualifier.Project}`,
  130. ),
  131. ).toBeInTheDocument();
  132. });
  133. async function appLoaded() {
  134. await waitFor(() => expect(screen.queryByLabelText(`loading`)).not.toBeInTheDocument());
  135. }
  136. function renderProjectBadges(props: Partial<ProjectBadgesProps> = {}) {
  137. return renderComponent(
  138. <ProjectBadges
  139. branchLike={mockBranch()}
  140. component={mockComponent({ configuration: { showSettings: true } })}
  141. {...props}
  142. />,
  143. );
  144. }