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 { mount, shallow } from 'enzyme';
21 import * as React from 'react';
22 import { IntlProvider } from 'react-intl';
23 import TimeFormatter from '../../../../components/intl/TimeFormatter';
24 import { scrollToElement } from '../../../../helpers/scrolling';
25 import { mockAnalysisEvent, mockParsedAnalysis } from '../../../../helpers/testMocks';
26 import { click } from '../../../../helpers/testUtils';
27 import AddEventForm from '../forms/AddEventForm';
28 import RemoveAnalysisForm from '../forms/RemoveAnalysisForm';
29 import { ProjectActivityAnalysis, ProjectActivityAnalysisProps } from '../ProjectActivityAnalysis';
31 jest.mock('../../../../helpers/dates', () => ({
33 valueOf: () => 1546333200000,
34 toISOString: () => '2019-01-01T09:00:00.000Z'
38 jest.mock('../../../../helpers/scrolling', () => ({
39 scrollToElement: jest.fn()
42 it('should render correctly', () => {
43 expect(shallowRender()).toMatchSnapshot('default');
45 shallowRender({ analysis: mockParsedAnalysis({ events: [mockAnalysisEvent()] }) })
46 ).toMatchSnapshot('with events');
48 shallowRender({ analysis: mockParsedAnalysis({ buildString: '1.0.234' }) })
49 ).toMatchSnapshot('with build string');
50 expect(shallowRender({ isBaseline: true })).toMatchSnapshot('with baseline marker');
54 canCreateVersion: true,
55 canDeleteAnalyses: true
57 ).toMatchSnapshot('with admin options');
59 const timeFormatter = shallowRender()
62 expect(timeFormatter!('formatted_time')).toMatchSnapshot('formatted time');
65 it('should show the correct admin options', () => {
66 const wrapper = shallowRender({
68 canCreateVersion: true,
69 canDeleteAnalyses: true
72 expect(wrapper.find('.js-add-version').exists()).toBe(true);
73 click(wrapper.find('.js-add-version'));
74 const addVersionForm = wrapper.find(AddEventForm);
75 expect(addVersionForm.exists()).toBe(true);
76 addVersionForm.prop('onClose')();
77 expect(wrapper.find(AddEventForm).exists()).toBe(false);
79 expect(wrapper.find('.js-add-event').exists()).toBe(true);
80 click(wrapper.find('.js-add-event'));
81 const addEventForm = wrapper.find(AddEventForm);
82 expect(addEventForm.exists()).toBe(true);
83 addEventForm.prop('onClose')();
84 expect(wrapper.find(AddEventForm).exists()).toBe(false);
86 expect(wrapper.find('.js-delete-analysis').exists()).toBe(true);
87 click(wrapper.find('.js-delete-analysis'));
88 const removeAnalysisForm = wrapper.find(RemoveAnalysisForm);
89 expect(removeAnalysisForm.exists()).toBe(true);
90 removeAnalysisForm.prop('onClose')();
91 expect(wrapper.find(RemoveAnalysisForm).exists()).toBe(false);
94 it('should not allow the first item to be deleted', () => {
98 canCreateVersion: true,
99 canDeleteAnalyses: true,
102 .find('.js-delete-analysis')
107 it('should be clickable', () => {
108 const date = new Date('2018-03-01T09:37:01+0100');
109 const updateSelectedDate = jest.fn();
110 const wrapper = shallowRender({ analysis: mockParsedAnalysis({ date }), updateSelectedDate });
112 expect(updateSelectedDate).toBeCalledWith(date);
115 it('should trigger a scroll to itself if selected', () => {
116 mountRender({ parentScrollContainer: document.createElement('ul'), selected: true });
117 expect(scrollToElement).toBeCalled();
120 function shallowRender(props: Partial<ProjectActivityAnalysisProps> = {}) {
121 return shallow(createComponent(props));
124 function mountRender(props: Partial<ProjectActivityAnalysisProps> = {}) {
125 return mount(<IntlProvider locale="en">{createComponent(props)}</IntlProvider>);
128 function createComponent(props: Partial<ProjectActivityAnalysisProps> = {}) {
130 <ProjectActivityAnalysis
131 addCustomEvent={jest.fn()}
132 addVersion={jest.fn()}
133 analysis={mockParsedAnalysis()}
134 canCreateVersion={false}
135 changeEvent={jest.fn()}
136 deleteAnalysis={jest.fn()}
137 deleteEvent={jest.fn()}
141 updateSelectedDate={jest.fn()}