useSortByFields.spec.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {makeTestQueryClient} from 'sentry-test/queryClient';
  4. import {renderHook} from 'sentry-test/reactTestingLibrary';
  5. import type {Organization} from 'sentry/types/organization';
  6. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  7. import {QueryClientProvider} from 'sentry/utils/queryClient';
  8. import {useLocation} from 'sentry/utils/useLocation';
  9. import {PageParamsProvider} from 'sentry/views/explore/contexts/pageParamsContext';
  10. import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
  11. import {SpanTagsProvider} from 'sentry/views/explore/contexts/spanTagsContext';
  12. import {useSortByFields} from 'sentry/views/explore/hooks/useSortByFields';
  13. import {OrganizationContext} from 'sentry/views/organizationContext';
  14. jest.mock('sentry/utils/useLocation');
  15. const mockedUsedLocation = jest.mocked(useLocation);
  16. function createWrapper(organization: Organization) {
  17. return function ({children}: {children?: React.ReactNode}) {
  18. return (
  19. <QueryClientProvider client={makeTestQueryClient()}>
  20. <OrganizationContext.Provider value={organization}>
  21. <PageParamsProvider>
  22. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  23. {children}
  24. </SpanTagsProvider>
  25. </PageParamsProvider>
  26. </OrganizationContext.Provider>
  27. </QueryClientProvider>
  28. );
  29. };
  30. }
  31. describe('useSortByFields', () => {
  32. const organization = OrganizationFixture();
  33. beforeEach(function () {
  34. MockApiClient.clearMockResponses();
  35. MockApiClient.addMockResponse({
  36. url: `/organizations/org-slug/spans/fields/`,
  37. body: [],
  38. });
  39. mockedUsedLocation.mockReturnValue(LocationFixture());
  40. });
  41. it('returns a valid list of field options in samples mode', () => {
  42. const {result} = renderHook(
  43. () =>
  44. useSortByFields({
  45. fields: [
  46. 'id',
  47. 'span.op',
  48. 'span.description',
  49. 'span.duration',
  50. 'transaction',
  51. 'timestamp',
  52. ],
  53. groupBys: [],
  54. yAxes: ['avg(span.duration)'],
  55. mode: Mode.SAMPLES,
  56. }),
  57. {
  58. wrapper: createWrapper(organization),
  59. }
  60. );
  61. expect(result.current.map(field => field.value)).toEqual([
  62. 'id',
  63. 'span.description',
  64. 'span.duration',
  65. 'span.op',
  66. 'timestamp',
  67. 'transaction',
  68. ]);
  69. });
  70. it('returns a valid list of field options in aggregate mode', () => {
  71. const {result} = renderHook(
  72. () =>
  73. useSortByFields({
  74. fields: ['span.op', 'span.description'],
  75. groupBys: ['span.op'],
  76. yAxes: ['avg(span.duration)'],
  77. mode: Mode.AGGREGATE,
  78. }),
  79. {
  80. wrapper: createWrapper(organization),
  81. }
  82. );
  83. expect(result.current.map(field => field.value)).toEqual([
  84. 'avg(span.duration)',
  85. 'span.op',
  86. ]);
  87. });
  88. });