dashboard.spec.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import Dashboard from 'sentry/views/dashboardsV2/dashboard';
  4. import {DisplayType, WidgetType} from 'sentry/views/dashboardsV2/types';
  5. describe('Dashboards > Dashboard', () => {
  6. const organization = TestStubs.Organization({
  7. features: ['dashboards-basic', 'dashboards-edit'],
  8. });
  9. const mockDashboard = {
  10. dateCreated: '2021-08-10T21:20:46.798237Z',
  11. id: '1',
  12. title: 'Test Dashboard',
  13. widgets: [],
  14. projects: [],
  15. filters: {},
  16. };
  17. const newWidget = {
  18. title: 'Test Query',
  19. displayType: DisplayType.LINE,
  20. widgetType: WidgetType.DISCOVER,
  21. interval: '5m',
  22. queries: [
  23. {
  24. name: '',
  25. conditions: '',
  26. fields: ['count()'],
  27. aggregates: ['count()'],
  28. columns: [],
  29. orderby: '',
  30. },
  31. ],
  32. };
  33. let initialData;
  34. beforeEach(() => {
  35. initialData = initializeOrg({organization, router: {}, project: 1, projects: []});
  36. MockApiClient.addMockResponse({
  37. url: `/organizations/org-slug/dashboards/widgets/`,
  38. method: 'POST',
  39. body: [],
  40. });
  41. MockApiClient.addMockResponse({
  42. url: '/organizations/org-slug/users/',
  43. method: 'GET',
  44. body: [],
  45. });
  46. MockApiClient.addMockResponse({
  47. url: '/organizations/org-slug/tags/',
  48. method: 'GET',
  49. body: TestStubs.Tags(),
  50. });
  51. });
  52. it('dashboard adds new widget if component is mounted with newWidget prop', async () => {
  53. const mockHandleAddCustomWidget = jest.fn();
  54. const mockCallbackToUnsetNewWidget = jest.fn();
  55. const wrapper = mountWithTheme(
  56. <Dashboard
  57. paramDashboardId="1"
  58. dashboard={mockDashboard}
  59. organization={initialData.organization}
  60. isEditing={false}
  61. onUpdate={() => undefined}
  62. handleUpdateWidgetList={() => undefined}
  63. handleAddCustomWidget={mockHandleAddCustomWidget}
  64. router={initialData.router}
  65. location={initialData.location}
  66. newWidget={newWidget}
  67. widgetLimitReached={false}
  68. onSetNewWidget={mockCallbackToUnsetNewWidget}
  69. />,
  70. initialData.routerContext
  71. );
  72. await tick();
  73. wrapper.update();
  74. expect(mockHandleAddCustomWidget).toHaveBeenCalled();
  75. expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled();
  76. });
  77. it('dashboard adds new widget if component updated with newWidget prop', async () => {
  78. const mockHandleAddCustomWidget = jest.fn();
  79. const mockCallbackToUnsetNewWidget = jest.fn();
  80. const wrapper = mountWithTheme(
  81. <Dashboard
  82. paramDashboardId="1"
  83. dashboard={mockDashboard}
  84. organization={initialData.organization}
  85. isEditing={false}
  86. onUpdate={() => undefined}
  87. handleUpdateWidgetList={() => undefined}
  88. handleAddCustomWidget={mockHandleAddCustomWidget}
  89. router={initialData.router}
  90. location={initialData.location}
  91. widgetLimitReached={false}
  92. onSetNewWidget={mockCallbackToUnsetNewWidget}
  93. />,
  94. initialData.routerContext
  95. );
  96. expect(mockHandleAddCustomWidget).not.toHaveBeenCalled();
  97. expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled();
  98. wrapper.setProps({newWidget});
  99. await tick();
  100. wrapper.update();
  101. expect(mockHandleAddCustomWidget).toHaveBeenCalled();
  102. expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled();
  103. });
  104. it('dashboard does not try to add new widget if no newWidget', async () => {
  105. const mockHandleAddCustomWidget = jest.fn();
  106. const mockCallbackToUnsetNewWidget = jest.fn();
  107. const wrapper = mountWithTheme(
  108. <Dashboard
  109. paramDashboardId="1"
  110. dashboard={mockDashboard}
  111. organization={initialData.organization}
  112. isEditing={false}
  113. onUpdate={() => undefined}
  114. handleUpdateWidgetList={() => undefined}
  115. handleAddCustomWidget={mockHandleAddCustomWidget}
  116. router={initialData.router}
  117. location={initialData.location}
  118. widgetLimitReached={false}
  119. onSetNewWidget={mockCallbackToUnsetNewWidget}
  120. />,
  121. initialData.routerContext
  122. );
  123. await tick();
  124. wrapper.update();
  125. expect(mockHandleAddCustomWidget).not.toHaveBeenCalled();
  126. expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled();
  127. });
  128. it('displays widgets with drag handle when in edit mode', () => {
  129. const dashboardWithOneWidget = {...mockDashboard, widgets: [newWidget]};
  130. const wrapper = mountWithTheme(
  131. <Dashboard
  132. paramDashboardId="1"
  133. dashboard={dashboardWithOneWidget}
  134. organization={initialData.organization}
  135. onUpdate={() => undefined}
  136. handleUpdateWidgetList={() => undefined}
  137. handleAddCustomWidget={() => undefined}
  138. router={initialData.router}
  139. location={initialData.router.location}
  140. widgetLimitReached={false}
  141. isEditing
  142. />,
  143. initialData.routerContext
  144. );
  145. expect(wrapper.find('GrabbableButton')).toHaveLength(1);
  146. });
  147. });