From d25724fc758d900c41fec2fa36a80971b891d5cd Mon Sep 17 00:00:00 2001 From: Mathieu Suen Date: Mon, 27 Feb 2023 09:34:36 +0100 Subject: [PATCH] SONAR-18561 Add IT to document SonarLint URL access --- .../api/mocks/SecurityHotspotServiceMock.ts | 97 ++++++++++++++++--- .../__tests__/SecurityHotspotsApp-it.tsx | 31 +++++- 2 files changed, 110 insertions(+), 18 deletions(-) diff --git a/server/sonar-web/src/main/js/api/mocks/SecurityHotspotServiceMock.ts b/server/sonar-web/src/main/js/api/mocks/SecurityHotspotServiceMock.ts index d906c95bd4d..32ba90c0426 100644 --- a/server/sonar-web/src/main/js/api/mocks/SecurityHotspotServiceMock.ts +++ b/server/sonar-web/src/main/js/api/mocks/SecurityHotspotServiceMock.ts @@ -27,7 +27,6 @@ import { import { mockSourceLine } from '../../helpers/mocks/sources'; import { getStandards } from '../../helpers/security-standard'; import { mockPaging, mockRuleDetails, mockUser } from '../../helpers/testMocks'; -import { BranchParameters } from '../../types/branch-like'; import { Hotspot, HotspotAssignRequest, @@ -132,10 +131,46 @@ export default class SecurityHotspotServiceMock { }); }; - handleGetSecurityHotspotList = () => { + handleGetSecurityHotspotList = ( + hotspotKeys: string[], + data: { + projectKey: string; + branch?: string; + } + ) => { + if (data?.branch === 'b1') { + return this.reply({ + paging: mockPaging(), + hotspots: [ + mockRawHotspot({ + assignee: 'John Doe', + key: 'b1-test-1', + message: "'F' is a magic number.", + }), + mockRawHotspot({ assignee: 'John Doe', key: 'b1-test-2' }), + ].filter((h) => hotspotKeys.includes(h.key) || hotspotKeys.length === 0), + components: [ + { + key: 'guillaume-peoch-sonarsource_benflix_AYGpXq2bd8qy4i0eO9ed:index.php', + qualifier: 'FIL', + name: 'index.php', + longName: 'index.php', + path: 'index.php', + }, + { + key: 'guillaume-peoch-sonarsource_benflix_AYGpXq2bd8qy4i0eO9ed', + qualifier: 'TRK', + name: 'benflix', + longName: 'benflix', + }, + ], + }); + } return this.reply({ paging: mockPaging(), - hotspots: [mockRawHotspot({ assignee: 'John Doe' })], + hotspots: this.mockRawHotspots(false).filter( + (h) => hotspotKeys.includes(h.key) || hotspotKeys.length === 0 + ), components: [ { key: 'guillaume-peoch-sonarsource_benflix_AYGpXq2bd8qy4i0eO9ed:index.php', @@ -154,17 +189,45 @@ export default class SecurityHotspotServiceMock { }); }; - handleGetSecurityHotspots = ( - data: { - projectKey: string; - p: number; - ps: number; - status?: HotspotStatus; - resolution?: HotspotResolution; - onlyMine?: boolean; - inNewCodePeriod?: boolean; - } & BranchParameters - ) => { + handleGetSecurityHotspots = (data: { + projectKey: string; + p: number; + ps: number; + status?: HotspotStatus; + resolution?: HotspotResolution; + onlyMine?: boolean; + inNewCodePeriod?: boolean; + branch?: string; + }) => { + if (data?.branch === 'b1') { + return this.reply({ + paging: mockPaging({ pageIndex: 1, pageSize: data.ps, total: 2 }), + hotspots: [ + mockRawHotspot({ + assignee: 'John Doe', + key: 'b1-test-1', + message: "'F' is a magic number.", + }), + mockRawHotspot({ assignee: 'John Doe', key: 'b1-test-2' }), + ], + components: [ + { + key: 'guillaume-peoch-sonarsource_benflix_AYGpXq2bd8qy4i0eO9ed:index.php', + qualifier: 'FIL', + name: 'index.php', + longName: 'index.php', + path: 'index.php', + }, + { + key: 'guillaume-peoch-sonarsource_benflix_AYGpXq2bd8qy4i0eO9ed', + qualifier: 'TRK', + name: 'benflix', + longName: 'benflix', + }, + ], + }); + } + return this.reply({ paging: mockPaging({ pageIndex: 1, pageSize: data.ps, total: this.hotspots.length }), hotspots: this.mockRawHotspots(data.onlyMine), @@ -245,6 +308,12 @@ export default class SecurityHotspotServiceMock { reset = () => { this.hotspots = [ + mockHotspot({ + assignee: 'John Doe', + key: 'b1-test-1', + message: "'F' is a magic number.", + }), + mockHotspot({ assignee: 'John Doe', key: 'b1-test-2' }), mockHotspot({ key: 'test-1', status: HotspotStatus.TO_REVIEW }), mockHotspot({ key: 'test-2', diff --git a/server/sonar-web/src/main/js/apps/security-hotspots/__tests__/SecurityHotspotsApp-it.tsx b/server/sonar-web/src/main/js/apps/security-hotspots/__tests__/SecurityHotspotsApp-it.tsx index a7ba5c392ef..0ee7b5b5e74 100644 --- a/server/sonar-web/src/main/js/apps/security-hotspots/__tests__/SecurityHotspotsApp-it.tsx +++ b/server/sonar-web/src/main/js/apps/security-hotspots/__tests__/SecurityHotspotsApp-it.tsx @@ -26,10 +26,11 @@ import { byDisplayValue, byRole, byTestId, byText } from 'testing-library-select import SecurityHotspotServiceMock from '../../../api/mocks/SecurityHotspotServiceMock'; import { getSecurityHotspots, setSecurityHotspotStatus } from '../../../api/security-hotspots'; import { searchUsers } from '../../../api/users'; -import { mockMainBranch } from '../../../helpers/mocks/branch-like'; +import { mockBranch, mockMainBranch } from '../../../helpers/mocks/branch-like'; import { mockComponent } from '../../../helpers/mocks/component'; import { mockLoggedInUser } from '../../../helpers/testMocks'; import { renderAppWithComponentContext } from '../../../helpers/testReactTestingUtils'; +import { ComponentContextShape } from '../../../types/component'; import SecurityHotspotsApp from '../SecurityHotspotsApp'; jest.mock('../../../api/measures'); @@ -59,7 +60,7 @@ const ui = { changeStatus: byRole('button', { name: 'hotspots.status.change_status' }), hotspotTitle: (name: string | RegExp) => byRole('heading', { name }), hotspotStatus: byRole('heading', { name: 'status: hotspots.status_option.FIXED' }), - hotpostListTitle: byRole('heading', { name: 'hotspots.list_title.TO_REVIEW.2' }), + hotpostListTitle: byRole('heading', { name: 'hotspots.list_title.TO_REVIEW.4' }), hotspotCommentBox: byRole('textbox', { name: 'hotspots.comment.field' }), commentSubmitButton: byRole('button', { name: 'hotspots.comment.submit' }), commentEditButton: byRole('button', { name: 'issue.comment.edit' }), @@ -81,6 +82,24 @@ afterEach(() => { handler.reset(); }); +it('should navigate when comming from SonarLint', async () => { + // On main branch + const rtl = renderSecurityHotspotsApp( + 'security_hotspots?id=guillaume-peoch-sonarsource_benflix_AYGpXq2bd8qy4i0eO9ed&hotspots=test-1' + ); + + expect(await ui.hotspotTitle(/'3' is a magic number./).find()).toBeInTheDocument(); + + // On specific branch + rtl.unmount(); + renderSecurityHotspotsApp( + 'security_hotspots?id=guillaume-peoch-sonarsource_benflix_AYGpXq2bd8qy4i0eO9ed&hotspots=b1-test-1&branch=b1', + { branchLike: mockBranch({ name: 'b1' }) } + ); + + expect(await ui.hotspotTitle(/'F' is a magic number./).find()).toBeInTheDocument(); +}); + it('should be able to self-assign a hotspot', async () => { const user = userEvent.setup(); renderSecurityHotspotsApp(); @@ -242,8 +261,11 @@ it('should be able to add, edit and remove own comments', async () => { expect(screen.queryByText(`${comment} test`)).not.toBeInTheDocument(); }); -function renderSecurityHotspotsApp(navigateTo?: string) { - renderAppWithComponentContext( +function renderSecurityHotspotsApp( + navigateTo?: string, + component?: Partial +) { + return renderAppWithComponentContext( 'security_hotspots', () => } />, { @@ -261,6 +283,7 @@ function renderSecurityHotspotsApp(navigateTo?: string) { key: 'guillaume-peoch-sonarsource_benflix_AYGpXq2bd8qy4i0eO9ed', name: 'benflix', }), + ...component, } ); } -- 2.39.5