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 { mount, shallow } from 'enzyme';
21 import { range } from 'lodash';
22 import * as React from 'react';
23 import { scrollHorizontally } from '../../../../helpers/scrolling';
24 import { mockIssue, mockSourceLine, mockSourceViewerFile } from '../../../../helpers/testMocks';
25 import SnippetViewer from '../SnippetViewer';
27 jest.mock('../../../../helpers/scrolling', () => ({
28 scrollHorizontally: jest.fn()
35 it('should render correctly', () => {
36 const snippet = range(5, 8).map(line => mockSourceLine({ line }));
37 const wrapper = shallowRender({
41 expect(wrapper).toMatchSnapshot();
44 it('should render correctly with no SCM', () => {
45 const snippet = range(5, 8).map(line => mockSourceLine({ line }));
46 const wrapper = shallowRender({
51 expect(wrapper).toMatchSnapshot();
54 it('should render additional child in line', () => {
55 const sourceline = mockSourceLine({ line: 42 });
57 const child = <div>child</div>;
58 const renderAdditionalChildInLine = jest.fn().mockReturnValue(child);
59 const wrapper = shallowRender({ renderAdditionalChildInLine, snippet: [sourceline] });
61 wrapper.instance().renderLine({
62 displayDuplications: false,
67 snippet: [sourceline],
72 expect(renderAdditionalChildInLine).toBeCalledWith(sourceline);
75 it('should render correctly when at the top of the file', () => {
76 const snippet = range(1, 8).map(line => mockSourceLine({ line }));
77 const wrapper = shallowRender({
81 expect(wrapper).toMatchSnapshot();
84 it('should render correctly when at the bottom of the file', () => {
85 const component = mockSourceViewerFile({ measures: { lines: '14' } });
86 const snippet = range(10, 14).map(line => mockSourceLine({ line }));
87 const wrapper = shallowRender({
92 expect(wrapper).toMatchSnapshot();
95 it('should correctly handle expansion', () => {
96 const snippet = range(5, 8).map(line => mockSourceLine({ line }));
97 const expandBlock = jest.fn(() => Promise.resolve());
99 const wrapper = shallowRender({
106 .find('.expand-block-above button')
109 expect(expandBlock).toHaveBeenCalledWith(2, 'up');
112 .find('.expand-block-below button')
115 expect(expandBlock).toHaveBeenCalledWith(2, 'down');
118 it('should handle scrolling', () => {
119 const scroll = jest.fn();
120 const wrapper = mountRender({ scroll });
122 const element = {} as HTMLElement;
124 wrapper.instance().doScroll(element);
126 expect(scroll).toHaveBeenCalledWith(element);
128 expect(scrollHorizontally).toHaveBeenCalled();
129 expect((scrollHorizontally as jest.Mock).mock.calls[0][0]).toBe(element);
132 it('should handle scrolling to expanded row', () => {
133 const scroll = jest.fn();
134 const wrapper = mountRender({ scroll });
136 wrapper.instance().scrollToLastExpandedRow();
138 expect(scroll).toHaveBeenCalled();
141 function shallowRender(props: Partial<SnippetViewer['props']> = {}) {
142 return shallow<SnippetViewer>(
144 component={mockSourceViewerFile()}
145 duplications={undefined}
146 duplicationsByLine={undefined}
147 expandBlock={jest.fn()}
148 handleCloseIssues={jest.fn()}
149 handleOpenIssues={jest.fn()}
150 handleSymbolClick={jest.fn()}
151 highlightedLocationMessage={{ index: 0, text: '' }}
152 highlightedSymbols={[]}
156 lastSnippetOfLastGroup={false}
157 loadDuplications={jest.fn()}
160 onLocationSelect={jest.fn()}
161 openIssuesByLine={{}}
162 renderDuplicationPopup={jest.fn()}
170 function mountRender(props: Partial<SnippetViewer['props']> = {}) {
171 return mount<SnippetViewer>(
173 component={mockSourceViewerFile()}
174 duplications={undefined}
175 duplicationsByLine={undefined}
176 expandBlock={jest.fn()}
177 handleCloseIssues={jest.fn()}
178 handleOpenIssues={jest.fn()}
179 handleSymbolClick={jest.fn()}
180 highlightedLocationMessage={{ index: 0, text: '' }}
181 highlightedSymbols={[]}
185 lastSnippetOfLastGroup={false}
186 loadDuplications={jest.fn()}
189 onLocationSelect={jest.fn()}
190 openIssuesByLine={{}}
191 renderDuplicationPopup={jest.fn()}
193 snippet={[mockSourceLine()]}