diff options
author | Wouter Admiraal <wouter.admiraal@sonarsource.com> | 2023-02-28 12:32:55 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-03-03 20:02:58 +0000 |
commit | a6c5e13026e8967d85d8f9914c801e8edfdeedbe (patch) | |
tree | 27d40b7742f804cf6f692255fa9f428f9b4b4867 /server/sonar-web/src/main/js/helpers | |
parent | 58abe872d6edb1ec16b2e78dc5d952085be64f0d (diff) | |
download | sonarqube-a6c5e13026e8967d85d8f9914c801e8edfdeedbe.tar.gz sonarqube-a6c5e13026e8967d85d8f9914c801e8edfdeedbe.zip |
SONAR-18431 Migrate activity tests to RTL
Diffstat (limited to 'server/sonar-web/src/main/js/helpers')
-rw-r--r-- | server/sonar-web/src/main/js/helpers/mocks/project-activity.ts | 22 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/helpers/testReactTestingUtils.tsx | 45 |
2 files changed, 63 insertions, 4 deletions
diff --git a/server/sonar-web/src/main/js/helpers/mocks/project-activity.ts b/server/sonar-web/src/main/js/helpers/mocks/project-activity.ts index 98d47a05bb5..c9faf55b54b 100644 --- a/server/sonar-web/src/main/js/helpers/mocks/project-activity.ts +++ b/server/sonar-web/src/main/js/helpers/mocks/project-activity.ts @@ -17,12 +17,15 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { MetricKey, MetricType } from '../../types/metrics'; import { Analysis, AnalysisEvent, HistoryItem, MeasureHistory, ParsedAnalysis, + ProjectAnalysisEventCategory, + Serie, } from '../../types/project-activity'; import { parseDate } from '../dates'; @@ -38,7 +41,7 @@ export function mockAnalysis(overrides: Partial<Analysis> = {}): Analysis { export function mockParsedAnalysis(overrides: Partial<ParsedAnalysis> = {}): ParsedAnalysis { return { - date: new Date('2017-03-01T09:37:01+0100'), + date: parseDate('2017-03-01T09:37:01+0100'), events: [], key: 'foo', projectVersion: '1.0', @@ -48,7 +51,7 @@ export function mockParsedAnalysis(overrides: Partial<ParsedAnalysis> = {}): Par export function mockAnalysisEvent(overrides: Partial<AnalysisEvent> = {}): AnalysisEvent { return { - category: 'QUALITY_GATE', + category: ProjectAnalysisEventCategory.QualityGate, key: 'E11', description: 'Lorem ipsum dolor sit amet', name: 'Lorem ipsum', @@ -74,7 +77,7 @@ export function mockAnalysisEvent(overrides: Partial<AnalysisEvent> = {}): Analy export function mockMeasureHistory(overrides: Partial<MeasureHistory> = {}): MeasureHistory { return { - metric: 'code_smells', + metric: MetricKey.code_smells, history: [ mockHistoryItem(), mockHistoryItem({ date: parseDate('2018-10-27T12:21:15+0200'), value: '1749' }), @@ -91,3 +94,16 @@ export function mockHistoryItem(overrides: Partial<HistoryItem> = {}): HistoryIt ...overrides, }; } + +export function mockSerie(overrides: Partial<Serie> = {}): Serie { + return { + data: [ + { x: parseDate('2017-04-27T08:21:32.000Z'), y: 2 }, + { x: parseDate('2017-04-30T23:06:24.000Z'), y: 2 }, + ], + name: 'foo', + translatedName: 'foo', + type: MetricType.Integer, + ...overrides, + }; +} diff --git a/server/sonar-web/src/main/js/helpers/testReactTestingUtils.tsx b/server/sonar-web/src/main/js/helpers/testReactTestingUtils.tsx index d3a32551d7b..1e132aaf840 100644 --- a/server/sonar-web/src/main/js/helpers/testReactTestingUtils.tsx +++ b/server/sonar-web/src/main/js/helpers/testReactTestingUtils.tsx @@ -17,7 +17,8 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { render, RenderResult } from '@testing-library/react'; +import { fireEvent, render, RenderResult } from '@testing-library/react'; +import { UserEvent } from '@testing-library/user-event/dist/types/setup/setup'; import { omit } from 'lodash'; import * as React from 'react'; import { HelmetProvider } from 'react-helmet-async'; @@ -202,3 +203,45 @@ function renderRoutedApp( </HelmetProvider> ); } + +/* eslint-disable testing-library/no-node-access */ +export function dateInputEvent(user: UserEvent) { + return { + async openDatePicker(element: HTMLElement) { + await user.click(element); + }, + async pickDate(element: HTMLElement, date: Date) { + await user.click(element); + + const monthSelect = + element.parentNode?.querySelector<HTMLSelectElement>('select[name="months"]'); + if (!monthSelect) { + throw new Error('Could not find the month selector of the date picker element'); + } + + const yearSelect = + element.parentNode?.querySelector<HTMLSelectElement>('select[name="years"]'); + if (!yearSelect) { + throw new Error('Could not find the year selector of the date picker element'); + } + + fireEvent.change(monthSelect, { target: { value: date.getMonth() } }); + fireEvent.change(yearSelect, { target: { value: date.getFullYear() } }); + + const dayButtons = + element.parentNode?.querySelectorAll<HTMLSelectElement>('button[name="day"]'); + if (!dayButtons) { + throw new Error('Could not find the day buttons of the date picker element'); + } + const dayButton = Array.from(dayButtons).find( + (button) => Number(button.textContent) === date.getDate() + ); + if (!dayButton) { + throw new Error(`Could not find the button for day ${date.getDate()}`); + } + + await user.click(dayButton); + }, + }; +} +/* eslint-enable testing-library/no-node-access */ |