-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for useFetchAgentFrameworkTraces Hook (#77)
* Add unit test for useFetchAgentFrameworkTraces Signed-off-by: Hailong Cui <[email protected]> * Add test case for unmount Signed-off-by: Hailong Cui <[email protected]> * Address review comments Signed-off-by: Hailong Cui <[email protected]> --------- Signed-off-by: Hailong Cui <[email protected]>
- Loading branch information
1 parent
866a679
commit 66292ce
Showing
2 changed files
with
128 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useFetchAgentFrameworkTraces } from './use_fetch_agentframework_traces'; | ||
import { createOpenSearchDashboardsReactContext } from '../../../../src/plugins/opensearch_dashboards_react/public'; | ||
import { coreMock } from '../../../../src/core/public/mocks'; | ||
import { HttpHandler } from '../../../../src/core/public'; | ||
import { AbortError } from '../../../../src/plugins/data/common'; | ||
|
||
describe('useFetchAgentFrameworkTraces hook', () => { | ||
const traceId = 'foo'; | ||
const services = coreMock.createStart(); | ||
const { Provider } = createOpenSearchDashboardsReactContext(services); | ||
const wrapper = { wrapper: Provider }; | ||
|
||
it('return undefined when trace id is not specfied', () => { | ||
const { result } = renderHook(() => useFetchAgentFrameworkTraces('')); | ||
expect(result.current).toMatchObject({ | ||
data: undefined, | ||
loading: false, | ||
}); | ||
}); | ||
|
||
it('return trace data successfully', async () => { | ||
const traces = [ | ||
{ | ||
interactionId: 'test_interactionId', | ||
parentInteractionId: 'test_parent_interactionId', | ||
input: 'input', | ||
output: 'output', | ||
createTime: '', | ||
origin: '', | ||
traceNumber: 1, | ||
}, | ||
]; | ||
|
||
services.http.get.mockResolvedValueOnce(traces); | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => useFetchAgentFrameworkTraces(traceId), | ||
wrapper | ||
); | ||
|
||
await waitForNextUpdate(); | ||
expect(services.http.get).toHaveBeenCalledWith( | ||
`/api/assistant/trace/${traceId}`, | ||
expect.objectContaining({ | ||
signal: expect.any(Object), | ||
}) | ||
); | ||
|
||
expect(result.current).toEqual({ | ||
data: traces, | ||
loading: false, | ||
}); | ||
}); | ||
|
||
it('return error when fetch trace error happend', async () => { | ||
services.http.get.mockRejectedValue(new Error('trace not found')); | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => useFetchAgentFrameworkTraces(traceId), | ||
wrapper | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(services.http.get).toHaveBeenCalledWith( | ||
`/api/assistant/trace/${traceId}`, | ||
expect.objectContaining({ | ||
signal: expect.any(Object), | ||
}) | ||
); | ||
|
||
expect(result.current).toEqual({ | ||
data: undefined, | ||
loading: false, | ||
error: new Error('trace not found'), | ||
}); | ||
}); | ||
|
||
it('abort the request when unmount', async () => { | ||
const abortError = new AbortError(); | ||
|
||
const abortFn = jest.fn(); | ||
|
||
// @ts-ignore | ||
global.AbortController = jest.fn(() => ({ | ||
abort: abortFn, | ||
signal: new Object(), | ||
})); | ||
|
||
services.http.get.mockImplementation(((_path, options) => { | ||
return new Promise((_resolve, reject) => { | ||
if (options?.signal) { | ||
options.signal.onabort = () => { | ||
reject(abortError); | ||
}; | ||
} | ||
}); | ||
}) as HttpHandler); | ||
|
||
const { unmount } = renderHook(() => useFetchAgentFrameworkTraces(traceId), wrapper); | ||
|
||
act(() => { | ||
unmount(); | ||
}); | ||
|
||
expect(services.http.get).toHaveBeenCalledWith( | ||
`/api/assistant/trace/${traceId}`, | ||
expect.objectContaining({ | ||
signal: expect.any(Object), | ||
}) | ||
); | ||
|
||
// expect the mock to be called | ||
expect(abortFn).toBeCalledTimes(1); | ||
}); | ||
}); |
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