3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 import { shallow } from 'enzyme';
21 import * as React from 'react';
22 import { changeEvent, createEvent } from '../../../../api/projectActivity';
23 import { mockComponent } from '../../../../helpers/mocks/component';
29 } from '../../../../helpers/testMocks';
30 import { waitAndUpdate } from '../../../../helpers/testUtils';
31 import { ComponentQualifier } from '../../../../types/component';
32 import { MetricKey } from '../../../../types/metrics';
33 import { ProjectActivityAppContainer } from '../ProjectActivityAppContainer';
35 jest.mock('../../../../helpers/dates', () => ({
36 parseDate: jest.fn(date => `PARSED:${date}`)
39 jest.mock('../../../../api/time-machine', () => {
40 const { mockPaging } = jest.requireActual('../../../../helpers/testMocks');
42 getAllTimeMachineData: jest.fn().mockResolvedValue({
46 history: [{ date: '2022-01-01', value: '10' }]
49 paging: mockPaging({ total: 1 })
54 jest.mock('../../../../api/metrics', () => {
55 const { mockMetric } = jest.requireActual('../../../../helpers/testMocks');
57 getAllMetrics: jest.fn().mockResolvedValue([mockMetric()])
61 jest.mock('../../../../api/projectActivity', () => {
62 const { mockAnalysis, mockPaging } = jest.requireActual('../../../../helpers/testMocks');
64 ...jest.requireActual('../../../../api/projectActivity'),
65 createEvent: jest.fn(),
66 changeEvent: jest.fn(),
67 getProjectActivity: jest.fn().mockResolvedValue({
68 analyses: [mockAnalysis({ key: 'foo' })],
69 paging: mockPaging({ total: 1 })
74 it('should render correctly', () => {
75 expect(shallowRender()).toMatchSnapshot();
78 it('should filter metric correctly', () => {
79 const wrapper = shallowRender();
82 .filterMetrics(mockComponent({ qualifier: ComponentQualifier.Project }), [
83 mockMetric({ key: MetricKey.bugs }),
84 mockMetric({ key: MetricKey.security_review_rating })
86 expect(metrics).toHaveLength(1);
89 .filterMetrics(mockComponent({ qualifier: ComponentQualifier.Portfolio }), [
90 mockMetric({ key: MetricKey.bugs }),
91 mockMetric({ key: MetricKey.security_hotspots_reviewed })
93 expect(metrics).toHaveLength(1);
96 it('should correctly create and update custom events', async () => {
97 const analysisKey = 'foo';
99 const newName = 'baz';
100 const event = mockAnalysisEvent({ name });
101 (createEvent as jest.Mock).mockResolvedValueOnce({ analysis: analysisKey, ...event });
102 (changeEvent as jest.Mock).mockResolvedValueOnce({
103 analysis: analysisKey,
108 const wrapper = shallowRender();
109 await waitAndUpdate(wrapper);
110 const instance = wrapper.instance();
112 instance.addCustomEvent(analysisKey, name);
113 expect(createEvent).toHaveBeenCalledWith(analysisKey, name, undefined);
114 await waitAndUpdate(wrapper);
115 expect(wrapper.state().analyses[0].events[0]).toEqual(event);
117 instance.changeEvent(event.key, newName);
118 expect(changeEvent).toHaveBeenCalledWith(event.key, newName);
119 await waitAndUpdate(wrapper);
120 expect(wrapper.state().analyses[0].events[0]).toEqual({ ...event, name: newName });
123 function shallowRender(props: Partial<ProjectActivityAppContainer['props']> = {}) {
124 return shallow<ProjectActivityAppContainer>(
125 <ProjectActivityAppContainer
126 component={mockComponent({ breadcrumbs: [mockComponent()] })}
127 location={mockLocation()}
128 router={mockRouter()}