project-list-root.test.tsx 39 KB

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