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';
27 mockSnippetsByComponent,
30 } from '../../../../helpers/testMocks';
31 import { waitAndUpdate } from '../../../../helpers/testUtils';
32 import CrossComponentSourceViewerWrapper from '../CrossComponentSourceViewerWrapper';
34 jest.mock('../../../../api/issues', () => {
35 const { mockSnippetsByComponent } = jest.requireActual('../../../../helpers/testMocks');
37 getIssueFlowSnippets: jest.fn().mockResolvedValue({ 'main.js': mockSnippetsByComponent() })
41 jest.mock('../../../../api/components', () => ({
42 getDuplications: jest.fn().mockResolvedValue({})
49 it('should render correctly', async () => {
50 const wrapper = shallowRender();
51 expect(wrapper).toMatchSnapshot();
53 await waitAndUpdate(wrapper);
54 expect(wrapper).toMatchSnapshot();
57 it('Should fetch data', async () => {
58 const wrapper = shallowRender();
59 wrapper.instance().fetchIssueFlowSnippets('124');
60 await waitAndUpdate(wrapper);
61 expect(getIssueFlowSnippets).toHaveBeenCalledWith('1');
62 expect(wrapper.state('components')).toEqual({ 'main.js': mockSnippetsByComponent() });
64 (getIssueFlowSnippets as jest.Mock).mockClear();
65 wrapper.setProps({ issue: mockIssue(true, { key: 'foo' }) });
66 expect(getIssueFlowSnippets).toBeCalledWith('foo');
69 it('Should handle no access rights', async () => {
70 (getIssueFlowSnippets as jest.Mock).mockRejectedValueOnce({ status: 403 });
72 const wrapper = shallowRender();
73 await waitAndUpdate(wrapper);
75 expect(wrapper.state().notAccessible).toBe(true);
76 expect(wrapper).toMatchSnapshot();
79 it('should handle issue popup', () => {
80 const wrapper = shallowRender();
82 wrapper.instance().handleIssuePopupToggle('1', 'popup1');
83 expect(wrapper.state('issuePopup')).toEqual({ issue: '1', name: 'popup1' });
86 wrapper.instance().handleIssuePopupToggle('1', 'popup1');
87 expect(wrapper.state('issuePopup')).toBeUndefined();
90 it('should handle duplication popup', async () => {
91 const files = { b: { key: 'b', name: 'B.tsx', project: 'foo', projectName: 'Foo' } };
92 const duplications = [{ blocks: [{ _ref: '1', from: 1, size: 2 }] }];
93 (getDuplications as jest.Mock).mockResolvedValueOnce({ duplications, files });
95 const wrapper = shallowRender();
96 await waitAndUpdate(wrapper);
98 wrapper.find('ComponentSourceSnippetGroupViewer').prop<Function>('loadDuplications')(
103 await waitAndUpdate(wrapper);
104 expect(getDuplications).toHaveBeenCalledWith({ key: 'foo' });
105 expect(wrapper.state('duplicatedFiles')).toEqual(files);
106 expect(wrapper.state('duplications')).toEqual(duplications);
107 expect(wrapper.state('duplicationsByLine')).toEqual({ '1': [0], '2': [0] });
110 wrapper.find('ComponentSourceSnippetGroupViewer').prop<Function>('renderDuplicationPopup')(
111 mockSourceViewerFile(),
118 function shallowRender(props: Partial<CrossComponentSourceViewerWrapper['props']> = {}) {
119 return shallow<CrossComponentSourceViewerWrapper>(
120 <CrossComponentSourceViewerWrapper
121 branchLike={undefined}
122 highlightedLocationMessage={undefined}
123 issue={mockIssue(true, { key: '1' })}
125 locations={[mockFlowLocation()]}
126 onIssueChange={jest.fn()}
128 onLocationSelect={jest.fn()}
130 selectedFlowIndex={0}