-
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.
Verify if there are indices before leverage search profile
- Loading branch information
1 parent
d0f0809
commit 5832a76
Showing
5 changed files
with
195 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
*/ | ||
|
||
export { useRequestProfile } from './use_request_profile'; | ||
export { useIndices } from './use_indices'; |
76 changes: 76 additions & 0 deletions
76
x-pack/plugins/searchprofiler/public/application/hooks/use_indices.test.ts
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,76 @@ | ||
/* | ||
* 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 } from 'react-dom/test-utils'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useAppContext } from '../contexts/app_context'; | ||
import { useIndices } from './use_indices'; | ||
import { waitFor } from '@testing-library/dom'; | ||
|
||
jest.mock('../contexts/app_context'); | ||
|
||
describe('useIndices', () => { | ||
let httpMock: { get: jest.Mock }; | ||
|
||
beforeEach(() => { | ||
httpMock = { | ||
get: jest.fn(), | ||
}; | ||
|
||
(useAppContext as jest.Mock).mockReturnValue({ http: httpMock }); | ||
}); | ||
|
||
it('should return true if response is ok and has indices', async () => { | ||
httpMock.get.mockResolvedValue({ ok: true, hasIndices: true }); | ||
|
||
const { result } = renderHook(() => useIndices()); | ||
|
||
act(() => { | ||
result.current().then((res: any) => { | ||
expect(res.hasIndices).toBe(true); | ||
}); | ||
}); | ||
|
||
await waitFor(() => expect(httpMock.get).toHaveBeenCalled()); | ||
}); | ||
|
||
it('should return false if response is ok and has indices', async () => { | ||
httpMock.get.mockResolvedValue({ ok: true, hasIndices: false }); | ||
|
||
const { result } = renderHook(() => useIndices()); | ||
|
||
act(() => { | ||
result.current().then((res: any) => { | ||
expect(res.hasIndices).toBe(false); | ||
}); | ||
}); | ||
|
||
await waitFor(() => expect(httpMock.get).toHaveBeenCalled()); | ||
}); | ||
|
||
it('should return hasIndices as false when API response is not ok', async () => { | ||
httpMock.get.mockResolvedValue({ ok: false, err: { msg: 'Error message' } }); | ||
|
||
const { result } = renderHook(() => useIndices()); | ||
|
||
act(() => { | ||
result.current().then((res: any) => { | ||
expect(res.hasIndices).toBe(false); | ||
}); | ||
}); | ||
|
||
await waitFor(() => expect(httpMock.get).toHaveBeenCalled()); | ||
}); | ||
|
||
it('should throw an error when the API call fails', async () => { | ||
httpMock.get.mockRejectedValue(new Error('Network error')); | ||
|
||
const { result } = renderHook(() => useIndices()); | ||
|
||
await expect(result.current()).rejects.toThrow('Error fetching indices:'); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
x-pack/plugins/searchprofiler/public/application/hooks/use_indices.ts
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,27 @@ | ||
/* | ||
* 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 { useAppContext } from '../contexts/app_context'; | ||
|
||
interface ReturnValue { | ||
hasIndices: boolean; | ||
} | ||
|
||
export const useIndices = () => { | ||
const { http } = useAppContext(); | ||
return async (): Promise<ReturnValue> => { | ||
try { | ||
const response = await http.get< | ||
{ ok: true; hasIndices: boolean } | { ok: false; err: { msg: string } } | ||
>('../api/searchprofiler/getIndices'); | ||
|
||
return { hasIndices: response.ok ? response.hasIndices : false }; | ||
} catch (e) { | ||
throw new Error('Error fetching indices:', e); | ||
} | ||
}; | ||
}; |
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