dashboardList.spec.jsx 8.0 KB

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