-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring network details to use useSearchStrategy (#135995)
* useSearchStrategy * fix unit tests Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
8985518
commit 2bda8aa
Showing
3 changed files
with
125 additions
and
100 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
x-pack/plugins/security_solution/public/network/containers/details/index.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,70 @@ | ||
/* | ||
* 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 { act, renderHook } from '@testing-library/react-hooks'; | ||
import { TestProviders } from '../../../common/mock'; | ||
import { ID, useNetworkDetails } from '.'; | ||
import { useSearchStrategy } from '../../../common/containers/use_search_strategy'; | ||
|
||
jest.mock('../../../common/containers/use_search_strategy', () => ({ | ||
useSearchStrategy: jest.fn(), | ||
})); | ||
const mockUseSearchStrategy = useSearchStrategy as jest.Mock; | ||
const mockSearch = jest.fn(); | ||
|
||
const defaultProps = { | ||
id: ID, | ||
indexNames: ['fakebeat-*'], | ||
ip: '192.168.1.1', | ||
skip: false, | ||
}; | ||
|
||
describe('useNetworkDetails', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockUseSearchStrategy.mockReturnValue({ | ||
loading: false, | ||
result: { | ||
networkDetails: {}, | ||
}, | ||
search: mockSearch, | ||
refetch: jest.fn(), | ||
inspect: {}, | ||
}); | ||
}); | ||
|
||
it('runs search', () => { | ||
renderHook(() => useNetworkDetails(defaultProps), { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
expect(mockSearch).toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not run search when skip = true', () => { | ||
const props = { | ||
...defaultProps, | ||
skip: true, | ||
}; | ||
renderHook(() => useNetworkDetails(props), { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
expect(mockSearch).not.toHaveBeenCalled(); | ||
}); | ||
it('skip = true will cancel any running request', () => { | ||
const props = { | ||
...defaultProps, | ||
}; | ||
const { rerender } = renderHook(() => useNetworkDetails(props), { | ||
wrapper: TestProviders, | ||
}); | ||
props.skip = true; | ||
act(() => rerender()); | ||
expect(mockUseSearchStrategy).toHaveBeenCalledTimes(2); | ||
expect(mockUseSearchStrategy.mock.calls[1][0].abort).toEqual(true); | ||
}); | ||
}); |
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