queries.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import type {PageFilters} from 'sentry/types';
  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. }
  63. describe('Queries', function () {
  64. it('span.duration shall be the default value in the "visualization" field', async function () {
  65. const {organization, project, router} = initializeOrg();
  66. jest.mocked(useLocation).mockReturnValue(router.location);
  67. usePageFilters.mockImplementation(() =>
  68. makeFilterProps({projects: [Number(project.id)]})
  69. );
  70. renderMockRequests({orgSlug: organization.slug, projectId: project.slug});
  71. render(
  72. <MetricsContextProvider>
  73. <Queries />
  74. </MetricsContextProvider>
  75. );
  76. const visualizeInputField = await screen.findByPlaceholderText('Select a metric');
  77. expect(visualizeInputField).toHaveValue('span.duration');
  78. });
  79. it('span.duration shall NOT be the default value in the "visualization" field if query params', async function () {
  80. const {organization, project, router} = initializeOrg();
  81. jest.mocked(useLocation).mockReturnValue({
  82. ...router.location,
  83. query: {
  84. widgets: JSON.stringify([
  85. {
  86. aggregation: 'avg',
  87. condition: undefined,
  88. displayType: 'line',
  89. focusedSeries: undefined,
  90. groupBy: undefined,
  91. id: 0,
  92. isHidden: false,
  93. mri: 'd:transactions/duration@millisecond',
  94. overlays: ['samples'],
  95. powerUserMode: false,
  96. query: '',
  97. sort: {name: undefined, order: 'asc'},
  98. type: 1,
  99. },
  100. ]),
  101. },
  102. });
  103. renderMockRequests({orgSlug: organization.slug, projectId: project.slug});
  104. render(
  105. <MetricsContextProvider>
  106. <Queries />
  107. </MetricsContextProvider>
  108. );
  109. const visualizeInputField = await screen.findByPlaceholderText('Select a metric');
  110. expect(visualizeInputField).toHaveValue('transaction.duration');
  111. });
  112. });