project-list-root.test.tsx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. const { fullList, currentList, trashedList } = makeLongProjectList(40)
  15. const userId = owner.id
  16. describe('<ProjectListRoot />', function () {
  17. const originalLocation = window.location
  18. const locationStub = sinon.stub()
  19. let sendSpy: sinon.SinonSpy
  20. beforeEach(async function () {
  21. global.localStorage.clear()
  22. sendSpy = sinon.spy(eventTracking, 'send')
  23. window.metaAttributesCache = new Map()
  24. this.tagId = '999fff999fff'
  25. this.tagName = 'First tag name'
  26. window.metaAttributesCache.set('ol-tags', [
  27. {
  28. _id: this.tagId,
  29. name: this.tagName,
  30. project_ids: [projectsData[0].id, projectsData[1].id],
  31. },
  32. ])
  33. window.metaAttributesCache.set('ol-ExposedSettings', {
  34. templateLinks: [],
  35. })
  36. window.metaAttributesCache.set('ol-userEmails', [
  37. { email: 'test@overleaf.com', default: true },
  38. ])
  39. window.user_id = userId
  40. Object.defineProperty(window, 'location', {
  41. value: { assign: locationStub },
  42. })
  43. })
  44. afterEach(function () {
  45. sendSpy.restore()
  46. window.user_id = undefined
  47. fetchMock.reset()
  48. Object.defineProperty(window, 'location', {
  49. value: originalLocation,
  50. })
  51. })
  52. describe('welcome page', function () {
  53. beforeEach(async function () {
  54. renderWithProjectListContext(<ProjectListRoot />, {
  55. projects: [],
  56. })
  57. await fetchMock.flush(true)
  58. })
  59. it('the welcome page is displayed', async function () {
  60. screen.getByRole('heading', { name: 'Welcome to Overleaf!' })
  61. })
  62. it('the email confirmation alert is not displayed', async function () {
  63. expect(
  64. screen.queryByText(
  65. 'Please confirm your email test@overleaf.com by clicking on the link in the confirmation email'
  66. )
  67. ).to.be.null
  68. })
  69. })
  70. describe('project table', function () {
  71. beforeEach(async function () {
  72. renderWithProjectListContext(<ProjectListRoot />, {
  73. projects: fullList,
  74. })
  75. await fetchMock.flush(true)
  76. await screen.findByRole('table')
  77. })
  78. describe('checkboxes', function () {
  79. let allCheckboxes: Array<HTMLInputElement> = []
  80. let actionsToolbar: HTMLElement
  81. let project1Id: string | null, project2Id: string | null
  82. describe('all projects', function () {
  83. beforeEach(function () {
  84. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  85. // first one is the select all checkbox
  86. fireEvent.click(allCheckboxes[1])
  87. fireEvent.click(allCheckboxes[2])
  88. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  89. project2Id = allCheckboxes[2].getAttribute('data-project-id')
  90. actionsToolbar = screen.getAllByRole('toolbar')[0]
  91. })
  92. it('downloads all selected projects and then unselects them', async function () {
  93. const downloadButton =
  94. within(actionsToolbar).getByLabelText('Download')
  95. fireEvent.click(downloadButton)
  96. await waitFor(() => {
  97. expect(locationStub).to.have.been.called
  98. })
  99. sinon.assert.calledWithMatch(
  100. locationStub,
  101. `/project/download/zip?project_ids=${project1Id},${project2Id}`
  102. )
  103. const allCheckboxes =
  104. screen.getAllByRole<HTMLInputElement>('checkbox')
  105. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  106. expect(allCheckboxesChecked.length).to.equal(0)
  107. })
  108. it('opens archive modal for all selected projects and archives all', async function () {
  109. const archiveProjectMock = fetchMock.post(
  110. `express:/project/:projectId/archive`,
  111. {
  112. status: 200,
  113. },
  114. { delay: 0 }
  115. )
  116. const archiveButton = within(actionsToolbar).getByLabelText('Archive')
  117. fireEvent.click(archiveButton)
  118. const confirmBtn = screen.getByText('Confirm') as HTMLButtonElement
  119. fireEvent.click(confirmBtn)
  120. expect(confirmBtn.disabled).to.be.true
  121. await waitFor(
  122. () =>
  123. expect(
  124. archiveProjectMock.called(`/project/${project1Id}/archive`)
  125. ).to.be.true
  126. )
  127. await waitFor(
  128. () =>
  129. expect(
  130. archiveProjectMock.called(`/project/${project2Id}/archive`)
  131. ).to.be.true
  132. )
  133. })
  134. it('opens trash modal for all selected projects and trashes all', async function () {
  135. const trashProjectMock = fetchMock.post(
  136. `express:/project/:projectId/trash`,
  137. {
  138. status: 200,
  139. },
  140. { delay: 0 }
  141. )
  142. const archiveButton = within(actionsToolbar).getByLabelText('Trash')
  143. fireEvent.click(archiveButton)
  144. const confirmBtn = screen.getByText('Confirm') as HTMLButtonElement
  145. fireEvent.click(confirmBtn)
  146. expect(confirmBtn.disabled).to.be.true
  147. await waitFor(
  148. () =>
  149. expect(trashProjectMock.called(`/project/${project1Id}/trash`)).to
  150. .be.true
  151. )
  152. await waitFor(
  153. () =>
  154. expect(trashProjectMock.called(`/project/${project2Id}/trash`)).to
  155. .be.true
  156. )
  157. })
  158. it('only checks the projects that are viewable when there is a load more button', async function () {
  159. // first one is the select all checkbox
  160. fireEvent.click(allCheckboxes[0])
  161. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  162. let checked = allCheckboxes.filter(c => c.checked)
  163. expect(checked.length).to.equal(21) // max projects viewable by default is 20, and plus one for check all
  164. const loadMoreButton = screen.getByLabelText('Show 17 more projects')
  165. fireEvent.click(loadMoreButton)
  166. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  167. expect(allCheckboxes.length).to.equal(currentList.length + 1)
  168. checked = allCheckboxes.filter(c => c.checked)
  169. expect(checked.length).to.equal(20) // remains same even after showing more
  170. })
  171. it('maintains viewable and selected projects after loading more and then selecting all', async function () {
  172. const loadMoreButton = screen.getByLabelText('Show 17 more projects')
  173. fireEvent.click(loadMoreButton)
  174. // verify button gone
  175. screen.getByText(
  176. `Showing ${currentList.length} out of ${currentList.length} projects.`
  177. )
  178. // first one is the select all checkbox
  179. fireEvent.click(allCheckboxes[0])
  180. // verify button still gone
  181. screen.getByText(
  182. `Showing ${currentList.length} out of ${currentList.length} projects.`
  183. )
  184. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  185. expect(allCheckboxes.length).to.equal(currentList.length + 1)
  186. })
  187. })
  188. describe('archived projects', function () {
  189. beforeEach(function () {
  190. const filterButton = screen.getAllByText('Archived Projects')[0]
  191. fireEvent.click(filterButton)
  192. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  193. expect(allCheckboxes.length === 2).to.be.true
  194. // first one is the select all checkbox
  195. fireEvent.click(allCheckboxes[1])
  196. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  197. actionsToolbar = screen.getAllByRole('toolbar')[0]
  198. })
  199. it('does not show the archive button in toolbar when archive view selected', function () {
  200. expect(screen.queryByLabelText('Archive')).to.be.null
  201. })
  202. it('restores all projects when selected', async function () {
  203. fetchMock.delete(`express:/project/:id/archive`, {
  204. status: 200,
  205. })
  206. const unarchiveButton =
  207. within(actionsToolbar).getByText<HTMLInputElement>('Restore')
  208. fireEvent.click(unarchiveButton)
  209. await fetchMock.flush(true)
  210. expect(fetchMock.done()).to.be.true
  211. screen.getByText('No projects')
  212. })
  213. it('only unarchive the selected projects', async function () {
  214. // beforeEach selected all, so uncheck the 1st project
  215. fireEvent.click(allCheckboxes[1])
  216. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  217. expect(allCheckboxesChecked.length).to.equal(
  218. archivedProjects.length - 1
  219. )
  220. await fetchMock.flush(true)
  221. expect(fetchMock.done()).to.be.true
  222. expect(screen.queryByText('No projects')).to.be.null
  223. })
  224. })
  225. describe('trashed projects', function () {
  226. beforeEach(function () {
  227. const filterButton = screen.getAllByText('Trashed Projects')[0]
  228. fireEvent.click(filterButton)
  229. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  230. // + 1 because of select all
  231. expect(allCheckboxes.length).to.equal(trashedList.length + 1)
  232. // first one is the select all checkbox
  233. fireEvent.click(allCheckboxes[0])
  234. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  235. // + 1 because of select all
  236. expect(allCheckboxesChecked.length).to.equal(trashedList.length + 1)
  237. actionsToolbar = screen.getAllByRole('toolbar')[0]
  238. })
  239. it('only shows the download, archive, and restore buttons in top toolbar', function () {
  240. expect(screen.queryByLabelText('Trash')).to.be.null
  241. within(actionsToolbar).queryByLabelText('Download')
  242. within(actionsToolbar).queryByLabelText('Archive')
  243. within(actionsToolbar).getByText('Restore') // no icon for this button
  244. })
  245. it('clears selected projects when filter changed', function () {
  246. const filterButton = screen.getAllByText('All Projects')[0]
  247. fireEvent.click(filterButton)
  248. const allCheckboxes =
  249. screen.getAllByRole<HTMLInputElement>('checkbox')
  250. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  251. expect(allCheckboxesChecked.length).to.equal(0)
  252. })
  253. it('untrashes all the projects', async function () {
  254. fetchMock.delete(`express:/project/:id/trash`, {
  255. status: 200,
  256. })
  257. const untrashButton =
  258. within(actionsToolbar).getByText<HTMLInputElement>('Restore')
  259. fireEvent.click(untrashButton)
  260. await fetchMock.flush(true)
  261. expect(fetchMock.done()).to.be.true
  262. screen.getByText('No projects')
  263. })
  264. it('only untrashes the selected projects', async function () {
  265. // beforeEach selected all, so uncheck the 1st project
  266. fireEvent.click(allCheckboxes[1])
  267. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  268. expect(allCheckboxesChecked.length).to.equal(trashedList.length - 1)
  269. await fetchMock.flush(true)
  270. expect(fetchMock.done()).to.be.true
  271. expect(screen.queryByText('No projects')).to.be.null
  272. })
  273. it('removes project from view when archiving', async function () {
  274. fetchMock.post(`express:/project/:id/archive`, {
  275. status: 200,
  276. })
  277. const untrashButton =
  278. within(actionsToolbar).getByLabelText<HTMLInputElement>('Archive')
  279. fireEvent.click(untrashButton)
  280. const confirmButton = screen.getByText<HTMLInputElement>('Confirm')
  281. fireEvent.click(confirmButton)
  282. expect(confirmButton.disabled).to.be.true
  283. await fetchMock.flush(true)
  284. expect(fetchMock.done()).to.be.true
  285. screen.getByText('No projects')
  286. })
  287. })
  288. describe('tags dropdown', function () {
  289. beforeEach(async function () {
  290. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  291. // first one is the select all checkbox
  292. fireEvent.click(allCheckboxes[1])
  293. fireEvent.click(allCheckboxes[2])
  294. actionsToolbar = screen.getAllByRole('toolbar')[0]
  295. this.newTagName = 'Some tag name'
  296. this.newTagId = 'abc123def456'
  297. })
  298. it('opens the tags dropdown and creates a new tag', async function () {
  299. fetchMock.post(`express:/tag`, {
  300. status: 200,
  301. body: {
  302. _id: this.newTagId,
  303. name: this.newTagName,
  304. project_ids: [],
  305. },
  306. })
  307. fetchMock.post(`express:/tag/:id/projects`, {
  308. status: 204,
  309. })
  310. await waitFor(() => {
  311. const tagsDropdown = within(actionsToolbar).getByLabelText('Tags')
  312. fireEvent.click(tagsDropdown)
  313. })
  314. screen.getByText('Add to folder')
  315. const newTagButton = screen.getByText('Create New Folder')
  316. fireEvent.click(newTagButton)
  317. const modal = screen.getAllByRole('dialog')[0]
  318. const input = within(modal).getByRole<HTMLInputElement>('textbox')
  319. fireEvent.change(input, {
  320. target: { value: this.newTagName },
  321. })
  322. const createButton = within(modal).getByRole('button', {
  323. name: 'Create',
  324. })
  325. fireEvent.click(createButton)
  326. await fetchMock.flush(true)
  327. expect(fetchMock.called('/tag', { name: this.newTagName })).to.be.true
  328. expect(
  329. fetchMock.called(`/tag/${this.newTagId}/projects`, {
  330. body: {
  331. projectIds: [projectsData[0].id, projectsData[1].id],
  332. },
  333. })
  334. ).to.be.true
  335. screen.getByRole('button', { name: `${this.newTagName} (2)` })
  336. })
  337. it('opens the tags dropdown and remove a tag from selected projects', async function () {
  338. const deleteProjectsFromTagMock = fetchMock.delete(
  339. `express:/tag/:id/projects`,
  340. {
  341. status: 204,
  342. }
  343. )
  344. screen.getByRole('button', { name: `${this.tagName} (2)` })
  345. const tagsDropdown = within(actionsToolbar).getByLabelText('Tags')
  346. fireEvent.click(tagsDropdown)
  347. within(actionsToolbar).getByText('Add to folder')
  348. const tagButton = within(actionsToolbar).getByLabelText(
  349. `Add or remove project from tag ${this.tagName}`
  350. )
  351. fireEvent.click(tagButton)
  352. await fetchMock.flush(true)
  353. expect(
  354. deleteProjectsFromTagMock.called(`/tag/${this.tagId}/projects`, {
  355. body: {
  356. projectIds: [projectsData[0].id, projectsData[1].id],
  357. },
  358. })
  359. ).to.be.true
  360. screen.getByRole('button', { name: `${this.tagName} (0)` })
  361. })
  362. it('select another project, opens the tags dropdown and add a tag only to the untagged project', async function () {
  363. const addProjectsToTagMock = fetchMock.post(
  364. `express:/tag/:id/projects`,
  365. {
  366. status: 204,
  367. }
  368. )
  369. fireEvent.click(allCheckboxes[3])
  370. screen.getByRole('button', { name: `${this.tagName} (2)` })
  371. const tagsDropdown = within(actionsToolbar).getByLabelText('Tags')
  372. fireEvent.click(tagsDropdown)
  373. within(actionsToolbar).getByText('Add to folder')
  374. const tagButton = within(actionsToolbar).getByLabelText(
  375. `Add or remove project from tag ${this.tagName}`
  376. )
  377. fireEvent.click(tagButton)
  378. await fetchMock.flush(true)
  379. expect(
  380. addProjectsToTagMock.called(`/tag/${this.tagId}/projects`, {
  381. body: {
  382. projectIds: [projectsData[2].id],
  383. },
  384. })
  385. ).to.be.true
  386. screen.getByRole('button', { name: `${this.tagName} (3)` })
  387. })
  388. })
  389. describe('project tools "More" dropdown', function () {
  390. beforeEach(async function () {
  391. const filterButton = screen.getAllByText('All Projects')[0]
  392. fireEvent.click(filterButton)
  393. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  394. // first one is the select all checkbox
  395. fireEvent.click(allCheckboxes[2])
  396. actionsToolbar = screen.getAllByRole('toolbar')[0]
  397. })
  398. it('does not show the dropdown when more than 1 project is selected', async function () {
  399. await waitFor(() => {
  400. within(actionsToolbar).getByText<HTMLElement>('More')
  401. })
  402. fireEvent.click(allCheckboxes[0])
  403. expect(within(actionsToolbar).queryByText<HTMLElement>('More')).to.be
  404. .null
  405. })
  406. it('validates the project name', async function () {
  407. const moreDropdown = await within(
  408. actionsToolbar
  409. ).findByText<HTMLElement>('More')
  410. fireEvent.click(moreDropdown)
  411. const renameButton =
  412. screen.getAllByText<HTMLInputElement>('Rename')[1] // first one is for the tag in the sidebar
  413. fireEvent.click(renameButton)
  414. const modals = await screen.findAllByRole('dialog')
  415. const modal = modals[0]
  416. expect(sendSpy).to.be.calledOnce
  417. expect(sendSpy).calledWith('project-list-page-interaction')
  418. // same name
  419. let confirmButton =
  420. within(modal).getByText<HTMLInputElement>('Rename')
  421. expect(confirmButton.disabled).to.be.true
  422. let input = screen.getByLabelText('New Name') as HTMLButtonElement
  423. // no name
  424. input = screen.getByLabelText('New Name') as HTMLButtonElement
  425. fireEvent.change(input, {
  426. target: { value: '' },
  427. })
  428. confirmButton = within(modal).getByText<HTMLInputElement>('Rename')
  429. expect(confirmButton.disabled).to.be.true
  430. })
  431. it('opens the rename modal, and can rename the project, and view updated', async function () {
  432. const renameProjectMock = fetchMock.post(
  433. `express:/project/:id/rename`,
  434. {
  435. status: 200,
  436. }
  437. )
  438. const moreDropdown = await within(
  439. actionsToolbar
  440. ).findByText<HTMLElement>('More')
  441. fireEvent.click(moreDropdown)
  442. const renameButton =
  443. within(actionsToolbar).getByText<HTMLInputElement>('Rename') // first one is for the tag in the sidebar
  444. fireEvent.click(renameButton)
  445. const modals = await screen.findAllByRole('dialog')
  446. const modal = modals[0]
  447. // a valid name
  448. const newProjectName = 'A new project name'
  449. const input = (await within(modal).findByLabelText(
  450. 'New Name'
  451. )) as HTMLButtonElement
  452. const oldName = input.value
  453. fireEvent.change(input, {
  454. target: { value: newProjectName },
  455. })
  456. const confirmButton =
  457. within(modal).getByText<HTMLInputElement>('Rename')
  458. expect(confirmButton.disabled).to.be.false
  459. fireEvent.click(confirmButton)
  460. await fetchMock.flush(true)
  461. expect(
  462. renameProjectMock.called(`/project/${projectsData[1].id}/rename`)
  463. ).to.be.true
  464. const table = await screen.findByRole('table')
  465. within(table).getByText(newProjectName)
  466. expect(within(table).queryByText(oldName)).to.be.null
  467. const allCheckboxes = await within(
  468. table
  469. ).findAllByRole<HTMLInputElement>('checkbox')
  470. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  471. expect(allCheckboxesChecked.length).to.equal(0)
  472. })
  473. it('opens the copy modal, can copy the project, and view updated', async function () {
  474. const tableRows = screen.getAllByRole('row')
  475. const linkForProjectToCopy = within(tableRows[1]).getByRole('link')
  476. const projectNameToCopy = linkForProjectToCopy.textContent || '' // needed for type checking
  477. screen.getByText(projectNameToCopy) // make sure not just empty string
  478. const copiedProjectName = `${projectNameToCopy} (Copy)`
  479. const cloneProjectMock = fetchMock.post(
  480. `express:/project/:id/clone`,
  481. {
  482. status: 200,
  483. body: {
  484. name: copiedProjectName,
  485. lastUpdated: new Date(),
  486. project_id: userId,
  487. owner_ref: userId,
  488. owner,
  489. id: '6328e14abec0df019fce0be5',
  490. lastUpdatedBy: owner,
  491. accessLevel: 'owner',
  492. source: 'owner',
  493. trashed: false,
  494. archived: false,
  495. },
  496. }
  497. )
  498. await waitFor(() => {
  499. const moreDropdown =
  500. within(actionsToolbar).getByText<HTMLElement>('More')
  501. fireEvent.click(moreDropdown)
  502. })
  503. const copyButton =
  504. within(actionsToolbar).getByText<HTMLInputElement>('Make a copy')
  505. fireEvent.click(copyButton)
  506. // confirm in modal
  507. const copyConfirmButton = document.querySelector(
  508. 'button[type="submit"]'
  509. ) as HTMLElement
  510. fireEvent.click(copyConfirmButton)
  511. await fetchMock.flush(true)
  512. expect(
  513. cloneProjectMock.called(`/project/${projectsData[1].id}/clone`)
  514. ).to.be.true
  515. expect(sendSpy).to.be.calledOnce
  516. expect(sendSpy).calledWith('project-list-page-interaction')
  517. screen.getByText(copiedProjectName)
  518. })
  519. })
  520. describe('projects list in actions modal', function () {
  521. let modal: HTMLElement
  522. let projectsToProcess: any[]
  523. function selectedProjectNames() {
  524. projectsToProcess = []
  525. // needs to be done ahead of opening modal
  526. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  527. // update list so we know which are checked
  528. const tableRows = screen.getAllByRole('row')
  529. for (const [index, checkbox] of allCheckboxes.entries()) {
  530. if (checkbox.checked) {
  531. const linkForProjectToCopy = within(tableRows[index]).getByRole(
  532. 'link'
  533. )
  534. const projectNameToCopy = linkForProjectToCopy.textContent
  535. projectsToProcess.push(projectNameToCopy)
  536. }
  537. }
  538. expect(projectsToProcess.length > 0).to.be.true
  539. }
  540. function selectedMatchesDisplayed(expectedLength: number) {
  541. selectedProjectNames()
  542. // any action will work for check since they all use the same modal
  543. const archiveButton = within(actionsToolbar).getByLabelText('Archive')
  544. fireEvent.click(archiveButton)
  545. modal = screen.getAllByRole('dialog')[0]
  546. const listitems = within(modal).getAllByRole('listitem')
  547. expect(listitems.length).to.equal(projectsToProcess.length)
  548. expect(listitems.length).to.equal(expectedLength)
  549. for (const projectName of projectsToProcess) {
  550. within(modal).getByText(projectName)
  551. }
  552. }
  553. beforeEach(function () {
  554. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  555. // first one is the select all checkbox, just check 2 at first
  556. fireEvent.click(allCheckboxes[1])
  557. fireEvent.click(allCheckboxes[2])
  558. actionsToolbar = screen.getAllByRole('toolbar')[0]
  559. })
  560. it('opens the modal with the 2 originally selected projects', function () {
  561. selectedMatchesDisplayed(2)
  562. })
  563. it('shows correct list after closing modal, changing selecting, and reopening modal', async function () {
  564. selectedMatchesDisplayed(2)
  565. const modal = screen.getAllByRole('dialog', { hidden: false })[0]
  566. const cancelButton = within(modal).getByRole('button', {
  567. name: 'Cancel',
  568. })
  569. fireEvent.click(cancelButton)
  570. expect(screen.queryByRole('dialog', { hidden: false })).to.be.null
  571. await screen.findAllByRole('checkbox')
  572. fireEvent.click(allCheckboxes[3])
  573. selectedMatchesDisplayed(3)
  574. })
  575. it('maintains original list even after some have been processed', async function () {
  576. const totalProjectsToProcess = 2
  577. selectedMatchesDisplayed(totalProjectsToProcess)
  578. const button = screen.getByRole('button', { name: 'Confirm' })
  579. fireEvent.click(button)
  580. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  581. fetchMock.post('express:/project/:id/archive', {
  582. status: 200,
  583. })
  584. fetchMock.post(`/project/${project2Id}/archive`, {
  585. status: 500,
  586. })
  587. await screen.findByRole('alert') // ensure that error was thrown for the 2nd project
  588. const listitems = within(modal).getAllByRole('listitem')
  589. expect(listitems.length).to.equal(totalProjectsToProcess)
  590. })
  591. })
  592. })
  593. describe('search', function () {
  594. it('shows only projects based on the input', async function () {
  595. const input = screen.getAllByRole('textbox', {
  596. name: /search projects/i,
  597. })[0]
  598. const value = currentList[0].name
  599. fireEvent.change(input, { target: { value } })
  600. const results = screen.getAllByRole('row')
  601. expect(results.length).to.equal(2) // first is header
  602. })
  603. })
  604. describe('copying project', function () {
  605. it('correctly updates the view after copying a shared project', async function () {
  606. const filterButton = screen.getAllByText('Shared with you')[0]
  607. fireEvent.click(filterButton)
  608. const tableRows = screen.getAllByRole('row')
  609. const linkForProjectToCopy = within(tableRows[1]).getByRole('link')
  610. const projectNameToCopy = linkForProjectToCopy.textContent
  611. const copiedProjectName = `${projectNameToCopy} Copy`
  612. fetchMock.post(`express:/project/:id/clone`, {
  613. status: 200,
  614. body: {
  615. name: copiedProjectName,
  616. lastUpdated: new Date(),
  617. project_id: userId,
  618. owner_ref: userId,
  619. owner,
  620. id: '6328e14abec0df019fce0be5',
  621. lastUpdatedBy: owner,
  622. accessLevel: 'owner',
  623. source: 'owner',
  624. trashed: false,
  625. archived: false,
  626. },
  627. })
  628. const copyButton = within(tableRows[1]).getAllByLabelText('Copy')[0]
  629. fireEvent.click(copyButton)
  630. // confirm in modal
  631. const copyConfirmButton = document.querySelector(
  632. 'button[type="submit"]'
  633. ) as HTMLElement
  634. fireEvent.click(copyConfirmButton)
  635. await fetchMock.flush(true)
  636. expect(fetchMock.done()).to.be.true
  637. expect(sendSpy).to.be.calledOnce
  638. expect(sendSpy).calledWith('project-list-page-interaction')
  639. expect(screen.queryByText(copiedProjectName)).to.be.null
  640. const yourProjectFilter = screen.getAllByText('Your Projects')[0]
  641. fireEvent.click(yourProjectFilter)
  642. screen.findByText(copiedProjectName)
  643. })
  644. })
  645. describe('notifications', function () {
  646. it('email confirmation alert is displayed', async function () {
  647. screen.getByText(
  648. 'Please confirm your email test@overleaf.com by clicking on the link in the confirmation email'
  649. )
  650. })
  651. })
  652. })
  653. })