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 { Button, EditButton } from '../../../../components/controls/buttons';
23 import Dropdown, { DropdownOverlay } from '../../../../components/controls/Dropdown';
24 import Toggler from '../../../../components/controls/Toggler';
25 import { mockIssueChangelog } from '../../../../helpers/mocks/issues';
26 import { mockHotspot, mockHotspotComment } from '../../../../helpers/mocks/security-hotspots';
27 import { mockUser } from '../../../../helpers/testMocks';
28 import HotspotCommentPopup from '../HotspotCommentPopup';
29 import HotspotReviewHistory, { HotspotReviewHistoryProps } from '../HotspotReviewHistory';
31 it('should render correctly', () => {
32 expect(shallowRender()).toMatchSnapshot('default');
33 expect(shallowRender({ showFullHistory: true })).toMatchSnapshot('show full list');
35 shallowRender({ showFullHistory: true })
38 ).toMatchSnapshot('edit comment overlay');
40 shallowRender({ showFullHistory: true })
43 ).toMatchSnapshot('delete comment overlay');
46 it('should correctly handle comment updating', () => {
47 return new Promise<void>((resolve, reject) => {
48 const setEditedCommentKey = jest.fn();
49 jest.spyOn(React, 'useState').mockImplementationOnce(() => ['', setEditedCommentKey]);
51 const onEditComment = jest.fn();
52 const wrapper = shallowRender({ onEditComment, showFullHistory: true });
54 // Closing the Toggler sets the edited key back to an empty string.
60 expect(setEditedCommentKey).toBeCalledWith('');
62 const editOnClick = wrapper
71 // Clicking on the EditButton correctly flags the comment for editing.
73 expect(setEditedCommentKey).toHaveBeenLastCalledWith('comment-1');
75 // Cancelling an edit sets the edited key back to an empty string
76 const dropdownOverlay = shallow(
80 .props().overlay as React.ReactElement<DropdownOverlay>
83 .find(HotspotCommentPopup)
86 expect(setEditedCommentKey).toHaveBeenLastCalledWith('');
88 // Updating the comment sets the edited key back to an empty string, and calls the
89 // prop to update the comment value.
91 .find(HotspotCommentPopup)
93 .onCommentEditSubmit('comment');
94 expect(onEditComment).toHaveBeenLastCalledWith('comment-1', 'comment');
95 expect(setEditedCommentKey).toHaveBeenLastCalledWith('');
96 expect(setEditedCommentKey).toHaveBeenCalledTimes(4);
102 it('should correctly handle comment deleting', () => {
103 return new Promise<void>((resolve, reject) => {
104 const setEditedCommentKey = jest.fn();
105 jest.spyOn(React, 'useState').mockImplementationOnce(() => ['', setEditedCommentKey]);
107 const onDeleteComment = jest.fn();
108 const wrapper = shallowRender({ onDeleteComment, showFullHistory: true });
110 // Opening the deletion Dropdown sets the edited key back to an empty string.
111 const dropdownOnOpen = wrapper
115 if (!dropdownOnOpen) {
120 expect(setEditedCommentKey).toHaveBeenLastCalledWith('');
122 // Confirming deletion calls the prop to delete the comment.
123 const dropdownOverlay = shallow(
127 .props().overlay as React.ReactElement<HTMLDivElement>
129 const deleteButtonOnClick = dropdownOverlay.find(Button).props().onClick;
130 if (!deleteButtonOnClick) {
135 deleteButtonOnClick();
136 expect(onDeleteComment).toBeCalledWith('comment-1');
142 function shallowRender(props?: Partial<HotspotReviewHistoryProps>) {
144 <HotspotReviewHistory
145 hotspot={mockHotspot({
146 creationDate: '2018-09-01',
148 mockIssueChangelog(),
150 creationDate: '2018-10-12'
158 mockHotspotComment({ key: 'comment-2', user: mockUser({ name: undefined }) }),
159 mockHotspotComment({ key: 'comment-3', user: mockUser({ active: false }) }),
160 mockHotspotComment({ key: 'comment-4' }),
161 mockHotspotComment({ key: 'comment-5' })
164 onDeleteComment={jest.fn()}
165 onEditComment={jest.fn()}
166 onShowFullHistory={jest.fn()}
167 showFullHistory={false}