useFunctions.spec.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 {useFunctions} from 'sentry/utils/profiling/hooks/useFunctions';
  6. import {OrganizationContext} from 'sentry/views/organizationContext';
  7. const project = TestStubs.Project();
  8. const selection: PageFilters = {
  9. datetime: {
  10. period: '14d',
  11. utc: null,
  12. start: null,
  13. end: null,
  14. },
  15. environments: [],
  16. projects: [],
  17. };
  18. function TestContext({children}: {children: ReactElement}) {
  19. const {organization} = useMemo(() => initializeOrg(), []);
  20. return (
  21. <OrganizationContext.Provider value={organization}>
  22. {children}
  23. </OrganizationContext.Provider>
  24. );
  25. }
  26. describe('useFunctions', function () {
  27. afterEach(function () {
  28. MockApiClient.clearMockResponses();
  29. });
  30. it('initializes with the loading state', function () {
  31. const hook = reactHooks.renderHook(
  32. () =>
  33. useFunctions({
  34. project,
  35. query: '',
  36. transaction: '',
  37. }),
  38. {wrapper: TestContext}
  39. );
  40. expect(hook.result.current).toEqual({type: 'initial'});
  41. });
  42. it('fetches functions', async function () {
  43. MockApiClient.addMockResponse({
  44. url: `/projects/org-slug/${project.slug}/profiling/functions/`,
  45. body: {
  46. functions: [],
  47. },
  48. });
  49. const hook = reactHooks.renderHook(
  50. () =>
  51. useFunctions({
  52. project,
  53. query: '',
  54. transaction: '',
  55. selection,
  56. }),
  57. {wrapper: TestContext}
  58. );
  59. expect(hook.result.current).toEqual({type: 'loading'});
  60. await hook.waitForNextUpdate();
  61. expect(hook.result.current).toEqual({
  62. type: 'resolved',
  63. data: [],
  64. });
  65. });
  66. });