useProfiles.spec.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {ReactNode, useMemo} from 'react';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  4. import {PageFilters} from 'sentry/types';
  5. import {useProfiles} from 'sentry/utils/profiling/hooks/useProfiles';
  6. import {OrganizationContext} from 'sentry/views/organizationContext';
  7. const selection: PageFilters = {
  8. datetime: {
  9. period: '14d',
  10. utc: null,
  11. start: null,
  12. end: null,
  13. },
  14. environments: [],
  15. projects: [],
  16. };
  17. function TestContext({children}: {children?: ReactNode}) {
  18. const {organization} = useMemo(() => initializeOrg(), []);
  19. return (
  20. <OrganizationContext.Provider value={organization}>
  21. {children}
  22. </OrganizationContext.Provider>
  23. );
  24. }
  25. describe('useProfiles', function () {
  26. afterEach(function () {
  27. MockApiClient.clearMockResponses();
  28. });
  29. it('initializes with the initial state', function () {
  30. const hook = reactHooks.renderHook(useProfiles, {
  31. wrapper: TestContext,
  32. initialProps: {query: ''},
  33. });
  34. expect(hook.result.current).toEqual({type: 'initial'});
  35. });
  36. it('fetches profiles', async function () {
  37. MockApiClient.addMockResponse({
  38. url: '/organizations/org-slug/profiling/profiles/',
  39. body: [],
  40. });
  41. const hook = reactHooks.renderHook(useProfiles, {
  42. wrapper: TestContext,
  43. initialProps: {query: '', selection},
  44. });
  45. expect(hook.result.current).toEqual({type: 'loading'});
  46. await hook.waitForNextUpdate();
  47. expect(hook.result.current).toEqual({
  48. type: 'resolved',
  49. data: {traces: [], pageLinks: null},
  50. });
  51. });
  52. });