orgDashboards.spec.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import {browserHistory} from 'sentry/utils/browserHistory';
  7. import DashboardDetail from 'sentry/views/dashboards/detail';
  8. import OrgDashboards from 'sentry/views/dashboards/orgDashboards';
  9. import {DashboardState} from 'sentry/views/dashboards/types';
  10. describe('OrgDashboards', () => {
  11. const api = new MockApiClient();
  12. const organization = OrganizationFixture({
  13. features: ['dashboards-basic', 'dashboards-edit'],
  14. });
  15. let initialData;
  16. beforeEach(() => {
  17. initialData = initializeOrg({
  18. organization,
  19. projects: [],
  20. router: {
  21. location: LocationFixture(),
  22. params: {orgId: 'org-slug'},
  23. },
  24. });
  25. const mockDashboard = {
  26. dateCreated: '2021-08-10T21:20:46.798237Z',
  27. id: '1',
  28. title: 'Test Dashboard',
  29. widgets: [],
  30. projects: [],
  31. filters: {},
  32. };
  33. MockApiClient.addMockResponse({
  34. url: `/organizations/org-slug/dashboards/1/`,
  35. method: 'GET',
  36. body: mockDashboard,
  37. });
  38. MockApiClient.addMockResponse({
  39. url: '/organizations/org-slug/dashboards/',
  40. body: [mockDashboard],
  41. });
  42. ProjectsStore.loadInitialData(initialData.projects);
  43. });
  44. afterEach(() => {
  45. MockApiClient.clearMockResponses();
  46. jest.clearAllMocks();
  47. });
  48. it('redirects to add query params for page filters if any are saved', async () => {
  49. const mockDashboardWithFilters = {
  50. dateCreated: '2021-08-10T21:20:46.798237Z',
  51. id: '1',
  52. title: 'Test Dashboard',
  53. widgets: [],
  54. projects: [1, 2],
  55. environment: ['alpha'],
  56. period: '7d',
  57. filters: {},
  58. };
  59. MockApiClient.addMockResponse({
  60. url: `/organizations/org-slug/dashboards/1/`,
  61. method: 'GET',
  62. body: mockDashboardWithFilters,
  63. });
  64. MockApiClient.addMockResponse({
  65. url: '/organizations/org-slug/dashboards/',
  66. body: [mockDashboardWithFilters],
  67. });
  68. render(
  69. <OrgDashboards
  70. api={api}
  71. location={LocationFixture()}
  72. organization={initialData.organization}
  73. params={{orgId: 'org-slug', dashboardId: '1'}}
  74. >
  75. {({dashboard, dashboards}) => {
  76. return dashboard ? (
  77. <DashboardDetail
  78. api={api}
  79. initialState={DashboardState.VIEW}
  80. location={initialData.router.location}
  81. router={initialData.router}
  82. dashboard={dashboard}
  83. dashboards={dashboards}
  84. {...initialData.router}
  85. />
  86. ) : (
  87. <div>loading</div>
  88. );
  89. }}
  90. </OrgDashboards>,
  91. {router: initialData.router}
  92. );
  93. await waitFor(() =>
  94. expect(browserHistory.replace).toHaveBeenCalledWith(
  95. expect.objectContaining({
  96. query: expect.objectContaining({
  97. project: [1, 2],
  98. environment: ['alpha'],
  99. statsPeriod: '7d',
  100. }),
  101. })
  102. )
  103. );
  104. });
  105. it('ignores query params that are not page filters for redirection', async () => {
  106. const mockDashboardWithFilters = {
  107. dateCreated: '2021-08-10T21:20:46.798237Z',
  108. id: '1',
  109. title: 'Test Dashboard',
  110. widgets: [],
  111. projects: [1, 2],
  112. environment: ['alpha'],
  113. period: '7d',
  114. filters: {},
  115. };
  116. MockApiClient.addMockResponse({
  117. url: `/organizations/org-slug/dashboards/1/`,
  118. method: 'GET',
  119. body: mockDashboardWithFilters,
  120. });
  121. MockApiClient.addMockResponse({
  122. url: '/organizations/org-slug/dashboards/',
  123. body: [mockDashboardWithFilters],
  124. });
  125. render(
  126. <OrgDashboards
  127. api={api}
  128. location={{
  129. ...LocationFixture(),
  130. query: {
  131. // This query param is not a page filter, so it should not interfere
  132. // with the redirect logic
  133. sort: 'recentlyViewed',
  134. },
  135. }}
  136. organization={initialData.organization}
  137. params={{orgId: 'org-slug', dashboardId: '1'}}
  138. >
  139. {({dashboard, dashboards}) => {
  140. return dashboard ? (
  141. <DashboardDetail
  142. api={api}
  143. initialState={DashboardState.VIEW}
  144. location={initialData.router.location}
  145. router={initialData.router}
  146. dashboard={dashboard}
  147. dashboards={dashboards}
  148. {...initialData.router}
  149. />
  150. ) : (
  151. <div>loading</div>
  152. );
  153. }}
  154. </OrgDashboards>,
  155. {router: initialData.router}
  156. );
  157. await waitFor(() =>
  158. expect(browserHistory.replace).toHaveBeenCalledWith(
  159. expect.objectContaining({
  160. query: expect.objectContaining({
  161. project: [1, 2],
  162. environment: ['alpha'],
  163. statsPeriod: '7d',
  164. }),
  165. })
  166. )
  167. );
  168. });
  169. it('does not add query params for page filters if one of the filters is defined', () => {
  170. initialData = initializeOrg({
  171. organization,
  172. projects: [],
  173. router: {
  174. location: {
  175. ...LocationFixture(),
  176. query: {
  177. // project is supplied in the URL, so we should avoid redirecting
  178. project: ['1'],
  179. },
  180. },
  181. params: {orgId: 'org-slug'},
  182. },
  183. });
  184. const mockDashboardWithFilters = {
  185. dateCreated: '2021-08-10T21:20:46.798237Z',
  186. id: '1',
  187. title: 'Test Dashboard',
  188. widgets: [],
  189. projects: [1, 2],
  190. environment: ['alpha'],
  191. period: '7d',
  192. filters: {},
  193. };
  194. MockApiClient.addMockResponse({
  195. url: `/organizations/org-slug/dashboards/1/`,
  196. method: 'GET',
  197. body: mockDashboardWithFilters,
  198. });
  199. MockApiClient.addMockResponse({
  200. url: '/organizations/org-slug/dashboards/',
  201. body: [mockDashboardWithFilters],
  202. });
  203. render(
  204. <OrgDashboards
  205. api={api}
  206. location={initialData.router.location}
  207. organization={initialData.organization}
  208. params={{orgId: 'org-slug', dashboardId: '1'}}
  209. >
  210. {({dashboard, dashboards}) => {
  211. return dashboard ? (
  212. <DashboardDetail
  213. api={api}
  214. initialState={DashboardState.VIEW}
  215. location={initialData.router.location}
  216. router={initialData.router}
  217. dashboard={dashboard}
  218. dashboards={dashboards}
  219. {...initialData.router}
  220. />
  221. ) : (
  222. <div>loading</div>
  223. );
  224. }}
  225. </OrgDashboards>,
  226. {router: initialData.router}
  227. );
  228. expect(browserHistory.replace).not.toHaveBeenCalled();
  229. });
  230. it('does not add query params for page filters if none are saved', () => {
  231. render(
  232. <OrgDashboards
  233. api={api}
  234. location={LocationFixture()}
  235. organization={initialData.organization}
  236. params={{orgId: 'org-slug', dashboardId: '1'}}
  237. >
  238. {({dashboard, dashboards}) => {
  239. return dashboard ? (
  240. <DashboardDetail
  241. api={api}
  242. initialState={DashboardState.VIEW}
  243. location={initialData.router.location}
  244. router={initialData.router}
  245. dashboard={dashboard}
  246. dashboards={dashboards}
  247. {...initialData.router}
  248. />
  249. ) : (
  250. <div>loading</div>
  251. );
  252. }}
  253. </OrgDashboards>,
  254. {router: initialData.router}
  255. );
  256. expect(browserHistory.replace).not.toHaveBeenCalled();
  257. });
  258. it('does not redirect to add query params if location is cleared manually', async () => {
  259. const mockDashboardWithFilters = {
  260. dateCreated: '2021-08-10T21:20:46.798237Z',
  261. id: '1',
  262. title: 'Test Dashboard',
  263. widgets: [],
  264. projects: [1],
  265. filters: {},
  266. };
  267. MockApiClient.addMockResponse({
  268. url: `/organizations/org-slug/dashboards/1/`,
  269. method: 'GET',
  270. body: mockDashboardWithFilters,
  271. });
  272. MockApiClient.addMockResponse({
  273. url: '/organizations/org-slug/dashboards/',
  274. body: [mockDashboardWithFilters],
  275. });
  276. const {rerender} = render(
  277. <OrgDashboards
  278. api={api}
  279. location={LocationFixture()}
  280. organization={initialData.organization}
  281. params={{orgId: 'org-slug', dashboardId: '1'}}
  282. >
  283. {({dashboard, dashboards}) => {
  284. return dashboard ? (
  285. <DashboardDetail
  286. api={api}
  287. initialState={DashboardState.VIEW}
  288. location={initialData.router.location}
  289. router={initialData.router}
  290. dashboard={dashboard}
  291. dashboards={dashboards}
  292. {...initialData.router}
  293. />
  294. ) : (
  295. <div>loading</div>
  296. );
  297. }}
  298. </OrgDashboards>,
  299. {router: initialData.router}
  300. );
  301. await waitFor(() => expect(browserHistory.replace).toHaveBeenCalledTimes(1));
  302. rerender(
  303. <OrgDashboards
  304. api={api}
  305. location={{...initialData.router.location, query: {}}}
  306. organization={initialData.organization}
  307. params={{orgId: 'org-slug', dashboardId: '1'}}
  308. >
  309. {({dashboard, dashboards}) => {
  310. return dashboard ? (
  311. <DashboardDetail
  312. api={api}
  313. initialState={DashboardState.VIEW}
  314. location={initialData.router.location}
  315. router={initialData.router}
  316. dashboard={dashboard}
  317. dashboards={dashboards}
  318. {...initialData.router}
  319. />
  320. ) : (
  321. <div>loading</div>
  322. );
  323. }}
  324. </OrgDashboards>
  325. );
  326. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  327. expect(browserHistory.replace).toHaveBeenCalledTimes(1);
  328. });
  329. });