queryList.spec.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import {act} from 'react-dom/test-utils';
  2. import {browserHistory} from 'react-router';
  3. import {selectDropdownMenuItem} from 'sentry-test/dropdownMenu';
  4. import {mountWithTheme} from 'sentry-test/enzyme';
  5. import {triggerPress} from 'sentry-test/utils';
  6. import {openAddToDashboardModal} from 'sentry/actionCreators/modal';
  7. import {DisplayModes} from 'sentry/utils/discover/types';
  8. import {DashboardWidgetSource, DisplayType} from 'sentry/views/dashboardsV2/types';
  9. import QueryList from 'sentry/views/eventsV2/queryList';
  10. jest.mock('sentry/actionCreators/modal');
  11. describe('EventsV2 > QueryList', function () {
  12. let location, savedQueries, organization, deleteMock, duplicateMock, queryChangeMock;
  13. beforeAll(async function () {
  14. await import('sentry/components/modals/widgetBuilder/addToDashboardModal');
  15. });
  16. beforeEach(function () {
  17. organization = TestStubs.Organization();
  18. savedQueries = [
  19. TestStubs.DiscoverSavedQuery(),
  20. TestStubs.DiscoverSavedQuery({name: 'saved query 2', id: '2'}),
  21. ];
  22. MockApiClient.addMockResponse({
  23. url: '/organizations/org-slug/events-stats/',
  24. method: 'GET',
  25. statusCode: 200,
  26. body: {data: []},
  27. });
  28. deleteMock = MockApiClient.addMockResponse({
  29. url: '/organizations/org-slug/discover/saved/2/',
  30. method: 'DELETE',
  31. statusCode: 200,
  32. body: {},
  33. });
  34. duplicateMock = MockApiClient.addMockResponse({
  35. url: '/organizations/org-slug/discover/saved/',
  36. method: 'POST',
  37. body: {
  38. id: '3',
  39. name: 'Saved query copy',
  40. },
  41. });
  42. location = {
  43. pathname: '/organizations/org-slug/discover/queries/',
  44. query: {cursor: '0:1:1', statsPeriod: '14d'},
  45. };
  46. queryChangeMock = jest.fn();
  47. });
  48. afterEach(() => {
  49. jest.clearAllMocks();
  50. });
  51. it('renders an empty list', function () {
  52. const wrapper = mountWithTheme(
  53. <QueryList
  54. organization={organization}
  55. savedQueries={[]}
  56. savedQuerySearchQuery="no matches"
  57. pageLinks=""
  58. onQueryChange={queryChangeMock}
  59. location={location}
  60. />
  61. );
  62. const content = wrapper.find('QueryCard');
  63. // No queries
  64. expect(content).toHaveLength(0);
  65. expect(wrapper.find('EmptyStateWarning')).toHaveLength(1);
  66. });
  67. it('renders pre-built queries and saved ones', function () {
  68. const wrapper = mountWithTheme(
  69. <QueryList
  70. organization={organization}
  71. savedQueries={savedQueries}
  72. renderPrebuilt
  73. pageLinks=""
  74. onQueryChange={queryChangeMock}
  75. location={location}
  76. />
  77. );
  78. const content = wrapper.find('QueryCard');
  79. // pre built + saved queries
  80. expect(content).toHaveLength(5);
  81. });
  82. it('can duplicate and trigger change callback', async function () {
  83. const wrapper = mountWithTheme(
  84. <QueryList
  85. organization={organization}
  86. savedQueries={savedQueries}
  87. pageLinks=""
  88. onQueryChange={queryChangeMock}
  89. location={location}
  90. />
  91. );
  92. const card = wrapper.find('QueryCard').last();
  93. expect(card.find('QueryCardContent').text()).toEqual(savedQueries[1].name);
  94. await selectDropdownMenuItem({
  95. wrapper,
  96. specifiers: {prefix: 'QueryCard', last: true},
  97. itemKey: 'duplicate',
  98. });
  99. expect(duplicateMock).toHaveBeenCalled();
  100. expect(queryChangeMock).toHaveBeenCalled();
  101. });
  102. it('can delete and trigger change callback', async function () {
  103. const wrapper = mountWithTheme(
  104. <QueryList
  105. organization={organization}
  106. savedQueries={savedQueries}
  107. pageLinks=""
  108. onQueryChange={queryChangeMock}
  109. location={location}
  110. />
  111. );
  112. const card = wrapper.find('QueryCard').last();
  113. expect(card.find('QueryCardContent').text()).toEqual(savedQueries[1].name);
  114. await selectDropdownMenuItem({
  115. wrapper,
  116. specifiers: {prefix: 'QueryCard', last: true},
  117. itemKey: 'delete',
  118. });
  119. expect(deleteMock).toHaveBeenCalled();
  120. expect(queryChangeMock).toHaveBeenCalled();
  121. });
  122. it('returns short url location for saved query', function () {
  123. const wrapper = mountWithTheme(
  124. <QueryList
  125. organization={organization}
  126. savedQueries={savedQueries}
  127. pageLinks=""
  128. onQueryChange={queryChangeMock}
  129. location={location}
  130. />
  131. );
  132. const card = wrapper.find('QueryCard').last();
  133. const link = card.find('Link').last().prop('to');
  134. expect(link.pathname).toEqual('/organizations/org-slug/discover/results/');
  135. expect(link.query).toEqual({
  136. id: '2',
  137. statsPeriod: '14d',
  138. });
  139. });
  140. it('can redirect on last query deletion', async function () {
  141. const wrapper = mountWithTheme(
  142. <QueryList
  143. organization={organization}
  144. savedQueries={savedQueries.slice(1)}
  145. pageLinks=""
  146. onQueryChange={queryChangeMock}
  147. location={location}
  148. />
  149. );
  150. const card = wrapper.find('QueryCard').last();
  151. expect(card.find('QueryCardContent').text()).toEqual(savedQueries[1].name);
  152. await selectDropdownMenuItem({
  153. wrapper,
  154. specifiers: {prefix: 'QueryCard', last: true},
  155. itemKey: 'delete',
  156. });
  157. expect(deleteMock).toHaveBeenCalled();
  158. expect(queryChangeMock).not.toHaveBeenCalled();
  159. expect(browserHistory.push).toHaveBeenCalledWith({
  160. pathname: location.pathname,
  161. query: {cursor: undefined, statsPeriod: '14d'},
  162. });
  163. });
  164. it('renders Add to Dashboard in context menu with feature flag', async function () {
  165. const featuredOrganization = TestStubs.Organization({
  166. features: ['dashboards-edit'],
  167. });
  168. const wrapper = mountWithTheme(
  169. <QueryList
  170. organization={featuredOrganization}
  171. savedQueries={savedQueries.slice(1)}
  172. pageLinks=""
  173. onQueryChange={queryChangeMock}
  174. location={location}
  175. />
  176. );
  177. let card = wrapper.find('QueryCard').last();
  178. await act(async () => {
  179. triggerPress(card.find('DropdownTrigger'));
  180. await tick();
  181. wrapper.update();
  182. });
  183. card = wrapper.find('QueryCard').last();
  184. const menuItems = card.find('MenuItemWrap');
  185. expect(menuItems.length).toEqual(3);
  186. expect(menuItems.at(0).text()).toEqual('Add to Dashboard');
  187. expect(menuItems.at(1).text()).toEqual('Duplicate Query');
  188. expect(menuItems.at(2).text()).toEqual('Delete Query');
  189. });
  190. it('only renders Delete Query and Duplicate Query in context menu', async function () {
  191. const wrapper = mountWithTheme(
  192. <QueryList
  193. organization={organization}
  194. savedQueries={savedQueries.slice(1)}
  195. pageLinks=""
  196. onQueryChange={queryChangeMock}
  197. location={location}
  198. />
  199. );
  200. let card = wrapper.find('QueryCard').last();
  201. await act(async () => {
  202. triggerPress(card.find('DropdownTrigger'));
  203. await tick();
  204. wrapper.update();
  205. });
  206. card = wrapper.find('QueryCard').last();
  207. const menuItems = card.find('MenuItemWrap');
  208. expect(menuItems.length).toEqual(2);
  209. expect(menuItems.at(0).text()).toEqual('Duplicate Query');
  210. expect(menuItems.at(1).text()).toEqual('Delete Query');
  211. });
  212. it('passes yAxis from the savedQuery to MiniGraph', function () {
  213. const featuredOrganization = TestStubs.Organization({
  214. features: ['dashboards-edit'],
  215. });
  216. const yAxis = ['count()', 'failure_count()'];
  217. const savedQueryWithMultiYAxis = {
  218. ...savedQueries.slice(1)[0],
  219. yAxis,
  220. };
  221. const wrapper = mountWithTheme(
  222. <QueryList
  223. organization={featuredOrganization}
  224. savedQueries={[savedQueryWithMultiYAxis]}
  225. pageLinks=""
  226. onQueryChange={queryChangeMock}
  227. location={location}
  228. />
  229. );
  230. const miniGraph = wrapper.find('MiniGraph');
  231. expect(miniGraph.props().yAxis).toEqual(['count()', 'failure_count()']);
  232. });
  233. describe('Add to Dashboard modal', () => {
  234. it('opens a modal with the correct params for Top 5 chart', async function () {
  235. const featuredOrganization = TestStubs.Organization({
  236. features: ['dashboards-edit', 'new-widget-builder-experience-design'],
  237. });
  238. const wrapper = mountWithTheme(
  239. <QueryList
  240. organization={featuredOrganization}
  241. savedQueries={[
  242. TestStubs.DiscoverSavedQuery({
  243. display: DisplayModes.TOP5,
  244. orderby: 'test',
  245. fields: ['test', 'count()'],
  246. yAxis: ['count()'],
  247. }),
  248. ]}
  249. pageLinks=""
  250. onQueryChange={queryChangeMock}
  251. location={location}
  252. />
  253. );
  254. let card = wrapper.find('QueryCard').last();
  255. await act(async () => {
  256. triggerPress(card.find('DropdownTrigger'));
  257. await tick();
  258. wrapper.update();
  259. });
  260. card = wrapper.find('QueryCard').last();
  261. const menuItems = card.find('MenuItemWrap');
  262. expect(menuItems.length).toEqual(3);
  263. expect(menuItems.at(0).text()).toEqual('Add to Dashboard');
  264. await act(async () => {
  265. triggerPress(menuItems.at(0));
  266. await tick();
  267. wrapper.update();
  268. });
  269. expect(openAddToDashboardModal).toHaveBeenCalledWith(
  270. expect.objectContaining({
  271. widget: {
  272. title: 'Saved query #1',
  273. displayType: DisplayType.AREA,
  274. limit: 5,
  275. queries: [
  276. {
  277. aggregates: ['count()'],
  278. columns: ['test'],
  279. conditions: '',
  280. fields: ['test', 'count()', 'count()'],
  281. name: '',
  282. orderby: 'test',
  283. },
  284. ],
  285. },
  286. widgetAsQueryParams: expect.objectContaining({
  287. defaultTableColumns: ['test', 'count()'],
  288. defaultTitle: 'Saved query #1',
  289. defaultWidgetQuery:
  290. 'name=&aggregates=count()&columns=test&fields=test%2Ccount()%2Ccount()&conditions=&orderby=test',
  291. displayType: DisplayType.AREA,
  292. source: DashboardWidgetSource.DISCOVERV2,
  293. }),
  294. })
  295. );
  296. });
  297. it('opens a modal with the correct params for other chart', async function () {
  298. const featuredOrganization = TestStubs.Organization({
  299. features: ['dashboards-edit', 'new-widget-builder-experience-design'],
  300. });
  301. const wrapper = mountWithTheme(
  302. <QueryList
  303. organization={featuredOrganization}
  304. savedQueries={[
  305. TestStubs.DiscoverSavedQuery({
  306. display: DisplayModes.DEFAULT,
  307. orderby: 'count()',
  308. fields: ['test', 'count()'],
  309. yAxis: ['count()'],
  310. }),
  311. ]}
  312. pageLinks=""
  313. onQueryChange={queryChangeMock}
  314. location={location}
  315. />
  316. );
  317. let card = wrapper.find('QueryCard').last();
  318. await act(async () => {
  319. triggerPress(card.find('DropdownTrigger'));
  320. await tick();
  321. wrapper.update();
  322. });
  323. card = wrapper.find('QueryCard').last();
  324. const menuItems = card.find('MenuItemWrap');
  325. expect(menuItems.length).toEqual(3);
  326. expect(menuItems.at(0).text()).toEqual('Add to Dashboard');
  327. await act(async () => {
  328. triggerPress(menuItems.at(0));
  329. await tick();
  330. wrapper.update();
  331. });
  332. expect(openAddToDashboardModal).toHaveBeenCalledWith(
  333. expect.objectContaining({
  334. widget: {
  335. title: 'Saved query #1',
  336. displayType: DisplayType.LINE,
  337. queries: [
  338. {
  339. aggregates: ['count()'],
  340. columns: [],
  341. conditions: '',
  342. fields: ['count()'],
  343. name: '',
  344. // Orderby gets dropped because ordering only applies to
  345. // Top-N and tables
  346. orderby: '',
  347. },
  348. ],
  349. },
  350. widgetAsQueryParams: expect.objectContaining({
  351. defaultTableColumns: ['test', 'count()'],
  352. defaultTitle: 'Saved query #1',
  353. defaultWidgetQuery:
  354. 'name=&aggregates=count()&columns=&fields=count()&conditions=&orderby=',
  355. displayType: DisplayType.LINE,
  356. source: DashboardWidgetSource.DISCOVERV2,
  357. }),
  358. })
  359. );
  360. });
  361. });
  362. });