-
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 host overview to use useSearchStrategy (#135860)
* useSearchStrategy * update unit tests * fix unit tests * update unit tests Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
a9780d2
commit 8985518
Showing
3 changed files
with
136 additions
and
131 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
x-pack/plugins/security_solution/public/hosts/containers/hosts/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,72 @@ | ||
/* | ||
* 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, useHostDetails } 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 = { | ||
endDate: '2020-07-08T08:20:18.966Z', | ||
hostName: 'my-macbook', | ||
id: ID, | ||
indexNames: ['fakebeat-*'], | ||
skip: false, | ||
startDate: '2020-07-07T08:20:18.966Z', | ||
}; | ||
|
||
describe('useHostDetails', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockUseSearchStrategy.mockReturnValue({ | ||
loading: false, | ||
result: { | ||
hostDetails: {}, | ||
}, | ||
search: mockSearch, | ||
refetch: jest.fn(), | ||
inspect: {}, | ||
}); | ||
}); | ||
|
||
it('runs search', () => { | ||
renderHook(() => useHostDetails(defaultProps), { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
expect(mockSearch).toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not run search when skip = true', () => { | ||
const props = { | ||
...defaultProps, | ||
skip: true, | ||
}; | ||
renderHook(() => useHostDetails(props), { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
expect(mockSearch).not.toHaveBeenCalled(); | ||
}); | ||
it('skip = true will cancel any running request', () => { | ||
const props = { | ||
...defaultProps, | ||
}; | ||
const { rerender } = renderHook(() => useHostDetails(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