]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21056 Code page: don't show data from default branch on refresh
authorDavid Cho-Lerat <david.cho-lerat@sonarsource.com>
Tue, 5 Dec 2023 13:55:58 +0000 (14:55 +0100)
committersonartech <sonartech@sonarsource.com>
Tue, 5 Dec 2023 20:02:45 +0000 (20:02 +0000)
server/sonar-web/src/main/js/apps/code/__tests__/Code-it.ts
server/sonar-web/src/main/js/apps/code/components/CodeApp.tsx

index 3584c5821817ef5b60b665ece5b7d6f3461e81d8..aaee0394a12459368387c992939f221d1529cdc2 100644 (file)
@@ -21,6 +21,7 @@ import { screen, waitFor, within } from '@testing-library/react';
 import userEvent from '@testing-library/user-event';
 import { UserEvent } from '@testing-library/user-event/dist/types/setup/setup';
 import { keyBy, times } from 'lodash';
+import BranchesServiceMock from '../../../api/mocks/BranchesServiceMock';
 import ComponentsServiceMock from '../../../api/mocks/ComponentsServiceMock';
 import IssuesServiceMock from '../../../api/mocks/IssuesServiceMock';
 import { isDiffMetric } from '../../../helpers/measures';
@@ -43,16 +44,6 @@ jest.mock('../../../components/SourceViewer/helpers/lines', () => {
   };
 });
 
-jest.mock('../../../api/branches', () => ({
-  deleteBranch: jest.fn(),
-  deletePullRequest: jest.fn(),
-  excludeBranchFromPurge: jest.fn(),
-  getBranches: jest.fn(),
-  getPullRequests: jest.fn(),
-  renameBranch: jest.fn(),
-  setMainBranch: jest.fn(),
-}));
-
 jest.mock('../../../api/quality-gates', () => ({
   getQualityGateProjectStatus: jest.fn(),
 }));
@@ -60,8 +51,9 @@ jest.mock('../../../api/quality-gates', () => ({
 const DEFAULT_LINES_LOADED = 19;
 const originalScrollTo = window.scrollTo;
 
-const issuesHandler = new IssuesServiceMock();
+const branchesHandler = new BranchesServiceMock();
 const componentsHandler = new ComponentsServiceMock();
+const issuesHandler = new IssuesServiceMock();
 
 beforeAll(() => {
   Object.defineProperty(window, 'scrollTo', {
@@ -80,8 +72,9 @@ afterAll(() => {
 });
 
 beforeEach(() => {
-  issuesHandler.reset();
+  branchesHandler.reset();
   componentsHandler.reset();
+  issuesHandler.reset();
 });
 
 it('should allow navigating through the tree', async () => {
index f86495158a545e932ea6508065ac1792e0af735d..9343d730df224e024df9e5e55f2f5ae2a80bbc7d 100644 (file)
@@ -22,22 +22,20 @@ import withComponentContext from '../../../app/components/componentContext/withC
 import withMetricsContext from '../../../app/components/metrics/withMetricsContext';
 import { Location, Router, withRouter } from '../../../components/hoc/withRouter';
 import { CodeScope, getCodeUrl, getProjectUrl } from '../../../helpers/urls';
-import { withBranchLikes } from '../../../queries/branch';
+import { useBranchesQuery } from '../../../queries/branch';
 import { BranchLike } from '../../../types/branch-like';
-import { ComponentQualifier } from '../../../types/component';
+import { ComponentQualifier, isPortfolioLike } from '../../../types/component';
 import { Breadcrumb, Component, ComponentMeasure, Dict, Metric } from '../../../types/types';
 import { addComponent, addComponentBreadcrumbs, clearBucket } from '../bucket';
 import '../code.css';
 import { loadMoreChildren, retrieveComponent, retrieveComponentChildren } from '../utils';
 import CodeAppRenderer from './CodeAppRenderer';
 
-interface Props {
-  branchLike?: BranchLike;
-  branchLikes: BranchLike[];
+interface Props extends WithBranchLikesProps {
   component: Component;
   location: Location;
-  router: Router;
   metrics: Dict<Metric>;
+  router: Router;
 }
 
 interface State {
@@ -46,11 +44,11 @@ interface State {
   components?: ComponentMeasure[];
   highlighted?: ComponentMeasure;
   loading: boolean;
+  newCodeSelected: boolean;
   page: number;
   searchResults?: ComponentMeasure[];
   sourceViewer?: ComponentMeasure;
   total: number;
-  newCodeSelected: boolean;
 }
 
 class CodeApp extends React.Component<Props, State> {
@@ -62,9 +60,9 @@ class CodeApp extends React.Component<Props, State> {
     this.state = {
       breadcrumbs: [],
       loading: true,
+      newCodeSelected: true,
       page: 0,
       total: 0,
-      newCodeSelected: true,
     };
   }
 
@@ -92,41 +90,37 @@ class CodeApp extends React.Component<Props, State> {
       this,
       this.props.branchLike,
     ).then((r) => {
-      if (this.mounted) {
-        if (
-          [ComponentQualifier.File, ComponentQualifier.TestFile].includes(
-            r.component.qualifier as ComponentQualifier,
-          )
-        ) {
-          this.setState({
-            breadcrumbs: r.breadcrumbs,
-            components: r.components,
-            loading: false,
-            page: 0,
-            searchResults: undefined,
-            sourceViewer: r.component,
-            total: 0,
-          });
-        } else {
-          this.setState({
-            baseComponent: r.component,
-            breadcrumbs: r.breadcrumbs,
-            components: r.components,
-            loading: false,
-            page: r.page,
-            searchResults: undefined,
-            sourceViewer: undefined,
-            total: r.total,
-          });
-        }
+      if (
+        [ComponentQualifier.File, ComponentQualifier.TestFile].includes(
+          r.component.qualifier as ComponentQualifier,
+        )
+      ) {
+        this.setState({
+          breadcrumbs: r.breadcrumbs,
+          components: r.components,
+          loading: false,
+          page: 0,
+          searchResults: undefined,
+          sourceViewer: r.component,
+          total: 0,
+        });
+      } else {
+        this.setState({
+          baseComponent: r.component,
+          breadcrumbs: r.breadcrumbs,
+          components: r.components,
+          loading: false,
+          page: r.page,
+          searchResults: undefined,
+          sourceViewer: undefined,
+          total: r.total,
+        });
       }
     }, this.stopLoading);
   };
 
   stopLoading = () => {
-    if (this.mounted) {
-      this.setState({ loading: false });
-    }
+    this.setState({ loading: false });
   };
 
   handleComponentChange = () => {
@@ -138,9 +132,7 @@ class CodeApp extends React.Component<Props, State> {
     this.setState({ loading: true });
     retrieveComponentChildren(component.key, component.qualifier, this, branchLike).then(() => {
       addComponent(component);
-      if (this.mounted) {
-        this.handleUpdate();
-      }
+      this.handleUpdate();
     }, this.stopLoading);
   };
 
@@ -156,7 +148,7 @@ class CodeApp extends React.Component<Props, State> {
       this,
       this.props.branchLike,
     ).then((r) => {
-      if (this.mounted && r.components.length) {
+      if (r.components.length) {
         this.setState({
           components: [...components, ...r.components],
           page: r.page,
@@ -233,4 +225,27 @@ class CodeApp extends React.Component<Props, State> {
   }
 }
 
+interface WithBranchLikesProps {
+  branchLikes?: BranchLike[];
+  branchLike?: BranchLike;
+}
+
+function withBranchLikes<P extends { component?: Component }>(
+  WrappedComponent: React.ComponentType<React.PropsWithChildren<P & WithBranchLikesProps>>,
+): React.ComponentType<React.PropsWithChildren<Omit<P, 'branchLike' | 'branchLikes'>>> {
+  return function WithBranchLike(p: P) {
+    const { data, isFetching } = useBranchesQuery(p.component);
+
+    return (
+      (isPortfolioLike(p.component?.qualifier) || !isFetching) && (
+        <WrappedComponent
+          branchLikes={data?.branchLikes ?? []}
+          branchLike={data?.branchLike}
+          {...p}
+        />
+      )
+    );
+  };
+}
+
 export default withRouter(withComponentContext(withMetricsContext(withBranchLikes(CodeApp))));