useProfileFunctionTrends.spec.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {ReactElement, useMemo} from 'react';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  4. import {useProfileFunctionTrends} from 'sentry/utils/profiling/hooks/useProfileFunctionTrends';
  5. import {QueryClient, QueryClientProvider} from 'sentry/utils/queryClient';
  6. import {OrganizationContext} from 'sentry/views/organizationContext';
  7. function TestContext({children}: {children: ReactElement}) {
  8. const {organization} = useMemo(() => initializeOrg(), []);
  9. // ensure client is rebuilt on each render otherwise caching will interfere with subsequent tests
  10. const client = useMemo(() => new QueryClient(), []);
  11. return (
  12. <QueryClientProvider client={client}>
  13. <OrganizationContext.Provider value={organization}>
  14. {children}
  15. </OrganizationContext.Provider>
  16. </QueryClientProvider>
  17. );
  18. }
  19. describe('useProfileFunctionTrendss', function () {
  20. afterEach(function () {
  21. MockApiClient.clearMockResponses();
  22. });
  23. it('initializes with the loading state', function () {
  24. MockApiClient.addMockResponse({
  25. url: '/organizations/org-slug/profiling/function-trends/',
  26. body: {data: []},
  27. });
  28. const hook = reactHooks.renderHook(
  29. () =>
  30. useProfileFunctionTrends({
  31. trendFunction: 'p95()',
  32. trendType: 'regression',
  33. }),
  34. {wrapper: TestContext}
  35. );
  36. expect(hook.result.current).toMatchObject(
  37. expect.objectContaining({
  38. isInitialLoading: true,
  39. })
  40. );
  41. });
  42. it('fetches functions', async function () {
  43. MockApiClient.addMockResponse({
  44. url: '/organizations/org-slug/profiling/function-trends/',
  45. body: {data: []},
  46. });
  47. const hook = reactHooks.renderHook(
  48. () =>
  49. useProfileFunctionTrends({
  50. trendFunction: 'p95()',
  51. trendType: 'regression',
  52. }),
  53. {wrapper: TestContext}
  54. );
  55. expect(hook.result.current.isLoading).toEqual(true);
  56. expect(hook.result.current.isFetched).toEqual(false);
  57. await hook.waitForNextUpdate();
  58. expect(hook.result.current).toMatchObject(
  59. expect.objectContaining({
  60. isLoading: false,
  61. isFetched: true,
  62. data: expect.objectContaining({
  63. data: expect.any(Array),
  64. }),
  65. })
  66. );
  67. });
  68. });