project-list-root.test.tsx 39 KB

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