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, ReactWrapper, shallow } from 'enzyme';
21 import { range, times } from 'lodash';
22 import * as React from 'react';
23 import { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
24 import { getSources } from '../../../../api/components';
25 import { mockBranch, mockMainBranch } from '../../../../helpers/mocks/branch-like';
29 mockSnippetsByComponent,
32 } from '../../../../helpers/testMocks';
33 import ComponentSourceSnippetGroupViewer from '../ComponentSourceSnippetGroupViewer';
35 jest.mock('../../../../api/components', () => ({
36 getSources: jest.fn().mockResolvedValue([])
43 it('should render correctly', () => {
44 expect(shallowRender()).toMatchSnapshot();
47 it('should render correctly with secondary locations', () => {
48 // issue with secondary locations but no flows
49 const issue = mockIssue(true, {
51 textRange: { startLine: 7, endLine: 7, startOffset: 5, endOffset: 10 }
54 const snippetGroup: T.SnippetGroup = {
57 component: issue.component,
58 textRange: { startLine: 34, endLine: 34, startOffset: 0, endOffset: 0 }
61 component: issue.component,
62 textRange: { startLine: 74, endLine: 74, startOffset: 0, endOffset: 0 }
65 ...mockSnippetsByComponent(issue.component, [
71 const wrapper = shallowRender({ issue, snippetGroup });
72 expect(wrapper.state('snippets')).toHaveLength(3);
73 expect(wrapper.state('snippets')[0]).toEqual({ index: 0, start: 2, end: 16 });
74 expect(wrapper.state('snippets')[1]).toEqual({ index: 1, start: 29, end: 39 });
75 expect(wrapper.state('snippets')[2]).toEqual({ index: 2, start: 69, end: 79 });
78 it('should render correctly with flows', () => {
79 // issue with flows but no secondary locations
80 const issue = mockIssue(true, {
81 secondaryLocations: [],
82 textRange: { startLine: 7, endLine: 7, startOffset: 5, endOffset: 10 }
85 const snippetGroup: T.SnippetGroup = {
88 component: issue.component,
89 textRange: { startLine: 34, endLine: 34, startOffset: 0, endOffset: 0 }
92 component: issue.component,
93 textRange: { startLine: 74, endLine: 74, startOffset: 0, endOffset: 0 }
96 ...mockSnippetsByComponent(issue.component, [
102 const wrapper = shallowRender({ issue, snippetGroup });
103 expect(wrapper.state('snippets')).toHaveLength(2);
104 expect(wrapper.state('snippets')[0]).toEqual({ index: 0, start: 29, end: 39 });
105 expect(wrapper.state('snippets')[1]).toEqual({ index: 1, start: 69, end: 79 });
108 it('should expand block', async () => {
109 (getSources as jest.Mock).mockResolvedValueOnce(
110 Object.values(mockSnippetsByComponent('a', range(6, 59)).sources)
112 const issue = mockIssue(true, {
113 textRange: { startLine: 74, endLine: 74, startOffset: 5, endOffset: 10 }
115 const snippetGroup: T.SnippetGroup = {
119 textRange: { startLine: 74, endLine: 74, startOffset: 0, endOffset: 0 }
123 textRange: { startLine: 107, endLine: 107, startOffset: 0, endOffset: 0 }
126 ...mockSnippetsByComponent('a', [...range(69, 83), ...range(102, 112)])
129 const wrapper = shallowRender({ issue, snippetGroup });
131 wrapper.instance().expandBlock(0, 'up');
132 await waitAndUpdate(wrapper);
134 expect(getSources).toHaveBeenCalledWith({ from: 9, key: 'a', to: 68 });
135 expect(wrapper.state('snippets')).toHaveLength(2);
136 expect(wrapper.state('snippets')[0]).toEqual({ index: 0, start: 19, end: 83 });
137 expect(Object.keys(wrapper.state('additionalLines'))).toHaveLength(53);
140 it('should expand full component', async () => {
141 (getSources as jest.Mock).mockResolvedValueOnce(
142 Object.values(mockSnippetsByComponent('a', times(14)).sources)
144 const snippetGroup: T.SnippetGroup = {
148 textRange: { startLine: 3, endLine: 3, startOffset: 0, endOffset: 0 }
152 textRange: { startLine: 12, endLine: 12, startOffset: 0, endOffset: 0 }
155 ...mockSnippetsByComponent('a', [1, 2, 3, 4, 5, 10, 11, 12, 13, 14])
158 const wrapper = shallowRender({ snippetGroup });
160 wrapper.instance().expandComponent();
161 await waitAndUpdate(wrapper);
163 expect(getSources).toHaveBeenCalledWith({ key: 'a' });
164 expect(wrapper.state('snippets')).toHaveLength(1);
165 expect(wrapper.state('snippets')[0]).toEqual({ index: -1, start: 0, end: 13 });
168 it('should get the right branch when expanding', async () => {
169 (getSources as jest.Mock).mockResolvedValueOnce(
171 mockSnippetsByComponent('a', [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]).sources
174 const snippetGroup: T.SnippetGroup = {
175 locations: [mockFlowLocation()],
176 ...mockSnippetsByComponent('a', [1, 2, 3, 4, 5, 6, 7])
179 const wrapper = shallowRender({
180 branchLike: mockBranch({ name: 'asdf' }),
184 wrapper.instance().expandBlock(0, 'down');
185 await waitAndUpdate(wrapper);
187 expect(getSources).toHaveBeenCalledWith({ branch: 'asdf', from: 8, key: 'a', to: 67 });
190 it('should handle correctly open/close issue', () => {
191 const wrapper = shallowRender();
192 const sourceLine = mockSourceLine();
193 expect(wrapper.state('openIssuesByLine')).toEqual({});
194 wrapper.instance().handleOpenIssues(sourceLine);
195 expect(wrapper.state('openIssuesByLine')).toEqual({ [sourceLine.line]: true });
196 wrapper.instance().handleCloseIssues(sourceLine);
197 expect(wrapper.state('openIssuesByLine')).toEqual({ [sourceLine.line]: false });
200 it('should handle symbol highlighting', () => {
201 const wrapper = shallowRender();
202 expect(wrapper.state('highlightedSymbols')).toEqual([]);
203 wrapper.instance().handleSymbolClick(['foo']);
204 expect(wrapper.state('highlightedSymbols')).toEqual(['foo']);
205 wrapper.instance().handleSymbolClick(['foo']);
206 expect(wrapper.state('highlightedSymbols')).toEqual([]);
209 it('should correctly handle lines actions', () => {
210 const snippetGroup: T.SnippetGroup = {
214 textRange: { startLine: 34, endLine: 34, startOffset: 0, endOffset: 0 }
218 textRange: { startLine: 54, endLine: 54, startOffset: 0, endOffset: 0 }
221 ...mockSnippetsByComponent('a', [32, 33, 34, 35, 36, 52, 53, 54, 55, 56])
223 const loadDuplications = jest.fn();
224 const renderDuplicationPopup = jest.fn();
226 const wrapper = shallowRender({
228 renderDuplicationPopup,
232 const line = mockSourceLine();
234 .find('SnippetViewer')
236 .prop<Function>('loadDuplications')(line);
237 expect(loadDuplications).toHaveBeenCalledWith('a', line);
240 .find('SnippetViewer')
242 .prop<Function>('renderDuplicationPopup')(1, 13);
243 expect(renderDuplicationPopup).toHaveBeenCalledWith(
244 mockSourceViewerFile({ key: 'a', path: 'a' }),
250 describe('getNodes', () => {
251 const snippetGroup: T.SnippetGroup = {
252 component: mockSourceViewerFile(),
256 const wrapper = mount<ComponentSourceSnippetGroupViewer>(
257 <ComponentSourceSnippetGroupViewer
258 branchLike={mockMainBranch()}
259 highlightedLocationMessage={{ index: 0, text: '' }}
262 lastSnippetGroup={false}
263 loadDuplications={jest.fn()}
265 onIssueChange={jest.fn()}
266 onIssuePopupToggle={jest.fn()}
267 onLocationSelect={jest.fn()}
268 renderDuplicationPopup={jest.fn()}
270 snippetGroup={snippetGroup}
274 it('should return undefined if any node is missing', async () => {
275 await waitAndUpdate(wrapper);
276 const rootNode = wrapper.instance().rootNodeRef;
277 mockDom(rootNode.current!);
278 expect(wrapper.instance().getNodes(0)).toBeUndefined();
279 expect(wrapper.instance().getNodes(1)).toBeUndefined();
280 expect(wrapper.instance().getNodes(2)).toBeUndefined();
283 it('should return elements if dom is correct', async () => {
284 await waitAndUpdate(wrapper);
285 const rootNode = wrapper.instance().rootNodeRef;
286 mockDom(rootNode.current!);
287 expect(wrapper.instance().getNodes(3)).not.toBeUndefined();
290 it('should enable cleaning the dom', async () => {
291 await waitAndUpdate(wrapper);
292 const rootNode = wrapper.instance().rootNodeRef;
293 mockDom(rootNode.current!);
295 wrapper.instance().cleanDom(3);
296 const nodes = wrapper.instance().getNodes(3);
297 expect(nodes!.wrapper.style.maxHeight).toBe('');
298 expect(nodes!.table.style.marginTop).toBe('');
302 describe('getHeight', () => {
303 jest.useFakeTimers();
305 const snippetGroup: T.SnippetGroup = {
306 component: mockSourceViewerFile(),
310 const wrapper = mount<ComponentSourceSnippetGroupViewer>(
311 <ComponentSourceSnippetGroupViewer
312 branchLike={mockMainBranch()}
313 highlightedLocationMessage={{ index: 0, text: '' }}
316 lastSnippetGroup={false}
317 loadDuplications={jest.fn()}
319 onIssueChange={jest.fn()}
320 onIssuePopupToggle={jest.fn()}
321 onLocationSelect={jest.fn()}
322 renderDuplicationPopup={jest.fn()}
324 snippetGroup={snippetGroup}
328 it('should set maxHeight to current height', async () => {
329 await waitAndUpdate(wrapper);
331 const nodes = mockDomForSizes(wrapper, { wrapperHeight: 42, tableHeight: 68 });
332 wrapper.instance().setMaxHeight(0);
334 expect(nodes.wrapper.getAttribute('style')).toBe('max-height: 88px;');
335 expect(nodes.table.getAttribute('style')).toBeNull();
338 it('should set margin and then maxHeight for a nice upwards animation', async () => {
339 await waitAndUpdate(wrapper);
341 const nodes = mockDomForSizes(wrapper, { wrapperHeight: 42, tableHeight: 68 });
342 wrapper.instance().setMaxHeight(0, undefined, true);
344 expect(nodes.wrapper.getAttribute('style')).toBeNull();
345 expect(nodes.table.getAttribute('style')).toBe('transition: none; margin-top: -26px;');
349 expect(nodes.wrapper.getAttribute('style')).toBe('max-height: 88px;');
350 expect(nodes.table.getAttribute('style')).toBe('margin-top: 0px;');
354 function shallowRender(props: Partial<ComponentSourceSnippetGroupViewer['props']> = {}) {
355 const snippetGroup: T.SnippetGroup = {
356 component: mockSourceViewerFile(),
360 return shallow<ComponentSourceSnippetGroupViewer>(
361 <ComponentSourceSnippetGroupViewer
362 branchLike={mockMainBranch()}
363 highlightedLocationMessage={{ index: 0, text: '' }}
366 lastSnippetGroup={false}
367 loadDuplications={jest.fn()}
369 onIssueChange={jest.fn()}
370 onIssuePopupToggle={jest.fn()}
371 onLocationSelect={jest.fn()}
372 renderDuplicationPopup={jest.fn()}
374 snippetGroup={snippetGroup}
380 function mockDom(refNode: HTMLDivElement) {
381 refNode.querySelector = jest.fn(query => {
382 const index = query.split('-').pop();
388 return mount(<div />).getDOMNode();
392 <div className="snippet" />
398 <div className="snippet">
409 function mockDomForSizes(
410 componentWrapper: ReactWrapper<{}, {}, ComponentSourceSnippetGroupViewer>,
411 { wrapperHeight = 0, tableHeight = 0 }
413 const wrapper = mount(<div className="snippet" />).getDOMNode();
414 wrapper.getBoundingClientRect = jest.fn().mockReturnValue({ height: wrapperHeight });
415 const table = mount(<div />).getDOMNode();
416 table.getBoundingClientRect = jest.fn().mockReturnValue({ height: tableHeight });
417 componentWrapper.instance().getNodes = jest.fn().mockReturnValue({
421 return { wrapper, table };