project-list-root.test.tsx 40 KB

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