3 * Copyright (C) 2009-2020 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 { mount, shallow } from 'enzyme';
21 import { range } from 'lodash';
22 import * as React from 'react';
23 import { scrollHorizontally } from 'sonar-ui-common/helpers/scrolling';
24 import { mockMainBranch } from '../../../../helpers/mocks/branch-like';
25 import { mockIssue, mockSourceLine, mockSourceViewerFile } from '../../../../helpers/testMocks';
26 import SnippetViewer from '../SnippetViewer';
28 jest.mock('sonar-ui-common/helpers/scrolling', () => ({
29 scrollHorizontally: jest.fn()
36 it('should render correctly', () => {
37 const snippet = range(5, 8).map(line => mockSourceLine({ line }));
38 const wrapper = shallowRender({
42 expect(wrapper).toMatchSnapshot();
45 it('should render correctly with no SCM', () => {
46 const snippet = range(5, 8).map(line => mockSourceLine({ line }));
47 const wrapper = shallowRender({
52 expect(wrapper).toMatchSnapshot();
55 it('should render correctly when at the top of the file', () => {
56 const snippet = range(1, 8).map(line => mockSourceLine({ line }));
57 const wrapper = shallowRender({
61 expect(wrapper).toMatchSnapshot();
64 it('should render correctly when at the bottom of the file', () => {
65 const component = mockSourceViewerFile({ measures: { lines: '14' } });
66 const snippet = range(10, 14).map(line => mockSourceLine({ line }));
67 const wrapper = shallowRender({
72 expect(wrapper).toMatchSnapshot();
75 it('should correctly handle expansion', () => {
76 const snippet = range(5, 8).map(line => mockSourceLine({ line }));
77 const expandBlock = jest.fn(() => Promise.resolve());
79 const wrapper = shallowRender({
86 .find('.expand-block-above button')
89 expect(expandBlock).toHaveBeenCalledWith(2, 'up');
92 .find('.expand-block-below button')
95 expect(expandBlock).toHaveBeenCalledWith(2, 'down');
98 it('should handle scrolling', () => {
99 const scroll = jest.fn();
100 const wrapper = mountRender({ scroll });
102 const element = {} as HTMLElement;
104 wrapper.instance().doScroll(element);
106 expect(scroll).toHaveBeenCalledWith(element);
108 expect(scrollHorizontally).toHaveBeenCalled();
109 expect((scrollHorizontally as jest.Mock).mock.calls[0][0]).toBe(element);
112 it('should handle scrolling to expanded row', () => {
113 const scroll = jest.fn();
114 const wrapper = mountRender({ scroll });
116 wrapper.instance().scrollToLastExpandedRow();
118 expect(scroll).toHaveBeenCalled();
121 function shallowRender(props: Partial<SnippetViewer['props']> = {}) {
122 return shallow<SnippetViewer>(
124 branchLike={mockMainBranch()}
125 component={mockSourceViewerFile()}
126 duplications={undefined}
127 duplicationsByLine={undefined}
128 expandBlock={jest.fn()}
129 handleCloseIssues={jest.fn()}
130 handleLinePopupToggle={jest.fn()}
131 handleOpenIssues={jest.fn()}
132 handleSymbolClick={jest.fn()}
133 highlightedLocationMessage={{ index: 0, text: '' }}
134 highlightedSymbols={[]}
138 lastSnippetOfLastGroup={false}
139 linePopup={undefined}
140 loadDuplications={jest.fn()}
143 onIssueChange={jest.fn()}
144 onIssuePopupToggle={jest.fn()}
145 onLocationSelect={jest.fn()}
146 openIssuesByLine={{}}
147 renderDuplicationPopup={jest.fn()}
155 function mountRender(props: Partial<SnippetViewer['props']> = {}) {
156 return mount<SnippetViewer>(
158 branchLike={mockMainBranch()}
159 component={mockSourceViewerFile()}
160 duplications={undefined}
161 duplicationsByLine={undefined}
162 expandBlock={jest.fn()}
163 handleCloseIssues={jest.fn()}
164 handleLinePopupToggle={jest.fn()}
165 handleOpenIssues={jest.fn()}
166 handleSymbolClick={jest.fn()}
167 highlightedLocationMessage={{ index: 0, text: '' }}
168 highlightedSymbols={[]}
172 lastSnippetOfLastGroup={false}
173 linePopup={undefined}
174 loadDuplications={jest.fn()}
177 onIssueChange={jest.fn()}
178 onIssuePopupToggle={jest.fn()}
179 onLocationSelect={jest.fn()}
180 openIssuesByLine={{}}
181 renderDuplicationPopup={jest.fn()}
183 snippet={[mockSourceLine()]}