]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12116 Add branch parameter when expanding a snippet
authorJeremy Davis <jeremy.davis@sonarsource.com>
Mon, 20 May 2019 09:43:38 +0000 (11:43 +0200)
committerSonarTech <sonartech@sonarsource.com>
Thu, 23 May 2019 18:21:10 +0000 (20:21 +0200)
server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/ComponentSourceSnippetViewer.tsx
server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/ComponentSourceSnippetViewer-test.tsx

index a58e208b87661dc045bba1e064368cb47b985b02..fe4b99a31fdb63b2d935b19dc869ca004759a366 100644 (file)
@@ -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<Pr
   }
 
   expandBlock = (snippetIndex: number, direction: T.ExpandDirection) => {
+    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<Pr
           };
 
     getSources({
-      key: this.props.snippetGroup.component.key,
-      ...range
+      key,
+      ...range,
+      ...getBranchLikeQuery(branchLike)
     })
       .then(lines =>
         lines.reduce((lineMap: T.Dict<T.SourceLine>, line) => {
@@ -155,11 +159,12 @@ export default class ComponentSourceSnippetViewer extends React.PureComponent<Pr
   };
 
   expandComponent = () => {
-    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] });
index 6b194e14452cee37970602c0075a3001fabb02be..6ec7748b8e2a449fecb740f5fd2caf97a1f7af69 100644 (file)
@@ -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();