From 6001a98a44f05705c07fab20c241ac301b17c2ee Mon Sep 17 00:00:00 2001 From: Wouter Admiraal Date: Tue, 6 Apr 2021 13:16:57 +0200 Subject: [PATCH] SONAR-13309 Do not jump to first issue location when only pressing ALT --- .../js/apps/issues/__tests__/actions-test.ts | 70 ++++++++++++++++++- .../src/main/js/apps/issues/actions.ts | 4 +- .../main/js/apps/issues/components/App.tsx | 3 +- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/issues/__tests__/actions-test.ts b/server/sonar-web/src/main/js/apps/issues/__tests__/actions-test.ts index bba4121229c..0d1d3c32168 100644 --- a/server/sonar-web/src/main/js/apps/issues/__tests__/actions-test.ts +++ b/server/sonar-web/src/main/js/apps/issues/__tests__/actions-test.ts @@ -18,7 +18,8 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import { mockIssue } from '../../../helpers/testMocks'; -import { selectFlow, selectLocation } from '../actions'; +import { enableLocationsNavigator, selectFlow, selectLocation } from '../actions'; +import { State } from '../components/App'; describe('selectFlow', () => { it('should select flow and enable locations navigator', () => { @@ -30,6 +31,73 @@ describe('selectFlow', () => { }); }); +describe('enableLocationsNavigator', () => { + it('should compute the correct flow index', () => { + expect( + enableLocationsNavigator({ openIssue: mockIssue(true), selectedFlowIndex: 20 } as State) + ).toEqual( + expect.objectContaining({ + locationsNavigator: true, + selectedFlowIndex: 20 + }) + ); + expect(enableLocationsNavigator({ openIssue: mockIssue(true) } as State)).toEqual( + expect.objectContaining({ + locationsNavigator: true, + selectedFlowIndex: 0 + }) + ); + expect( + enableLocationsNavigator({ openIssue: mockIssue(true, { flows: [] }) } as State) + ).toEqual( + expect.objectContaining({ + locationsNavigator: true, + selectedFlowIndex: undefined + }) + ); + }); + + it('should compute the correct selected location index', () => { + expect(enableLocationsNavigator({ openIssue: mockIssue(true) } as State)).toEqual( + expect.objectContaining({ + locationsNavigator: true, + selectedLocationIndex: undefined + }) + ); + + expect( + enableLocationsNavigator({ openIssue: mockIssue(true), selectedLocationIndex: -1 } as State) + ).toEqual( + expect.objectContaining({ + locationsNavigator: true, + selectedLocationIndex: 0 + }) + ); + + expect( + enableLocationsNavigator({ openIssue: mockIssue(true), selectedLocationIndex: 20 } as State) + ).toEqual( + expect.objectContaining({ + locationsNavigator: true, + selectedLocationIndex: 20 + }) + ); + }); + + it('should do nothing if the open issue has no secondary locations or any flows', () => { + expect(enableLocationsNavigator({ openIssue: mockIssue() } as State)).toBeNull(); + expect( + enableLocationsNavigator({ + openIssue: mockIssue(true, { flows: [], secondaryLocations: [] }) + } as State) + ).toBeNull(); + }); + + it('should do nothing if there is no open issue', () => { + expect(enableLocationsNavigator({} as State)).toBeNull(); + }); +}); + describe('selectLocation', () => { it('should select location and enable locations navigator', () => { expect(selectLocation(5)({ openIssue: mockIssue() })).toEqual({ diff --git a/server/sonar-web/src/main/js/apps/issues/actions.ts b/server/sonar-web/src/main/js/apps/issues/actions.ts index e2cae9cfa39..5458a267cc9 100644 --- a/server/sonar-web/src/main/js/apps/issues/actions.ts +++ b/server/sonar-web/src/main/js/apps/issues/actions.ts @@ -30,7 +30,7 @@ export function enableLocationsNavigator(state: State) { selectedFlowIndex, // Also reset index = -1 to 0, we don't want to start on the issue when enabling the location navigator selectedLocationIndex: - !selectedLocationIndex || selectedLocationIndex < 0 ? 0 : selectedLocationIndex + selectedLocationIndex && selectedLocationIndex < 0 ? 0 : selectedLocationIndex }; } return null; @@ -61,7 +61,7 @@ export function selectLocation(nextIndex: number) { export function selectNextLocation( state: Pick ) { - const { selectedFlowIndex, selectedLocationIndex: index, openIssue } = state; + const { selectedFlowIndex, selectedLocationIndex: index = -1, openIssue } = state; if (openIssue) { const locations = selectedFlowIndex !== undefined diff --git a/server/sonar-web/src/main/js/apps/issues/components/App.tsx b/server/sonar-web/src/main/js/apps/issues/components/App.tsx index b3d8ce3ee68..ed5155c767f 100644 --- a/server/sonar-web/src/main/js/apps/issues/components/App.tsx +++ b/server/sonar-web/src/main/js/apps/issues/components/App.tsx @@ -1124,8 +1124,7 @@ export default class App extends React.PureComponent { } render() { - const { openIssue, paging } = this.state; - const selectedIndex = this.getSelectedIndex(); + const { openIssue } = this.state; return (
-- 2.39.5