useProfileEvents.spec.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {ReactNode} from 'react';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  4. import {EventsResults} from 'sentry/utils/profiling/hooks/types';
  5. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  6. import {formatSort} from 'sentry/utils/profiling/hooks/utils';
  7. import {QueryClient, QueryClientProvider} from 'sentry/utils/queryClient';
  8. import {OrganizationContext} from 'sentry/views/organizationContext';
  9. const {organization} = initializeOrg();
  10. const client = new QueryClient();
  11. function TestContext({children}: {children?: ReactNode}) {
  12. return (
  13. <QueryClientProvider client={client}>
  14. <OrganizationContext.Provider value={organization}>
  15. {children}
  16. </OrganizationContext.Provider>
  17. </QueryClientProvider>
  18. );
  19. }
  20. describe('useProfileEvents', function () {
  21. afterEach(function () {
  22. MockApiClient.clearMockResponses();
  23. });
  24. it('handles querying the api', async function () {
  25. const fields = ['count()'];
  26. const body: EventsResults<(typeof fields)[number]> = {
  27. data: [],
  28. meta: {fields: {}, units: {}},
  29. };
  30. MockApiClient.addMockResponse({
  31. url: `/organizations/${organization.slug}/events/`,
  32. body,
  33. match: [MockApiClient.matchQuery({dataset: 'profiles'})],
  34. });
  35. const {result, waitFor} = reactHooks.renderHook(useProfileEvents, {
  36. wrapper: TestContext,
  37. initialProps: {
  38. fields,
  39. sort: {key: 'count()', order: 'desc' as const},
  40. referrer: '',
  41. },
  42. });
  43. await waitFor(() => result.current.isSuccess);
  44. expect(result.current.data).toEqual(body);
  45. });
  46. it('handles api errors', async function () {
  47. jest.spyOn(console, 'error').mockImplementation(() => {});
  48. MockApiClient.addMockResponse({
  49. url: `/organizations/${organization.slug}/events/`,
  50. status: 400,
  51. statusCode: 400,
  52. match: [MockApiClient.matchQuery({dataset: 'profiles'})],
  53. });
  54. const {result, waitFor} = reactHooks.renderHook(useProfileEvents, {
  55. wrapper: TestContext,
  56. initialProps: {
  57. fields: ['count()'],
  58. sort: {key: 'count()', order: 'desc' as const},
  59. referrer: '',
  60. },
  61. });
  62. await waitFor(() => result.current.isError);
  63. expect(result.current.status).toEqual('error');
  64. });
  65. });
  66. describe('formatSort', function () {
  67. it('uses the desc fallback', function () {
  68. const sort = formatSort(undefined, ['count()'], {
  69. key: 'count()',
  70. order: 'desc' as const,
  71. });
  72. expect(sort).toEqual({
  73. key: 'count()',
  74. order: 'desc',
  75. });
  76. });
  77. it('uses the asc fallback', function () {
  78. const sort = formatSort(undefined, ['count()'], {
  79. key: 'count()',
  80. order: 'asc' as const,
  81. });
  82. expect(sort).toEqual({
  83. key: 'count()',
  84. order: 'asc',
  85. });
  86. });
  87. it('uses the desc value', function () {
  88. const sort = formatSort('-p95()', ['p95()', 'count()'], {
  89. key: 'count()',
  90. order: 'asc' as const,
  91. });
  92. expect(sort).toEqual({
  93. key: 'p95()',
  94. order: 'desc',
  95. });
  96. });
  97. it('uses the asc value', function () {
  98. const sort = formatSort('p95()', ['p95()', 'count()'], {
  99. key: 'count()',
  100. order: 'desc' as const,
  101. });
  102. expect(sort).toEqual({
  103. key: 'p95()',
  104. order: 'asc',
  105. });
  106. });
  107. });