useFunctions.spec.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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', async function () {
  44. MockApiClient.addMockResponse({
  45. url: `/projects/org-slug/${project.slug}/profiling/functions/`,
  46. body: {functions: []},
  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: [],
  65. pageLinks: null,
  66. },
  67. });
  68. });
  69. it('fetches application functions', async function () {
  70. const mock = MockApiClient.addMockResponse({
  71. url: `/projects/org-slug/${project.slug}/profiling/functions/`,
  72. body: {functions: []},
  73. match: [MockApiClient.matchQuery({is_application: '1'})],
  74. });
  75. const hook = reactHooks.renderHook(
  76. () =>
  77. useFunctions({
  78. functionType: 'application',
  79. project,
  80. query: '',
  81. transaction: '',
  82. selection,
  83. sort: '-p99',
  84. }),
  85. {wrapper: TestContext}
  86. );
  87. expect(hook.result.current).toEqual({type: 'loading'});
  88. await hook.waitForNextUpdate();
  89. expect(hook.result.current).toEqual({
  90. type: 'resolved',
  91. data: {
  92. functions: [],
  93. pageLinks: null,
  94. },
  95. });
  96. expect(mock).toHaveBeenCalledTimes(1);
  97. });
  98. it('fetches system functions', async function () {
  99. const mock = MockApiClient.addMockResponse({
  100. url: `/projects/org-slug/${project.slug}/profiling/functions/`,
  101. body: {functions: []},
  102. match: [MockApiClient.matchQuery({is_application: '0'})],
  103. });
  104. const hook = reactHooks.renderHook(
  105. () =>
  106. useFunctions({
  107. functionType: 'system',
  108. project,
  109. query: '',
  110. transaction: '',
  111. selection,
  112. sort: '-p99',
  113. }),
  114. {wrapper: TestContext}
  115. );
  116. expect(hook.result.current).toEqual({type: 'loading'});
  117. await hook.waitForNextUpdate();
  118. expect(hook.result.current).toEqual({
  119. type: 'resolved',
  120. data: {
  121. functions: [],
  122. pageLinks: null,
  123. },
  124. });
  125. expect(mock).toHaveBeenCalledTimes(1);
  126. });
  127. it('fetches all functions', async function () {
  128. const mock = MockApiClient.addMockResponse({
  129. url: `/projects/org-slug/${project.slug}/profiling/functions/`,
  130. body: {functions: []},
  131. match: [MockApiClient.matchQuery({is_application: undefined})],
  132. });
  133. const hook = reactHooks.renderHook(
  134. () =>
  135. useFunctions({
  136. functionType: 'all',
  137. project,
  138. query: '',
  139. transaction: '',
  140. selection,
  141. sort: '-p99',
  142. }),
  143. {wrapper: TestContext}
  144. );
  145. expect(hook.result.current).toEqual({type: 'loading'});
  146. await hook.waitForNextUpdate();
  147. expect(hook.result.current).toEqual({
  148. type: 'resolved',
  149. data: {
  150. functions: [],
  151. pageLinks: null,
  152. },
  153. });
  154. expect(mock).toHaveBeenCalledTimes(1);
  155. });
  156. });