orgDashboards.spec.tsx 9.7 KB

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