project-list-root.test.tsx 39 KB

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