project-list-root.test.tsx 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. import {
  2. fireEvent,
  3. screen,
  4. waitFor,
  5. waitForElementToBeRemoved,
  6. within,
  7. } from '@testing-library/react'
  8. import { expect } from 'chai'
  9. import fetchMock from 'fetch-mock'
  10. import sinon from 'sinon'
  11. import ProjectListRoot from '../../../../../frontend/js/features/project-list/components/project-list-root'
  12. import { renderWithProjectListContext } from '../helpers/render-with-context'
  13. import * as eventTracking from '@/infrastructure/event-tracking'
  14. import {
  15. projectsData,
  16. owner,
  17. archivedProjects,
  18. makeLongProjectList,
  19. archiveableProject,
  20. copyableProject,
  21. } from '../fixtures/projects-data'
  22. import { location } from '@/shared/components/location'
  23. import getMeta from '@/utils/meta'
  24. const {
  25. fullList,
  26. currentList,
  27. archivedList,
  28. trashedList,
  29. leavableList,
  30. deletableList,
  31. } = makeLongProjectList(40)
  32. const userId = owner.id
  33. describe('<ProjectListRoot />', function () {
  34. this.timeout('10s')
  35. let sendMBSpy: sinon.SinonSpy
  36. beforeEach(async function () {
  37. global.localStorage.clear()
  38. sendMBSpy = sinon.spy(eventTracking, 'sendMB')
  39. this.tagId = '999fff999fff'
  40. this.tagName = 'First tag name'
  41. window.metaAttributesCache.set('ol-tags', [
  42. {
  43. _id: this.tagId,
  44. name: this.tagName,
  45. project_ids: [projectsData[0].id, projectsData[1].id],
  46. },
  47. ])
  48. Object.assign(getMeta('ol-ExposedSettings'), {
  49. templateLinks: [],
  50. })
  51. window.metaAttributesCache.set('ol-userEmails', [
  52. { email: 'test@overleaf.com', default: true },
  53. ])
  54. // we need a blank user here since its used in checking if we should display certain ads
  55. window.metaAttributesCache.set('ol-user', {})
  56. window.metaAttributesCache.set('ol-user_id', userId)
  57. window.metaAttributesCache.set('ol-footer', {
  58. showThinFooter: false,
  59. translatedLanguages: { en: 'English' },
  60. subdomainLang: { en: { lngCode: 'en', url: 'overleaf.com' } },
  61. })
  62. window.metaAttributesCache.set('ol-navbar', {
  63. items: [],
  64. })
  65. this.locationWrapperSandbox = sinon.createSandbox()
  66. this.locationWrapperStub = this.locationWrapperSandbox.stub(location)
  67. })
  68. afterEach(function () {
  69. sendMBSpy.restore()
  70. fetchMock.removeRoutes().clearHistory()
  71. this.locationWrapperSandbox.restore()
  72. })
  73. describe('welcome page', function () {
  74. beforeEach(async function () {
  75. renderWithProjectListContext(<ProjectListRoot />, {
  76. projects: [],
  77. })
  78. await fetchMock.callHistory.flush(true)
  79. })
  80. it('the welcome page is displayed', async function () {
  81. await screen.findByRole('heading', { name: 'Welcome to Overleaf' })
  82. })
  83. it('the email confirmation alert is not displayed', async function () {
  84. expect(
  85. screen.queryByText(
  86. 'Please confirm your primary email address test@overleaf.com by clicking on the link in the confirmation email.'
  87. )
  88. ).to.be.null
  89. })
  90. })
  91. describe('project table', function () {
  92. beforeEach(async function () {
  93. const { unmount } = renderWithProjectListContext(<ProjectListRoot />, {
  94. projects: fullList,
  95. })
  96. this.unmount = unmount
  97. await fetchMock.callHistory.flush(true)
  98. await screen.findByRole('table')
  99. })
  100. describe('checkboxes', function () {
  101. let allCheckboxes: Array<HTMLInputElement> = []
  102. let actionsToolbar: HTMLElement
  103. let project1Id: string | null, project2Id: string | null
  104. describe('all projects', function () {
  105. beforeEach(function () {
  106. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  107. // first one is the select all checkbox
  108. fireEvent.click(allCheckboxes[1])
  109. fireEvent.click(allCheckboxes[2])
  110. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  111. project2Id = allCheckboxes[2].getAttribute('data-project-id')
  112. actionsToolbar = screen.getAllByRole('toolbar')[0]
  113. })
  114. it('downloads all selected projects and then unselects them', async function () {
  115. const downloadButton =
  116. within(actionsToolbar).getByLabelText('Download')
  117. fireEvent.click(downloadButton)
  118. const assignStub = this.locationWrapperStub.assign
  119. await waitFor(() => {
  120. expect(assignStub).to.have.been.called
  121. })
  122. sinon.assert.calledWithMatch(
  123. assignStub,
  124. `/project/download/zip?project_ids=${project1Id},${project2Id}`
  125. )
  126. const allCheckboxes =
  127. screen.getAllByRole<HTMLInputElement>('checkbox')
  128. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  129. expect(allCheckboxesChecked.length).to.equal(0)
  130. })
  131. it('opens archive modal for all selected projects and archives all', async function () {
  132. const archiveProjectMock = fetchMock.post(
  133. `express:/project/:projectId/archive`,
  134. {
  135. status: 200,
  136. },
  137. { delay: 0 }
  138. )
  139. const archiveButton = within(actionsToolbar).getByLabelText('Archive')
  140. fireEvent.click(archiveButton)
  141. const confirmBtn = screen.getByRole('button', {
  142. name: 'Confirm',
  143. }) as HTMLButtonElement
  144. fireEvent.click(confirmBtn)
  145. expect(confirmBtn.disabled).to.be.true
  146. await waitFor(
  147. () =>
  148. expect(
  149. archiveProjectMock.callHistory.called(
  150. `/project/${project1Id}/archive`
  151. )
  152. ).to.be.true
  153. )
  154. await waitFor(
  155. () =>
  156. expect(
  157. archiveProjectMock.callHistory.called(
  158. `/project/${project2Id}/archive`
  159. )
  160. ).to.be.true
  161. )
  162. })
  163. it('opens trash modal for all selected projects and trashes all', async function () {
  164. const trashProjectMock = fetchMock.post(
  165. `express:/project/:projectId/trash`,
  166. {
  167. status: 200,
  168. },
  169. { delay: 0 }
  170. )
  171. const archiveButton = within(actionsToolbar).getByLabelText('Trash')
  172. fireEvent.click(archiveButton)
  173. const confirmBtn = screen.getByRole('button', {
  174. name: 'Confirm',
  175. }) as HTMLButtonElement
  176. fireEvent.click(confirmBtn)
  177. expect(confirmBtn.disabled).to.be.true
  178. await waitFor(
  179. () =>
  180. expect(
  181. trashProjectMock.callHistory.called(
  182. `/project/${project1Id}/trash`
  183. )
  184. ).to.be.true
  185. )
  186. await waitFor(
  187. () =>
  188. expect(
  189. trashProjectMock.callHistory.called(
  190. `/project/${project2Id}/trash`
  191. )
  192. ).to.be.true
  193. )
  194. })
  195. it('only checks the projects that are viewable when there is a load more button', async function () {
  196. // first one is the select all checkbox
  197. fireEvent.click(allCheckboxes[0])
  198. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  199. let checked = allCheckboxes.filter(c => c.checked)
  200. expect(checked.length).to.equal(21) // max projects viewable by default is 20, and plus one for check all
  201. const loadMoreButton = screen.getByRole('button', {
  202. name: 'Show 17 more projects',
  203. })
  204. fireEvent.click(loadMoreButton)
  205. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  206. expect(allCheckboxes.length).to.equal(currentList.length + 1)
  207. checked = allCheckboxes.filter(c => c.checked)
  208. expect(checked.length).to.equal(20) // remains same even after showing more
  209. })
  210. it('maintains viewable and selected projects after loading more and then selecting all', async function () {
  211. const loadMoreButton = screen.getByRole('button', {
  212. name: 'Show 17 more projects',
  213. })
  214. fireEvent.click(loadMoreButton)
  215. // verify button gone
  216. screen.getByText(
  217. `Showing ${currentList.length} out of ${currentList.length} projects.`
  218. )
  219. // first one is the select all checkbox
  220. fireEvent.click(allCheckboxes[0])
  221. // verify button still gone
  222. screen.getByText(
  223. `Showing ${currentList.length} out of ${currentList.length} projects.`
  224. )
  225. // allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  226. // expect(allCheckboxes.length).to.equal(currentList.length + 1)
  227. })
  228. })
  229. describe('archived projects', function () {
  230. beforeEach(function () {
  231. const filterButton = screen.getAllByText('Archived Projects')[0]
  232. fireEvent.click(filterButton)
  233. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  234. expect(allCheckboxes.length === 2).to.be.true
  235. // first one is the select all checkbox
  236. fireEvent.click(allCheckboxes[1])
  237. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  238. actionsToolbar = screen.getAllByRole('toolbar')[0]
  239. })
  240. it('does not show the archive button in toolbar when archive view selected', function () {
  241. expect(screen.queryByLabelText('Archive')).to.be.null
  242. })
  243. it('restores all projects when selected', async function () {
  244. fetchMock.delete(`express:/project/:id/archive`, {
  245. status: 200,
  246. })
  247. const unarchiveButton = within(actionsToolbar).getByRole('button', {
  248. name: 'Restore',
  249. })
  250. fireEvent.click(unarchiveButton)
  251. await fetchMock.callHistory.flush(true)
  252. expect(fetchMock.callHistory.done()).to.be.true
  253. await screen.findByText('No projects')
  254. })
  255. it('only unarchive the selected projects', async function () {
  256. // beforeEach selected all, so uncheck the 1st project
  257. fireEvent.click(allCheckboxes[1])
  258. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  259. expect(allCheckboxesChecked.length).to.equal(
  260. archivedProjects.length - 1
  261. )
  262. await fetchMock.callHistory.flush(true)
  263. expect(fetchMock.callHistory.done()).to.be.true
  264. expect(screen.queryByText('No projects')).to.be.null
  265. })
  266. })
  267. describe('trashed projects', function () {
  268. beforeEach(function () {
  269. const filterButton = screen.getAllByText('Trashed Projects')[0]
  270. fireEvent.click(filterButton)
  271. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  272. // + 1 because of select all
  273. expect(allCheckboxes.length).to.equal(trashedList.length + 1)
  274. // first one is the select all checkbox
  275. fireEvent.click(allCheckboxes[0])
  276. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  277. // + 1 because of select all
  278. expect(allCheckboxesChecked.length).to.equal(trashedList.length + 1)
  279. actionsToolbar = screen.getAllByRole('toolbar')[0]
  280. })
  281. it('shows the download, archive, and restore buttons in top toolbar', function () {
  282. expect(screen.queryByLabelText('Trash')).to.be.null
  283. within(actionsToolbar).queryByLabelText('Download')
  284. within(actionsToolbar).queryByLabelText('Archive')
  285. within(actionsToolbar).getByText('Restore') // no icon for this button
  286. })
  287. it('clears selected projects when filter changed', function () {
  288. const filterButton = screen.getAllByText('All Projects')[0]
  289. fireEvent.click(filterButton)
  290. const allCheckboxes =
  291. screen.getAllByRole<HTMLInputElement>('checkbox')
  292. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  293. expect(allCheckboxesChecked.length).to.equal(0)
  294. })
  295. it('untrashes all the projects', async function () {
  296. fetchMock.delete(`express:/project/:id/trash`, {
  297. status: 200,
  298. })
  299. const untrashButton =
  300. within(actionsToolbar).getByText<HTMLButtonElement>('Restore')
  301. fireEvent.click(untrashButton)
  302. await fetchMock.callHistory.flush(true)
  303. expect(fetchMock.callHistory.done()).to.be.true
  304. await screen.findByText('No projects')
  305. })
  306. it('only untrashes the selected projects', async function () {
  307. // beforeEach selected all, so uncheck the 1st project
  308. fireEvent.click(allCheckboxes[1])
  309. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  310. expect(allCheckboxesChecked.length).to.equal(trashedList.length - 1)
  311. await fetchMock.callHistory.flush(true)
  312. expect(fetchMock.callHistory.done()).to.be.true
  313. expect(screen.queryByText('No projects')).to.be.null
  314. })
  315. it('removes project from view when archiving', async function () {
  316. fetchMock.post(
  317. `express:/project/:id/archive`,
  318. {
  319. status: 200,
  320. },
  321. { repeat: trashedList.length }
  322. )
  323. const archiveButton =
  324. within(actionsToolbar).getByLabelText<HTMLButtonElement>('Archive')
  325. fireEvent.click(archiveButton)
  326. const confirmButton = screen.getByRole('button', {
  327. name: 'Confirm',
  328. }) as HTMLButtonElement
  329. fireEvent.click(confirmButton)
  330. expect(confirmButton.disabled).to.be.true
  331. await fetchMock.callHistory.flush(true)
  332. expect(fetchMock.callHistory.done()).to.be.true
  333. const calls = fetchMock.callHistory.calls().map(({ url }) => url)
  334. trashedList.forEach(project => {
  335. expect(calls).to.contain(
  336. `https://www.test-overleaf.com/project/${project.id}/archive`
  337. )
  338. })
  339. })
  340. it('removes only selected projects from view when leaving', async function () {
  341. // rerender content with different projects
  342. this.unmount()
  343. fetchMock.removeRoutes().clearHistory()
  344. renderWithProjectListContext(<ProjectListRoot />, {
  345. projects: leavableList,
  346. })
  347. await fetchMock.callHistory.flush(true)
  348. await screen.findByRole('table')
  349. expect(leavableList.length).to.be.greaterThan(0)
  350. fetchMock.post(
  351. `express:/project/:id/leave`,
  352. {
  353. status: 200,
  354. },
  355. { repeat: leavableList.length }
  356. )
  357. // + 1 because of select all
  358. await waitFor(() =>
  359. expect(
  360. screen.getAllByRole<HTMLInputElement>('checkbox').length
  361. ).to.equal(leavableList.length + 1)
  362. )
  363. // first one is the select all checkbox
  364. fireEvent.click(screen.getAllByRole('checkbox')[0])
  365. actionsToolbar = screen.getAllByRole('toolbar')[0]
  366. const toolbar = within(actionsToolbar)
  367. expect(toolbar.queryByRole('button', { name: /delete/i })).to.be.null
  368. expect(
  369. toolbar.queryByRole('button', {
  370. name: /delete \/ leave/i,
  371. })
  372. ).to.be.null
  373. const leaveButton = toolbar.getByRole('button', {
  374. name: /leave/i,
  375. })
  376. fireEvent.click(leaveButton)
  377. const confirmButton = screen.getByRole('button', {
  378. name: 'Confirm',
  379. }) as HTMLButtonElement
  380. fireEvent.click(confirmButton)
  381. expect(confirmButton.disabled).to.be.true
  382. await fetchMock.callHistory.flush(true)
  383. expect(fetchMock.callHistory.done()).to.be.true
  384. const calls = fetchMock.callHistory.calls().map(({ url }) => url)
  385. leavableList.forEach(project => {
  386. expect(calls).to.contain(
  387. `https://www.test-overleaf.com/project/${project.id}/leave`
  388. )
  389. })
  390. })
  391. it('removes only selected projects from view when deleting', async function () {
  392. // rerender content with different projects
  393. this.unmount()
  394. fetchMock.removeRoutes().clearHistory()
  395. renderWithProjectListContext(<ProjectListRoot />, {
  396. projects: deletableList,
  397. })
  398. await fetchMock.callHistory.flush(true)
  399. await screen.findByRole('table')
  400. expect(deletableList.length).to.be.greaterThan(0)
  401. fetchMock.delete(
  402. `express:/project/:id`,
  403. {
  404. status: 200,
  405. },
  406. { repeat: deletableList.length }
  407. )
  408. await waitFor(() => {
  409. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  410. // + 1 because of select all
  411. expect(allCheckboxes.length).to.equal(deletableList.length + 1)
  412. })
  413. // first one is the select all checkbox
  414. fireEvent.click(allCheckboxes[0])
  415. actionsToolbar = screen.getAllByRole('toolbar')[0]
  416. const toolbar = within(actionsToolbar)
  417. expect(toolbar.queryByRole('button', { name: /leave/i })).to.be.null
  418. expect(
  419. toolbar.queryByRole('button', {
  420. name: /delete \/ leave/i,
  421. })
  422. ).to.be.null
  423. const deleteButton = toolbar.getByRole('button', {
  424. name: /delete/i,
  425. })
  426. fireEvent.click(deleteButton)
  427. const confirmButton = screen.getByRole('button', {
  428. name: 'Confirm',
  429. }) as HTMLButtonElement
  430. fireEvent.click(confirmButton)
  431. expect(confirmButton.disabled).to.be.true
  432. await fetchMock.callHistory.flush(true)
  433. expect(fetchMock.callHistory.done()).to.be.true
  434. const calls = fetchMock.callHistory.calls().map(({ url }) => url)
  435. deletableList.forEach(project => {
  436. expect(calls).to.contain(
  437. `https://www.test-overleaf.com/project/${project.id}`
  438. )
  439. })
  440. })
  441. it('removes only selected projects from view when deleting and leaving', async function () {
  442. // rerender content with different projects
  443. this.unmount()
  444. fetchMock.removeRoutes().clearHistory()
  445. const deletableAndLeavableList = [...deletableList, ...leavableList]
  446. renderWithProjectListContext(<ProjectListRoot />, {
  447. projects: deletableAndLeavableList,
  448. })
  449. await fetchMock.callHistory.flush(true)
  450. await screen.findByRole('table')
  451. expect(deletableList.length).to.be.greaterThan(0)
  452. expect(leavableList.length).to.be.greaterThan(0)
  453. fetchMock
  454. .delete(
  455. `express:/project/:id`,
  456. {
  457. status: 200,
  458. },
  459. { repeat: deletableList.length }
  460. )
  461. .post(
  462. `express:/project/:id/leave`,
  463. {
  464. status: 200,
  465. },
  466. { repeat: leavableList.length }
  467. )
  468. // + 1 because of select all
  469. await waitFor(() =>
  470. expect(
  471. screen.getAllByRole<HTMLInputElement>('checkbox').length
  472. ).to.equal(deletableAndLeavableList.length + 1)
  473. )
  474. // first one is the select all checkbox
  475. fireEvent.click(screen.getAllByRole<HTMLInputElement>('checkbox')[0])
  476. actionsToolbar = screen.getAllByRole('toolbar')[0]
  477. const toolbar = within(actionsToolbar)
  478. expect(toolbar.queryByRole('button', { name: 'Leave' })).to.be.null
  479. expect(toolbar.queryByRole('button', { name: 'Delete' })).to.be.null
  480. const deleteLeaveButton = toolbar.getByRole('button', {
  481. name: /delete \/ leave/i,
  482. })
  483. fireEvent.click(deleteLeaveButton)
  484. const confirmButton = screen.getByRole('button', {
  485. name: 'Confirm',
  486. }) as HTMLButtonElement
  487. fireEvent.click(confirmButton)
  488. expect(confirmButton.disabled).to.be.true
  489. await fetchMock.callHistory.flush(true)
  490. expect(fetchMock.callHistory.done()).to.be.true
  491. const calls = fetchMock.callHistory.calls().map(({ url }) => url)
  492. deletableAndLeavableList.forEach(project => {
  493. expect(calls).to.contain.oneOf([
  494. `https://www.test-overleaf.com/project/${project.id}`,
  495. `https://www.test-overleaf.com/project/${project.id}/leave`,
  496. ])
  497. })
  498. })
  499. })
  500. describe('tags', function () {
  501. it('does not show archived or trashed project', async function () {
  502. this.unmount()
  503. fetchMock.removeRoutes().clearHistory()
  504. window.metaAttributesCache.set('ol-tags', [
  505. {
  506. _id: this.tagId,
  507. name: this.tagName,
  508. project_ids: [
  509. projectsData[0].id,
  510. projectsData[1].id,
  511. ...archivedList.map(p => p.id),
  512. ...trashedList.map(p => p.id),
  513. ],
  514. },
  515. ])
  516. const trashProjectMock = fetchMock.post(
  517. `express:/project/:projectId/trash`,
  518. { status: 200 }
  519. )
  520. renderWithProjectListContext(<ProjectListRoot />, {
  521. projects: fullList,
  522. })
  523. await screen.findByRole('table')
  524. let visibleProjectsCount = 2
  525. const [tagBtn] = screen.getAllByRole('button', {
  526. name: `${this.tagName} (${visibleProjectsCount})`,
  527. })
  528. fireEvent.click(tagBtn)
  529. const nonArchivedAndTrashedProjects = [
  530. projectsData[0],
  531. projectsData[1],
  532. ]
  533. nonArchivedAndTrashedProjects.forEach(p => {
  534. screen.getByText(p.name)
  535. })
  536. const archivedAndTrashedProjects = [...archivedList, ...trashedList]
  537. archivedAndTrashedProjects.forEach(p => {
  538. expect(screen.queryByText(p.name)).to.be.null
  539. })
  540. const trashBtns = screen.getAllByRole('button', { name: 'Trash' })
  541. for (const [index, trashBtn] of trashBtns.entries()) {
  542. fireEvent.click(trashBtn)
  543. fireEvent.click(
  544. screen.getByRole('button', {
  545. name: 'Confirm',
  546. })
  547. )
  548. await waitFor(() => {
  549. expect(
  550. trashProjectMock.callHistory.called(
  551. `/project/${projectsData[index].id}/trash`
  552. )
  553. ).to.be.true
  554. })
  555. await waitFor(() => {
  556. expect(
  557. screen.queryAllByText(projectsData[index].name)
  558. ).to.have.length(0)
  559. })
  560. screen.getAllByRole('button', {
  561. name: `${this.tagName} (${--visibleProjectsCount})`,
  562. })
  563. }
  564. })
  565. })
  566. describe('tags dropdown', function () {
  567. beforeEach(async function () {
  568. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  569. // first one is the select all checkbox
  570. fireEvent.click(allCheckboxes[1])
  571. fireEvent.click(allCheckboxes[2])
  572. actionsToolbar = screen.getAllByRole('toolbar')[0]
  573. this.newTagName = 'Some tag name'
  574. this.newTagId = 'abc123def456'
  575. })
  576. it('opens the tags dropdown and creates a new tag', async function () {
  577. fetchMock.post(`express:/tag`, {
  578. status: 200,
  579. body: {
  580. _id: this.newTagId,
  581. name: this.newTagName,
  582. project_ids: [],
  583. },
  584. })
  585. fetchMock.post(`express:/tag/:id/projects`, {
  586. status: 204,
  587. })
  588. const tagsDropdown =
  589. await within(actionsToolbar).findByLabelText('Tags')
  590. fireEvent.click(tagsDropdown)
  591. screen.getByText('Add to tag')
  592. const newTagButton = screen.getByRole('menuitem', {
  593. name: 'Create new tag',
  594. })
  595. fireEvent.click(newTagButton)
  596. const modal = screen.getAllByRole('dialog')[0]
  597. const input = within(modal).getByRole<HTMLInputElement>('textbox')
  598. fireEvent.change(input, {
  599. target: { value: this.newTagName },
  600. })
  601. const createButton = within(modal).getByRole('button', {
  602. name: 'Create',
  603. })
  604. fireEvent.click(createButton)
  605. await fetchMock.callHistory.flush(true)
  606. expect(
  607. fetchMock.callHistory.called('/tag', { name: this.newTagName })
  608. ).to.be.true
  609. expect(
  610. fetchMock.callHistory.called(`/tag/${this.newTagId}/projects`, {
  611. body: {
  612. projectIds: [projectsData[0].id, projectsData[1].id],
  613. },
  614. })
  615. ).to.be.true
  616. await screen.findByRole(
  617. 'button',
  618. { name: `${this.newTagName} (2)` },
  619. { timeout: 5000 }
  620. )
  621. })
  622. it('opens the tags dropdown and remove a tag from selected projects', async function () {
  623. const deleteProjectsFromTagMock = fetchMock.post(
  624. `express:/tag/:id/projects/remove`,
  625. {
  626. status: 204,
  627. }
  628. )
  629. screen.getByRole('button', { name: `${this.tagName} (2)` })
  630. const tagsDropdown = within(actionsToolbar).getByLabelText('Tags')
  631. fireEvent.click(tagsDropdown)
  632. within(actionsToolbar).getByText('Add to tag')
  633. const tagButton = within(actionsToolbar).getByLabelText(
  634. `Add or remove project from tag ${this.tagName}`
  635. )
  636. fireEvent.click(tagButton)
  637. await fetchMock.callHistory.flush(true)
  638. expect(
  639. deleteProjectsFromTagMock.callHistory.called(
  640. `/tag/${this.tagId}/projects/remove`,
  641. {
  642. body: {
  643. projectIds: [projectsData[0].id, projectsData[1].id],
  644. },
  645. }
  646. )
  647. ).to.be.true
  648. screen.getByRole('button', { name: `${this.tagName} (0)` })
  649. })
  650. it('select another project, opens the tags dropdown and add a tag only to the untagged project', async function () {
  651. const addProjectsToTagMock = fetchMock.post(
  652. `express:/tag/:id/projects`,
  653. {
  654. status: 204,
  655. }
  656. )
  657. fireEvent.click(allCheckboxes[3])
  658. screen.getByRole('button', { name: `${this.tagName} (2)` })
  659. const tagsDropdown = within(actionsToolbar).getByLabelText('Tags')
  660. fireEvent.click(tagsDropdown)
  661. within(actionsToolbar).getByText('Add to tag')
  662. const tagButton = within(actionsToolbar).getByLabelText(
  663. `Add or remove project from tag ${this.tagName}`
  664. )
  665. fireEvent.click(tagButton)
  666. await fetchMock.callHistory.flush(true)
  667. expect(
  668. addProjectsToTagMock.callHistory.called(
  669. `/tag/${this.tagId}/projects`,
  670. {
  671. body: {
  672. projectIds: [projectsData[2].id],
  673. },
  674. }
  675. )
  676. ).to.be.true
  677. screen.getByRole('button', { name: `${this.tagName} (3)` })
  678. })
  679. })
  680. describe('project tools', function () {
  681. it('renders download, archive, trash buttons followed by tags and "more" dropdowns when selecting an archived or trashed filter before selecting tag filter', function () {
  682. const assertToolbarButtonsExists = () => {
  683. within(actionsToolbar).getByLabelText(/download/i)
  684. within(actionsToolbar).getByLabelText(/archive/i)
  685. within(actionsToolbar).getByLabelText(/trash/i)
  686. within(actionsToolbar).getByLabelText(/tags/i)
  687. within(actionsToolbar).getByText(/more/i)
  688. }
  689. // Select archived projects
  690. const [archivedProjectsButton] =
  691. screen.getAllByText(/archived projects/i)
  692. fireEvent.click(archivedProjectsButton)
  693. const [tag] = screen.getAllByText(this.tagName)
  694. fireEvent.click(tag)
  695. allCheckboxes = screen.getAllByRole('checkbox')
  696. fireEvent.click(allCheckboxes[1])
  697. actionsToolbar = screen.getAllByRole('toolbar')[0]
  698. assertToolbarButtonsExists()
  699. // select trashed projects
  700. const [trashedProjectsButton] =
  701. screen.getAllByText(/trashed projects/i)
  702. fireEvent.click(trashedProjectsButton)
  703. fireEvent.click(tag)
  704. allCheckboxes = screen.getAllByRole('checkbox')
  705. fireEvent.click(allCheckboxes[1])
  706. actionsToolbar = screen.getAllByRole('toolbar')[0]
  707. assertToolbarButtonsExists()
  708. })
  709. describe('"More" dropdown', function () {
  710. beforeEach(async function () {
  711. const filterButton = screen.getAllByText('All Projects')[0]
  712. fireEvent.click(filterButton)
  713. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  714. })
  715. it('does not show the dropdown when more than 1 project is selected', async function () {
  716. fireEvent.click(allCheckboxes[2]) // select a project
  717. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  718. await waitFor(() => {
  719. within(actionsToolbar).getByText<HTMLElement>('More')
  720. })
  721. fireEvent.click(allCheckboxes[0]) // select all
  722. expect(within(actionsToolbar).queryByText<HTMLElement>('More')).to
  723. .be.null
  724. })
  725. it('validates the project name', async function () {
  726. fireEvent.click(allCheckboxes[1]) // select a project owned by the current user
  727. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  728. const moreDropdown =
  729. await within(actionsToolbar).findByText<HTMLElement>('More')
  730. fireEvent.click(moreDropdown)
  731. const editButton =
  732. screen.getAllByText<HTMLButtonElement>('Rename')[0]
  733. fireEvent.click(editButton)
  734. const modals = await screen.findAllByRole('dialog')
  735. const modal = modals[0]
  736. expect(sendMBSpy).to.have.been.calledTwice
  737. expect(sendMBSpy).to.have.been.calledWith('loads_v2_dash')
  738. expect(sendMBSpy).to.have.been.calledWith(
  739. 'project-list-page-interaction',
  740. {
  741. action: 'rename',
  742. page: '/',
  743. projectId: copyableProject.id,
  744. isSmallDevice: true,
  745. }
  746. )
  747. // same name
  748. let confirmButton = within(modal).getByRole('button', {
  749. name: 'Rename',
  750. }) as HTMLButtonElement
  751. expect(confirmButton.disabled).to.be.true
  752. // no name
  753. const input = screen.getByLabelText('New Name') as HTMLButtonElement
  754. fireEvent.change(input, {
  755. target: { value: '' },
  756. })
  757. confirmButton = within(modal).getByRole('button', {
  758. name: 'Rename',
  759. }) as HTMLButtonElement
  760. expect(confirmButton.disabled).to.be.true
  761. })
  762. it('opens the rename modal, and can rename the project, and view updated', async function () {
  763. fireEvent.click(allCheckboxes[1]) // select a project owned by the current user
  764. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  765. const renameProjectMock = fetchMock.post(
  766. `express:/project/:id/rename`,
  767. {
  768. status: 200,
  769. }
  770. )
  771. const moreDropdown =
  772. await within(actionsToolbar).findByText<HTMLElement>('More')
  773. fireEvent.click(moreDropdown)
  774. const renameButton =
  775. within(actionsToolbar).getByText<HTMLButtonElement>('Rename')
  776. fireEvent.click(renameButton)
  777. const modals = await screen.findAllByRole('dialog')
  778. const modal = modals[0]
  779. // a valid name
  780. const newProjectName = 'A new project name'
  781. const input = (await within(modal).findByLabelText(
  782. 'New Name'
  783. )) as HTMLButtonElement
  784. const oldName = input.value
  785. fireEvent.change(input, {
  786. target: { value: newProjectName },
  787. })
  788. const confirmButton = within(modal).getByRole('button', {
  789. name: 'Rename',
  790. }) as HTMLButtonElement
  791. expect(confirmButton.disabled).to.be.false
  792. fireEvent.click(confirmButton)
  793. await fetchMock.callHistory.flush(true)
  794. expect(
  795. renameProjectMock.callHistory.called(
  796. `/project/${projectsData[0].id}/rename`
  797. )
  798. ).to.be.true
  799. const table = await screen.findByRole('table')
  800. await within(table).findByText(newProjectName)
  801. expect(within(table).queryByText(oldName)).to.be.null
  802. const allCheckboxesInTable =
  803. await within(table).findAllByRole<HTMLInputElement>('checkbox')
  804. const allCheckboxesChecked = allCheckboxesInTable.filter(
  805. c => c.checked
  806. )
  807. expect(allCheckboxesChecked.length).to.equal(0)
  808. })
  809. it('opens the copy modal, can copy the project, and view updated', async function () {
  810. fireEvent.click(allCheckboxes[2])
  811. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  812. const tableRows = screen.getAllByRole('row')
  813. const linkForProjectToCopy = within(tableRows[1]).getByRole('link')
  814. const projectNameToCopy = linkForProjectToCopy.textContent || '' // needed for type checking
  815. screen.getByText(projectNameToCopy) // make sure not just empty string
  816. const copiedProjectName = `${projectNameToCopy} (Copy)`
  817. const cloneProjectMock = fetchMock.post(
  818. `express:/project/:id/clone`,
  819. {
  820. status: 200,
  821. body: {
  822. name: copiedProjectName,
  823. lastUpdated: new Date(),
  824. project_id: userId,
  825. owner_ref: userId,
  826. owner,
  827. id: '6328e14abec0df019fce0be5',
  828. lastUpdatedBy: owner,
  829. accessLevel: 'owner',
  830. source: 'owner',
  831. trashed: false,
  832. archived: false,
  833. },
  834. }
  835. )
  836. await waitFor(() => {
  837. const moreDropdown =
  838. within(actionsToolbar).getByText<HTMLElement>('More')
  839. fireEvent.click(moreDropdown)
  840. })
  841. const copyButton =
  842. within(actionsToolbar).getByText<HTMLButtonElement>('Make a copy')
  843. fireEvent.click(copyButton)
  844. // confirm in modal
  845. const copyConfirmButton = document.querySelector(
  846. 'button[type="submit"]'
  847. ) as HTMLElement
  848. fireEvent.click(copyConfirmButton)
  849. await fetchMock.callHistory.flush(true)
  850. expect(
  851. cloneProjectMock.callHistory.called(
  852. `/project/${projectsData[1].id}/clone`
  853. )
  854. ).to.be.true
  855. expect(sendMBSpy).to.have.been.calledTwice
  856. expect(sendMBSpy).to.have.been.calledWith('loads_v2_dash')
  857. expect(sendMBSpy).to.have.been.calledWith(
  858. 'project-list-page-interaction',
  859. {
  860. action: 'clone',
  861. page: '/',
  862. projectId: archiveableProject.id,
  863. isSmallDevice: true,
  864. }
  865. )
  866. await screen.findByText(copiedProjectName)
  867. })
  868. })
  869. })
  870. describe('projects list in actions modal', function () {
  871. let modal: HTMLElement
  872. let projectsToProcess: any[]
  873. function selectedProjectNames() {
  874. projectsToProcess = []
  875. // needs to be done ahead of opening modal
  876. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  877. // update list so we know which are checked
  878. const tableRows = screen.getAllByRole('row')
  879. for (const [index, checkbox] of allCheckboxes.entries()) {
  880. if (checkbox.checked) {
  881. const linkForProjectToCopy = within(tableRows[index]).getByRole(
  882. 'link'
  883. )
  884. const projectNameToCopy = linkForProjectToCopy.textContent
  885. projectsToProcess.push(projectNameToCopy)
  886. }
  887. }
  888. expect(projectsToProcess.length > 0).to.be.true
  889. }
  890. function selectedMatchesDisplayed(expectedLength: number) {
  891. selectedProjectNames()
  892. // any action will work for check since they all use the same modal
  893. const archiveButton = within(actionsToolbar).getByLabelText('Archive')
  894. fireEvent.click(archiveButton)
  895. modal = screen.getAllByRole('dialog')[0]
  896. const listitems = within(modal).getAllByRole('listitem')
  897. expect(listitems.length).to.equal(projectsToProcess.length)
  898. expect(listitems.length).to.equal(expectedLength)
  899. for (const projectName of projectsToProcess) {
  900. within(modal).getByText(projectName)
  901. }
  902. }
  903. beforeEach(function () {
  904. fetchMock.removeRoutes().clearHistory()
  905. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  906. // first one is the select all checkbox, just check 2 at first
  907. fireEvent.click(allCheckboxes[1])
  908. fireEvent.click(allCheckboxes[2])
  909. actionsToolbar = screen.getAllByRole('toolbar')[0]
  910. })
  911. it('opens the modal with the 2 originally selected projects', function () {
  912. selectedMatchesDisplayed(2)
  913. })
  914. it('shows correct list after closing modal, changing selection, and reopening modal', async function () {
  915. selectedMatchesDisplayed(2)
  916. const modal = screen.getAllByRole('dialog', { hidden: false })[0]
  917. const cancelButton = within(modal).getByRole('button', {
  918. name: 'Cancel',
  919. })
  920. fireEvent.click(cancelButton)
  921. await waitForElementToBeRemoved(
  922. screen.getByRole('dialog', { hidden: false })
  923. )
  924. await screen.findAllByRole('checkbox')
  925. fireEvent.click(allCheckboxes[3])
  926. selectedMatchesDisplayed(3)
  927. })
  928. it('maintains original list even after some have been processed', async function () {
  929. const totalProjectsToProcess = 2
  930. selectedMatchesDisplayed(totalProjectsToProcess)
  931. const button = screen.getByRole('button', { name: 'Confirm' })
  932. fireEvent.click(button)
  933. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  934. fetchMock.post('express:/project/:id/archive', {
  935. status: 200,
  936. })
  937. fetchMock.post(`/project/${project2Id}/archive`, {
  938. status: 500,
  939. })
  940. await screen.findByRole('alert') // ensure that error was thrown for the 2nd project
  941. const listitems = within(modal).getAllByRole('listitem')
  942. expect(listitems.length).to.equal(totalProjectsToProcess)
  943. })
  944. })
  945. })
  946. describe('search', function () {
  947. it('shows only projects based on the input', async function () {
  948. const input = screen.getAllByRole('textbox', {
  949. name: /search in all projects/i,
  950. })[0]
  951. const value = currentList[0].name
  952. fireEvent.change(input, { target: { value } })
  953. const results = screen.getAllByRole('row')
  954. expect(results.length).to.equal(2) // first is header
  955. })
  956. })
  957. describe('copying project', function () {
  958. it('correctly updates the view after copying a shared project', async function () {
  959. const filterButton = screen.getAllByText('Shared with you')[0]
  960. fireEvent.click(filterButton)
  961. const tableRows = screen.getAllByRole('row')
  962. const linkForProjectToCopy = within(tableRows[1]).getByRole('link')
  963. const projectNameToCopy = linkForProjectToCopy.textContent
  964. const copiedProjectName = `${projectNameToCopy} Copy`
  965. fetchMock.post(`express:/project/:id/clone`, {
  966. status: 200,
  967. body: {
  968. name: copiedProjectName,
  969. lastUpdated: new Date(),
  970. project_id: userId,
  971. owner_ref: userId,
  972. owner,
  973. id: '6328e14abec0df019fce0be5',
  974. lastUpdatedBy: owner,
  975. accessLevel: 'owner',
  976. source: 'owner',
  977. trashed: false,
  978. archived: false,
  979. },
  980. })
  981. const copyButton = within(tableRows[1]).getAllByRole('button', {
  982. name: 'Copy',
  983. })[0]
  984. fireEvent.click(copyButton)
  985. // confirm in modal
  986. const copyConfirmButton = document.querySelector(
  987. 'button[type="submit"]'
  988. ) as HTMLElement
  989. fireEvent.click(copyConfirmButton)
  990. await fetchMock.callHistory.flush(true)
  991. expect(fetchMock.callHistory.done()).to.be.true
  992. expect(sendMBSpy).to.have.been.calledTwice
  993. expect(sendMBSpy).to.have.been.calledWith('loads_v2_dash')
  994. expect(sendMBSpy).to.have.been.calledWith(
  995. 'project-list-page-interaction',
  996. {
  997. action: 'clone',
  998. page: '/',
  999. projectId: archiveableProject.id,
  1000. isSmallDevice: true,
  1001. }
  1002. )
  1003. expect(screen.queryByText(copiedProjectName)).to.be.null
  1004. const yourProjectFilter = screen.getAllByText('Your Projects')[0]
  1005. fireEvent.click(yourProjectFilter)
  1006. await screen.findByText(copiedProjectName)
  1007. })
  1008. })
  1009. })
  1010. })