Files
sentinel/static/app/utils/profiling/hooks/useProfileFunctionTrends.spec.tsx
2024-02-15 17:01:50 +00:00

78 lines
2.2 KiB
TypeScript

import type {ReactElement} from 'react';
import {useMemo} from 'react';
import {initializeOrg} from 'sentry-test/initializeOrg';
import {makeTestQueryClient} from 'sentry-test/queryClient';
import {reactHooks} from 'sentry-test/reactTestingLibrary';
import {useProfileFunctionTrends} from 'sentry/utils/profiling/hooks/useProfileFunctionTrends';
import {QueryClientProvider} from 'sentry/utils/queryClient';
import {OrganizationContext} from 'sentry/views/organizationContext';
function TestContext({children}: {children: ReactElement}) {
const {organization} = useMemo(() => initializeOrg(), []);
return (
<QueryClientProvider client={makeTestQueryClient()}>
<OrganizationContext.Provider value={organization}>
{children}
</OrganizationContext.Provider>
</QueryClientProvider>
);
}
describe('useProfileFunctionTrendss', function () {
afterEach(function () {
MockApiClient.clearMockResponses();
});
it('initializes with the loading state', function () {
MockApiClient.addMockResponse({
url: '/organizations/org-slug/profiling/function-trends/',
body: {data: []},
});
const hook = reactHooks.renderHook(
() =>
useProfileFunctionTrends({
trendFunction: 'p95()',
trendType: 'regression',
}),
{wrapper: TestContext}
);
expect(hook.result.current).toMatchObject(
expect.objectContaining({
isInitialLoading: true,
})
);
});
it('fetches functions', async function () {
MockApiClient.addMockResponse({
url: '/organizations/org-slug/profiling/function-trends/',
body: {data: []},
});
const hook = reactHooks.renderHook(
() =>
useProfileFunctionTrends({
trendFunction: 'p95()',
trendType: 'regression',
}),
{wrapper: TestContext}
);
expect(hook.result.current.isLoading).toEqual(true);
expect(hook.result.current.isFetched).toEqual(false);
await hook.waitForNextUpdate();
expect(hook.result.current).toMatchObject(
expect.objectContaining({
isLoading: false,
isFetched: true,
data: expect.objectContaining({
data: expect.any(Array),
}),
})
);
});
});