3 * Copyright (C) 2009-2019 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 * as React from 'react';
21 import { shallow } from 'enzyme';
22 import CrossComponentSourceViewerWrapper from '../CrossComponentSourceViewerWrapper';
26 mockSnippetsByComponent,
29 } from '../../../../helpers/testMocks';
30 import { waitAndUpdate } from '../../../../helpers/testUtils';
31 import { getIssueFlowSnippets } from '../../../../api/issues';
32 import { getDuplications } from '../../../../api/components';
34 jest.mock('../../../../api/issues', () => {
35 const { mockSnippetsByComponent } = require.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 issue popup', () => {
70 const wrapper = shallowRender();
72 wrapper.instance().handleIssuePopupToggle('1', 'popup1');
73 expect(wrapper.state('issuePopup')).toEqual({ issue: '1', name: 'popup1' });
76 wrapper.instance().handleIssuePopupToggle('1', 'popup1');
77 expect(wrapper.state('issuePopup')).toBeUndefined();
80 it('should handle line popup', async () => {
81 const wrapper = shallowRender();
82 await waitAndUpdate(wrapper);
84 const linePopup = { component: 'foo', index: 0, line: 16, name: 'b.tsx' };
85 wrapper.find('ComponentSourceSnippetViewer').prop<Function>('onLinePopupToggle')(linePopup);
86 expect(wrapper.state('linePopup')).toEqual(linePopup);
88 wrapper.find('ComponentSourceSnippetViewer').prop<Function>('onLinePopupToggle')(linePopup);
89 expect(wrapper.state('linePopup')).toEqual(undefined);
91 const openLinePopup = { ...linePopup, open: true };
92 wrapper.find('ComponentSourceSnippetViewer').prop<Function>('onLinePopupToggle')(openLinePopup);
93 wrapper.find('ComponentSourceSnippetViewer').prop<Function>('onLinePopupToggle')(openLinePopup);
94 expect(wrapper.state('linePopup')).toEqual(linePopup);
97 it('should handle duplication popup', async () => {
98 const files = { b: { key: 'b', name: 'B.tsx', project: 'foo', projectName: 'Foo' } };
99 const duplications = [{ blocks: [{ _ref: '1', from: 1, size: 2 }] }];
100 (getDuplications as jest.Mock).mockResolvedValueOnce({ duplications, files });
102 const wrapper = shallowRender();
103 await waitAndUpdate(wrapper);
105 wrapper.find('ComponentSourceSnippetViewer').prop<Function>('loadDuplications')(
110 await waitAndUpdate(wrapper);
111 expect(getDuplications).toHaveBeenCalledWith({ key: 'foo' });
112 expect(wrapper.state('duplicatedFiles')).toEqual(files);
113 expect(wrapper.state('duplications')).toEqual(duplications);
114 expect(wrapper.state('duplicationsByLine')).toEqual({ '1': [0], '2': [0] });
115 expect(wrapper.state('linePopup')).toEqual({
123 wrapper.find('ComponentSourceSnippetViewer').prop<Function>('renderDuplicationPopup')(
124 mockSourceViewerFile(),
131 function shallowRender(props: Partial<CrossComponentSourceViewerWrapper['props']> = {}) {
132 return shallow<CrossComponentSourceViewerWrapper>(
133 <CrossComponentSourceViewerWrapper
134 branchLike={undefined}
135 highlightedLocationMessage={undefined}
136 issue={mockIssue(true, { key: '1' })}
138 locations={[mockFlowLocation()]}
139 onIssueChange={jest.fn()}
141 onLocationSelect={jest.fn()}
143 selectedFlowIndex={0}