useFunctions.spec.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. sort: '-p99',
  38. }),
  39. {wrapper: TestContext}
  40. );
  41. expect(hook.result.current).toEqual({type: 'initial'});
  42. });
  43. it('fetches functions legacy', async function () {
  44. MockApiClient.addMockResponse({
  45. url: `/projects/org-slug/${project.slug}/profiling/functions/`,
  46. body: {functions: [{symbol: ''}]}, // only the legacy response contains symbol
  47. });
  48. const hook = reactHooks.renderHook(
  49. () =>
  50. useFunctions({
  51. project,
  52. query: '',
  53. transaction: '',
  54. selection,
  55. sort: '-p99',
  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. functions: [{symbol: ''}],
  65. version: 1,
  66. },
  67. });
  68. });
  69. it('fetches functions', async function () {
  70. MockApiClient.addMockResponse({
  71. url: `/projects/org-slug/${project.slug}/profiling/functions/`,
  72. body: {functions: []},
  73. });
  74. const hook = reactHooks.renderHook(
  75. () =>
  76. useFunctions({
  77. project,
  78. query: '',
  79. transaction: '',
  80. selection,
  81. sort: '-p99',
  82. }),
  83. {wrapper: TestContext}
  84. );
  85. expect(hook.result.current).toEqual({type: 'loading'});
  86. await hook.waitForNextUpdate();
  87. expect(hook.result.current).toEqual({
  88. type: 'resolved',
  89. data: {
  90. functions: [],
  91. pageLinks: null,
  92. version: 2,
  93. },
  94. });
  95. });
  96. });