project-list-root.test.tsx 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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.post(
  563. `express:/tag/:id/projects/remove`,
  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(
  579. `/tag/${this.tagId}/projects/remove`,
  580. {
  581. body: {
  582. projectIds: [projectsData[0].id, projectsData[1].id],
  583. },
  584. }
  585. )
  586. ).to.be.true
  587. screen.getByRole('button', { name: `${this.tagName} (0)` })
  588. })
  589. it('select another project, opens the tags dropdown and add a tag only to the untagged project', async function () {
  590. const addProjectsToTagMock = fetchMock.post(
  591. `express:/tag/:id/projects`,
  592. {
  593. status: 204,
  594. }
  595. )
  596. fireEvent.click(allCheckboxes[3])
  597. screen.getByRole('button', { name: `${this.tagName} (2)` })
  598. const tagsDropdown = within(actionsToolbar).getByLabelText('Tags')
  599. fireEvent.click(tagsDropdown)
  600. within(actionsToolbar).getByText('Add to tag')
  601. const tagButton = within(actionsToolbar).getByLabelText(
  602. `Add or remove project from tag ${this.tagName}`
  603. )
  604. fireEvent.click(tagButton)
  605. await fetchMock.flush(true)
  606. expect(
  607. addProjectsToTagMock.called(`/tag/${this.tagId}/projects`, {
  608. body: {
  609. projectIds: [projectsData[2].id],
  610. },
  611. })
  612. ).to.be.true
  613. screen.getByRole('button', { name: `${this.tagName} (3)` })
  614. })
  615. })
  616. describe('project tools', function () {
  617. it('renders download, archive, trash buttons followed by tags and "more" dropdowns when selecting an archived or trashed filter before selecting tag filter', function () {
  618. const assertToolbarButtonsExists = () => {
  619. within(actionsToolbar).getByLabelText(/download/i)
  620. within(actionsToolbar).getByLabelText(/archive/i)
  621. within(actionsToolbar).getByLabelText(/trash/i)
  622. within(actionsToolbar).getByLabelText(/tags/i)
  623. within(actionsToolbar).getByText(/more/i)
  624. }
  625. // Select archived projects
  626. const [archivedProjectsButton] =
  627. screen.getAllByText(/archived projects/i)
  628. fireEvent.click(archivedProjectsButton)
  629. const [tag] = screen.getAllByText(this.tagName)
  630. fireEvent.click(tag)
  631. allCheckboxes = screen.getAllByRole('checkbox')
  632. fireEvent.click(allCheckboxes[1])
  633. actionsToolbar = screen.getAllByRole('toolbar')[0]
  634. assertToolbarButtonsExists()
  635. // select trashed projects
  636. const [trashedProjectsButton] =
  637. screen.getAllByText(/trashed projects/i)
  638. fireEvent.click(trashedProjectsButton)
  639. fireEvent.click(tag)
  640. allCheckboxes = screen.getAllByRole('checkbox')
  641. fireEvent.click(allCheckboxes[1])
  642. actionsToolbar = screen.getAllByRole('toolbar')[0]
  643. assertToolbarButtonsExists()
  644. })
  645. describe('"More" dropdown', function () {
  646. beforeEach(async function () {
  647. const filterButton = screen.getAllByText('All Projects')[0]
  648. fireEvent.click(filterButton)
  649. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  650. })
  651. it('does not show the dropdown when more than 1 project is selected', async function () {
  652. fireEvent.click(allCheckboxes[2]) // select a project
  653. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  654. await waitFor(() => {
  655. within(actionsToolbar).getByText<HTMLElement>('More')
  656. })
  657. fireEvent.click(allCheckboxes[0]) // select all
  658. expect(within(actionsToolbar).queryByText<HTMLElement>('More')).to
  659. .be.null
  660. })
  661. it('validates the project name', async function () {
  662. fireEvent.click(allCheckboxes[1]) // select a project owned by the current user
  663. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  664. const moreDropdown =
  665. await within(actionsToolbar).findByText<HTMLElement>('More')
  666. fireEvent.click(moreDropdown)
  667. const editButton =
  668. screen.getAllByText<HTMLButtonElement>('Rename')[0]
  669. fireEvent.click(editButton)
  670. const modals = await screen.findAllByRole('dialog')
  671. const modal = modals[0]
  672. expect(sendMBSpy).to.have.been.calledTwice
  673. expect(sendMBSpy).to.have.been.calledWith('loads_v2_dash')
  674. expect(sendMBSpy).to.have.been.calledWith(
  675. 'project-list-page-interaction',
  676. {
  677. action: 'rename',
  678. page: '/',
  679. projectId: copyableProject.id,
  680. isSmallDevice: true,
  681. }
  682. )
  683. // same name
  684. let confirmButton =
  685. within(modal).getByText<HTMLButtonElement>('Rename')
  686. expect(confirmButton.disabled).to.be.true
  687. // no name
  688. const input = screen.getByLabelText('New Name') as HTMLButtonElement
  689. fireEvent.change(input, {
  690. target: { value: '' },
  691. })
  692. confirmButton = within(modal).getByText<HTMLButtonElement>('Rename')
  693. expect(confirmButton.disabled).to.be.true
  694. })
  695. it('opens the rename modal, and can rename the project, and view updated', async function () {
  696. fireEvent.click(allCheckboxes[1]) // select a project owned by the current user
  697. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  698. const renameProjectMock = fetchMock.post(
  699. `express:/project/:id/rename`,
  700. {
  701. status: 200,
  702. }
  703. )
  704. const moreDropdown =
  705. await within(actionsToolbar).findByText<HTMLElement>('More')
  706. fireEvent.click(moreDropdown)
  707. const renameButton =
  708. within(actionsToolbar).getByText<HTMLButtonElement>('Rename')
  709. fireEvent.click(renameButton)
  710. const modals = await screen.findAllByRole('dialog')
  711. const modal = modals[0]
  712. // a valid name
  713. const newProjectName = 'A new project name'
  714. const input = (await within(modal).findByLabelText(
  715. 'New Name'
  716. )) as HTMLButtonElement
  717. const oldName = input.value
  718. fireEvent.change(input, {
  719. target: { value: newProjectName },
  720. })
  721. const confirmButton =
  722. within(modal).getByText<HTMLButtonElement>('Rename')
  723. expect(confirmButton.disabled).to.be.false
  724. fireEvent.click(confirmButton)
  725. await fetchMock.flush(true)
  726. expect(
  727. renameProjectMock.called(`/project/${projectsData[0].id}/rename`)
  728. ).to.be.true
  729. const table = await screen.findByRole('table')
  730. within(table).getByText(newProjectName)
  731. expect(within(table).queryByText(oldName)).to.be.null
  732. const allCheckboxesInTable =
  733. await within(table).findAllByRole<HTMLInputElement>('checkbox')
  734. const allCheckboxesChecked = allCheckboxesInTable.filter(
  735. c => c.checked
  736. )
  737. expect(allCheckboxesChecked.length).to.equal(0)
  738. })
  739. it('opens the copy modal, can copy the project, and view updated', async function () {
  740. fireEvent.click(allCheckboxes[2])
  741. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  742. const tableRows = screen.getAllByRole('row')
  743. const linkForProjectToCopy = within(tableRows[1]).getByRole('link')
  744. const projectNameToCopy = linkForProjectToCopy.textContent || '' // needed for type checking
  745. screen.getByText(projectNameToCopy) // make sure not just empty string
  746. const copiedProjectName = `${projectNameToCopy} (Copy)`
  747. const cloneProjectMock = fetchMock.post(
  748. `express:/project/:id/clone`,
  749. {
  750. status: 200,
  751. body: {
  752. name: copiedProjectName,
  753. lastUpdated: new Date(),
  754. project_id: userId,
  755. owner_ref: userId,
  756. owner,
  757. id: '6328e14abec0df019fce0be5',
  758. lastUpdatedBy: owner,
  759. accessLevel: 'owner',
  760. source: 'owner',
  761. trashed: false,
  762. archived: false,
  763. },
  764. }
  765. )
  766. await waitFor(() => {
  767. const moreDropdown =
  768. within(actionsToolbar).getByText<HTMLElement>('More')
  769. fireEvent.click(moreDropdown)
  770. })
  771. const copyButton =
  772. within(actionsToolbar).getByText<HTMLButtonElement>('Make a copy')
  773. fireEvent.click(copyButton)
  774. // confirm in modal
  775. const copyConfirmButton = document.querySelector(
  776. 'button[type="submit"]'
  777. ) as HTMLElement
  778. fireEvent.click(copyConfirmButton)
  779. await fetchMock.flush(true)
  780. expect(
  781. cloneProjectMock.called(`/project/${projectsData[1].id}/clone`)
  782. ).to.be.true
  783. expect(sendMBSpy).to.have.been.calledTwice
  784. expect(sendMBSpy).to.have.been.calledWith('loads_v2_dash')
  785. expect(sendMBSpy).to.have.been.calledWith(
  786. 'project-list-page-interaction',
  787. {
  788. action: 'clone',
  789. page: '/',
  790. projectId: archiveableProject.id,
  791. isSmallDevice: true,
  792. }
  793. )
  794. screen.getByText(copiedProjectName)
  795. })
  796. })
  797. })
  798. describe('projects list in actions modal', function () {
  799. let modal: HTMLElement
  800. let projectsToProcess: any[]
  801. function selectedProjectNames() {
  802. projectsToProcess = []
  803. // needs to be done ahead of opening modal
  804. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  805. // update list so we know which are checked
  806. const tableRows = screen.getAllByRole('row')
  807. for (const [index, checkbox] of allCheckboxes.entries()) {
  808. if (checkbox.checked) {
  809. const linkForProjectToCopy = within(tableRows[index]).getByRole(
  810. 'link'
  811. )
  812. const projectNameToCopy = linkForProjectToCopy.textContent
  813. projectsToProcess.push(projectNameToCopy)
  814. }
  815. }
  816. expect(projectsToProcess.length > 0).to.be.true
  817. }
  818. function selectedMatchesDisplayed(expectedLength: number) {
  819. selectedProjectNames()
  820. // any action will work for check since they all use the same modal
  821. const archiveButton = within(actionsToolbar).getByLabelText('Archive')
  822. fireEvent.click(archiveButton)
  823. modal = screen.getAllByRole('dialog')[0]
  824. const listitems = within(modal).getAllByRole('listitem')
  825. expect(listitems.length).to.equal(projectsToProcess.length)
  826. expect(listitems.length).to.equal(expectedLength)
  827. for (const projectName of projectsToProcess) {
  828. within(modal).getByText(projectName)
  829. }
  830. }
  831. beforeEach(function () {
  832. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  833. // first one is the select all checkbox, just check 2 at first
  834. fireEvent.click(allCheckboxes[1])
  835. fireEvent.click(allCheckboxes[2])
  836. actionsToolbar = screen.getAllByRole('toolbar')[0]
  837. })
  838. it('opens the modal with the 2 originally selected projects', function () {
  839. selectedMatchesDisplayed(2)
  840. })
  841. it('shows correct list after closing modal, changing selecting, and reopening modal', async function () {
  842. selectedMatchesDisplayed(2)
  843. const modal = screen.getAllByRole('dialog', { hidden: false })[0]
  844. const cancelButton = within(modal).getByRole('button', {
  845. name: 'Cancel',
  846. })
  847. fireEvent.click(cancelButton)
  848. expect(screen.queryByRole('dialog', { hidden: false })).to.be.null
  849. await screen.findAllByRole('checkbox')
  850. fireEvent.click(allCheckboxes[3])
  851. selectedMatchesDisplayed(3)
  852. })
  853. it('maintains original list even after some have been processed', async function () {
  854. const totalProjectsToProcess = 2
  855. selectedMatchesDisplayed(totalProjectsToProcess)
  856. const button = screen.getByRole('button', { name: 'Confirm' })
  857. fireEvent.click(button)
  858. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  859. fetchMock.post('express:/project/:id/archive', {
  860. status: 200,
  861. })
  862. fetchMock.post(`/project/${project2Id}/archive`, {
  863. status: 500,
  864. })
  865. await screen.findByRole('alert') // ensure that error was thrown for the 2nd project
  866. const listitems = within(modal).getAllByRole('listitem')
  867. expect(listitems.length).to.equal(totalProjectsToProcess)
  868. })
  869. })
  870. })
  871. describe('search', function () {
  872. it('shows only projects based on the input', async function () {
  873. const input = screen.getAllByRole('textbox', {
  874. name: /search in all projects/i,
  875. })[0]
  876. const value = currentList[0].name
  877. fireEvent.change(input, { target: { value } })
  878. const results = screen.getAllByRole('row')
  879. expect(results.length).to.equal(2) // first is header
  880. })
  881. })
  882. describe('copying project', function () {
  883. it('correctly updates the view after copying a shared project', async function () {
  884. const filterButton = screen.getAllByText('Shared with you')[0]
  885. fireEvent.click(filterButton)
  886. const tableRows = screen.getAllByRole('row')
  887. const linkForProjectToCopy = within(tableRows[1]).getByRole('link')
  888. const projectNameToCopy = linkForProjectToCopy.textContent
  889. const copiedProjectName = `${projectNameToCopy} Copy`
  890. fetchMock.post(`express:/project/:id/clone`, {
  891. status: 200,
  892. body: {
  893. name: copiedProjectName,
  894. lastUpdated: new Date(),
  895. project_id: userId,
  896. owner_ref: userId,
  897. owner,
  898. id: '6328e14abec0df019fce0be5',
  899. lastUpdatedBy: owner,
  900. accessLevel: 'owner',
  901. source: 'owner',
  902. trashed: false,
  903. archived: false,
  904. },
  905. })
  906. const copyButton = within(tableRows[1]).getAllByLabelText('Copy')[0]
  907. fireEvent.click(copyButton)
  908. // confirm in modal
  909. const copyConfirmButton = document.querySelector(
  910. 'button[type="submit"]'
  911. ) as HTMLElement
  912. fireEvent.click(copyConfirmButton)
  913. await fetchMock.flush(true)
  914. expect(fetchMock.done()).to.be.true
  915. expect(sendMBSpy).to.have.been.calledTwice
  916. expect(sendMBSpy).to.have.been.calledWith('loads_v2_dash')
  917. expect(sendMBSpy).to.have.been.calledWith(
  918. 'project-list-page-interaction',
  919. {
  920. action: 'clone',
  921. page: '/',
  922. projectId: archiveableProject.id,
  923. isSmallDevice: true,
  924. }
  925. )
  926. expect(screen.queryByText(copiedProjectName)).to.be.null
  927. const yourProjectFilter = screen.getAllByText('Your Projects')[0]
  928. fireEvent.click(yourProjectFilter)
  929. screen.findByText(copiedProjectName)
  930. })
  931. })
  932. describe('notifications', function () {
  933. it('email confirmation alert is displayed', async function () {
  934. screen.getByText(
  935. 'Please confirm your email test@overleaf.com by clicking on the link in the confirmation email'
  936. )
  937. })
  938. })
  939. })
  940. })