From 63a3a432ef8a6127aa690b2bda2abab3c0ff82a5 Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Mon, 20 May 2019 11:43:38 +0200 Subject: [PATCH] SONAR-12116 Add branch parameter when expanding a snippet --- .../ComponentSourceSnippetViewer.tsx | 13 +++++++--- .../ComponentSourceSnippetViewer-test.tsx | 25 ++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/ComponentSourceSnippetViewer.tsx b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/ComponentSourceSnippetViewer.tsx index a58e208b876..fe4b99a31fd 100644 --- a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/ComponentSourceSnippetViewer.tsx +++ b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/ComponentSourceSnippetViewer.tsx @@ -40,6 +40,7 @@ import { optimizeSelectedIssue } from '../../../components/SourceViewer/helpers/lines'; import { translate } from '../../../helpers/l10n'; +import { getBranchLikeQuery } from '../../../helpers/branches'; interface Props { branchLike: T.BranchLike | undefined; @@ -103,6 +104,8 @@ export default class ComponentSourceSnippetViewer extends React.PureComponent { + const { branchLike, snippetGroup } = this.props; + const { key } = snippetGroup.component; const { snippets } = this.state; const snippet = snippets[snippetIndex]; @@ -122,8 +125,9 @@ export default class ComponentSourceSnippetViewer extends React.PureComponent lines.reduce((lineMap: T.Dict, line) => { @@ -155,11 +159,12 @@ export default class ComponentSourceSnippetViewer extends React.PureComponent { - const { key } = this.props.snippetGroup.component; + const { branchLike, snippetGroup } = this.props; + const { key } = snippetGroup.component; this.setState({ loading: true }); - getSources({ key }).then( + getSources({ key, ...getBranchLikeQuery(branchLike) }).then( lines => { if (this.mounted) { this.setState({ loading: false, snippets: [lines] }); diff --git a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/ComponentSourceSnippetViewer-test.tsx b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/ComponentSourceSnippetViewer-test.tsx index 6b194e14452..6ec7748b8e2 100644 --- a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/ComponentSourceSnippetViewer-test.tsx +++ b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/ComponentSourceSnippetViewer-test.tsx @@ -27,7 +27,8 @@ import { mockSourceViewerFile, mockFlowLocation, mockSnippetsByComponent, - mockSourceLine + mockSourceLine, + mockShortLivingBranch } from '../../../../helpers/testMocks'; import { waitAndUpdate } from '../../../../helpers/testUtils'; import { getSources } from '../../../../api/components'; @@ -101,6 +102,28 @@ it('should expand full component', async () => { expect(wrapper.state('snippets')[0]).toHaveLength(14); }); +it.only('should get the right branch when expanding', async () => { + (getSources as jest.Mock).mockResolvedValueOnce( + Object.values( + mockSnippetsByComponent('a', [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]).sources + ) + ); + const snippetGroup: T.SnippetGroup = { + locations: [mockFlowLocation()], + ...mockSnippetsByComponent('a', [1, 2, 3, 4]) + }; + + const wrapper = shallowRender({ + branchLike: mockShortLivingBranch({ name: 'asdf' }), + snippetGroup + }); + + wrapper.instance().expandBlock(0, 'down'); + await waitAndUpdate(wrapper); + + expect(getSources).toHaveBeenCalledWith({ branch: 'asdf', from: 5, key: 'a', to: 17 }); +}); + it('should handle correctly open/close issue', () => { const wrapper = shallowRender(); const sourceLine = mockSourceLine(); -- 2.39.5