project-list-root.test.tsx 38 KB

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