globalSelectionHeader.spec.jsx 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {mockRouterPush} from 'sentry-test/mockRouterPush';
  4. import * as globalActions from 'app/actionCreators/globalSelection';
  5. import OrganizationActions from 'app/actions/organizationActions';
  6. import GlobalSelectionHeader from 'app/components/organizations/globalSelectionHeader';
  7. import ConfigStore from 'app/stores/configStore';
  8. import GlobalSelectionStore from 'app/stores/globalSelectionStore';
  9. import ProjectsStore from 'app/stores/projectsStore';
  10. import {getItem} from 'app/utils/localStorage';
  11. const changeQuery = (routerContext, query) => ({
  12. ...routerContext,
  13. context: {
  14. ...routerContext.context,
  15. router: {
  16. ...routerContext.context.router,
  17. location: {
  18. query,
  19. },
  20. },
  21. },
  22. });
  23. jest.mock('app/utils/localStorage', () => ({
  24. getItem: jest.fn(),
  25. setItem: jest.fn(),
  26. }));
  27. describe('GlobalSelectionHeader', function () {
  28. let wrapper;
  29. const {organization, router, routerContext} = initializeOrg({
  30. organization: {features: ['global-views']},
  31. projects: [
  32. {
  33. id: 2,
  34. slug: 'project-2',
  35. },
  36. {
  37. id: 3,
  38. slug: 'project-3',
  39. environments: ['prod', 'staging'],
  40. },
  41. ],
  42. router: {
  43. location: {query: {}},
  44. params: {orgId: 'org-slug'},
  45. },
  46. });
  47. beforeAll(function () {
  48. jest.spyOn(globalActions, 'updateDateTime');
  49. jest.spyOn(globalActions, 'updateEnvironments');
  50. jest.spyOn(globalActions, 'updateProjects');
  51. jest.spyOn(globalActions, 'updateParams');
  52. jest.spyOn(globalActions, 'updateParamsWithoutHistory');
  53. });
  54. beforeEach(function () {
  55. MockApiClient.clearMockResponses();
  56. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => organization.projects);
  57. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  58. getItem.mockImplementation(() => null);
  59. MockApiClient.addMockResponse({
  60. url: '/organizations/org-slug/projects/',
  61. body: [],
  62. });
  63. });
  64. afterEach(function () {
  65. wrapper.unmount();
  66. [
  67. globalActions.updateDateTime,
  68. globalActions.updateProjects,
  69. globalActions.updateEnvironments,
  70. globalActions.updateParams,
  71. globalActions.updateParamsWithoutHistory,
  72. router.push,
  73. router.replace,
  74. getItem,
  75. ].forEach(mock => mock.mockClear());
  76. GlobalSelectionStore.reset();
  77. });
  78. it('does not update router if there is custom routing', function () {
  79. wrapper = mountWithTheme(
  80. <GlobalSelectionHeader organization={organization} hasCustomRouting />,
  81. routerContext
  82. );
  83. expect(router.push).not.toHaveBeenCalled();
  84. });
  85. it('does not update router if org in URL params is different than org in context/props', function () {
  86. wrapper = mountWithTheme(
  87. <GlobalSelectionHeader organization={organization} hasCustomRouting />,
  88. {
  89. ...routerContext,
  90. context: {
  91. ...routerContext.context,
  92. router: {...routerContext.context.router, params: {orgId: 'diff-org'}},
  93. },
  94. }
  95. );
  96. expect(router.push).not.toHaveBeenCalled();
  97. });
  98. it('does not replace URL with values from store when mounted with no query params', function () {
  99. wrapper = mountWithTheme(
  100. <GlobalSelectionHeader organization={organization} />,
  101. routerContext
  102. );
  103. expect(router.replace).not.toHaveBeenCalled();
  104. });
  105. it('only updates GlobalSelection store when mounted with query params', async function () {
  106. wrapper = mountWithTheme(
  107. <GlobalSelectionHeader
  108. organization={organization}
  109. params={{orgId: organization.slug}}
  110. />,
  111. changeQuery(routerContext, {
  112. statsPeriod: '7d',
  113. })
  114. );
  115. expect(router.push).not.toHaveBeenCalled();
  116. await tick();
  117. expect(GlobalSelectionStore.get().selection).toEqual({
  118. datetime: {
  119. period: '7d',
  120. utc: null,
  121. start: null,
  122. end: null,
  123. },
  124. environments: [],
  125. projects: [],
  126. });
  127. });
  128. it('updates environments when switching projects', async function () {
  129. wrapper = mountWithTheme(
  130. <GlobalSelectionHeader
  131. organization={organization}
  132. projects={organization.projects}
  133. />,
  134. routerContext
  135. );
  136. await tick();
  137. wrapper.update();
  138. mockRouterPush(wrapper, router);
  139. // Open dropdown and select both projects
  140. wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
  141. wrapper.find('MultipleProjectSelector CheckboxFancy').at(0).simulate('click');
  142. wrapper.find('MultipleProjectSelector CheckboxFancy').at(1).simulate('click');
  143. wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
  144. await tick();
  145. wrapper.update();
  146. expect(wrapper.find('MultipleProjectSelector Content').text()).toBe(
  147. 'project-2, project-3'
  148. );
  149. // Select environment
  150. wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  151. wrapper.find('MultipleEnvironmentSelector CheckboxFancy').at(1).simulate('click');
  152. wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  153. await tick();
  154. expect(wrapper.find('MultipleEnvironmentSelector Content').text()).toBe('staging');
  155. expect(GlobalSelectionStore.get().selection).toEqual({
  156. datetime: {
  157. period: '14d',
  158. utc: null,
  159. start: null,
  160. end: null,
  161. },
  162. environments: ['staging'],
  163. projects: [2, 3],
  164. });
  165. const query = wrapper.prop('location').query;
  166. expect(query).toEqual({
  167. environment: 'staging',
  168. project: ['2', '3'],
  169. });
  170. // Now change projects, first project has no environments
  171. wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
  172. wrapper.find('MultipleProjectSelector CheckboxFancy').at(1).simulate('click');
  173. wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
  174. await tick();
  175. wrapper.update();
  176. // Store should not have any environments selected
  177. expect(GlobalSelectionStore.get().selection).toEqual({
  178. datetime: {
  179. period: '14d',
  180. utc: null,
  181. start: null,
  182. end: null,
  183. },
  184. environments: [],
  185. projects: [2],
  186. });
  187. expect(wrapper.find('MultipleEnvironmentSelector Content').text()).toBe(
  188. 'All Environments'
  189. );
  190. });
  191. it('shows environments for non-member projects', async function () {
  192. const initialData = initializeOrg({
  193. organization: {features: ['global-views']},
  194. projects: [
  195. {id: 1, slug: 'staging-project', environments: ['staging'], isMember: false},
  196. {id: 2, slug: 'prod-project', environments: ['prod']},
  197. ],
  198. router: {
  199. location: {query: {project: [1]}},
  200. params: {orgId: 'org-slug'},
  201. },
  202. });
  203. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => initialData.projects);
  204. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  205. wrapper = mountWithTheme(
  206. <GlobalSelectionHeader
  207. router={initialData.router}
  208. organization={initialData.organization}
  209. projects={initialData.projects}
  210. />,
  211. changeQuery(initialData.routerContext, {project: 1})
  212. );
  213. await tick();
  214. wrapper.update();
  215. // Open environment picker
  216. wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  217. const checkboxes = wrapper.find('MultipleEnvironmentSelector AutoCompleteItem');
  218. expect(checkboxes).toHaveLength(1);
  219. expect(checkboxes.text()).toBe('staging');
  220. });
  221. it('updates GlobalSelection store with default period', async function () {
  222. wrapper = mountWithTheme(
  223. <GlobalSelectionHeader organization={organization} />,
  224. changeQuery(routerContext, {
  225. environment: 'prod',
  226. })
  227. );
  228. await tick();
  229. expect(GlobalSelectionStore.get()).toEqual({
  230. isReady: true,
  231. selection: {
  232. datetime: {
  233. period: '14d',
  234. utc: null,
  235. start: null,
  236. end: null,
  237. },
  238. environments: ['prod'],
  239. projects: [],
  240. },
  241. });
  242. // Not called because of the default date
  243. expect(router.replace).not.toHaveBeenCalled();
  244. });
  245. it('updates GlobalSelection store with empty dates in URL', async function () {
  246. wrapper = mountWithTheme(
  247. <GlobalSelectionHeader organization={organization} />,
  248. changeQuery(routerContext, {
  249. statsPeriod: null,
  250. })
  251. );
  252. await tick();
  253. expect(GlobalSelectionStore.get()).toEqual({
  254. isReady: true,
  255. selection: {
  256. datetime: {
  257. period: '14d',
  258. utc: null,
  259. start: null,
  260. end: null,
  261. },
  262. environments: [],
  263. projects: [],
  264. },
  265. });
  266. });
  267. it('resets start&end if showAbsolute prop is false', async function () {
  268. wrapper = mountWithTheme(
  269. <GlobalSelectionHeader organization={organization} showAbsolute={false} />,
  270. changeQuery(routerContext, {
  271. start: '2020-05-05T07:26:53.000',
  272. end: '2020-05-05T09:19:12.000',
  273. })
  274. );
  275. await tick();
  276. expect(GlobalSelectionStore.get()).toEqual({
  277. isReady: true,
  278. selection: {
  279. datetime: {
  280. period: '14d',
  281. utc: null,
  282. start: null,
  283. end: null,
  284. },
  285. environments: [],
  286. projects: [],
  287. },
  288. });
  289. });
  290. /**
  291. * I don't think this test is really applicable anymore
  292. */
  293. it('does not update store if url params have not changed', async function () {
  294. wrapper = mountWithTheme(
  295. <GlobalSelectionHeader organization={organization} />,
  296. changeQuery(routerContext, {
  297. statsPeriod: '7d',
  298. })
  299. );
  300. [
  301. globalActions.updateDateTime,
  302. globalActions.updateProjects,
  303. globalActions.updateEnvironments,
  304. ].forEach(mock => mock.mockClear());
  305. wrapper.setContext(
  306. changeQuery(routerContext, {
  307. statsPeriod: '7d',
  308. }).context
  309. );
  310. await tick();
  311. wrapper.update();
  312. expect(globalActions.updateDateTime).not.toHaveBeenCalled();
  313. expect(globalActions.updateProjects).not.toHaveBeenCalled();
  314. expect(globalActions.updateEnvironments).not.toHaveBeenCalled();
  315. expect(GlobalSelectionStore.get()).toEqual({
  316. isReady: true,
  317. selection: {
  318. datetime: {
  319. period: '7d',
  320. utc: null,
  321. start: null,
  322. end: null,
  323. },
  324. environments: [],
  325. projects: [],
  326. },
  327. });
  328. });
  329. it('loads from local storage when no URL parameters', async function () {
  330. getItem.mockImplementation(() =>
  331. JSON.stringify({projects: [3], environments: ['staging']})
  332. );
  333. const initializationObj = initializeOrg({
  334. organization: {
  335. features: ['global-views'],
  336. },
  337. router: {
  338. // we need this to be set to make sure org in context is same as
  339. // current org in URL
  340. params: {orgId: 'org-slug'},
  341. },
  342. });
  343. wrapper = mountWithTheme(
  344. <GlobalSelectionHeader organization={initializationObj.organization} />,
  345. initializationObj.routerContext
  346. );
  347. await tick(); // reflux tick
  348. expect(GlobalSelectionStore.get().selection.projects).toEqual([3]);
  349. // Since these are coming from URL, there should be no changes and
  350. // router does not need to be called
  351. expect(initializationObj.router.replace).toHaveBeenLastCalledWith(
  352. expect.objectContaining({
  353. query: {
  354. environment: ['staging'],
  355. project: [3],
  356. },
  357. })
  358. );
  359. });
  360. it('does not load from local storage when there are URL params', async function () {
  361. getItem.mockImplementation(() =>
  362. JSON.stringify({projects: [3], environments: ['staging']})
  363. );
  364. const initializationObj = initializeOrg({
  365. organization: {
  366. features: ['global-views'],
  367. },
  368. router: {
  369. // we need this to be set to make sure org in context is same as
  370. // current org in URL
  371. params: {orgId: 'org-slug'},
  372. location: {query: {project: [1, 2]}},
  373. },
  374. });
  375. wrapper = mountWithTheme(
  376. <GlobalSelectionHeader organization={initializationObj.organization} />,
  377. initializationObj.routerContext
  378. );
  379. await tick(); // reflux tick
  380. expect(GlobalSelectionStore.get().selection.projects).toEqual([1, 2]);
  381. // Since these are coming from URL, there should be no changes and
  382. // router does not need to be called
  383. expect(initializationObj.router.replace).not.toHaveBeenCalled();
  384. });
  385. it('updates store when there are query params in URL', async function () {
  386. const initializationObj = initializeOrg({
  387. organization: {
  388. features: ['global-views'],
  389. },
  390. router: {
  391. // we need this to be set to make sure org in context is same as
  392. // current org in URL
  393. params: {orgId: 'org-slug'},
  394. location: {query: {project: [1, 2]}},
  395. },
  396. });
  397. wrapper = mountWithTheme(
  398. <GlobalSelectionHeader organization={initializationObj.organization} />,
  399. initializationObj.routerContext
  400. );
  401. await tick(); // reflux tick
  402. expect(GlobalSelectionStore.get().selection.projects).toEqual([1, 2]);
  403. // Since these are coming from URL, there should be no changes and
  404. // router does not need to be called
  405. expect(initializationObj.router.replace).not.toHaveBeenCalled();
  406. });
  407. it('updates store with default values when there are no query params in URL', async function () {
  408. const initializationObj = initializeOrg({
  409. organization: {
  410. features: ['global-views'],
  411. },
  412. router: {
  413. // we need this to be set to make sure org in context is same as
  414. // current org in URL
  415. params: {orgId: 'org-slug'},
  416. location: {query: {}},
  417. },
  418. });
  419. wrapper = mountWithTheme(
  420. <GlobalSelectionHeader organization={initializationObj.organization} />,
  421. initializationObj.routerContext
  422. );
  423. // Router does not update because params have not changed
  424. expect(initializationObj.router.replace).not.toHaveBeenCalled();
  425. });
  426. /**
  427. * GSH: (no global-views)
  428. * - mounts with no state from router
  429. * - params org id === org.slug
  430. *
  431. * - updateProjects should not be called (enforceSingleProject should not be
  432. * called)
  433. *
  434. * - componentDidUpdate with loadingProjects === true, and pass in list of
  435. * projects (via projects store)
  436. *
  437. * - enforceProject should be called and updateProjects() called with the new
  438. * project
  439. * - variation:
  440. * - params.orgId !== org.slug (e.g. just switched orgs)
  441. *
  442. * When switching orgs when not in Issues view, the issues view gets rendered
  443. * with params.orgId !== org.slug
  444. *
  445. * Global selection header gets unmounted and mounted, and in this case
  446. * nothing should be done until it gets updated and params.orgId === org.slug
  447. *
  448. * Separate issue:
  449. *
  450. * IssuesList ("child view") renders before a single project is enforced,
  451. * will require refactoring views so that they depend on GSH enforcing a
  452. * single project first IF they don't have required feature (and no project id
  453. * in URL).
  454. */
  455. describe('Single project selection mode', function () {
  456. it('does not do anything while organization is switching in single project', async function () {
  457. const initialData = initializeOrg({
  458. organization: {slug: 'old-org-slug'},
  459. router: {
  460. // we need this to be set to make sure org in context is same as
  461. // current org in URL
  462. params: {orgId: 'org-slug'},
  463. location: {query: {project: [1]}},
  464. },
  465. });
  466. ProjectsStore.isLoading.mockRestore();
  467. ProjectsStore.getAll.mockRestore();
  468. MockApiClient.addMockResponse({
  469. url: '/organizations/old-org-slug/projects/',
  470. body: [],
  471. });
  472. // This can happen when you switch organization so params.orgId !== the
  473. // current org in context In this case params.orgId = 'org-slug'
  474. wrapper = mountWithTheme(
  475. <GlobalSelectionHeader organization={initialData.organization} />,
  476. initialData.routerContext
  477. );
  478. expect(globalActions.updateProjects).not.toHaveBeenCalled();
  479. const updatedOrganization = {
  480. ...organization,
  481. slug: 'org-slug',
  482. features: [],
  483. projects: [TestStubs.Project({id: '123', slug: 'org-slug-project1'})],
  484. };
  485. MockApiClient.addMockResponse({
  486. url: '/organizations/org-slug/',
  487. body: updatedOrganization,
  488. });
  489. // Eventually OrganizationContext will fetch org details for `org-slug`
  490. // and update `organization` prop emulate fetchOrganizationDetails
  491. OrganizationActions.update(updatedOrganization);
  492. wrapper.setContext({
  493. organization: updatedOrganization,
  494. location: {query: {}},
  495. router: {
  496. ...initialData.router,
  497. location: {query: {}},
  498. },
  499. });
  500. wrapper.setProps({organization: updatedOrganization});
  501. ProjectsStore.loadInitialData(updatedOrganization.projects);
  502. expect(initialData.router.replace).toHaveBeenLastCalledWith(
  503. expect.objectContaining({
  504. query: {environment: [], project: [123]},
  505. })
  506. );
  507. });
  508. it('selects first project if more than one is requested', function () {
  509. const initializationObj = initializeOrg({
  510. router: {
  511. // we need this to be set to make sure org in context is same as
  512. // current org in URL
  513. params: {orgId: 'org-slug'},
  514. location: {query: {project: [1, 2]}},
  515. },
  516. });
  517. wrapper = mountWithTheme(
  518. <GlobalSelectionHeader organization={initializationObj.organization} />,
  519. initializationObj.routerContext
  520. );
  521. expect(initializationObj.router.replace).toHaveBeenCalledWith(
  522. expect.objectContaining({
  523. query: {environment: [], project: [1]},
  524. })
  525. );
  526. });
  527. it('selects first project if none (i.e. all) is requested', async function () {
  528. const project = TestStubs.Project({id: '3'});
  529. const org = TestStubs.Organization({projects: [project]});
  530. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => org.projects);
  531. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  532. const initializationObj = initializeOrg({
  533. organization: org,
  534. router: {
  535. params: {orgId: 'org-slug'},
  536. location: {query: {}},
  537. },
  538. });
  539. wrapper = mountWithTheme(
  540. <GlobalSelectionHeader organization={initializationObj.organization} />,
  541. initializationObj.routerContext
  542. );
  543. expect(initializationObj.router.replace).toHaveBeenCalledWith(
  544. expect.objectContaining({
  545. query: {environment: [], project: [3]},
  546. })
  547. );
  548. });
  549. });
  550. describe('forceProject selection mode', function () {
  551. beforeEach(function () {
  552. MockApiClient.addMockResponse({
  553. url: '/organizations/org-slug/projects/',
  554. body: [],
  555. });
  556. const initialData = initializeOrg({
  557. organization: {features: ['global-views']},
  558. projects: [
  559. {id: 1, slug: 'staging-project', environments: ['staging']},
  560. {id: 2, slug: 'prod-project', environments: ['prod']},
  561. ],
  562. router: {
  563. location: {query: {}},
  564. },
  565. });
  566. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => initialData.projects);
  567. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  568. wrapper = mountWithTheme(
  569. <GlobalSelectionHeader
  570. organization={initialData.organization}
  571. shouldForceProject
  572. forceProject={initialData.projects[0]}
  573. showIssueStreamLink
  574. />,
  575. initialData.routerContext
  576. );
  577. });
  578. it('renders a back button to the forced project', function () {
  579. const back = wrapper.find('BackButtonWrapper');
  580. expect(back).toHaveLength(1);
  581. });
  582. it('renders only environments from the forced project', async function () {
  583. await wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  584. await wrapper.update();
  585. const items = wrapper.find('MultipleEnvironmentSelector EnvironmentSelectorItem');
  586. expect(items.length).toEqual(1);
  587. expect(items.at(0).text()).toBe('staging');
  588. });
  589. });
  590. describe('without global-views (multi-project feature)', function () {
  591. describe('without existing URL params', function () {
  592. const initialData = initializeOrg({
  593. projects: [
  594. {id: 0, slug: 'random project', isMember: true},
  595. {id: 1, slug: 'staging-project', environments: ['staging']},
  596. {id: 2, slug: 'prod-project', environments: ['prod']},
  597. ],
  598. router: {
  599. location: {query: {}},
  600. params: {orgId: 'org-slug'},
  601. },
  602. });
  603. const createWrapper = props => {
  604. wrapper = mountWithTheme(
  605. <GlobalSelectionHeader
  606. params={{orgId: initialData.organization.slug}}
  607. organization={initialData.organization}
  608. {...props}
  609. />,
  610. initialData.routerContext
  611. );
  612. return wrapper;
  613. };
  614. beforeEach(function () {
  615. jest
  616. .spyOn(ProjectsStore, 'getAll')
  617. .mockImplementation(() => initialData.projects);
  618. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  619. initialData.router.push.mockClear();
  620. initialData.router.replace.mockClear();
  621. });
  622. it('uses first project in org projects when mounting', async function () {
  623. createWrapper();
  624. await tick();
  625. wrapper.update();
  626. expect(initialData.router.replace).toHaveBeenLastCalledWith({
  627. pathname: undefined,
  628. query: {environment: [], project: [0]},
  629. });
  630. });
  631. it('appends projectId to URL when `forceProject` becomes available (async)', async function () {
  632. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => []);
  633. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => true);
  634. // forceProject generally starts undefined
  635. createWrapper({shouldForceProject: true});
  636. // load the projects
  637. jest
  638. .spyOn(ProjectsStore, 'getAll')
  639. .mockImplementation(() => initialData.projects);
  640. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  641. wrapper.setProps({
  642. forceProject: initialData.projects[1],
  643. });
  644. // Force the withProjects HoC to re-render
  645. ProjectsStore.trigger();
  646. wrapper.update();
  647. expect(initialData.router.replace).toHaveBeenLastCalledWith({
  648. pathname: undefined,
  649. query: {environment: [], project: [1]},
  650. });
  651. expect(initialData.router.replace).toHaveBeenCalledTimes(1);
  652. });
  653. it('does not append projectId to URL when `forceProject` becomes available but project id already exists in URL', async function () {
  654. // forceProject generally starts undefined
  655. createWrapper({shouldForceProject: true});
  656. wrapper.setContext({
  657. router: {
  658. ...initialData.router,
  659. location: {
  660. ...initialData.router.location,
  661. query: {
  662. project: 321,
  663. },
  664. },
  665. },
  666. });
  667. wrapper.setProps({
  668. forceProject: initialData.projects[1],
  669. });
  670. wrapper.update();
  671. expect(initialData.router.replace).not.toHaveBeenCalled();
  672. });
  673. it('appends projectId to URL when mounted with `forceProject`', async function () {
  674. // forceProject generally starts undefined
  675. createWrapper({
  676. shouldForceProject: true,
  677. forceProject: initialData.projects[1],
  678. });
  679. wrapper.update();
  680. expect(initialData.router.replace).toHaveBeenLastCalledWith({
  681. pathname: undefined,
  682. query: {environment: [], project: [1]},
  683. });
  684. });
  685. });
  686. describe('with existing URL params', function () {
  687. const initialData = initializeOrg({
  688. projects: [
  689. {id: 0, slug: 'random project', isMember: true},
  690. {id: 1, slug: 'staging-project', environments: ['staging']},
  691. {id: 2, slug: 'prod-project', environments: ['prod']},
  692. ],
  693. router: {
  694. location: {query: {statsPeriod: '90d'}},
  695. params: {orgId: 'org-slug'},
  696. },
  697. });
  698. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => initialData.projects);
  699. const createWrapper = props => {
  700. wrapper = mountWithTheme(
  701. <GlobalSelectionHeader
  702. params={{orgId: initialData.organization.slug}}
  703. organization={initialData.organization}
  704. {...props}
  705. />,
  706. initialData.routerContext
  707. );
  708. return wrapper;
  709. };
  710. beforeEach(function () {
  711. initialData.router.push.mockClear();
  712. initialData.router.replace.mockClear();
  713. });
  714. it('appends projectId to URL when mounted with `forceProject`', async function () {
  715. // forceProject generally starts undefined
  716. createWrapper({
  717. shouldForceProject: true,
  718. forceProject: initialData.projects[1],
  719. });
  720. wrapper.update();
  721. expect(initialData.router.replace).toHaveBeenLastCalledWith({
  722. pathname: undefined,
  723. query: {environment: [], project: [1], statsPeriod: '90d'},
  724. });
  725. });
  726. });
  727. });
  728. describe('with global-views (multi-project feature)', function () {
  729. describe('without existing URL params', function () {
  730. const initialData = initializeOrg({
  731. organization: {features: ['global-views']},
  732. projects: [
  733. {id: 0, slug: 'random project', isMember: true},
  734. {id: 1, slug: 'staging-project', environments: ['staging']},
  735. {id: 2, slug: 'prod-project', environments: ['prod']},
  736. ],
  737. router: {
  738. location: {query: {}},
  739. params: {orgId: 'org-slug'},
  740. },
  741. });
  742. const createWrapper = (props, ctx) => {
  743. wrapper = mountWithTheme(
  744. <GlobalSelectionHeader
  745. params={{orgId: initialData.organization.slug}}
  746. organization={initialData.organization}
  747. {...props}
  748. />,
  749. {
  750. ...initialData.routerContext,
  751. ...ctx,
  752. }
  753. );
  754. return wrapper;
  755. };
  756. beforeEach(function () {
  757. jest
  758. .spyOn(ProjectsStore, 'getAll')
  759. .mockImplementation(() => initialData.projects);
  760. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  761. initialData.router.push.mockClear();
  762. initialData.router.replace.mockClear();
  763. });
  764. it('does not use first project in org projects when mounting (and without localStorage data)', async function () {
  765. createWrapper();
  766. await tick();
  767. wrapper.update();
  768. expect(initialData.router.replace).not.toHaveBeenCalled();
  769. });
  770. it('does not append projectId to URL when `loadingProjects` changes and finishes loading', async function () {
  771. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => []);
  772. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => true);
  773. createWrapper();
  774. // load the projects
  775. jest
  776. .spyOn(ProjectsStore, 'getAll')
  777. .mockImplementation(() => initialData.projects);
  778. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  779. wrapper.setProps({
  780. forceProject: initialData.projects[1],
  781. });
  782. // Force the withProjects HoC to re-render
  783. ProjectsStore.trigger();
  784. expect(initialData.router.replace).not.toHaveBeenCalled();
  785. });
  786. it('appends projectId to URL when `forceProject` becomes available (async)', async function () {
  787. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => []);
  788. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => true);
  789. // forceProject generally starts undefined
  790. createWrapper({shouldForceProject: true});
  791. // load the projects
  792. jest
  793. .spyOn(ProjectsStore, 'getAll')
  794. .mockImplementation(() => initialData.projects);
  795. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  796. wrapper.setProps({
  797. forceProject: initialData.projects[1],
  798. });
  799. // Force the withProjects HoC to re-render
  800. ProjectsStore.trigger();
  801. expect(initialData.router.replace).toHaveBeenLastCalledWith({
  802. pathname: undefined,
  803. query: {environment: [], project: [1]},
  804. });
  805. expect(initialData.router.replace).toHaveBeenCalledTimes(1);
  806. });
  807. it('does not append projectId to URL when `forceProject` becomes available but project id already exists in URL', async function () {
  808. // forceProject generally starts undefined
  809. createWrapper(
  810. {shouldForceProject: true},
  811. changeQuery(initialData.routerContext, {project: 321})
  812. );
  813. await tick();
  814. wrapper.setProps({
  815. forceProject: initialData.projects[1],
  816. });
  817. wrapper.update();
  818. expect(initialData.router.replace).not.toHaveBeenCalled();
  819. });
  820. });
  821. });
  822. describe('projects list', function () {
  823. let memberProject, nonMemberProject, initialData;
  824. beforeEach(function () {
  825. memberProject = TestStubs.Project({id: '3', isMember: true});
  826. nonMemberProject = TestStubs.Project({id: '4', isMember: false});
  827. initialData = initializeOrg({
  828. projects: [memberProject, nonMemberProject],
  829. router: {
  830. location: {query: {}},
  831. params: {
  832. orgId: 'org-slug',
  833. },
  834. },
  835. });
  836. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => initialData.projects);
  837. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  838. wrapper = mountWithTheme(
  839. <GlobalSelectionHeader organization={initialData.organization} />,
  840. initialData.routerContext
  841. );
  842. });
  843. it('gets member projects', function () {
  844. expect(wrapper.find('MultipleProjectSelector').prop('projects')).toEqual([
  845. memberProject,
  846. ]);
  847. });
  848. it('gets all projects if superuser', function () {
  849. ConfigStore.config = {
  850. user: {
  851. isSuperuser: true,
  852. },
  853. };
  854. wrapper = mountWithTheme(
  855. <GlobalSelectionHeader organization={initialData.organization} />,
  856. initialData.routerContext
  857. );
  858. expect(wrapper.find('MultipleProjectSelector').prop('projects')).toEqual([
  859. memberProject,
  860. ]);
  861. expect(wrapper.find('MultipleProjectSelector').prop('nonMemberProjects')).toEqual([
  862. nonMemberProject,
  863. ]);
  864. });
  865. it('shows "My Projects" button', async function () {
  866. initialData.organization.features.push('global-views');
  867. wrapper = mountWithTheme(
  868. <GlobalSelectionHeader
  869. organization={initialData.organization}
  870. projects={initialData.projects}
  871. />,
  872. initialData.routerContext
  873. );
  874. await tick();
  875. wrapper.update();
  876. // open the project menu.
  877. wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
  878. const projectSelector = wrapper.find('MultipleProjectSelector');
  879. // Two projects
  880. expect(projectSelector.find('AutoCompleteItem')).toHaveLength(2);
  881. // My projects in the footer
  882. expect(
  883. projectSelector.find('SelectorFooterControls Button').first().text()
  884. ).toEqual('View My Projects');
  885. });
  886. it('shows "All Projects" button based on features', async function () {
  887. initialData.organization.features.push('global-views');
  888. initialData.organization.features.push('open-membership');
  889. wrapper = mountWithTheme(
  890. <GlobalSelectionHeader
  891. organization={initialData.organization}
  892. projects={initialData.projects}
  893. />,
  894. initialData.routerContext
  895. );
  896. await tick();
  897. wrapper.update();
  898. // open the project menu.
  899. wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
  900. const projectSelector = wrapper.find('MultipleProjectSelector');
  901. // Two projects
  902. expect(projectSelector.find('AutoCompleteItem')).toHaveLength(2);
  903. // All projects in the footer
  904. expect(
  905. projectSelector.find('SelectorFooterControls Button').first().text()
  906. ).toEqual('View All Projects');
  907. });
  908. it('shows "All Projects" button based on role', async function () {
  909. initialData.organization.features.push('global-views');
  910. initialData.organization.role = 'owner';
  911. wrapper = mountWithTheme(
  912. <GlobalSelectionHeader
  913. organization={initialData.organization}
  914. projects={initialData.projects}
  915. />,
  916. initialData.routerContext
  917. );
  918. await tick();
  919. wrapper.update();
  920. // open the project menu.
  921. wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
  922. const projectSelector = wrapper.find('MultipleProjectSelector');
  923. // Two projects
  924. expect(projectSelector.find('AutoCompleteItem')).toHaveLength(2);
  925. // All projects in the footer
  926. expect(
  927. projectSelector.find('SelectorFooterControls Button').first().text()
  928. ).toEqual('View All Projects');
  929. });
  930. it('shows "My Projects" when "all projects" is selected', async function () {
  931. initialData.organization.features.push('global-views');
  932. initialData.organization.role = 'owner';
  933. wrapper = mountWithTheme(
  934. <GlobalSelectionHeader
  935. organization={initialData.organization}
  936. projects={initialData.projects}
  937. />,
  938. changeQuery(initialData.routerContext, {project: -1})
  939. );
  940. await tick();
  941. wrapper.update();
  942. // open the project menu.
  943. wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
  944. const projectSelector = wrapper.find('MultipleProjectSelector');
  945. // My projects in the footer
  946. expect(
  947. projectSelector.find('SelectorFooterControls Button').first().text()
  948. ).toEqual('View My Projects');
  949. });
  950. });
  951. describe('project icons', function () {
  952. const initialData = initializeOrg({
  953. organization: {features: ['global-views']},
  954. projects: [
  955. {id: 0, slug: 'go', platform: 'go'},
  956. {id: 1, slug: 'javascript', platform: 'javascript'},
  957. {id: 2, slug: 'other', platform: 'other'},
  958. {id: 3, slug: 'php', platform: 'php'},
  959. {id: 4, slug: 'python', platform: 'python'},
  960. {id: 5, slug: 'rust', platform: 'rust'},
  961. {id: 6, slug: 'swift', platform: 'swift'},
  962. ],
  963. });
  964. beforeEach(function () {
  965. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => initialData.projects);
  966. jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
  967. });
  968. it('shows IconProject when no projects are selected', async function () {
  969. wrapper = mountWithTheme(
  970. <GlobalSelectionHeader
  971. organization={initialData.organization}
  972. projects={initialData.projects}
  973. />,
  974. changeQuery(initialData.routerContext, {project: -1})
  975. );
  976. await tick();
  977. wrapper.update();
  978. const projectSelector = wrapper.find('MultipleProjectSelector');
  979. expect(projectSelector.find('IconContainer svg').exists()).toBeTruthy();
  980. expect(projectSelector.find('PlatformIcon').exists()).toBeFalsy();
  981. expect(projectSelector.find('Content').text()).toEqual('All Projects');
  982. });
  983. it('shows PlatformIcon when one project is selected', async function () {
  984. wrapper = mountWithTheme(
  985. <GlobalSelectionHeader
  986. organization={initialData.organization}
  987. projects={initialData.projects}
  988. />,
  989. changeQuery(initialData.routerContext, {project: 1})
  990. );
  991. await tick();
  992. wrapper.update();
  993. const projectSelector = wrapper.find('MultipleProjectSelector');
  994. expect(projectSelector.find('StyledPlatformIcon').props().platform).toEqual(
  995. 'javascript'
  996. );
  997. expect(projectSelector.find('Content').text()).toEqual('javascript');
  998. });
  999. it('shows multiple PlatformIcons when multiple projects are selected, no more than 5', async function () {
  1000. wrapper = mountWithTheme(
  1001. <GlobalSelectionHeader
  1002. organization={initialData.organization}
  1003. projects={initialData.projects}
  1004. />,
  1005. initialData.routerContext
  1006. );
  1007. await tick();
  1008. wrapper.update();
  1009. // select 6 projects
  1010. const headerItem = wrapper.find('MultipleProjectSelector HeaderItem');
  1011. headerItem.simulate('click');
  1012. wrapper
  1013. .find('MultipleProjectSelector CheckboxFancy')
  1014. .forEach(project => project.simulate('click'));
  1015. headerItem.simulate('click');
  1016. await tick();
  1017. wrapper.update();
  1018. // assert title and icons
  1019. const title = wrapper.find('MultipleProjectSelector Content');
  1020. const icons = wrapper.find('MultipleProjectSelector StyledPlatformIcon');
  1021. expect(title.text()).toBe('javascript, other, php, python, rust, swift');
  1022. expect(icons.length).toBe(5);
  1023. expect(icons.at(3).props().platform).toBe('rust');
  1024. expect(icons.at(4).props().platform).toBe('swift');
  1025. });
  1026. });
  1027. });