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, {
31 HotspotSnippetContainerRendererProps
32 } from '../HotspotSnippetContainerRenderer';
34 jest.mock('../../../../helpers/scrolling', () => ({
35 scrollToElement: jest.fn()
39 jest.spyOn(React, 'useMemo').mockImplementationOnce(f => f());
42 it('should render correctly', () => {
43 expect(shallowRender()).toMatchSnapshot();
44 expect(shallowRender({ sourceLines: [mockSourceLine()] })).toMatchSnapshot('with sourcelines');
47 it('should render a HotspotPrimaryLocationBox', () => {
48 const wrapper = shallowRender({
49 hotspot: mockHotspot({ line: 42 }),
50 sourceLines: [mockSourceLine()]
53 const { renderAdditionalChildInLine } = wrapper.find(SnippetViewer).props();
55 expect(renderAdditionalChildInLine!(mockSourceLine({ line: 10 }))).toBeUndefined();
56 expect(renderAdditionalChildInLine!(mockSourceLine({ line: 42 }))).not.toBeUndefined();
59 it('should render correctly when secondary location is selected', () => {
60 const wrapper = shallowRender({
61 selectedHotspotLocation: 1
63 expect(wrapper).toMatchSnapshot('with selected hotspot location');
66 describe('scrolling', () => {
79 it('should scroll to element if parent is defined', () => {
80 const ref: RefObject<HTMLDivElement> = {
81 current: document.createElement('div')
84 const scrollHandler = getScrollHandler(ref);
86 const targetElement = document.createElement('div');
88 scrollHandler(targetElement);
91 expect(scrollToElement).toBeCalled();
94 it('should not scroll if parent is undefined', () => {
95 const ref: RefObject<HTMLDivElement> = {
99 const scrollHandler = getScrollHandler(ref);
101 const targetElement = document.createElement('div');
103 scrollHandler(targetElement);
106 expect(scrollToElement).not.toBeCalled();
110 describe('expand', () => {
111 it('should work as expected', async () => {
112 jest.useFakeTimers();
113 const onExpandBlock = jest.fn().mockResolvedValue({});
115 const scrollableNode = document.createElement('div');
116 scrollableNode.scrollTo = jest.fn();
117 const ref: RefObject<HTMLDivElement> = {
118 current: scrollableNode
121 jest.spyOn(React, 'useRef').mockReturnValue(ref);
123 const snippet = document.createElement('div');
124 const table = document.createElement('table');
125 snippet.appendChild(table);
126 scrollableNode.querySelector = jest.fn().mockReturnValue(snippet);
129 .spyOn(table, 'getBoundingClientRect')
130 .mockReturnValueOnce({ height: 42 } as DOMRect)
131 .mockReturnValueOnce({ height: 99 } as DOMRect)
132 .mockReturnValueOnce({ height: 99 } as DOMRect)
133 .mockReturnValueOnce({ height: 112 } as DOMRect);
135 await animateExpansion(ref, onExpandBlock, 'up');
136 expect(onExpandBlock).toBeCalledWith('up');
138 expect(snippet.style.maxHeight).toBe('42px');
139 expect(table.style.marginTop).toBe('-57px');
141 jest.advanceTimersByTime(100);
143 expect(snippet.style.maxHeight).toBe('99px');
144 expect(table.style.marginTop).toBe('0px');
146 expect(scrollableNode.scrollTo).not.toBeCalled();
150 await animateExpansion(ref, onExpandBlock, 'down');
151 expect(onExpandBlock).toBeCalledWith('down');
152 expect(snippet.style.maxHeight).toBe('112px');
154 jest.advanceTimersByTime(250);
155 expect(scrollableNode.scrollTo).toBeCalled();
157 jest.useRealTimers();
161 function shallowRender(props?: Partial<HotspotSnippetContainerRendererProps>) {
163 <HotspotSnippetContainerRenderer
164 branchLike={mockMainBranch()}
165 highlightedSymbols={[]}
166 hotspot={mockHotspot()}
169 onCommentButtonClick={jest.fn()}
170 onExpandBlock={jest.fn()}
171 onSymbolClick={jest.fn()}
172 secondaryLocations={[]}
174 sourceViewerFile={mockSourceViewerFile()}
175 component={mockComponent()}
176 onLocationSelect={jest.fn()}