project-list-root.test.tsx 34 KB

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