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.

DataTableModal-it.tsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 { screen } from '@testing-library/react';
  21. import { times } from 'lodash';
  22. import * as React from 'react';
  23. import { parseDate } from '../../../helpers/dates';
  24. import {
  25. mockAnalysisEvent,
  26. mockHistoryItem,
  27. mockMeasureHistory,
  28. mockParsedAnalysis,
  29. } from '../../../helpers/mocks/project-activity';
  30. import { mockMetric } from '../../../helpers/testMocks';
  31. import { renderComponent } from '../../../helpers/testReactTestingUtils';
  32. import { MetricKey } from '../../../types/metrics';
  33. import {
  34. GraphType,
  35. MeasureHistory,
  36. ProjectAnalysisEventCategory,
  37. } from '../../../types/project-activity';
  38. import { Metric } from '../../../types/types';
  39. import DataTableModal, { DataTableModalProps, MAX_DATA_TABLE_ROWS } from '../DataTableModal';
  40. import { generateSeries, getDisplayedHistoryMetrics } from '../utils';
  41. it('should render correctly if there are no series', () => {
  42. renderDataTableModal({ series: [] });
  43. expect(
  44. screen.getByText('project_activity.graphs.data_table.no_data_warning'),
  45. ).toBeInTheDocument();
  46. });
  47. it('should render correctly if there are events', () => {
  48. renderDataTableModal({
  49. analyses: [
  50. mockParsedAnalysis({
  51. date: parseDate('2016-01-01T00:00:00+0200'),
  52. events: [
  53. mockAnalysisEvent({ key: '1', category: ProjectAnalysisEventCategory.QualityGate }),
  54. ],
  55. }),
  56. ],
  57. });
  58. expect(screen.getByText('event.category.QUALITY_GATE', { exact: false })).toBeInTheDocument();
  59. });
  60. it('should render correctly if there is too much data', () => {
  61. renderDataTableModal({ series: mockSeries(MAX_DATA_TABLE_ROWS + 1) });
  62. expect(
  63. screen.getByText(`project_activity.graphs.data_table.max_lines_warning.${MAX_DATA_TABLE_ROWS}`),
  64. ).toBeInTheDocument();
  65. });
  66. it('should render correctly if there is no data and we have a start date', () => {
  67. renderDataTableModal({ graphStartDate: parseDate('3022-01-01') });
  68. expect(
  69. screen.getByText('project_activity.graphs.data_table.no_data_warning_check_dates_x', {
  70. exact: false,
  71. }),
  72. ).toBeInTheDocument();
  73. });
  74. it('should render correctly if there is no data and we have an end date', () => {
  75. renderDataTableModal({ graphEndDate: parseDate('2015-01-01') });
  76. expect(
  77. screen.getByText('project_activity.graphs.data_table.no_data_warning_check_dates_y', {
  78. exact: false,
  79. }),
  80. ).toBeInTheDocument();
  81. });
  82. it('should render correctly if there is no data and we have a date range', () => {
  83. renderDataTableModal({
  84. graphEndDate: parseDate('2015-01-01'),
  85. graphStartDate: parseDate('2014-01-01'),
  86. });
  87. expect(
  88. screen.getByText('project_activity.graphs.data_table.no_data_warning_check_dates_x_y', {
  89. exact: false,
  90. }),
  91. ).toBeInTheDocument();
  92. });
  93. function renderDataTableModal(props: Partial<DataTableModalProps> = {}) {
  94. return renderComponent(
  95. <DataTableModal analyses={[]} series={mockSeries()} onClose={jest.fn()} {...props} />,
  96. );
  97. }
  98. function mockSeries(n = 10) {
  99. const measuresHistory: MeasureHistory[] = [];
  100. const metrics: Metric[] = [];
  101. [MetricKey.violations].forEach((metric) => {
  102. const history = times(n, (i) => {
  103. const date = parseDate('2016-01-01T00:00:00+0200');
  104. date.setDate(date.getDate() + 365 * i);
  105. return mockHistoryItem({ date, value: i.toString() });
  106. });
  107. measuresHistory.push(mockMeasureHistory({ metric, history }));
  108. metrics.push(
  109. mockMetric({
  110. key: metric,
  111. name: metric,
  112. type: 'INT',
  113. }),
  114. );
  115. });
  116. return generateSeries(
  117. measuresHistory,
  118. GraphType.issues,
  119. metrics,
  120. getDisplayedHistoryMetrics(GraphType.issues, [
  121. MetricKey.bugs,
  122. MetricKey.code_smells,
  123. MetricKey.vulnerabilities,
  124. ]),
  125. );
  126. }