project-list-root.test.tsx 39 KB

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