queries.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import type {PageFilters} from 'sentry/types/core';
  4. import {useLocation} from 'sentry/utils/useLocation';
  5. import importedUsePageFilters from 'sentry/utils/usePageFilters';
  6. import {MetricsContextProvider} from 'sentry/views/metrics/context';
  7. import {Queries} from 'sentry/views/metrics/queries';
  8. jest.mock('sentry/utils/useLocation');
  9. jest.mock('echarts/core', () => {
  10. return {
  11. connect: jest.fn(),
  12. use: jest.fn(),
  13. };
  14. });
  15. jest.mock('sentry/utils/usePageFilters');
  16. const usePageFilters = jest.mocked(importedUsePageFilters);
  17. const makeFilterProps = (
  18. filters: Partial<PageFilters>
  19. ): ReturnType<typeof importedUsePageFilters> => {
  20. return {
  21. isReady: true,
  22. shouldPersist: true,
  23. desyncedFilters: new Set(),
  24. pinnedFilters: new Set(),
  25. selection: {
  26. projects: [1],
  27. environments: ['prod'],
  28. datetime: {start: new Date(), end: new Date(), period: '14d', utc: true},
  29. ...filters,
  30. },
  31. };
  32. };
  33. function renderMockRequests({orgSlug, projectId}: {orgSlug: string; projectId: string}) {
  34. MockApiClient.addMockResponse({
  35. url: `/organizations/${orgSlug}/metrics/meta/`,
  36. body: [
  37. {
  38. type: 'd',
  39. name: 'duration',
  40. unit: 'millisecond',
  41. mri: 'd:transactions/duration@millisecond',
  42. operations: ['avg', 'count'],
  43. projectIds: [projectId],
  44. blockingStatus: [],
  45. },
  46. {
  47. type: 'd',
  48. name: 'duration',
  49. unit: 'millisecond',
  50. mri: 'd:spans/duration@millisecond',
  51. operations: ['avg', 'count'],
  52. projectIds: [projectId],
  53. blockingStatus: [],
  54. },
  55. ],
  56. });
  57. MockApiClient.addMockResponse({
  58. url: `/organizations/${orgSlug}/metrics/tags/`,
  59. method: 'GET',
  60. body: [],
  61. });
  62. MockApiClient.addMockResponse({
  63. method: 'GET',
  64. url: `/organizations/${orgSlug}/recent-searches/`,
  65. body: [],
  66. });
  67. }
  68. describe('Queries', function () {
  69. it('span.duration shall be the default value in the "visualization" field', async function () {
  70. const {organization, project, router} = initializeOrg();
  71. jest.mocked(useLocation).mockReturnValue(router.location);
  72. usePageFilters.mockImplementation(() =>
  73. makeFilterProps({projects: [Number(project.id)]})
  74. );
  75. renderMockRequests({orgSlug: organization.slug, projectId: project.slug});
  76. render(
  77. <MetricsContextProvider>
  78. <Queries />
  79. </MetricsContextProvider>
  80. );
  81. const visualizeInputField = await screen.findByPlaceholderText('Select a metric');
  82. expect(visualizeInputField).toHaveValue('span.duration');
  83. });
  84. it('span.duration shall NOT be the default value in the "visualization" field if query params', async function () {
  85. const {organization, project, router} = initializeOrg();
  86. jest.mocked(useLocation).mockReturnValue({
  87. ...router.location,
  88. query: {
  89. widgets: JSON.stringify([
  90. {
  91. aggregation: 'avg',
  92. condition: undefined,
  93. displayType: 'line',
  94. focusedSeries: undefined,
  95. groupBy: undefined,
  96. id: 0,
  97. isHidden: false,
  98. mri: 'd:transactions/duration@millisecond',
  99. overlays: ['samples'],
  100. powerUserMode: false,
  101. query: '',
  102. sort: {name: undefined, order: 'asc'},
  103. type: 1,
  104. },
  105. ]),
  106. },
  107. });
  108. renderMockRequests({orgSlug: organization.slug, projectId: project.slug});
  109. render(
  110. <MetricsContextProvider>
  111. <Queries />
  112. </MetricsContextProvider>
  113. );
  114. const visualizeInputField = await screen.findByPlaceholderText('Select a metric');
  115. expect(visualizeInputField).toHaveValue('transaction.duration');
  116. });
  117. });