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. window.user_id = userId
  50. assignStub = sinon.stub()
  51. this.locationStub = sinon.stub(useLocationModule, 'useLocation').returns({
  52. assign: assignStub,
  53. reload: sinon.stub(),
  54. })
  55. })
  56. afterEach(function () {
  57. sendMBSpy.restore()
  58. window.user_id = undefined
  59. fetchMock.reset()
  60. this.locationStub.restore()
  61. })
  62. describe('welcome page', function () {
  63. beforeEach(async function () {
  64. renderWithProjectListContext(<ProjectListRoot />, {
  65. projects: [],
  66. })
  67. await fetchMock.flush(true)
  68. })
  69. it('the welcome page is displayed', async function () {
  70. screen.getByRole('heading', { name: 'Welcome to Overleaf!' })
  71. })
  72. it('the email confirmation alert is not displayed', async function () {
  73. expect(
  74. screen.queryByText(
  75. 'Please confirm your email test@overleaf.com by clicking on the link in the confirmation email'
  76. )
  77. ).to.be.null
  78. })
  79. })
  80. describe('project table', function () {
  81. beforeEach(async function () {
  82. const { unmount } = renderWithProjectListContext(<ProjectListRoot />, {
  83. projects: fullList,
  84. })
  85. this.unmount = unmount
  86. await fetchMock.flush(true)
  87. await screen.findByRole('table')
  88. })
  89. describe('checkboxes', function () {
  90. let allCheckboxes: Array<HTMLInputElement> = []
  91. let actionsToolbar: HTMLElement
  92. let project1Id: string | null, project2Id: string | null
  93. describe('all projects', function () {
  94. beforeEach(function () {
  95. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  96. // first one is the select all checkbox
  97. fireEvent.click(allCheckboxes[1])
  98. fireEvent.click(allCheckboxes[2])
  99. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  100. project2Id = allCheckboxes[2].getAttribute('data-project-id')
  101. actionsToolbar = screen.getAllByRole('toolbar')[0]
  102. })
  103. it('downloads all selected projects and then unselects them', async function () {
  104. const downloadButton =
  105. within(actionsToolbar).getByLabelText('Download')
  106. fireEvent.click(downloadButton)
  107. await waitFor(() => {
  108. expect(assignStub).to.have.been.called
  109. })
  110. sinon.assert.calledWithMatch(
  111. assignStub,
  112. `/project/download/zip?project_ids=${project1Id},${project2Id}`
  113. )
  114. const allCheckboxes =
  115. screen.getAllByRole<HTMLInputElement>('checkbox')
  116. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  117. expect(allCheckboxesChecked.length).to.equal(0)
  118. })
  119. it('opens archive modal for all selected projects and archives all', async function () {
  120. const archiveProjectMock = fetchMock.post(
  121. `express:/project/:projectId/archive`,
  122. {
  123. status: 200,
  124. },
  125. { delay: 0 }
  126. )
  127. const archiveButton = within(actionsToolbar).getByLabelText('Archive')
  128. fireEvent.click(archiveButton)
  129. const confirmBtn = screen.getByText('Confirm') as HTMLButtonElement
  130. fireEvent.click(confirmBtn)
  131. expect(confirmBtn.disabled).to.be.true
  132. await waitFor(
  133. () =>
  134. expect(
  135. archiveProjectMock.called(`/project/${project1Id}/archive`)
  136. ).to.be.true
  137. )
  138. await waitFor(
  139. () =>
  140. expect(
  141. archiveProjectMock.called(`/project/${project2Id}/archive`)
  142. ).to.be.true
  143. )
  144. })
  145. it('opens trash modal for all selected projects and trashes all', async function () {
  146. const trashProjectMock = fetchMock.post(
  147. `express:/project/:projectId/trash`,
  148. {
  149. status: 200,
  150. },
  151. { delay: 0 }
  152. )
  153. const archiveButton = within(actionsToolbar).getByLabelText('Trash')
  154. fireEvent.click(archiveButton)
  155. const confirmBtn = screen.getByText('Confirm') as HTMLButtonElement
  156. fireEvent.click(confirmBtn)
  157. expect(confirmBtn.disabled).to.be.true
  158. await waitFor(
  159. () =>
  160. expect(trashProjectMock.called(`/project/${project1Id}/trash`)).to
  161. .be.true
  162. )
  163. await waitFor(
  164. () =>
  165. expect(trashProjectMock.called(`/project/${project2Id}/trash`)).to
  166. .be.true
  167. )
  168. })
  169. it('only checks the projects that are viewable when there is a load more button', async function () {
  170. // first one is the select all checkbox
  171. fireEvent.click(allCheckboxes[0])
  172. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  173. let checked = allCheckboxes.filter(c => c.checked)
  174. expect(checked.length).to.equal(21) // max projects viewable by default is 20, and plus one for check all
  175. const loadMoreButton = screen.getByLabelText('Show 17 more projects')
  176. fireEvent.click(loadMoreButton)
  177. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  178. expect(allCheckboxes.length).to.equal(currentList.length + 1)
  179. checked = allCheckboxes.filter(c => c.checked)
  180. expect(checked.length).to.equal(20) // remains same even after showing more
  181. })
  182. it('maintains viewable and selected projects after loading more and then selecting all', async function () {
  183. const loadMoreButton = screen.getByLabelText('Show 17 more projects')
  184. fireEvent.click(loadMoreButton)
  185. // verify button gone
  186. screen.getByText(
  187. `Showing ${currentList.length} out of ${currentList.length} projects.`
  188. )
  189. // first one is the select all checkbox
  190. fireEvent.click(allCheckboxes[0])
  191. // verify button still gone
  192. screen.getByText(
  193. `Showing ${currentList.length} out of ${currentList.length} projects.`
  194. )
  195. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  196. expect(allCheckboxes.length).to.equal(currentList.length + 1)
  197. })
  198. })
  199. describe('archived projects', function () {
  200. beforeEach(function () {
  201. const filterButton = screen.getAllByText('Archived Projects')[0]
  202. fireEvent.click(filterButton)
  203. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  204. expect(allCheckboxes.length === 2).to.be.true
  205. // first one is the select all checkbox
  206. fireEvent.click(allCheckboxes[1])
  207. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  208. actionsToolbar = screen.getAllByRole('toolbar')[0]
  209. })
  210. it('does not show the archive button in toolbar when archive view selected', function () {
  211. expect(screen.queryByLabelText('Archive')).to.be.null
  212. })
  213. it('restores all projects when selected', async function () {
  214. fetchMock.delete(`express:/project/:id/archive`, {
  215. status: 200,
  216. })
  217. const unarchiveButton =
  218. within(actionsToolbar).getByText<HTMLButtonElement>('Restore')
  219. fireEvent.click(unarchiveButton)
  220. await fetchMock.flush(true)
  221. expect(fetchMock.done()).to.be.true
  222. screen.getByText('No projects')
  223. })
  224. it('only unarchive the selected projects', async function () {
  225. // beforeEach selected all, so uncheck the 1st project
  226. fireEvent.click(allCheckboxes[1])
  227. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  228. expect(allCheckboxesChecked.length).to.equal(
  229. archivedProjects.length - 1
  230. )
  231. await fetchMock.flush(true)
  232. expect(fetchMock.done()).to.be.true
  233. expect(screen.queryByText('No projects')).to.be.null
  234. })
  235. })
  236. describe('trashed projects', function () {
  237. beforeEach(function () {
  238. const filterButton = screen.getAllByText('Trashed Projects')[0]
  239. fireEvent.click(filterButton)
  240. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  241. // + 1 because of select all
  242. expect(allCheckboxes.length).to.equal(trashedList.length + 1)
  243. // first one is the select all checkbox
  244. fireEvent.click(allCheckboxes[0])
  245. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  246. // + 1 because of select all
  247. expect(allCheckboxesChecked.length).to.equal(trashedList.length + 1)
  248. actionsToolbar = screen.getAllByRole('toolbar')[0]
  249. })
  250. it('shows the download, archive, and restore buttons in top toolbar', function () {
  251. expect(screen.queryByLabelText('Trash')).to.be.null
  252. within(actionsToolbar).queryByLabelText('Download')
  253. within(actionsToolbar).queryByLabelText('Archive')
  254. within(actionsToolbar).getByText('Restore') // no icon for this button
  255. })
  256. it('clears selected projects when filter changed', function () {
  257. const filterButton = screen.getAllByText('All Projects')[0]
  258. fireEvent.click(filterButton)
  259. const allCheckboxes =
  260. screen.getAllByRole<HTMLInputElement>('checkbox')
  261. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  262. expect(allCheckboxesChecked.length).to.equal(0)
  263. })
  264. it('untrashes all the projects', async function () {
  265. fetchMock.delete(`express:/project/:id/trash`, {
  266. status: 200,
  267. })
  268. const untrashButton =
  269. within(actionsToolbar).getByText<HTMLButtonElement>('Restore')
  270. fireEvent.click(untrashButton)
  271. await fetchMock.flush(true)
  272. expect(fetchMock.done()).to.be.true
  273. screen.getByText('No projects')
  274. })
  275. it('only untrashes the selected projects', async function () {
  276. // beforeEach selected all, so uncheck the 1st project
  277. fireEvent.click(allCheckboxes[1])
  278. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  279. expect(allCheckboxesChecked.length).to.equal(trashedList.length - 1)
  280. await fetchMock.flush(true)
  281. expect(fetchMock.done()).to.be.true
  282. expect(screen.queryByText('No projects')).to.be.null
  283. })
  284. it('removes project from view when archiving', async function () {
  285. fetchMock.post(
  286. `express:/project/:id/archive`,
  287. {
  288. status: 200,
  289. },
  290. { repeat: trashedList.length }
  291. )
  292. const archiveButton =
  293. within(actionsToolbar).getByLabelText<HTMLButtonElement>('Archive')
  294. fireEvent.click(archiveButton)
  295. const confirmButton = screen.getByText<HTMLButtonElement>('Confirm')
  296. fireEvent.click(confirmButton)
  297. expect(confirmButton.disabled).to.be.true
  298. await fetchMock.flush(true)
  299. expect(fetchMock.done()).to.be.true
  300. const calls = fetchMock.calls().map(([url]) => url)
  301. trashedList.forEach(project => {
  302. expect(calls).to.contain(`/project/${project.id}/archive`)
  303. })
  304. })
  305. it('removes only selected projects from view when leaving', async function () {
  306. // rerender content with different projects
  307. this.unmount()
  308. fetchMock.restore()
  309. renderWithProjectListContext(<ProjectListRoot />, {
  310. projects: leavableList,
  311. })
  312. await fetchMock.flush(true)
  313. await screen.findByRole('table')
  314. expect(leavableList.length).to.be.greaterThan(0)
  315. fetchMock.post(
  316. `express:/project/:id/leave`,
  317. {
  318. status: 200,
  319. },
  320. { repeat: leavableList.length }
  321. )
  322. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  323. // + 1 because of select all
  324. expect(allCheckboxes.length).to.equal(leavableList.length + 1)
  325. // first one is the select all checkbox
  326. fireEvent.click(allCheckboxes[0])
  327. actionsToolbar = screen.getAllByRole('toolbar')[0]
  328. const toolbar = within(actionsToolbar)
  329. expect(toolbar.queryByRole('button', { name: /delete/i })).to.be.null
  330. expect(
  331. toolbar.queryByRole('button', {
  332. name: /delete \/ leave/i,
  333. })
  334. ).to.be.null
  335. const leaveButton = toolbar.getByRole('button', {
  336. name: /leave/i,
  337. })
  338. fireEvent.click(leaveButton)
  339. const confirmButton = screen.getByText<HTMLButtonElement>('Confirm')
  340. fireEvent.click(confirmButton)
  341. expect(confirmButton.disabled).to.be.true
  342. await fetchMock.flush(true)
  343. expect(fetchMock.done()).to.be.true
  344. const calls = fetchMock.calls().map(([url]) => url)
  345. leavableList.forEach(project => {
  346. expect(calls).to.contain(`/project/${project.id}/leave`)
  347. })
  348. })
  349. it('removes only selected projects from view when deleting', async function () {
  350. // rerender content with different projects
  351. this.unmount()
  352. fetchMock.restore()
  353. renderWithProjectListContext(<ProjectListRoot />, {
  354. projects: deletableList,
  355. })
  356. await fetchMock.flush(true)
  357. await screen.findByRole('table')
  358. expect(deletableList.length).to.be.greaterThan(0)
  359. fetchMock.delete(
  360. `express:/project/:id`,
  361. {
  362. status: 200,
  363. },
  364. { repeat: deletableList.length }
  365. )
  366. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  367. // + 1 because of select all
  368. expect(allCheckboxes.length).to.equal(deletableList.length + 1)
  369. // first one is the select all checkbox
  370. fireEvent.click(allCheckboxes[0])
  371. actionsToolbar = screen.getAllByRole('toolbar')[0]
  372. const toolbar = within(actionsToolbar)
  373. expect(toolbar.queryByRole('button', { name: /leave/i })).to.be.null
  374. expect(
  375. toolbar.queryByRole('button', {
  376. name: /delete \/ leave/i,
  377. })
  378. ).to.be.null
  379. const deleteButton = toolbar.getByRole('button', {
  380. name: /delete/i,
  381. })
  382. fireEvent.click(deleteButton)
  383. const confirmButton = screen.getByText<HTMLButtonElement>('Confirm')
  384. fireEvent.click(confirmButton)
  385. expect(confirmButton.disabled).to.be.true
  386. await fetchMock.flush(true)
  387. expect(fetchMock.done()).to.be.true
  388. const calls = fetchMock.calls().map(([url]) => url)
  389. deletableList.forEach(project => {
  390. expect(calls).to.contain(`/project/${project.id}`)
  391. })
  392. })
  393. it('removes only selected projects from view when deleting and leaving', async function () {
  394. // rerender content with different projects
  395. this.unmount()
  396. fetchMock.restore()
  397. const deletableAndLeavableList = [...deletableList, ...leavableList]
  398. renderWithProjectListContext(<ProjectListRoot />, {
  399. projects: deletableAndLeavableList,
  400. })
  401. await fetchMock.flush(true)
  402. await screen.findByRole('table')
  403. expect(deletableList.length).to.be.greaterThan(0)
  404. expect(leavableList.length).to.be.greaterThan(0)
  405. fetchMock
  406. .delete(
  407. `express:/project/:id`,
  408. {
  409. status: 200,
  410. },
  411. { repeat: deletableList.length }
  412. )
  413. .post(
  414. `express:/project/:id/leave`,
  415. {
  416. status: 200,
  417. },
  418. { repeat: leavableList.length }
  419. )
  420. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  421. // + 1 because of select all
  422. expect(allCheckboxes.length).to.equal(
  423. deletableAndLeavableList.length + 1
  424. )
  425. // first one is the select all checkbox
  426. fireEvent.click(allCheckboxes[0])
  427. actionsToolbar = screen.getAllByRole('toolbar')[0]
  428. const toolbar = within(actionsToolbar)
  429. expect(toolbar.queryByRole('button', { name: 'Leave' })).to.be.null
  430. expect(toolbar.queryByRole('button', { name: 'Delete' })).to.be.null
  431. const deleteLeaveButton = toolbar.getByRole('button', {
  432. name: /delete \/ leave/i,
  433. })
  434. fireEvent.click(deleteLeaveButton)
  435. const confirmButton = screen.getByText<HTMLButtonElement>('Confirm')
  436. fireEvent.click(confirmButton)
  437. expect(confirmButton.disabled).to.be.true
  438. await fetchMock.flush(true)
  439. expect(fetchMock.done()).to.be.true
  440. const calls = fetchMock.calls().map(([url]) => url)
  441. deletableAndLeavableList.forEach(project => {
  442. expect(calls).to.contain.oneOf([
  443. `/project/${project.id}`,
  444. `/project/${project.id}/leave`,
  445. ])
  446. })
  447. })
  448. })
  449. describe('tags', function () {
  450. it('does not show archived or trashed project', async function () {
  451. this.unmount()
  452. fetchMock.restore()
  453. window.metaAttributesCache.set('ol-tags', [
  454. {
  455. _id: this.tagId,
  456. name: this.tagName,
  457. project_ids: [
  458. projectsData[0].id,
  459. projectsData[1].id,
  460. ...archivedList.map(p => p.id),
  461. ...trashedList.map(p => p.id),
  462. ],
  463. },
  464. ])
  465. const trashProjectMock = fetchMock.post(
  466. `express:/project/:projectId/trash`,
  467. { status: 200 }
  468. )
  469. renderWithProjectListContext(<ProjectListRoot />, {
  470. projects: fullList,
  471. })
  472. await screen.findByRole('table')
  473. let visibleProjectsCount = 2
  474. const [tagBtn] = screen.getAllByRole('button', {
  475. name: `${this.tagName} (${visibleProjectsCount})`,
  476. })
  477. fireEvent.click(tagBtn)
  478. const nonArchivedAndTrashedProjects = [
  479. projectsData[0],
  480. projectsData[1],
  481. ]
  482. nonArchivedAndTrashedProjects.forEach(p => {
  483. screen.getByText(p.name)
  484. })
  485. const archivedAndTrashedProjects = [...archivedList, ...trashedList]
  486. archivedAndTrashedProjects.forEach(p => {
  487. expect(screen.queryByText(p.name)).to.be.null
  488. })
  489. const trashBtns = screen.getAllByRole('button', { name: 'Trash' })
  490. for (const [index, trashBtn] of trashBtns.entries()) {
  491. fireEvent.click(trashBtn)
  492. fireEvent.click(screen.getByText<HTMLButtonElement>('Confirm'))
  493. await waitFor(() => {
  494. expect(
  495. trashProjectMock.called(
  496. `/project/${projectsData[index].id}/trash`
  497. )
  498. ).to.be.true
  499. })
  500. expect(
  501. screen.queryAllByText(projectsData[index].name)
  502. ).to.have.length(0)
  503. screen.getAllByRole('button', {
  504. name: `${this.tagName} (${--visibleProjectsCount})`,
  505. })
  506. }
  507. })
  508. })
  509. describe('tags dropdown', function () {
  510. beforeEach(async function () {
  511. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  512. // first one is the select all checkbox
  513. fireEvent.click(allCheckboxes[1])
  514. fireEvent.click(allCheckboxes[2])
  515. actionsToolbar = screen.getAllByRole('toolbar')[0]
  516. this.newTagName = 'Some tag name'
  517. this.newTagId = 'abc123def456'
  518. })
  519. it('opens the tags dropdown and creates a new tag', async function () {
  520. fetchMock.post(`express:/tag`, {
  521. status: 200,
  522. body: {
  523. _id: this.newTagId,
  524. name: this.newTagName,
  525. project_ids: [],
  526. },
  527. })
  528. fetchMock.post(`express:/tag/:id/projects`, {
  529. status: 204,
  530. })
  531. await waitFor(() => {
  532. const tagsDropdown = within(actionsToolbar).getByLabelText('Tags')
  533. fireEvent.click(tagsDropdown)
  534. })
  535. screen.getByText('Add to tag')
  536. const newTagButton = screen.getByText('Create new tag')
  537. fireEvent.click(newTagButton)
  538. const modal = screen.getAllByRole('dialog')[0]
  539. const input = within(modal).getByRole<HTMLInputElement>('textbox')
  540. fireEvent.change(input, {
  541. target: { value: this.newTagName },
  542. })
  543. const createButton = within(modal).getByRole('button', {
  544. name: 'Create',
  545. })
  546. fireEvent.click(createButton)
  547. await fetchMock.flush(true)
  548. expect(fetchMock.called('/tag', { name: this.newTagName })).to.be.true
  549. expect(
  550. fetchMock.called(`/tag/${this.newTagId}/projects`, {
  551. body: {
  552. projectIds: [projectsData[0].id, projectsData[1].id],
  553. },
  554. })
  555. ).to.be.true
  556. screen.getByRole('button', { name: `${this.newTagName} (2)` })
  557. })
  558. it('opens the tags dropdown and remove a tag from selected projects', async function () {
  559. const deleteProjectsFromTagMock = fetchMock.delete(
  560. `express:/tag/:id/projects`,
  561. {
  562. status: 204,
  563. }
  564. )
  565. screen.getByRole('button', { name: `${this.tagName} (2)` })
  566. const tagsDropdown = within(actionsToolbar).getByLabelText('Tags')
  567. fireEvent.click(tagsDropdown)
  568. within(actionsToolbar).getByText('Add to tag')
  569. const tagButton = within(actionsToolbar).getByLabelText(
  570. `Add or remove project from tag ${this.tagName}`
  571. )
  572. fireEvent.click(tagButton)
  573. await fetchMock.flush(true)
  574. expect(
  575. deleteProjectsFromTagMock.called(`/tag/${this.tagId}/projects`, {
  576. body: {
  577. projectIds: [projectsData[0].id, projectsData[1].id],
  578. },
  579. })
  580. ).to.be.true
  581. screen.getByRole('button', { name: `${this.tagName} (0)` })
  582. })
  583. it('select another project, opens the tags dropdown and add a tag only to the untagged project', async function () {
  584. const addProjectsToTagMock = fetchMock.post(
  585. `express:/tag/:id/projects`,
  586. {
  587. status: 204,
  588. }
  589. )
  590. fireEvent.click(allCheckboxes[3])
  591. screen.getByRole('button', { name: `${this.tagName} (2)` })
  592. const tagsDropdown = within(actionsToolbar).getByLabelText('Tags')
  593. fireEvent.click(tagsDropdown)
  594. within(actionsToolbar).getByText('Add to tag')
  595. const tagButton = within(actionsToolbar).getByLabelText(
  596. `Add or remove project from tag ${this.tagName}`
  597. )
  598. fireEvent.click(tagButton)
  599. await fetchMock.flush(true)
  600. expect(
  601. addProjectsToTagMock.called(`/tag/${this.tagId}/projects`, {
  602. body: {
  603. projectIds: [projectsData[2].id],
  604. },
  605. })
  606. ).to.be.true
  607. screen.getByRole('button', { name: `${this.tagName} (3)` })
  608. })
  609. })
  610. describe('project tools', function () {
  611. it('renders download, archive, trash buttons followed by tags and "more" dropdowns when selecting an archived or trashed filter before selecting tag filter', function () {
  612. const assertToolbarButtonsExists = () => {
  613. within(actionsToolbar).getByLabelText(/download/i)
  614. within(actionsToolbar).getByLabelText(/archive/i)
  615. within(actionsToolbar).getByLabelText(/trash/i)
  616. within(actionsToolbar).getByLabelText(/tags/i)
  617. within(actionsToolbar).getByText(/more/i)
  618. }
  619. // Select archived projects
  620. const [archivedProjectsButton] =
  621. screen.getAllByText(/archived projects/i)
  622. fireEvent.click(archivedProjectsButton)
  623. const [tag] = screen.getAllByText(this.tagName)
  624. fireEvent.click(tag)
  625. allCheckboxes = screen.getAllByRole('checkbox')
  626. fireEvent.click(allCheckboxes[1])
  627. actionsToolbar = screen.getAllByRole('toolbar')[0]
  628. assertToolbarButtonsExists()
  629. // select trashed projects
  630. const [trashedProjectsButton] =
  631. screen.getAllByText(/trashed projects/i)
  632. fireEvent.click(trashedProjectsButton)
  633. fireEvent.click(tag)
  634. allCheckboxes = screen.getAllByRole('checkbox')
  635. fireEvent.click(allCheckboxes[1])
  636. actionsToolbar = screen.getAllByRole('toolbar')[0]
  637. assertToolbarButtonsExists()
  638. })
  639. describe('"More" dropdown', function () {
  640. beforeEach(async function () {
  641. const filterButton = screen.getAllByText('All Projects')[0]
  642. fireEvent.click(filterButton)
  643. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  644. })
  645. it('does not show the dropdown when more than 1 project is selected', async function () {
  646. fireEvent.click(allCheckboxes[2]) // select a project
  647. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  648. await waitFor(() => {
  649. within(actionsToolbar).getByText<HTMLElement>('More')
  650. })
  651. fireEvent.click(allCheckboxes[0]) // select all
  652. expect(within(actionsToolbar).queryByText<HTMLElement>('More')).to
  653. .be.null
  654. })
  655. it('validates the project name', async function () {
  656. fireEvent.click(allCheckboxes[1]) // select a project owned by the current user
  657. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  658. const moreDropdown = await within(
  659. actionsToolbar
  660. ).findByText<HTMLElement>('More')
  661. fireEvent.click(moreDropdown)
  662. const editButton =
  663. screen.getAllByText<HTMLButtonElement>('Rename')[0]
  664. fireEvent.click(editButton)
  665. const modals = await screen.findAllByRole('dialog')
  666. const modal = modals[0]
  667. expect(sendMBSpy).to.have.been.calledTwice
  668. expect(sendMBSpy).to.have.been.calledWith('loads_v2_dash')
  669. expect(sendMBSpy).to.have.been.calledWith(
  670. 'project-list-page-interaction',
  671. {
  672. action: 'rename',
  673. page: '/',
  674. projectId: copyableProject.id,
  675. isSmallDevice: true,
  676. }
  677. )
  678. // same name
  679. let confirmButton =
  680. within(modal).getByText<HTMLButtonElement>('Rename')
  681. expect(confirmButton.disabled).to.be.true
  682. // no name
  683. const input = screen.getByLabelText('New Name') as HTMLButtonElement
  684. fireEvent.change(input, {
  685. target: { value: '' },
  686. })
  687. confirmButton = within(modal).getByText<HTMLButtonElement>('Rename')
  688. expect(confirmButton.disabled).to.be.true
  689. })
  690. it('opens the rename modal, and can rename the project, and view updated', async function () {
  691. fireEvent.click(allCheckboxes[1]) // select a project owned by the current user
  692. const actionsToolbar = screen.getAllByRole('toolbar')[0]
  693. const renameProjectMock = fetchMock.post(
  694. `express:/project/:id/rename`,
  695. {
  696. status: 200,
  697. }
  698. )
  699. const moreDropdown = await within(
  700. actionsToolbar
  701. ).findByText<HTMLElement>('More')
  702. fireEvent.click(moreDropdown)
  703. const renameButton =
  704. within(actionsToolbar).getByText<HTMLButtonElement>('Rename')
  705. fireEvent.click(renameButton)
  706. const modals = await screen.findAllByRole('dialog')
  707. const modal = modals[0]
  708. // a valid name
  709. const newProjectName = 'A new project name'
  710. const input = (await within(modal).findByLabelText(
  711. 'New Name'
  712. )) as HTMLButtonElement
  713. const oldName = input.value
  714. fireEvent.change(input, {
  715. target: { value: newProjectName },
  716. })
  717. const confirmButton =
  718. within(modal).getByText<HTMLButtonElement>('Rename')
  719. expect(confirmButton.disabled).to.be.false
  720. fireEvent.click(confirmButton)
  721. await fetchMock.flush(true)
  722. expect(
  723. renameProjectMock.called(`/project/${projectsData[0].id}/rename`)
  724. ).to.be.true
  725. const table = await screen.findByRole('table')
  726. within(table).getByText(newProjectName)
  727. expect(within(table).queryByText(oldName)).to.be.null
  728. const allCheckboxesInTable = await within(
  729. table
  730. ).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. })