3 * Copyright (C) 2009-2023 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 { screen } from '@testing-library/react';
21 import { range } from 'lodash';
22 import * as React from 'react';
23 import { byRole } from 'testing-library-selector';
24 import { mockSourceLine, mockSourceViewerFile } from '../../../../helpers/mocks/sources';
25 import { mockIssue } from '../../../../helpers/testMocks';
26 import { renderComponent } from '../../../../helpers/testReactTestingUtils';
27 import SnippetViewer from '../SnippetViewer';
29 jest.mock('../../../../helpers/scrolling', () => ({
30 scrollHorizontally: jest.fn(),
38 expandAbove: byRole('button', { name: 'source_viewer.expand_above' }),
39 expandBelow: byRole('button', { name: 'source_viewer.expand_below' }),
40 scmInfo: byRole('button', {
41 name: 'source_viewer.author_X.simon.brandhof@sonarsource.com, source_viewer.click_for_scm_info.5',
45 it('should render correctly', () => {
46 const snippet = range(5, 8).map((line) => mockSourceLine({ line }));
51 expect(ui.expandAbove.get()).toBeInTheDocument();
52 expect(ui.expandBelow.get()).toBeInTheDocument();
53 expect(ui.scmInfo.get()).toBeInTheDocument();
56 it('should render correctly when at the top of the file', () => {
57 const snippet = range(1, 8).map((line) => mockSourceLine({ line }));
62 expect(ui.expandAbove.query()).not.toBeInTheDocument();
63 expect(ui.expandBelow.get()).toBeInTheDocument();
66 it('should render correctly when at the bottom of the file', () => {
67 const component = mockSourceViewerFile('foo/bar.ts', 'my-project', { measures: { lines: '14' } });
68 const snippet = range(10, 15).map((line) => mockSourceLine({ line }));
74 expect(ui.expandAbove.get()).toBeInTheDocument();
75 expect(ui.expandBelow.query()).not.toBeInTheDocument();
78 it('should render correctly with no SCM', () => {
79 const snippet = range(5, 8).map((line) => mockSourceLine({ line }));
85 expect(ui.scmInfo.query()).not.toBeInTheDocument();
88 it('should render additional child in line', () => {
89 const sourceline = mockSourceLine({ line: 42 });
91 const child = <div data-testid="additional-child">child</div>;
92 const renderAdditionalChildInLine = jest.fn().mockReturnValue(child);
93 renderSnippetViewer({ renderAdditionalChildInLine, snippet: [sourceline] });
95 expect(screen.getByTestId('additional-child')).toBeInTheDocument();
98 function renderSnippetViewer(props: Partial<SnippetViewer['props']> = {}) {
99 return renderComponent(
101 component={mockSourceViewerFile()}
102 duplications={undefined}
103 duplicationsByLine={undefined}
104 expandBlock={jest.fn()}
105 handleSymbolClick={jest.fn()}
106 highlightedLocationMessage={{ index: 0, text: '' }}
107 highlightedSymbols={[]}
110 lastSnippetOfLastGroup={false}
111 loadDuplications={jest.fn()}
114 onLocationSelect={jest.fn()}
115 renderDuplicationPopup={jest.fn()}