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 { getDuplications } from '../../../../api/components';
23 import { getIssueFlowSnippets } from '../../../../api/issues';
25 mockSnippetsByComponent,
28 } from '../../../../helpers/mocks/sources';
29 import { mockFlowLocation, mockIssue } from '../../../../helpers/testMocks';
30 import { waitAndUpdate } from '../../../../helpers/testUtils';
31 import ComponentSourceSnippetGroupViewer from '../ComponentSourceSnippetGroupViewer';
32 import CrossComponentSourceViewer from '../CrossComponentSourceViewer';
34 jest.mock('../../../../api/issues', () => {
35 const { mockSnippetsByComponent } = jest.requireActual('../../../../helpers/mocks/sources');
37 getIssueFlowSnippets: jest.fn().mockResolvedValue({ 'main.js': mockSnippetsByComponent() })
41 jest.mock('../../../../api/components', () => ({
42 getDuplications: jest.fn().mockResolvedValue({}),
43 getComponentForSourceViewer: jest.fn().mockResolvedValue({})
50 it('should render correctly', async () => {
51 let wrapper = shallowRender();
52 expect(wrapper).toMatchSnapshot();
54 await waitAndUpdate(wrapper);
55 expect(wrapper).toMatchSnapshot();
57 wrapper = shallowRender({ issue: mockIssue(true, { component: 'test.js', key: 'unknown' }) });
58 await waitAndUpdate(wrapper);
60 expect(wrapper).toMatchSnapshot('no component found');
63 it('Should fetch data', async () => {
64 const wrapper = shallowRender();
65 wrapper.instance().fetchIssueFlowSnippets();
66 await waitAndUpdate(wrapper);
67 expect(getIssueFlowSnippets).toHaveBeenCalledWith('1');
68 expect(wrapper.state('components')).toEqual(
69 expect.objectContaining({ 'main.js': mockSnippetsByComponent() })
72 (getIssueFlowSnippets as jest.Mock).mockClear();
73 wrapper.setProps({ issue: mockIssue(true, { key: 'foo' }) });
74 expect(getIssueFlowSnippets).toBeCalledWith('foo');
77 it('Should handle no access rights', async () => {
78 (getIssueFlowSnippets as jest.Mock).mockRejectedValueOnce({ status: 403 });
80 const wrapper = shallowRender();
81 await waitAndUpdate(wrapper);
83 expect(wrapper.state().notAccessible).toBe(true);
84 expect(wrapper).toMatchSnapshot();
87 it('should handle duplication popup', async () => {
88 const files = { b: { key: 'b', name: 'B.tsx', project: 'foo', projectName: 'Foo' } };
89 const duplications = [{ blocks: [{ _ref: '1', from: 1, size: 2 }] }];
90 (getDuplications as jest.Mock).mockResolvedValueOnce({ duplications, files });
92 const wrapper = shallowRender();
93 await waitAndUpdate(wrapper);
96 .find(ComponentSourceSnippetGroupViewer)
98 .loadDuplications('foo', mockSourceLine());
100 await waitAndUpdate(wrapper);
101 expect(getDuplications).toHaveBeenCalledWith({ key: 'foo' });
102 expect(wrapper.state('duplicatedFiles')).toEqual(files);
103 expect(wrapper.state('duplications')).toEqual(duplications);
104 expect(wrapper.state('duplicationsByLine')).toEqual({ '1': [0], '2': [0] });
108 .find(ComponentSourceSnippetGroupViewer)
110 .renderDuplicationPopup(mockSourceViewerFile(), 0, 16)
114 function shallowRender(props: Partial<CrossComponentSourceViewer['props']> = {}) {
115 return shallow<CrossComponentSourceViewer>(
116 <CrossComponentSourceViewer
117 branchLike={undefined}
118 highlightedLocationMessage={undefined}
119 issue={mockIssue(true, {
121 component: 'project:main.js',
122 textRange: { startLine: 1, endLine: 2, startOffset: 0, endOffset: 15 }
125 locations={[mockFlowLocation({ component: 'project:main.js' })]}
127 onIssueSelect={jest.fn()}
128 onLocationSelect={jest.fn()}
129 selectedFlowIndex={0}