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 from 'react';
22 import { ButtonLink } from '../../../../components/controls/buttons';
23 import { mockHotspot, mockHotspotRule } from '../../../../helpers/mocks/security-hotspots';
24 import { mockCurrentUser, mockLoggedInUser } from '../../../../helpers/testMocks';
25 import { RiskExposure } from '../../../../types/security-hotspots';
27 HotspotPrimaryLocationBox,
28 HotspotPrimaryLocationBoxProps
29 } from '../HotspotPrimaryLocationBox';
31 it('should render correctly', () => {
32 expect(shallowRender()).toMatchSnapshot('User logged in');
33 expect(shallowRender({ currentUser: mockCurrentUser() })).toMatchSnapshot('User not logged in ');
36 it.each([[RiskExposure.HIGH], [RiskExposure.MEDIUM], [RiskExposure.LOW]])(
37 'should indicate risk exposure: %s',
38 vulnerabilityProbability => {
39 const wrapper = shallowRender({
40 hotspot: mockHotspot({ rule: mockHotspotRule({ vulnerabilityProbability }) })
43 expect(wrapper.hasClass(`hotspot-risk-exposure-${vulnerabilityProbability}`)).toBe(true);
47 it('should handle click', () => {
48 const onCommentClick = jest.fn();
49 const wrapper = shallowRender({ onCommentClick });
51 wrapper.find(ButtonLink).simulate('click');
53 expect(onCommentClick).toBeCalled();
56 it('should scroll on load if no secondary locations selected', () => {
57 const node = document.createElement('div');
58 jest.spyOn(React, 'useRef').mockImplementationOnce(() => ({ current: node }));
59 jest.spyOn(React, 'useEffect').mockImplementationOnce(f => f());
61 const scroll = jest.fn();
62 shallowRender({ scroll });
64 expect(scroll).toBeCalled();
67 it('should not scroll on load if a secondary location is selected', () => {
68 const node = document.createElement('div');
69 jest.spyOn(React, 'useRef').mockImplementationOnce(() => ({ current: node }));
70 jest.spyOn(React, 'useEffect').mockImplementationOnce(f => f());
72 const scroll = jest.fn();
73 shallowRender({ scroll, secondaryLocationSelected: true });
75 expect(scroll).not.toBeCalled();
78 it('should not scroll on load if node is not defined', () => {
79 jest.spyOn(React, 'useRef').mockImplementationOnce(() => ({ current: undefined }));
80 jest.spyOn(React, 'useEffect').mockImplementationOnce(f => f());
82 const scroll = jest.fn();
83 shallowRender({ scroll });
85 expect(scroll).not.toBeCalled();
88 function shallowRender(props: Partial<HotspotPrimaryLocationBoxProps> = {}) {
90 <HotspotPrimaryLocationBox
91 currentUser={mockLoggedInUser()}
92 hotspot={mockHotspot()}
93 onCommentClick={jest.fn()}
95 secondaryLocationSelected={false}