diff options
author | Wouter Admiraal <wouter.admiraal@sonarsource.com> | 2019-04-02 11:06:47 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-05-29 20:21:13 +0200 |
commit | 7853e9f613ae662e17a568bd70b3d65e5898ba80 (patch) | |
tree | 39887f6d6f27a0f9a1dc4ae70b978dc385922f73 /server/sonar-web/src/main/js/components/workspace | |
parent | 7ae08845cfe069d2a4df4d788e9713dc25900f3c (diff) | |
download | sonarqube-7853e9f613ae662e17a568bd70b3d65e5898ba80.tar.gz sonarqube-7853e9f613ae662e17a568bd70b3d65e5898ba80.zip |
SONAR-11875 Connect WorkspaceComponentViewer to the branch redux store, and refresh branch status when updating an issue
Diffstat (limited to 'server/sonar-web/src/main/js/components/workspace')
3 files changed, 66 insertions, 5 deletions
diff --git a/server/sonar-web/src/main/js/components/workspace/WorkspaceComponentViewer.tsx b/server/sonar-web/src/main/js/components/workspace/WorkspaceComponentViewer.tsx index ac309125b1e..2e3ae7561cd 100644 --- a/server/sonar-web/src/main/js/components/workspace/WorkspaceComponentViewer.tsx +++ b/server/sonar-web/src/main/js/components/workspace/WorkspaceComponentViewer.tsx @@ -18,20 +18,25 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; +import { connect } from 'react-redux'; import { ComponentDescriptor } from './context'; import WorkspaceHeader, { Props as WorkspaceHeaderProps } from './WorkspaceHeader'; import WorkspaceComponentTitle from './WorkspaceComponentTitle'; import SourceViewer from '../SourceViewer/SourceViewer'; import { scrollToElement } from '../../helpers/scrolling'; +import { isPullRequest, isShortLivingBranch } from '../../helpers/branches'; +import { fetchBranchStatus } from '../../store/rootActions'; +import { getParents } from '../../api/components'; export interface Props extends T.Omit<WorkspaceHeaderProps, 'children' | 'onClose'> { component: ComponentDescriptor; + fetchBranchStatus: (branchLike: T.BranchLike, projectKey: string) => Promise<void>; height: number; onClose: (componentKey: string) => void; onLoad: (details: { key: string; name: string; qualifier: string }) => void; } -export default class WorkspaceComponentViewer extends React.PureComponent<Props> { +export class WorkspaceComponentViewer extends React.PureComponent<Props> { container?: HTMLElement | null; componentDidMount() { @@ -50,6 +55,10 @@ export default class WorkspaceComponentViewer extends React.PureComponent<Props> this.props.onClose(this.props.component.key); }; + handleIssueChange = (_: T.Issue) => { + this.refreshBranchStatus(); + }; + handleLoaded = (component: T.SourceViewerFile) => { this.props.onLoad({ key: this.props.component.key, @@ -72,6 +81,21 @@ export default class WorkspaceComponentViewer extends React.PureComponent<Props> } }; + refreshBranchStatus = () => { + const { component } = this.props; + const { branchLike } = component; + if (branchLike && (isPullRequest(branchLike) || isShortLivingBranch(branchLike))) { + getParents(component.key).then( + (parents?: any[]) => { + if (parents && parents.length > 0) { + this.props.fetchBranchStatus(branchLike, parents.pop().key); + } + }, + () => {} + ); + } + }; + render() { const { component } = this.props; @@ -96,6 +120,7 @@ export default class WorkspaceComponentViewer extends React.PureComponent<Props> branchLike={component.branchLike} component={component.key} highlightedLine={component.line} + onIssueChange={this.handleIssueChange} onLoaded={this.handleLoaded} /> </div> @@ -103,3 +128,10 @@ export default class WorkspaceComponentViewer extends React.PureComponent<Props> ); } } + +const mapDispatchToProps = { fetchBranchStatus: fetchBranchStatus as any }; + +export default connect( + null, + mapDispatchToProps +)(WorkspaceComponentViewer); diff --git a/server/sonar-web/src/main/js/components/workspace/__tests__/WorkspaceComponentViewer-test.tsx b/server/sonar-web/src/main/js/components/workspace/__tests__/WorkspaceComponentViewer-test.tsx index 6617b3d7b67..c287f6214a1 100644 --- a/server/sonar-web/src/main/js/components/workspace/__tests__/WorkspaceComponentViewer-test.tsx +++ b/server/sonar-web/src/main/js/components/workspace/__tests__/WorkspaceComponentViewer-test.tsx @@ -19,7 +19,18 @@ */ import * as React from 'react'; import { shallow } from 'enzyme'; -import WorkspaceComponentViewer, { Props } from '../WorkspaceComponentViewer'; +import { WorkspaceComponentViewer, Props } from '../WorkspaceComponentViewer'; +import { mockPullRequest, mockIssue } from '../../../helpers/testMocks'; +import { waitAndUpdate } from '../../../helpers/testUtils'; +import { getParents } from '../../../api/components'; + +jest.mock('../../../api/components', () => ({ + getParents: jest.fn().mockResolvedValue([{ key: 'bar' }]) +})); + +beforeEach(() => { + jest.clearAllMocks(); +}); it('should render', () => { expect(shallowRender()).toMatchSnapshot(); @@ -43,11 +54,28 @@ it('should call back after load', () => { expect(onLoad).toBeCalledWith({ key: 'foo', name: 'src/foo.js', qualifier: 'FIL' }); }); +it('should refresh branch status if issues are updated', async () => { + const fetchBranchStatus = jest.fn(); + const branchLike = mockPullRequest(); + const component = { + branchLike, + key: 'foo' + }; + const wrapper = shallowRender({ component, fetchBranchStatus }); + const instance = wrapper.instance(); + await waitAndUpdate(wrapper); + + instance.handleIssueChange(mockIssue()); + expect(getParents).toBeCalledWith(component.key); + await waitAndUpdate(wrapper); + expect(fetchBranchStatus).toBeCalledWith(branchLike, 'bar'); +}); + function shallowRender(props?: Partial<Props>) { - const component = { branchLike: undefined, key: 'foo' }; - return shallow( + return shallow<WorkspaceComponentViewer>( <WorkspaceComponentViewer - component={component} + component={{ branchLike: undefined, key: 'foo' }} + fetchBranchStatus={jest.fn()} height={300} onClose={jest.fn()} onCollapse={jest.fn()} diff --git a/server/sonar-web/src/main/js/components/workspace/__tests__/__snapshots__/WorkspaceComponentViewer-test.tsx.snap b/server/sonar-web/src/main/js/components/workspace/__tests__/__snapshots__/WorkspaceComponentViewer-test.tsx.snap index 79e79272640..72da3f75715 100644 --- a/server/sonar-web/src/main/js/components/workspace/__tests__/__snapshots__/WorkspaceComponentViewer-test.tsx.snap +++ b/server/sonar-web/src/main/js/components/workspace/__tests__/__snapshots__/WorkspaceComponentViewer-test.tsx.snap @@ -30,6 +30,7 @@ exports[`should render 1`] = ` > <SourceViewer component="foo" + onIssueChange={[Function]} onLoaded={[Function]} /> </div> |