123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /*
- * SonarQube
- * Copyright (C) 2009-2022 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- import { shallow } from 'enzyme';
- import { range } from 'lodash';
- import * as React from 'react';
- import { getSources } from '../../../../api/components';
- import { mockBranch } from '../../../../helpers/mocks/branch-like';
- import { mockComponent } from '../../../../helpers/mocks/component';
- import { mockHotspot, mockHotspotComponent } from '../../../../helpers/mocks/security-hotspots';
- import { mockSourceLine } from '../../../../helpers/mocks/sources';
- import { mockFlowLocation } from '../../../../helpers/testMocks';
- import { waitAndUpdate } from '../../../../helpers/testUtils';
- import { ComponentQualifier } from '../../../../types/component';
- import HotspotSnippetContainer from '../HotspotSnippetContainer';
- import HotspotSnippetContainerRenderer from '../HotspotSnippetContainerRenderer';
-
- jest.mock('../../../../api/components', () => ({
- getSources: jest.fn().mockResolvedValue([])
- }));
-
- jest.mock('../../../../helpers/scrolling', () => ({
- scrollToElement: jest.fn()
- }));
-
- beforeEach(() => jest.clearAllMocks());
-
- const branch = mockBranch();
-
- it('should render correctly', () => {
- expect(shallowRender()).toMatchSnapshot();
- });
-
- it('should load sources on mount', async () => {
- (getSources as jest.Mock).mockResolvedValueOnce(
- range(1, 25).map(line => mockSourceLine({ line }))
- );
-
- const hotspot = mockHotspot({
- project: mockHotspotComponent({ branch: branch.name, qualifier: ComponentQualifier.Project }),
- textRange: { startLine: 10, endLine: 11, startOffset: 0, endOffset: 12 },
- flows: [
- {
- locations: [
- mockFlowLocation({
- textRange: { startLine: 8, endLine: 8, startOffset: 0, endOffset: 1 }
- }),
- mockFlowLocation({
- textRange: { startLine: 13, endLine: 13, startOffset: 0, endOffset: 1 }
- })
- ]
- }
- ]
- });
-
- const wrapper = shallowRender({ hotspot });
-
- await waitAndUpdate(wrapper);
-
- expect(getSources).toBeCalledWith(
- expect.objectContaining({
- key: hotspot.component.key,
- branch: branch.name,
- from: 1,
- to: 24
- })
- );
- expect(wrapper.state().lastLine).toBeUndefined();
- expect(wrapper.state().sourceLines).toHaveLength(23);
- });
-
- it('should handle load sources failure', async () => {
- (getSources as jest.Mock).mockRejectedValueOnce(null);
-
- const wrapper = shallowRender();
-
- await waitAndUpdate(wrapper);
-
- expect(getSources).toHaveBeenCalled();
- expect(wrapper.state().loading).toBe(false);
- expect(wrapper.state().lastLine).toBeUndefined();
- expect(wrapper.state().sourceLines).toHaveLength(0);
- });
-
- it('should not load sources on mount when the hotspot is not associated to any loc', async () => {
- const hotspot = mockHotspot({
- line: undefined,
- textRange: undefined
- });
-
- const wrapper = shallowRender({ hotspot });
-
- await waitAndUpdate(wrapper);
-
- expect(getSources).not.toBeCalled();
- expect(wrapper.state().lastLine).toBeUndefined();
- expect(wrapper.state().sourceLines).toHaveLength(0);
- });
-
- it('should handle end-of-file on mount', async () => {
- (getSources as jest.Mock).mockResolvedValueOnce(
- range(5, 15).map(line => mockSourceLine({ line }))
- );
-
- const hotspot = mockHotspot({
- textRange: { startLine: 10, endLine: 11, startOffset: 0, endOffset: 12 }
- });
-
- const wrapper = shallowRender({ hotspot });
-
- await waitAndUpdate(wrapper);
-
- expect(getSources).toBeCalled();
- expect(wrapper.state().lastLine).toBe(14);
- expect(wrapper.state().sourceLines).toHaveLength(10);
- });
-
- describe('Expansion', () => {
- beforeEach(() => {
- (getSources as jest.Mock).mockResolvedValueOnce(
- range(10, 32).map(line => mockSourceLine({ line }))
- );
- });
-
- const hotspot = mockHotspot({
- project: mockHotspotComponent({ branch: branch.name, qualifier: ComponentQualifier.Project }),
- textRange: { startLine: 20, endLine: 21, startOffset: 0, endOffset: 12 }
- });
-
- it('up should work', async () => {
- (getSources as jest.Mock).mockResolvedValueOnce(
- range(1, 10).map(line => mockSourceLine({ line }))
- );
-
- const wrapper = shallowRender({ hotspot });
- await waitAndUpdate(wrapper);
-
- wrapper
- .find(HotspotSnippetContainerRenderer)
- .props()
- .onExpandBlock('up');
-
- await waitAndUpdate(wrapper);
-
- expect(getSources).toBeCalledWith(
- expect.objectContaining({
- branch: branch.name
- })
- );
- expect(wrapper.state().sourceLines).toHaveLength(31);
- });
-
- it('down should work', async () => {
- (getSources as jest.Mock).mockResolvedValueOnce(
- // lastLine + expand + extra for EOF check + range end is excluded
- // 31 + 50 + 1 + 1
- range(32, 83).map(line => mockSourceLine({ line }))
- );
-
- const wrapper = shallowRender({ hotspot });
- await waitAndUpdate(wrapper);
-
- wrapper
- .find(HotspotSnippetContainerRenderer)
- .props()
- .onExpandBlock('down');
-
- await waitAndUpdate(wrapper);
-
- expect(wrapper.state().lastLine).toBeUndefined();
- expect(wrapper.state().sourceLines).toHaveLength(72);
- });
-
- it('down should work and handle EOF', async () => {
- (getSources as jest.Mock).mockResolvedValueOnce(
- // lastLine + expand + extra for EOF check + range end is excluded - 1 to trigger end-of-file
- // 26 + 50 + 1 + 1 - 1
- range(27, 77).map(line => mockSourceLine({ line }))
- );
-
- const wrapper = shallowRender({ hotspot });
- await waitAndUpdate(wrapper);
-
- wrapper
- .find(HotspotSnippetContainerRenderer)
- .props()
- .onExpandBlock('down');
-
- await waitAndUpdate(wrapper);
-
- expect(wrapper.state().lastLine).toBe(76);
- expect(wrapper.state().sourceLines).toHaveLength(72);
- });
- });
-
- it('should handle symbol click', () => {
- const wrapper = shallowRender();
- const symbols = ['symbol'];
- wrapper
- .find(HotspotSnippetContainerRenderer)
- .props()
- .onSymbolClick(symbols);
- expect(wrapper.state().highlightedSymbols).toBe(symbols);
- });
-
- function shallowRender(props?: Partial<HotspotSnippetContainer['props']>) {
- return shallow<HotspotSnippetContainer>(
- <HotspotSnippetContainer
- branchLike={branch}
- component={mockComponent()}
- hotspot={mockHotspot()}
- onCommentButtonClick={jest.fn()}
- onLocationSelect={jest.fn()}
- {...props}
- />
- );
- }
|