forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes to make session_view work with generated data
- Loading branch information
1 parent
86aa348
commit fa00f8b
Showing
10 changed files
with
367 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
.../security_solution/public/timelines/components/side_panel/hooks/use_detail_panel.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useDetailPanel, UseDetailPanelConfig } from './use_detail_panel'; | ||
import { timelineActions } from '../../../store/timeline'; | ||
import { useDeepEqualSelector } from '../../../../common/hooks/use_selector'; | ||
import { SourcererScopeName } from '../../../../common/store/sourcerer/model'; | ||
import { TimelineId, TimelineTabs } from '../../../../../common/types'; | ||
|
||
const mockDispatch = jest.fn(); | ||
jest.mock('../../../../common/lib/kibana'); | ||
jest.mock('../../../../common/hooks/use_selector'); | ||
jest.mock('../../../store/timeline'); | ||
jest.mock('react-redux', () => { | ||
const original = jest.requireActual('react-redux'); | ||
return { | ||
...original, | ||
useDispatch: () => mockDispatch, | ||
}; | ||
}); | ||
jest.mock('../../../../common/containers/sourcerer', () => { | ||
const mockSourcererReturn = { | ||
browserFields: {}, | ||
docValueFields: [], | ||
loading: true, | ||
indexPattern: {}, | ||
selectedPatterns: [], | ||
missingPatterns: [], | ||
}; | ||
return { | ||
useSourcererDataView: jest.fn().mockReturnValue(mockSourcererReturn), | ||
}; | ||
}); | ||
|
||
describe('useDetailPanel', () => { | ||
const defaultProps: UseDetailPanelConfig = { | ||
sourcererScope: SourcererScopeName.detections, | ||
timelineId: TimelineId.test, | ||
}; | ||
const mockGetExpandedDetail = jest.fn().mockImplementation(() => ({})); | ||
beforeEach(() => { | ||
(useDeepEqualSelector as jest.Mock).mockImplementation((cb) => { | ||
return mockGetExpandedDetail(); | ||
}); | ||
}); | ||
afterEach(() => { | ||
(useDeepEqualSelector as jest.Mock).mockClear(); | ||
}); | ||
|
||
test('should return openDetailsPanel fn, handleOnDetailsPanelClosed fn, shouldShowDetailsPanel, and the DetailsPanel component', async () => { | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => { | ||
return useDetailPanel(defaultProps); | ||
}); | ||
await waitForNextUpdate(); | ||
|
||
expect(result.current.openDetailsPanel).toBeDefined(); | ||
expect(result.current.handleOnDetailsPanelClosed).toBeDefined(); | ||
expect(result.current.shouldShowDetailsPanel).toBe(false); | ||
expect(result.current.DetailsPanel).toBeNull(); | ||
}); | ||
}); | ||
|
||
test('should fire redux action to open details panel', async () => { | ||
const testEventId = '123'; | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => { | ||
return useDetailPanel(defaultProps); | ||
}); | ||
await waitForNextUpdate(); | ||
|
||
result.current?.openDetailsPanel(testEventId); | ||
|
||
expect(mockDispatch).toHaveBeenCalled(); | ||
expect(timelineActions.toggleDetailPanel).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
test('should call provided onClose callback provided to openDetailsPanel fn', async () => { | ||
const testEventId = '123'; | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => { | ||
return useDetailPanel(defaultProps); | ||
}); | ||
await waitForNextUpdate(); | ||
|
||
const mockOnClose = jest.fn(); | ||
result.current?.openDetailsPanel(testEventId, mockOnClose); | ||
result.current?.handleOnDetailsPanelClosed(); | ||
|
||
expect(mockOnClose).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
test('should call the last onClose callback provided to openDetailsPanel fn', async () => { | ||
// Test that the onClose ref is properly updated | ||
const testEventId = '123'; | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => { | ||
return useDetailPanel(defaultProps); | ||
}); | ||
await waitForNextUpdate(); | ||
|
||
const mockOnClose = jest.fn(); | ||
const secondMockOnClose = jest.fn(); | ||
result.current?.openDetailsPanel(testEventId, mockOnClose); | ||
result.current?.handleOnDetailsPanelClosed(); | ||
|
||
expect(mockOnClose).toHaveBeenCalled(); | ||
|
||
result.current?.openDetailsPanel(testEventId, secondMockOnClose); | ||
result.current?.handleOnDetailsPanelClosed(); | ||
|
||
expect(secondMockOnClose).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
test('should show the details panel', async () => { | ||
mockGetExpandedDetail.mockImplementation(() => ({ | ||
[TimelineTabs.session]: { | ||
panelView: 'somePanel', | ||
}, | ||
})); | ||
const updatedProps = { | ||
...defaultProps, | ||
tabType: TimelineTabs.session, | ||
}; | ||
|
||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => { | ||
return useDetailPanel(updatedProps); | ||
}); | ||
await waitForNextUpdate(); | ||
|
||
expect(result.current.DetailsPanel).toMatchInlineSnapshot(` | ||
<Memo(DetailsPanel) | ||
browserFields={Object {}} | ||
docValueFields={Array []} | ||
handleOnPanelClosed={[Function]} | ||
tabType="session" | ||
timelineId="test" | ||
/> | ||
`); | ||
}); | ||
}); | ||
}); |
139 changes: 139 additions & 0 deletions
139
...ugins/security_solution/public/timelines/components/side_panel/hooks/use_detail_panel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { useMemo, useCallback, useRef } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { timelineActions, timelineSelectors } from '../../../store/timeline'; | ||
import type { EntityType } from '../../../../../../timelines/common'; | ||
import { useSourcererDataView } from '../../../../common/containers/sourcerer'; | ||
import { SourcererScopeName } from '../../../../common/store/sourcerer/model'; | ||
import { activeTimeline } from '../../../containers/active_timeline_context'; | ||
import { TimelineId, TimelineTabs } from '../../../../../common/types/timeline'; | ||
import { timelineDefaults } from '../../../store/timeline/defaults'; | ||
import { useDeepEqualSelector } from '../../../../common/hooks/use_selector'; | ||
import { DetailsPanel as DetailsPanelComponent } from '..'; | ||
|
||
export interface UseDetailPanelConfig { | ||
entityType?: EntityType; | ||
isFlyoutView?: boolean; | ||
sourcererScope: SourcererScopeName; | ||
timelineId: TimelineId; | ||
tabType?: TimelineTabs; | ||
} | ||
|
||
export interface UseDetailPanelReturn { | ||
openDetailsPanel: (eventId?: string, onClose?: () => void) => void; | ||
handleOnDetailsPanelClosed: () => void; | ||
DetailsPanel: JSX.Element | null; | ||
shouldShowDetailsPanel: boolean; | ||
} | ||
|
||
export const useDetailPanel = ({ | ||
entityType, | ||
isFlyoutView, | ||
sourcererScope, | ||
timelineId, | ||
tabType, | ||
}: UseDetailPanelConfig): UseDetailPanelReturn => { | ||
const { browserFields, docValueFields, selectedPatterns, runtimeMappings } = | ||
useSourcererDataView(sourcererScope); | ||
const getTimeline = useMemo(() => timelineSelectors.getTimelineByIdSelector(), []); | ||
const dispatch = useDispatch(); | ||
|
||
const expandedDetail = useDeepEqualSelector( | ||
(state) => (getTimeline(state, timelineId) ?? timelineDefaults)?.expandedDetail | ||
); | ||
const onPanelClose = useRef(() => {}); | ||
|
||
const shouldShowDetailsPanel = useMemo(() => { | ||
if ( | ||
tabType && | ||
expandedDetail && | ||
expandedDetail[tabType] && | ||
!!expandedDetail[tabType]?.panelView | ||
) { | ||
return true; | ||
} | ||
return false; | ||
}, [expandedDetail, tabType]); | ||
|
||
const loadDetailsPanel = useCallback( | ||
(eventId?: string) => { | ||
if (eventId) { | ||
dispatch( | ||
timelineActions.toggleDetailPanel({ | ||
panelView: 'eventDetail', | ||
tabType, | ||
timelineId, | ||
params: { | ||
eventId, | ||
indexName: selectedPatterns.join(','), | ||
}, | ||
}) | ||
); | ||
} | ||
}, | ||
[dispatch, selectedPatterns, tabType, timelineId] | ||
); | ||
|
||
const openDetailsPanel = useCallback( | ||
(eventId?: string, onClose?: () => void) => { | ||
loadDetailsPanel(eventId); | ||
onPanelClose.current = onClose ?? (() => {}); | ||
}, | ||
[loadDetailsPanel] | ||
); | ||
|
||
const handleOnDetailsPanelClosed = useCallback(() => { | ||
console.log('ON PANEL CLOSE: ', onPanelClose.current); // eslint-disable-line | ||
if (onPanelClose.current) onPanelClose.current(); | ||
dispatch(timelineActions.toggleDetailPanel({ tabType, timelineId })); | ||
|
||
if ( | ||
tabType && | ||
expandedDetail[tabType]?.panelView && | ||
timelineId === TimelineId.active && | ||
shouldShowDetailsPanel | ||
) { | ||
activeTimeline.toggleExpandedDetail({}); | ||
} | ||
}, [dispatch, timelineId, expandedDetail, tabType, shouldShowDetailsPanel]); | ||
|
||
const DetailsPanel = useMemo( | ||
() => | ||
shouldShowDetailsPanel ? ( | ||
<DetailsPanelComponent | ||
browserFields={browserFields} | ||
docValueFields={docValueFields} | ||
entityType={entityType} | ||
handleOnPanelClosed={handleOnDetailsPanelClosed} | ||
isFlyoutView={isFlyoutView} | ||
runtimeMappings={runtimeMappings} | ||
tabType={tabType} | ||
timelineId={timelineId} | ||
/> | ||
) : null, | ||
[ | ||
browserFields, | ||
docValueFields, | ||
entityType, | ||
handleOnDetailsPanelClosed, | ||
isFlyoutView, | ||
runtimeMappings, | ||
shouldShowDetailsPanel, | ||
tabType, | ||
timelineId, | ||
] | ||
); | ||
|
||
return { | ||
openDetailsPanel, | ||
handleOnDetailsPanelClosed, | ||
shouldShowDetailsPanel, | ||
DetailsPanel, | ||
}; | ||
}; |
Oops, something went wrong.