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({}),
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 issue popup', () => {
88 const wrapper = shallowRender();
90 wrapper.instance().handleIssuePopupToggle('1', 'popup1');
91 expect(wrapper.state('issuePopup')).toEqual({ issue: '1', name: 'popup1' });
94 wrapper.instance().handleIssuePopupToggle('1', 'popup1');
95 expect(wrapper.state('issuePopup')).toBeUndefined();
98 it('should handle duplication popup', async () => {
99 const files = { b: { key: 'b', name: 'B.tsx', project: 'foo', projectName: 'Foo' } };
100 const duplications = [{ blocks: [{ _ref: '1', from: 1, size: 2 }] }];
101 (getDuplications as jest.Mock).mockResolvedValueOnce({ duplications, files });
103 const wrapper = shallowRender();
104 await waitAndUpdate(wrapper);
106 wrapper.find('ComponentSourceSnippetGroupViewer').prop<Function>('loadDuplications')(
111 await waitAndUpdate(wrapper);
112 expect(getDuplications).toHaveBeenCalledWith({ key: 'foo' });
113 expect(wrapper.state('duplicatedFiles')).toEqual(files);
114 expect(wrapper.state('duplications')).toEqual(duplications);
115 expect(wrapper.state('duplicationsByLine')).toEqual({ '1': [0], '2': [0] });
118 wrapper.find('ComponentSourceSnippetGroupViewer').prop<Function>('renderDuplicationPopup')(
119 mockSourceViewerFile(),
126 function shallowRender(props: Partial<CrossComponentSourceViewerWrapper['props']> = {}) {
127 return shallow<CrossComponentSourceViewerWrapper>(
128 <CrossComponentSourceViewerWrapper
129 branchLike={undefined}
130 highlightedLocationMessage={undefined}
131 issue={mockIssue(true, { key: '1' })}
133 locations={[mockFlowLocation()]}
134 onIssueChange={jest.fn()}
136 onLocationSelect={jest.fn()}
138 selectedFlowIndex={0}