From ba329e99b6994a1753154d1db68cc9ba13ed3095 Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Tue, 7 Jan 2020 17:44:20 +0100 Subject: [PATCH] SONAR-12727 Fix initial filtering --- .../securityHotspots/SecurityHotspotsApp.tsx | 16 +++++------ .../__tests__/SecurityHotspotsApp-test.tsx | 28 ++++++++++++++++--- .../SecurityHotspotsAppRenderer-test.tsx | 2 +- .../SecurityHotspotsApp-test.tsx.snap | 2 +- .../SecurityHotspotsAppRenderer-test.tsx.snap | 2 +- .../securityHotspots/components/FilterBar.tsx | 4 +-- .../components/__tests__/FilterBar-test.tsx | 4 +-- .../src/main/js/types/security-hotspots.ts | 2 +- 8 files changed, 40 insertions(+), 20 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/securityHotspots/SecurityHotspotsApp.tsx b/server/sonar-web/src/main/js/apps/securityHotspots/SecurityHotspotsApp.tsx index 1e8f7221f4a..8af095d3ca7 100644 --- a/server/sonar-web/src/main/js/apps/securityHotspots/SecurityHotspotsApp.tsx +++ b/server/sonar-web/src/main/js/apps/securityHotspots/SecurityHotspotsApp.tsx @@ -99,7 +99,7 @@ export class SecurityHotspotsApp extends React.PureComponent { !isSameBranchLike(this.props.branchLike, previous.branchLike) || isLoggedIn(this.props.currentUser) !== isLoggedIn(previous.currentUser) || this.props.location.query.assignedToMe !== previous.location.query.assignedToMe || - this.props.location.query.newCode !== previous.location.query.newCode + this.props.location.query.sinceLeakPeriod !== previous.location.query.sinceLeakPeriod ) { this.setState(({ filters }) => ({ filters: { ...this.constructFiltersFromProps, ...filters } @@ -112,13 +112,13 @@ export class SecurityHotspotsApp extends React.PureComponent { this.mounted = false; } - constructFiltersFromProps(props: Props): Pick { + constructFiltersFromProps( + props: Props + ): Pick { return { - assignedToMe: - props.location.query.assignedToMe !== undefined - ? props.location.query.assignedToMe === 'true' - : isLoggedIn(props.currentUser), - newCode: isPullRequest(props.branchLike) || props.location.query.newCode === 'true' + assignedToMe: props.location.query.assignedToMe === 'true' && isLoggedIn(props.currentUser), + sinceLeakPeriod: + isPullRequest(props.branchLike) || props.location.query.sinceLeakPeriod === 'true' }; } @@ -180,7 +180,7 @@ export class SecurityHotspotsApp extends React.PureComponent { status, resolution, onlyMine: filters.assignedToMe, - sinceLeakPeriod: filters.newCode, + sinceLeakPeriod: filters.sinceLeakPeriod, ...getBranchLikeQuery(branchLike) }); } diff --git a/server/sonar-web/src/main/js/apps/securityHotspots/__tests__/SecurityHotspotsApp-test.tsx b/server/sonar-web/src/main/js/apps/securityHotspots/__tests__/SecurityHotspotsApp-test.tsx index 15315cd7c8f..8d42b5b6748 100644 --- a/server/sonar-web/src/main/js/apps/securityHotspots/__tests__/SecurityHotspotsApp-test.tsx +++ b/server/sonar-web/src/main/js/apps/securityHotspots/__tests__/SecurityHotspotsApp-test.tsx @@ -29,6 +29,7 @@ import { mockComponent, mockCurrentUser, mockLocation, + mockLoggedInUser, mockRouter } from '../../../helpers/testMocks'; import { @@ -143,11 +144,30 @@ it('should load data correctly when hotspot key list is forced', async () => { }); it('should set "leakperiod" filter according to context (branchlike & location query)', () => { - expect(shallowRender().state().filters.newCode).toBe(false); - expect(shallowRender({ branchLike: mockPullRequest() }).state().filters.newCode).toBe(true); + expect(shallowRender().state().filters.sinceLeakPeriod).toBe(false); + expect(shallowRender({ branchLike: mockPullRequest() }).state().filters.sinceLeakPeriod).toBe( + true + ); + expect( + shallowRender({ location: mockLocation({ query: { sinceLeakPeriod: 'true' } }) }).state() + .filters.sinceLeakPeriod + ).toBe(true); +}); + +it('should set "assigned to me" filter according to context (logged in & explicit location query)', () => { + expect(shallowRender().state().filters.assignedToMe).toBe(false); + expect( + shallowRender({ location: mockLocation({ query: { assignedToMe: 'true' } }) }).state().filters + .assignedToMe + ).toBe(false); + expect(shallowRender({ currentUser: mockLoggedInUser() }).state().filters.assignedToMe).toBe( + false + ); expect( - shallowRender({ location: mockLocation({ query: { newCode: 'true' } }) }).state().filters - .newCode + shallowRender({ + location: mockLocation({ query: { assignedToMe: 'true' } }), + currentUser: mockLoggedInUser() + }).state().filters.assignedToMe ).toBe(true); }); diff --git a/server/sonar-web/src/main/js/apps/securityHotspots/__tests__/SecurityHotspotsAppRenderer-test.tsx b/server/sonar-web/src/main/js/apps/securityHotspots/__tests__/SecurityHotspotsAppRenderer-test.tsx index 4be898d2b22..f970783dc17 100644 --- a/server/sonar-web/src/main/js/apps/securityHotspots/__tests__/SecurityHotspotsAppRenderer-test.tsx +++ b/server/sonar-web/src/main/js/apps/securityHotspots/__tests__/SecurityHotspotsAppRenderer-test.tsx @@ -67,7 +67,7 @@ function shallowRender(props: Partial = {}) { - props.onChangeFilters({ newCode: option.value }) + props.onChangeFilters({ sinceLeakPeriod: option.value }) } options={periodOptions} searchable={false} - value={filters.newCode} + value={filters.sinceLeakPeriod} /> )} diff --git a/server/sonar-web/src/main/js/apps/securityHotspots/components/__tests__/FilterBar-test.tsx b/server/sonar-web/src/main/js/apps/securityHotspots/components/__tests__/FilterBar-test.tsx index 88363cc7d03..8a7a32c1877 100644 --- a/server/sonar-web/src/main/js/apps/securityHotspots/components/__tests__/FilterBar-test.tsx +++ b/server/sonar-web/src/main/js/apps/securityHotspots/components/__tests__/FilterBar-test.tsx @@ -86,7 +86,7 @@ it('should trigger onChange for leak period', () => { return fail("Select's onChange should be defined"); } onChange({ value: true }); - expect(onChangeFilters).toBeCalledWith({ newCode: true }); + expect(onChangeFilters).toBeCalledWith({ sinceLeakPeriod: true }); }); function shallowRender(props: Partial = {}) { @@ -95,7 +95,7 @@ function shallowRender(props: Partial = {}) { currentUser={mockCurrentUser()} filters={{ assignedToMe: false, - newCode: false, + sinceLeakPeriod: false, status: HotspotStatusFilter.TO_REVIEW }} isStaticListOfHotspots={false} diff --git a/server/sonar-web/src/main/js/types/security-hotspots.ts b/server/sonar-web/src/main/js/types/security-hotspots.ts index 1c3cebe1887..db11ef566b9 100644 --- a/server/sonar-web/src/main/js/types/security-hotspots.ts +++ b/server/sonar-web/src/main/js/types/security-hotspots.ts @@ -47,7 +47,7 @@ export enum HotspotStatusOption { export interface HotspotFilters { assignedToMe: boolean; - newCode: boolean; + sinceLeakPeriod: boolean; status: HotspotStatusFilter; } -- 2.39.5