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 React, { RefObject } from 'react';
22 import { mockMainBranch } from '../../../../helpers/mocks/branch-like';
23 import { mockComponent } from '../../../../helpers/mocks/component';
24 import { mockHotspot } from '../../../../helpers/mocks/security-hotspots';
25 import { scrollToElement } from '../../../../helpers/scrolling';
26 import { mockSourceLine, mockSourceViewerFile } from '../../../../helpers/testMocks';
27 import SnippetViewer from '../../../issues/crossComponentSourceViewer/SnippetViewer';
28 import HotspotSnippetContainerRenderer, {
30 HotspotSnippetContainerRendererProps
31 } from '../HotspotSnippetContainerRenderer';
33 jest.mock('../../../../helpers/scrolling', () => ({
34 scrollToElement: jest.fn()
38 jest.spyOn(React, 'useMemo').mockImplementationOnce(f => f());
41 it('should render correctly', () => {
42 expect(shallowRender()).toMatchSnapshot();
43 expect(shallowRender({ sourceLines: [mockSourceLine()] })).toMatchSnapshot('with sourcelines');
46 it('should render a HotspotPrimaryLocationBox', () => {
47 const wrapper = shallowRender({
48 hotspot: mockHotspot({ line: 42 }),
49 sourceLines: [mockSourceLine()]
52 const { renderAdditionalChildInLine } = wrapper.find(SnippetViewer).props();
54 expect(renderAdditionalChildInLine!(mockSourceLine({ line: 10 }))).toBeUndefined();
55 expect(renderAdditionalChildInLine!(mockSourceLine({ line: 42 }))).not.toBeUndefined();
58 it('should render correctly when secondary location is selected', () => {
59 const wrapper = shallowRender({
60 selectedHotspotLocation: 1
62 expect(wrapper).toMatchSnapshot('with selected hotspot location');
65 describe('scrolling', () => {
78 it('should scroll to element if parent is defined', () => {
79 const ref: RefObject<HTMLDivElement> = {
80 current: document.createElement('div')
83 const scrollHandler = getScrollHandler(ref);
85 const targetElement = document.createElement('div');
87 scrollHandler(targetElement);
90 expect(scrollToElement).toBeCalled();
93 it('should not scroll if parent is undefined', () => {
94 const ref: RefObject<HTMLDivElement> = {
98 const scrollHandler = getScrollHandler(ref);
100 const targetElement = document.createElement('div');
102 scrollHandler(targetElement);
105 expect(scrollToElement).not.toBeCalled();
109 function shallowRender(props?: Partial<HotspotSnippetContainerRendererProps>) {
111 <HotspotSnippetContainerRenderer
112 branchLike={mockMainBranch()}
113 highlightedSymbols={[]}
114 hotspot={mockHotspot()}
117 onCommentButtonClick={jest.fn()}
118 onExpandBlock={jest.fn()}
119 onSymbolClick={jest.fn()}
120 secondaryLocations={[]}
122 sourceViewerFile={mockSourceViewerFile()}
123 component={mockComponent()}
124 onLocationSelect={jest.fn()}