Skip to content

Commit

Permalink
[SIEM] Replace WithSource with useWithSource hook (elastic#68722)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykkopycinski committed Jun 25, 2020
1 parent 0a30e34 commit a220883
Show file tree
Hide file tree
Showing 25 changed files with 1,215 additions and 1,358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import '../../../common/mock/match_media';
import { setAbsoluteRangeDatePicker } from '../../../common/store/inputs/actions';
import { DetectionEnginePageComponent } from './detection_engine';
import { useUserInfo } from '../../components/user_info';
import { useWithSource } from '../../../common/containers/source';

jest.mock('../../components/user_info');
jest.mock('../../../common/lib/kibana');
jest.mock('../../../common/containers/source');
jest.mock('../../../common/components/link_to');
jest.mock('react-router-dom', () => {
const originalModule = jest.requireActual('react-router-dom');
Expand All @@ -30,7 +31,12 @@ describe('DetectionEnginePageComponent', () => {
beforeAll(() => {
(useParams as jest.Mock).mockReturnValue({});
(useUserInfo as jest.Mock).mockReturnValue({});
(useWithSource as jest.Mock).mockReturnValue({
indicesExist: true,
indexPattern: {},
});
});

it('renders correctly', () => {
const wrapper = shallow(
<DetectionEnginePageComponent
Expand All @@ -40,6 +46,6 @@ describe('DetectionEnginePageComponent', () => {
/>
);

expect(wrapper.find('WithSource')).toHaveLength(1);
expect(wrapper.find('FiltersGlobal')).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import { useHistory } from 'react-router-dom';
import { SecurityPageName } from '../../../app/types';
import { TimelineId } from '../../../../common/types/timeline';
import { GlobalTime } from '../../../common/containers/global_time';
import {
indicesExistOrDataTemporarilyUnavailable,
WithSource,
} from '../../../common/containers/source';
import { useWithSource } from '../../../common/containers/source';
import { UpdateDateRange } from '../../../common/components/charts/common';
import { FiltersGlobal } from '../../../common/components/filters_global';
import { getRulesUrl } from '../../../common/components/link_to/redirect_to_detection_engine';
Expand Down Expand Up @@ -82,6 +79,7 @@ export const DetectionEnginePageComponent: React.FC<PropsFromRedux> = ({
const indexToAdd = useMemo(() => (signalIndexName == null ? [] : [signalIndexName]), [
signalIndexName,
]);
const { indicesExist, indexPattern } = useWithSource('default', indexToAdd);

if (isUserAuthenticated != null && !isUserAuthenticated && !loading) {
return (
Expand All @@ -104,77 +102,73 @@ export const DetectionEnginePageComponent: React.FC<PropsFromRedux> = ({
<>
{hasEncryptionKey != null && !hasEncryptionKey && <NoApiIntegrationKeyCallOut />}
{hasIndexWrite != null && !hasIndexWrite && <NoWriteAlertsCallOut />}
<WithSource sourceId="default" indexToAdd={indexToAdd}>
{({ indicesExist, indexPattern }) => {
return indicesExistOrDataTemporarilyUnavailable(indicesExist) ? (
<StickyContainer>
<FiltersGlobal>
<SiemSearchBar id="global" indexPattern={indexPattern} />
</FiltersGlobal>
<WrapperPage>
<DetectionEngineHeaderPage
subtitle={
lastAlerts != null && (
<>
{i18n.LAST_ALERT}
{': '}
{lastAlerts}
</>
)
}
title={i18n.PAGE_TITLE}
>
<LinkButton
fill
onClick={goToRules}
href={formatUrl(getRulesUrl())}
iconType="gear"
data-test-subj="manage-alert-detection-rules"
>
{i18n.BUTTON_MANAGE_RULES}
</LinkButton>
</DetectionEngineHeaderPage>
{indicesExist ? (
<StickyContainer>
<FiltersGlobal>
<SiemSearchBar id="global" indexPattern={indexPattern} />
</FiltersGlobal>
<WrapperPage>
<DetectionEngineHeaderPage
subtitle={
lastAlerts != null && (
<>
{i18n.LAST_ALERT}
{': '}
{lastAlerts}
</>
)
}
title={i18n.PAGE_TITLE}
>
<LinkButton
fill
onClick={goToRules}
href={formatUrl(getRulesUrl())}
iconType="gear"
data-test-subj="manage-alert-detection-rules"
>
{i18n.BUTTON_MANAGE_RULES}
</LinkButton>
</DetectionEngineHeaderPage>

<GlobalTime>
{({ to, from, deleteQuery, setQuery }) => (
<>
<>
<AlertsHistogramPanel
deleteQuery={deleteQuery}
filters={filters}
from={from}
query={query}
setQuery={setQuery}
showTotalAlertsCount={true}
signalIndexName={signalIndexName}
stackByOptions={alertsHistogramOptions}
to={to}
updateDateRange={updateDateRangeCallback}
/>
<EuiSpacer size="l" />
<AlertsTable
timelineId={TimelineId.alertsPage}
loading={loading}
hasIndexWrite={hasIndexWrite ?? false}
canUserCRUD={(canUserCRUD ?? false) && (hasEncryptionKey ?? false)}
from={from}
signalsIndex={signalIndexName ?? ''}
to={to}
/>
</>
</>
)}
</GlobalTime>
</WrapperPage>
</StickyContainer>
) : (
<WrapperPage>
<DetectionEngineHeaderPage border title={i18n.PAGE_TITLE} />
<DetectionEngineEmptyPage />
</WrapperPage>
);
}}
</WithSource>
<GlobalTime>
{({ to, from, deleteQuery, setQuery }) => (
<>
<>
<AlertsHistogramPanel
deleteQuery={deleteQuery}
filters={filters}
from={from}
query={query}
setQuery={setQuery}
showTotalAlertsCount={true}
signalIndexName={signalIndexName}
stackByOptions={alertsHistogramOptions}
to={to}
updateDateRange={updateDateRangeCallback}
/>
<EuiSpacer size="l" />
<AlertsTable
timelineId={TimelineId.alertsPage}
loading={loading}
hasIndexWrite={hasIndexWrite ?? false}
canUserCRUD={(canUserCRUD ?? false) && (hasEncryptionKey ?? false)}
from={from}
signalsIndex={signalIndexName ?? ''}
to={to}
/>
</>
</>
)}
</GlobalTime>
</WrapperPage>
</StickyContainer>
) : (
<WrapperPage>
<DetectionEngineHeaderPage border title={i18n.PAGE_TITLE} />
<DetectionEngineEmptyPage />
</WrapperPage>
)}
<SpyRoute pageName={SecurityPageName.alerts} />
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import { TestProviders } from '../../../../../common/mock';
import { RuleDetailsPageComponent } from './index';
import { setAbsoluteRangeDatePicker } from '../../../../../common/store/inputs/actions';
import { useUserInfo } from '../../../../components/user_info';
import { useWithSource } from '../../../../../common/containers/source';
import { useParams } from 'react-router-dom';

jest.mock('../../../../../common/components/link_to');
jest.mock('../../../../components/user_info');
jest.mock('../../../../../common/containers/source');
jest.mock('react-router-dom', () => {
const originalModule = jest.requireActual('react-router-dom');

Expand All @@ -30,6 +32,10 @@ describe('RuleDetailsPageComponent', () => {
beforeAll(() => {
(useUserInfo as jest.Mock).mockReturnValue({});
(useParams as jest.Mock).mockReturnValue({});
(useWithSource as jest.Mock).mockReturnValue({
indicesExist: true,
indexPattern: {},
});
});

it('renders correctly', () => {
Expand All @@ -44,6 +50,6 @@ describe('RuleDetailsPageComponent', () => {
}
);

expect(wrapper.find('WithSource')).toHaveLength(1);
expect(wrapper.find('GlobalTime')).toHaveLength(1);
});
});
Loading

0 comments on commit a220883

Please sign in to comment.