3 * Copyright (C) 2009-2023 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 { DEFAULT_GRAPH } from '../../../../components/activity-graph/utils';
23 import { parseDate } from '../../../../helpers/dates';
24 import { mockParsedAnalysis } from '../../../../helpers/mocks/project-activity';
25 import { ComponentQualifier } from '../../../../types/component';
26 import ProjectActivityAnalysesList from '../ProjectActivityAnalysesList';
28 jest.mock('date-fns', () => {
29 const actual = jest.requireActual('date-fns');
32 startOfDay: (date: Date) => {
33 const startDay = new Date(date);
34 startDay.setUTCHours(0, 0, 0, 0);
40 jest.mock('../../../../helpers/dates', () => {
41 const actual = jest.requireActual('../../../../helpers/dates');
42 return { ...actual, toShortNotSoISOString: (date: string) => 'ISO.' + date };
45 const DATE = parseDate('2016-10-27T16:33:50+0000');
47 const DEFAULT_QUERY = {
51 project: 'org.sonarsource.sonarqube:sonarqube',
54 it('should render correctly', () => {
55 expect(shallowRender()).toMatchSnapshot('default');
56 expect(shallowRender({ project: { qualifier: ComponentQualifier.Application } })).toMatchSnapshot(
59 expect(shallowRender({ analyses: [], initializing: true })).toMatchSnapshot('loading');
60 expect(shallowRender({ analyses: [] })).toMatchSnapshot('no analyses');
63 it('should correctly filter analyses by category', () => {
64 const wrapper = shallowRender();
65 wrapper.setProps({ query: { ...DEFAULT_QUERY, category: 'QUALITY_GATE' } });
66 expect(wrapper).toMatchSnapshot();
69 it('should correctly filter analyses by date range', () => {
70 const wrapper = shallowRender();
78 expect(wrapper).toMatchSnapshot();
81 it('should correctly update the selected date', () => {
82 const selectedDate = new Date();
83 const updateQuery = jest.fn();
84 const wrapper = shallowRender({ updateQuery });
85 wrapper.instance().updateSelectedDate(selectedDate);
86 expect(updateQuery).toHaveBeenCalledWith({ selectedDate });
89 it('should correctly reset scroll if filters change', () => {
90 const wrapper = shallowRender();
91 const scrollContainer = document.createElement('ul');
92 scrollContainer.scrollTop = 100;
94 // Saves us a call to mount().
95 wrapper.instance().scrollContainer = scrollContainer;
97 wrapper.setProps({ query: { ...DEFAULT_QUERY, category: 'OTHER' } });
98 expect(scrollContainer.scrollTop).toBe(0);
101 function shallowRender(props: Partial<ProjectActivityAnalysesList['props']> = {}) {
102 return shallow<ProjectActivityAnalysesList>(
103 <ProjectActivityAnalysesList
104 addCustomEvent={jest.fn().mockResolvedValue(undefined)}
105 addVersion={jest.fn().mockResolvedValue(undefined)}
110 events: [{ key: 'E1', category: 'VERSION', name: '6.5-SNAPSHOT' }],
112 mockParsedAnalysis({ key: 'A2', date: parseDate('2016-10-27T12:21:15+0000') }),
115 date: parseDate('2016-10-26T12:17:29+0000'),
117 { key: 'E2', category: 'VERSION', name: '6.4' },
118 { key: 'E3', category: 'OTHER', name: 'foo' },
123 date: parseDate('2016-10-24T16:33:50+0000'),
124 events: [{ key: 'E1', category: 'QUALITY_GATE', name: 'Quality gate changed to red...' }],
127 analysesLoading={false}
129 changeEvent={jest.fn().mockResolvedValue(undefined)}
130 deleteAnalysis={jest.fn().mockResolvedValue(undefined)}
131 deleteEvent={jest.fn().mockResolvedValue(undefined)}
133 leakPeriodDate={parseDate('2016-10-27T12:21:15+0000')}
134 project={{ qualifier: ComponentQualifier.Project }}
135 query={DEFAULT_QUERY}
136 updateQuery={jest.fn()}