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 * as React from 'react';
22 import { setSecurityHotspotStatus } from '../../../../../api/security-hotspots';
23 import { mockHotspot } from '../../../../../helpers/mocks/security-hotspots';
24 import { waitAndUpdate } from '../../../../../helpers/testUtils';
25 import { HotspotStatus, HotspotStatusOption } from '../../../../../types/security-hotspots';
26 import StatusSelection from '../StatusSelection';
27 import StatusSelectionRenderer from '../StatusSelectionRenderer';
29 jest.mock('../../../../../api/security-hotspots', () => ({
30 setSecurityHotspotStatus: jest.fn()
33 it('should render correctly', () => {
34 expect(shallowRender()).toMatchSnapshot();
37 it('should properly deal with comment/status/submit events', async () => {
38 const hotspot = mockHotspot();
39 const onStatusOptionChange = jest.fn();
40 const wrapper = shallowRender({ hotspot, onStatusOptionChange });
42 const newStatusOption = HotspotStatusOption.SAFE;
44 .find(StatusSelectionRenderer)
46 .onStatusChange(newStatusOption);
47 expect(wrapper.state().selectedStatus).toBe(newStatusOption);
48 expect(wrapper.find(StatusSelectionRenderer).props().submitDisabled).toBe(false);
50 const newComment = 'TEST-COMMENT';
52 .find(StatusSelectionRenderer)
54 .onCommentChange(newComment);
55 expect(wrapper.state().comment).toBe(newComment);
57 (setSecurityHotspotStatus as jest.Mock).mockResolvedValueOnce({});
59 .find(StatusSelectionRenderer)
62 expect(setSecurityHotspotStatus).toHaveBeenCalledWith(hotspot.key, {
63 status: HotspotStatus.REVIEWED,
64 resolution: HotspotStatusOption.SAFE,
68 await waitAndUpdate(wrapper);
70 expect(onStatusOptionChange).toHaveBeenCalledWith(newStatusOption);
73 function shallowRender(props?: Partial<StatusSelection['props']>) {
74 return shallow<StatusSelection>(
75 <StatusSelection hotspot={mockHotspot()} onStatusOptionChange={jest.fn()} {...props} />