From 44c191614e29783fccdcd19b6d670c95d17ee9df Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Thu, 7 Jan 2021 18:10:00 +0100 Subject: [PATCH] SONAR-14312 Improve hotspot update handling --- .../js/apps/security-hotspots/SecurityHotspotsApp.tsx | 6 +++--- .../components/HotspotSnippetContainerRenderer.tsx | 2 +- .../security-hotspots/components/HotspotViewer.tsx | 10 ++++------ .../components/HotspotViewerRenderer.tsx | 2 +- .../components/__tests__/HotspotViewer-test.tsx | 9 ++++----- .../HotspotSnippetContainerRenderer-test.tsx.snap | 2 ++ .../__snapshots__/HotspotViewerRenderer-test.tsx.snap | 6 ++++++ .../security-hotspots/components/status/Status.tsx | 6 +++--- .../components/status/StatusSelection.tsx | 6 +++--- 9 files changed, 27 insertions(+), 22 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/security-hotspots/SecurityHotspotsApp.tsx b/server/sonar-web/src/main/js/apps/security-hotspots/SecurityHotspotsApp.tsx index c4d7f6d76f8..f91c3d44f91 100644 --- a/server/sonar-web/src/main/js/apps/security-hotspots/SecurityHotspotsApp.tsx +++ b/server/sonar-web/src/main/js/apps/security-hotspots/SecurityHotspotsApp.tsx @@ -337,12 +337,12 @@ export class SecurityHotspotsApp extends React.PureComponent { const nextHotspot = allHotspots[Math.min(index, allHotspots.length - 1)]; - this.setState({ + this.setState(({ selectedHotspot }) => ({ hotspots: allHotspots, hotspotsPageIndex: paging.pageIndex, hotspotsTotal: paging.total, - selectedHotspot: nextHotspot - }); + selectedHotspot: selectedHotspot?.key === hotspotKey ? nextHotspot : selectedHotspot + })); }) .then(this.fetchSecurityHotspotsReviewed); }; diff --git a/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotSnippetContainerRenderer.tsx b/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotSnippetContainerRenderer.tsx index c3778956658..ab47a648f7d 100644 --- a/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotSnippetContainerRenderer.tsx +++ b/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotSnippetContainerRenderer.tsx @@ -70,7 +70,7 @@ export default function HotspotSnippetContainerRenderer( onExpand={noop} sourceViewerFile={sourceViewerFile} /> - + {sourceLines.length > 0 && ( diff --git a/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotViewer.tsx b/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotViewer.tsx index 1bebc190874..839db3bdcc5 100644 --- a/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotViewer.tsx +++ b/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotViewer.tsx @@ -79,12 +79,10 @@ export default class HotspotViewer extends React.PureComponent { .catch(() => this.mounted && this.setState({ loading: false })); }; - handleHotspotUpdate = () => { - return this.fetchHotspot().then((hotspot?: Hotspot) => { - if (hotspot) { - return this.props.onUpdateHotspot(hotspot.key); - } - }); + handleHotspotUpdate = async () => { + const { hotspotKey } = this.props; + + await this.props.onUpdateHotspot(hotspotKey); }; handleOpenComment = () => { diff --git a/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotViewerRenderer.tsx b/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotViewerRenderer.tsx index 5d685f17df5..4947981b877 100644 --- a/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotViewerRenderer.tsx +++ b/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotViewerRenderer.tsx @@ -74,7 +74,7 @@ export function HotspotViewerRenderer(props: HotspotViewerRendererProps) { ); return ( - + {hotspot && (
diff --git a/server/sonar-web/src/main/js/apps/security-hotspots/components/__tests__/HotspotViewer-test.tsx b/server/sonar-web/src/main/js/apps/security-hotspots/components/__tests__/HotspotViewer-test.tsx index 6d5601a4d01..022a4764aef 100644 --- a/server/sonar-web/src/main/js/apps/security-hotspots/components/__tests__/HotspotViewer-test.tsx +++ b/server/sonar-web/src/main/js/apps/security-hotspots/components/__tests__/HotspotViewer-test.tsx @@ -51,12 +51,11 @@ it('should render correctly', async () => { expect(getSecurityHotspotDetails).toHaveBeenCalledWith(newHotspotKey); }); -it('should update refresh hotspot on update', () => { - const wrapper = shallowRender(); - const mockGetHostpot = getSecurityHotspotDetails as jest.Mock; - mockGetHostpot.mockClear(); +it('should refresh hotspot on update', () => { + const onUpdateHotspot = jest.fn(); + const wrapper = shallowRender({ onUpdateHotspot }); wrapper.find(HotspotViewerRenderer).simulate('updateHotspot'); - expect(mockGetHostpot).toHaveBeenCalledTimes(1); + expect(onUpdateHotspot).toHaveBeenCalled(); }); it('should open comment form when scroll to comment', () => { diff --git a/server/sonar-web/src/main/js/apps/security-hotspots/components/__tests__/__snapshots__/HotspotSnippetContainerRenderer-test.tsx.snap b/server/sonar-web/src/main/js/apps/security-hotspots/components/__tests__/__snapshots__/HotspotSnippetContainerRenderer-test.tsx.snap index a5d92a6d758..6ddb9d1ee5f 100644 --- a/server/sonar-web/src/main/js/apps/security-hotspots/components/__tests__/__snapshots__/HotspotSnippetContainerRenderer-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/security-hotspots/components/__tests__/__snapshots__/HotspotSnippetContainerRenderer-test.tsx.snap @@ -42,6 +42,7 @@ exports[`should render correctly 1`] = ` } />
@@ -85,6 +86,7 @@ exports[`should render correctly: with sourcelines 1`] = ` } />
`; exports[`should render correctly: unassigned 1`] = `
void; + onStatusChange: () => Promise; } export function Status(props: StatusProps) { @@ -79,9 +79,9 @@ export function Status(props: StatusProps) { { + onStatusOptionChange={async () => { + await props.onStatusChange(); setIsOpen(false); - props.onStatusChange(); }} /> diff --git a/server/sonar-web/src/main/js/apps/security-hotspots/components/status/StatusSelection.tsx b/server/sonar-web/src/main/js/apps/security-hotspots/components/status/StatusSelection.tsx index 1a6c683aa5a..b029f33193e 100644 --- a/server/sonar-web/src/main/js/apps/security-hotspots/components/status/StatusSelection.tsx +++ b/server/sonar-web/src/main/js/apps/security-hotspots/components/status/StatusSelection.tsx @@ -30,7 +30,7 @@ import StatusSelectionRenderer from './StatusSelectionRenderer'; interface Props { hotspot: Hotspot; - onStatusOptionChange: (statusOption: HotspotStatusOption) => void; + onStatusOptionChange: (statusOption: HotspotStatusOption) => Promise; } interface State { @@ -84,9 +84,9 @@ export default class StatusSelection extends React.PureComponent { ...getStatusAndResolutionFromStatusOption(selectedStatus), comment: comment || undefined }) - .then(() => { + .then(async () => { + await this.props.onStatusOptionChange(selectedStatus); this.setState({ loading: false }); - this.props.onStatusOptionChange(selectedStatus); }) .then(() => addGlobalSuccessMessage( -- 2.39.5