import * as key from 'keymaster';
import { flatMap, range } from 'lodash';
import * as React from 'react';
+import { connect } from 'react-redux';
import { addSideBarClass, removeSideBarClass } from 'sonar-ui-common/helpers/pages';
import { getMeasures } from '../../api/measures';
import { getSecurityHotspotList, getSecurityHotspots } from '../../api/security-hotspots';
import { getBranchLikeQuery, isPullRequest, isSameBranchLike } from '../../helpers/branch-like';
import { getStandards } from '../../helpers/security-standard';
import { isLoggedIn } from '../../helpers/users';
+import { fetchBranchStatus } from '../../store/rootActions';
import { BranchLike } from '../../types/branch-like';
import { SecurityStandard, Standards } from '../../types/security';
import {
const HOTSPOT_KEYMASTER_SCOPE = 'hotspots-list';
const PAGE_SIZE = 500;
+interface DispatchProps {
+ fetchBranchStatus: (branchLike: BranchLike, projectKey: string) => void;
+}
-interface Props {
+interface OwnProps {
branchLike?: BranchLike;
currentUser: T.CurrentUser;
component: T.Component;
router: Router;
}
+type Props = DispatchProps & OwnProps;
+
interface State {
filterByCategory?: { standard: SecurityStandard; category: string };
filters: HotspotFilters;
handleHotspotUpdate = (hotspotKey: string) => {
const { hotspots, hotspotsPageIndex } = this.state;
+ const { branchLike, component } = this.props;
const index = hotspots.findIndex(h => h.key === hotspotKey);
+ if (isPullRequest(branchLike)) {
+ this.props.fetchBranchStatus(branchLike, component.key);
+ }
+
return Promise.all(
range(hotspotsPageIndex).map(p => this.fetchSecurityHotspots(p + 1 /* pages are 1-indexed */))
)
}
}
-export default withCurrentUser(SecurityHotspotsApp);
+const mapDispatchToProps = { fetchBranchStatus };
+
+export default withCurrentUser(connect(null, mapDispatchToProps)(SecurityHotspotsApp));
it('should handle hotspot update', async () => {
const key = 'hotspotKey';
const hotspots = [mockRawHotspot(), mockRawHotspot({ key })];
+ const fetchBranchStatusMock = jest.fn();
+ const branchLike = mockPullRequest();
+ const componentKey = 'test';
+
(getSecurityHotspots as jest.Mock).mockResolvedValueOnce({
hotspots,
paging: { pageIndex: 1, total: 1252 }
});
- const wrapper = shallowRender();
+ let wrapper = shallowRender();
await waitAndUpdate(wrapper);
wrapper.setState({ hotspotsPageIndex: 2 });
).toBe(selectedHotspotIndex);
expect(getMeasures).toBeCalled();
+
+ (getSecurityHotspots as jest.Mock).mockResolvedValueOnce({
+ hotspots,
+ paging: { pageIndex: 1, total: 1252 }
+ });
+
+ wrapper = shallowRender({
+ branchLike,
+ fetchBranchStatus: fetchBranchStatusMock,
+ component: mockComponent({ key: componentKey })
+ });
+ await wrapper
+ .find(SecurityHotspotsAppRenderer)
+ .props()
+ .onUpdateHotspot(key);
+ expect(fetchBranchStatusMock).toBeCalledWith(branchLike, componentKey);
});
it('should handle status filter change', async () => {
function shallowRender(props: Partial<SecurityHotspotsApp['props']> = {}) {
return shallow<SecurityHotspotsApp>(
<SecurityHotspotsApp
+ fetchBranchStatus={jest.fn()}
branchLike={branch}
component={mockComponent()}
currentUser={mockCurrentUser()}