dashboardList.spec.jsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import DashboardList from 'app/views/dashboardsV2/manage/dashboardList';
  3. function openContextMenu(card) {
  4. card.find('DropdownMenu MoreOptions svg').simulate('click');
  5. }
  6. function clickMenuItem(card, selector) {
  7. card.find(`DropdownMenu MenuItem[data-test-id="${selector}"]`).simulate('click');
  8. }
  9. describe('Dashboards > DashboardList', function () {
  10. let dashboards, widgets, deleteMock, dashboardUpdateMock, createMock;
  11. const organization = TestStubs.Organization({
  12. features: ['global-views', 'dashboards-basic', 'dashboards-edit', 'discover-query'],
  13. projects: [TestStubs.Project()],
  14. });
  15. beforeEach(function () {
  16. MockApiClient.addMockResponse({
  17. url: '/organizations/org-slug/projects/',
  18. body: [],
  19. });
  20. widgets = [
  21. TestStubs.Widget(
  22. [{name: '', conditions: 'event.type:error', fields: ['count()']}],
  23. {
  24. title: 'Errors',
  25. interval: '1d',
  26. id: '1',
  27. }
  28. ),
  29. TestStubs.Widget(
  30. [{name: '', conditions: 'event.type:transaction', fields: ['count()']}],
  31. {
  32. title: 'Transactions',
  33. interval: '1d',
  34. id: '2',
  35. }
  36. ),
  37. TestStubs.Widget(
  38. [
  39. {
  40. name: '',
  41. conditions: 'event.type:transaction transaction:/api/cats',
  42. fields: ['p50()'],
  43. },
  44. ],
  45. {
  46. title: 'p50 of /api/cats',
  47. interval: '1d',
  48. id: '3',
  49. }
  50. ),
  51. ];
  52. dashboards = [
  53. TestStubs.Dashboard([], {
  54. id: '1',
  55. title: 'Dashboard 1',
  56. dateCreated: '2021-04-19T13:13:23.962105Z',
  57. createdBy: {id: '1'},
  58. widgetDisplay: [],
  59. }),
  60. TestStubs.Dashboard(widgets, {
  61. id: '2',
  62. title: 'Dashboard 2',
  63. dateCreated: '2021-04-19T13:13:23.962105Z',
  64. createdBy: {id: '1'},
  65. widgetDisplay: ['line', 'table'],
  66. }),
  67. ];
  68. deleteMock = MockApiClient.addMockResponse({
  69. url: '/organizations/org-slug/dashboards/2/',
  70. method: 'DELETE',
  71. statusCode: 200,
  72. });
  73. MockApiClient.addMockResponse({
  74. url: '/organizations/org-slug/dashboards/2/',
  75. method: 'GET',
  76. statusCode: 200,
  77. body: {
  78. id: '2',
  79. title: 'Dashboard Demo',
  80. widgets: [
  81. {
  82. id: '1',
  83. title: 'Errors',
  84. displayType: 'big_number',
  85. interval: '5m',
  86. },
  87. {
  88. id: '2',
  89. title: 'Transactions',
  90. displayType: 'big_number',
  91. interval: '5m',
  92. },
  93. {
  94. id: '3',
  95. title: 'p50 of /api/cat',
  96. displayType: 'big_number',
  97. interval: '5m',
  98. },
  99. ],
  100. },
  101. });
  102. createMock = MockApiClient.addMockResponse({
  103. url: '/organizations/org-slug/dashboards/',
  104. method: 'POST',
  105. statusCode: 200,
  106. });
  107. dashboardUpdateMock = jest.fn();
  108. });
  109. afterEach(function () {
  110. MockApiClient.clearMockResponses();
  111. });
  112. it('renders an empty list', function () {
  113. const wrapper = mountWithTheme(
  114. <DashboardList
  115. organization={organization}
  116. dashboards={[]}
  117. pageLinks=""
  118. location={{query: {}}}
  119. />
  120. );
  121. const content = wrapper.find('DashboardCard');
  122. // No dashboards
  123. expect(content).toHaveLength(0);
  124. expect(wrapper.find('EmptyStateWarning')).toHaveLength(1);
  125. });
  126. it('renders dashboard list', function () {
  127. const wrapper = mountWithTheme(
  128. <DashboardList
  129. organization={organization}
  130. dashboards={dashboards}
  131. pageLinks=""
  132. location={{query: {}}}
  133. />
  134. );
  135. const content = wrapper.find('DashboardCard');
  136. expect(content).toHaveLength(2);
  137. });
  138. it('returns landing page url for dashboards', function () {
  139. const wrapper = mountWithTheme(
  140. <DashboardList
  141. organization={organization}
  142. dashboards={dashboards}
  143. pageLinks=""
  144. location={{query: {}}}
  145. />
  146. );
  147. const card = wrapper.find('DashboardCard').last();
  148. const link = card.find('Link').last().prop('to');
  149. expect(link.pathname).toEqual(`/organizations/org-slug/dashboard/2/`);
  150. });
  151. it('persists global selection headers', function () {
  152. const wrapper = mountWithTheme(
  153. <DashboardList
  154. organization={organization}
  155. dashboards={dashboards}
  156. pageLinks=""
  157. location={{query: {statsPeriod: '7d'}}}
  158. />
  159. );
  160. const card = wrapper.find('DashboardCard').last();
  161. const link = card.find('Link').last().prop('to');
  162. expect(link.pathname).toEqual(`/organizations/org-slug/dashboard/2/`);
  163. expect(link.query).toEqual({statsPeriod: '7d'});
  164. });
  165. it('can delete dashboards', async function () {
  166. const wrapper = mountWithTheme(
  167. <DashboardList
  168. organization={organization}
  169. dashboards={dashboards}
  170. pageLinks=""
  171. location={{query: {}}}
  172. onDashboardsChange={dashboardUpdateMock}
  173. />
  174. );
  175. let card = wrapper.find('DashboardCard').last();
  176. expect(card.find('Title').text()).toEqual(dashboards[1].title);
  177. openContextMenu(card);
  178. wrapper.update();
  179. card = wrapper.find('DashboardCard').last();
  180. clickMenuItem(card, 'dashboard-delete');
  181. await tick();
  182. expect(deleteMock).toHaveBeenCalled();
  183. expect(dashboardUpdateMock).toHaveBeenCalled();
  184. });
  185. it('cannot delete last dashboard', async function () {
  186. const singleDashboard = [
  187. TestStubs.Dashboard([], {
  188. id: '1',
  189. title: 'Dashboard 1',
  190. dateCreated: '2021-04-19T13:13:23.962105Z',
  191. createdBy: {id: '1'},
  192. widgetDisplay: [],
  193. }),
  194. ];
  195. const wrapper = mountWithTheme(
  196. <DashboardList
  197. organization={organization}
  198. dashboards={singleDashboard}
  199. pageLinks=""
  200. location={{query: {}}}
  201. onDashboardsChange={dashboardUpdateMock}
  202. />
  203. );
  204. let card = wrapper.find('DashboardCard').last();
  205. expect(card.find('Title').text()).toEqual(singleDashboard[0].title);
  206. openContextMenu(card);
  207. wrapper.update();
  208. card = wrapper.find('DashboardCard').last();
  209. const dashboardDelete = card.find(
  210. `DropdownMenu MenuItem[data-test-id="dashboard-delete"]`
  211. );
  212. await tick();
  213. expect(dashboardDelete.prop('disabled')).toBe(true);
  214. });
  215. it('can duplicate dashboards', async function () {
  216. const wrapper = mountWithTheme(
  217. <DashboardList
  218. organization={organization}
  219. dashboards={dashboards}
  220. pageLinks=""
  221. location={{query: {}}}
  222. onDashboardsChange={dashboardUpdateMock}
  223. />
  224. );
  225. let card = wrapper.find('DashboardCard').last();
  226. expect(card.find('Title').text()).toEqual(dashboards[1].title);
  227. openContextMenu(card);
  228. wrapper.update();
  229. card = wrapper.find('DashboardCard').last();
  230. clickMenuItem(card, 'dashboard-duplicate');
  231. await tick();
  232. expect(createMock).toHaveBeenCalled();
  233. expect(dashboardUpdateMock).toHaveBeenCalled();
  234. });
  235. });