project-list-root.test.tsx 38 KB

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