project-list-root.test.tsx 41 KB

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