useProfiles.spec.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import {ReactElement, 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: ReactElement}) {
  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({query: ''}), {
  31. wrapper: TestContext,
  32. });
  33. expect(hook.result.current).toEqual({type: 'initial'});
  34. });
  35. it('fetches profiles', async function () {
  36. MockApiClient.addMockResponse({
  37. url: '/organizations/org-slug/profiling/profiles/',
  38. body: [],
  39. });
  40. const hook = reactHooks.renderHook(() => useProfiles({query: '', selection}), {
  41. wrapper: TestContext,
  42. });
  43. expect(hook.result.current).toEqual({type: 'loading'});
  44. await hook.waitForNextUpdate();
  45. expect(hook.result.current).toEqual({
  46. type: 'resolved',
  47. data: {traces: [], pageLinks: null},
  48. });
  49. });
  50. });