dashboardWidgetQuerySelectorModal.spec.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {Client} from 'sentry/api';
  4. import DashboardWidgetQuerySelectorModal from 'sentry/components/modals/dashboardWidgetQuerySelectorModal';
  5. import {t} from 'sentry/locale';
  6. import {DisplayType} from 'sentry/views/dashboardsV2/types';
  7. const stubEl: any = (props: any) => <div>{props.children}</div>;
  8. const api: Client = new Client();
  9. function mountModal({initialData, widget}) {
  10. return mountWithTheme(
  11. <DashboardWidgetQuerySelectorModal
  12. Header={stubEl}
  13. Footer={stubEl}
  14. Body={stubEl}
  15. CloseButton={stubEl}
  16. closeModal={() => {}}
  17. organization={initialData.organization}
  18. widget={widget}
  19. api={api}
  20. />,
  21. initialData.routerContext
  22. );
  23. }
  24. describe('Modals -> AddDashboardWidgetModal', function () {
  25. const initialData = initializeOrg({
  26. organization: {
  27. features: ['performance-view', 'discover-query'],
  28. apdexThreshold: 400,
  29. },
  30. router: {},
  31. project: 1,
  32. projects: [],
  33. });
  34. let mockQuery;
  35. let mockWidget;
  36. beforeEach(function () {
  37. mockQuery = {
  38. conditions: 'title:/organizations/:orgId/performance/summary/',
  39. fields: ['count()', 'failure_count()'],
  40. aggregates: ['count()', 'failure_count()'],
  41. columns: [],
  42. id: '1',
  43. name: 'Query Name',
  44. orderby: '',
  45. };
  46. mockWidget = {
  47. title: 'Test Widget',
  48. displayType: DisplayType.AREA,
  49. interval: '5m',
  50. queries: [mockQuery],
  51. };
  52. MockApiClient.addMockResponse({
  53. url: '/organizations/org-slug/dashboards/widgets/',
  54. method: 'POST',
  55. statusCode: 200,
  56. body: [],
  57. });
  58. MockApiClient.addMockResponse({
  59. url: '/organizations/org-slug/events-stats/',
  60. body: [],
  61. });
  62. MockApiClient.addMockResponse({
  63. url: '/organizations/org-slug/eventsv2/',
  64. body: {data: [{'event.type': 'error'}], meta: {'event.type': 'string'}},
  65. });
  66. MockApiClient.addMockResponse({
  67. url: '/organizations/org-slug/events-geo/',
  68. body: {data: [], meta: {}},
  69. });
  70. MockApiClient.addMockResponse({
  71. url: '/organizations/org-slug/recent-searches/',
  72. body: [],
  73. });
  74. MockApiClient.addMockResponse({
  75. url: '/organizations/org-slug/dashboards/',
  76. body: [{id: '1', title: t('Test Dashboard')}],
  77. });
  78. });
  79. afterEach(() => {
  80. MockApiClient.clearMockResponses();
  81. });
  82. it('renders a single query selection when the widget only has one query', async function () {
  83. const wrapper = mountModal({initialData, widget: mockWidget});
  84. await tick();
  85. expect(wrapper.find('StyledInput').length).toEqual(1);
  86. expect(wrapper.find('StyledInput').props().value).toEqual(
  87. 'title:/organizations/:orgId/performance/summary/'
  88. );
  89. expect(wrapper.find('OpenInDiscoverButton').length).toEqual(1);
  90. wrapper.unmount();
  91. });
  92. it('renders a multiple query selections when the widget only has multiple queries', async function () {
  93. mockWidget.queries.push({
  94. ...mockQuery,
  95. conditions: 'title:/organizations/:orgId/performance/',
  96. id: '2',
  97. });
  98. mockWidget.queries.push({
  99. ...mockQuery,
  100. conditions: 'title:/organizations/:orgId/',
  101. id: '3',
  102. });
  103. const wrapper = mountModal({initialData, widget: mockWidget});
  104. await tick();
  105. expect(wrapper.find('StyledInput').length).toEqual(3);
  106. expect(wrapper.find('StyledInput').at(0).props().value).toEqual(
  107. 'title:/organizations/:orgId/performance/summary/'
  108. );
  109. expect(wrapper.find('StyledInput').at(1).props().value).toEqual(
  110. 'title:/organizations/:orgId/performance/'
  111. );
  112. expect(wrapper.find('StyledInput').at(2).props().value).toEqual(
  113. 'title:/organizations/:orgId/'
  114. );
  115. expect(wrapper.find('OpenInDiscoverButton').length).toEqual(3);
  116. wrapper.unmount();
  117. });
  118. it('links user to the query in discover when a query is selected from the modal', async function () {
  119. const wrapper = mountModal({initialData, widget: mockWidget});
  120. await tick();
  121. expect(wrapper.find('QueryContainer').find('Link').props().to).toEqual(
  122. '/organizations/org-slug/discover/results/?field=count%28%29&field=failure_count%28%29&name=Test%20Widget&query=title%3A%2Forganizations%2F%3AorgId%2Fperformance%2Fsummary%2F&statsPeriod=14d&yAxis=count%28%29&yAxis=failure_count%28%29'
  123. );
  124. wrapper.unmount();
  125. });
  126. it('links user to the query in discover with additional field when a world map query is selected from the modal', async function () {
  127. mockWidget.queries[0].fields = ['count()'];
  128. mockWidget.queries[0].aggregates = ['count()'];
  129. mockWidget.displayType = DisplayType.WORLD_MAP;
  130. const wrapper = mountModal({initialData, widget: mockWidget});
  131. await tick();
  132. expect(wrapper.find('QueryContainer').find('Link').props().to).toEqual(
  133. '/organizations/org-slug/discover/results/?display=worldmap&field=geo.country_code&field=count%28%29&name=Test%20Widget&query=title%3A%2Forganizations%2F%3AorgId%2Fperformance%2Fsummary%2F%20has%3Ageo.country_code&statsPeriod=14d&yAxis=count%28%29'
  134. );
  135. wrapper.unmount();
  136. });
  137. });