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 { range, times } from 'lodash';
22 import * as React from 'react';
23 import { FormattedMessage } from 'react-intl';
24 import { getSources } from '../../../../api/components';
25 import IssueMessageBox from '../../../../components/issue/IssueMessageBox';
26 import { mockBranch, mockMainBranch } from '../../../../helpers/mocks/branch-like';
28 mockSnippetsByComponent,
31 } from '../../../../helpers/mocks/sources';
32 import { mockFlowLocation, mockIssue } from '../../../../helpers/testMocks';
33 import { waitAndUpdate } from '../../../../helpers/testUtils';
34 import { ComponentQualifier } from '../../../../types/component';
35 import { IssueStatus } from '../../../../types/issues';
36 import { SnippetGroup } from '../../../../types/types';
37 import ComponentSourceSnippetGroupViewer from '../ComponentSourceSnippetGroupViewer';
38 import SnippetViewer from '../SnippetViewer';
40 jest.mock('../../../../api/components', () => ({
41 getSources: jest.fn().mockResolvedValue([]),
48 it('should render correctly', () => {
49 expect(shallowRender()).toMatchSnapshot();
52 it('should render correctly with secondary locations', () => {
53 // issue with secondary locations but no flows
54 const issue = mockIssue(true, {
55 component: 'project:main.js',
57 textRange: { startLine: 7, endLine: 7, startOffset: 5, endOffset: 10 },
60 const snippetGroup: SnippetGroup = {
63 component: issue.component,
64 textRange: { startLine: 34, endLine: 34, startOffset: 0, endOffset: 0 },
67 component: issue.component,
68 textRange: { startLine: 74, endLine: 74, startOffset: 0, endOffset: 0 },
71 ...mockSnippetsByComponent('main.js', 'project', [
77 const wrapper = shallowRender({ issue, snippetGroup });
78 expect(wrapper.state('snippets')).toHaveLength(3);
79 expect(wrapper.state('snippets')[0]).toEqual({ index: 0, start: 2, end: 16 });
80 expect(wrapper.state('snippets')[1]).toEqual({ index: 2, start: 29, end: 39 });
81 expect(wrapper.state('snippets')[2]).toEqual({ index: 3, start: 69, end: 79 });
84 it('should render correctly with flows', () => {
85 // issue with flows but no secondary locations
86 const issue = mockIssue(true, {
87 component: 'project:main.js',
88 secondaryLocations: [],
89 textRange: { startLine: 7, endLine: 7, startOffset: 5, endOffset: 10 },
92 const snippetGroup: SnippetGroup = {
95 component: issue.component,
96 textRange: { startLine: 34, endLine: 34, startOffset: 0, endOffset: 0 },
99 component: issue.component,
100 textRange: { startLine: 74, endLine: 74, startOffset: 0, endOffset: 0 },
103 ...mockSnippetsByComponent('main.js', 'project', [
109 const wrapper = shallowRender({ issue, snippetGroup });
110 expect(wrapper.state('snippets')).toHaveLength(3);
111 expect(wrapper.state('snippets')[0]).toEqual({ index: 0, start: 2, end: 16 });
112 expect(wrapper.state('snippets')[1]).toEqual({ index: 1, start: 29, end: 39 });
113 expect(wrapper.state('snippets')[2]).toEqual({ index: 2, start: 69, end: 79 });
115 // Check that locationsByLine is defined when isLastOccurenceOfPrimaryComponent
116 expect(wrapper.find(SnippetViewer).at(0).props().locationsByLine).not.toEqual({});
118 // If not, it should be an empty object:
119 const snippets = shallowRender({
120 isLastOccurenceOfPrimaryComponent: false,
123 }).find(SnippetViewer);
125 expect(snippets.at(0).props().locationsByLine).toEqual({});
126 expect(snippets.at(1).props().locationsByLine).toEqual({});
129 it('should render file-level issue correctly', () => {
130 // issue with secondary locations and no primary location
131 const issue = mockIssue(true, {
132 component: 'project:main.js',
134 textRange: undefined,
137 const wrapper = shallowRender({
142 component: issue.component,
143 textRange: { startLine: 34, endLine: 34, startOffset: 0, endOffset: 0 },
146 ...mockSnippetsByComponent('main.js', 'project', range(29, 39)),
150 expect(wrapper.find('ContextConsumer').dive().find(IssueMessageBox).exists()).toBe(true);
154 ['file-level', ComponentQualifier.File, 'issue.closed.file_level'],
155 ['project-level', ComponentQualifier.Project, 'issue.closed.project_level'],
157 'should render a closed %s issue correctly',
158 async (_level, componentQualifier, expectedLabel) => {
159 // issue with secondary locations and no primary location
160 const issue = mockIssue(true, {
161 component: 'project:main.js',
164 textRange: undefined,
165 status: IssueStatus.Closed,
168 const wrapper = shallowRender({
172 ...mockSnippetsByComponent('main.js', 'project', range(1, 10)),
176 await waitAndUpdate(wrapper);
178 expect(wrapper.find<FormattedMessage>(FormattedMessage).prop('id')).toEqual(expectedLabel);
179 expect(wrapper.find('ContextConsumer').exists()).toBe(false);
183 it('should expand block', async () => {
184 (getSources as jest.Mock).mockResolvedValueOnce(
185 Object.values(mockSnippetsByComponent('a', 'project', range(6, 59)).sources)
187 const issue = mockIssue(true, {
188 textRange: { startLine: 74, endLine: 74, startOffset: 5, endOffset: 10 },
190 const snippetGroup: SnippetGroup = {
194 textRange: { startLine: 74, endLine: 74, startOffset: 0, endOffset: 0 },
198 textRange: { startLine: 107, endLine: 107, startOffset: 0, endOffset: 0 },
201 ...mockSnippetsByComponent('a', 'project', [...range(69, 83), ...range(102, 112)]),
204 const wrapper = shallowRender({ issue, snippetGroup });
206 wrapper.instance().expandBlock(0, 'up');
207 await waitAndUpdate(wrapper);
209 expect(getSources).toHaveBeenCalledWith({ from: 9, key: 'project:a', to: 68 });
210 expect(wrapper.state('snippets')).toHaveLength(2);
211 expect(wrapper.state('snippets')[0]).toEqual({ index: 0, start: 19, end: 83 });
212 expect(Object.keys(wrapper.state('additionalLines'))).toHaveLength(53);
215 it('should expand full component', async () => {
216 (getSources as jest.Mock).mockResolvedValueOnce(
217 Object.values(mockSnippetsByComponent('a', 'project', times(14)).sources)
219 const snippetGroup: SnippetGroup = {
223 textRange: { startLine: 3, endLine: 3, startOffset: 0, endOffset: 0 },
227 textRange: { startLine: 12, endLine: 12, startOffset: 0, endOffset: 0 },
230 ...mockSnippetsByComponent('a', 'project', [1, 2, 3, 4, 5, 10, 11, 12, 13, 14]),
233 const wrapper = shallowRender({ snippetGroup });
235 wrapper.instance().expandComponent();
236 await waitAndUpdate(wrapper);
238 expect(getSources).toHaveBeenCalledWith({ key: 'project:a' });
239 expect(wrapper.state('snippets')).toHaveLength(1);
240 expect(wrapper.state('snippets')[0]).toEqual({ index: -1, start: 0, end: 13 });
243 it('should get the right branch when expanding', async () => {
244 (getSources as jest.Mock).mockResolvedValueOnce(
246 mockSnippetsByComponent('a', 'project', [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
250 const snippetGroup: SnippetGroup = {
251 locations: [mockFlowLocation()],
252 ...mockSnippetsByComponent('a', 'project', [1, 2, 3, 4, 5, 6, 7]),
255 const wrapper = shallowRender({
256 branchLike: mockBranch({ name: 'asdf' }),
260 wrapper.instance().expandBlock(0, 'down');
261 await waitAndUpdate(wrapper);
263 expect(getSources).toHaveBeenCalledWith({ branch: 'asdf', from: 36, key: 'project:a', to: 95 });
266 it('should handle symbol highlighting', () => {
267 const wrapper = shallowRender();
268 expect(wrapper.state('highlightedSymbols')).toEqual([]);
269 wrapper.instance().handleSymbolClick(['foo']);
270 expect(wrapper.state('highlightedSymbols')).toEqual(['foo']);
271 wrapper.instance().handleSymbolClick(['foo']);
272 expect(wrapper.state('highlightedSymbols')).toEqual([]);
275 it('should correctly handle lines actions', () => {
276 const snippetGroup: SnippetGroup = {
279 component: 'my-project:foo/bar.ts',
280 textRange: { startLine: 34, endLine: 34, startOffset: 0, endOffset: 0 },
283 component: 'my-project:foo/bar.ts',
284 textRange: { startLine: 54, endLine: 54, startOffset: 0, endOffset: 0 },
287 ...mockSnippetsByComponent(
290 [32, 33, 34, 35, 36, 52, 53, 54, 55, 56]
293 const loadDuplications = jest.fn();
294 const renderDuplicationPopup = jest.fn();
296 const wrapper = shallowRender({
298 renderDuplicationPopup,
302 const line = mockSourceLine();
303 wrapper.find('SnippetViewer').first().prop<Function>('loadDuplications')(line);
304 expect(loadDuplications).toHaveBeenCalledWith('my-project:foo/bar.ts', line);
306 wrapper.find('SnippetViewer').first().prop<Function>('renderDuplicationPopup')(1, 13);
307 expect(renderDuplicationPopup).toHaveBeenCalledWith(
308 mockSourceViewerFile('foo/bar.ts', 'my-project'),
314 function shallowRender(props: Partial<ComponentSourceSnippetGroupViewer['props']> = {}) {
315 const snippetGroup: SnippetGroup = {
316 component: mockSourceViewerFile(),
320 return shallow<ComponentSourceSnippetGroupViewer>(
321 <ComponentSourceSnippetGroupViewer
322 branchLike={mockMainBranch()}
323 highlightedLocationMessage={{ index: 0, text: '' }}
324 isLastOccurenceOfPrimaryComponent={true}
327 lastSnippetGroup={false}
328 loadDuplications={jest.fn()}
330 onIssueSelect={jest.fn()}
331 onLocationSelect={jest.fn()}
332 renderDuplicationPopup={jest.fn()}
333 snippetGroup={snippetGroup}