project-list-root.test.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. owner,
  10. archivedProjects,
  11. makeLongProjectList,
  12. } from '../fixtures/projects-data'
  13. const { fullList, currentList, trashedList } = makeLongProjectList(40)
  14. const userId = owner.id
  15. describe('<ProjectListRoot />', function () {
  16. const originalLocation = window.location
  17. const locationStub = sinon.stub()
  18. let sendSpy: sinon.SinonSpy
  19. beforeEach(async function () {
  20. global.localStorage.clear()
  21. sendSpy = sinon.spy(eventTracking, 'send')
  22. window.metaAttributesCache = new Map()
  23. window.metaAttributesCache.set('ol-tags', [])
  24. window.metaAttributesCache.set('ol-ExposedSettings', { templateLinks: [] })
  25. window.user_id = userId
  26. Object.defineProperty(window, 'location', {
  27. value: { assign: locationStub },
  28. })
  29. renderWithProjectListContext(<ProjectListRoot />, {
  30. projects: fullList,
  31. })
  32. await fetchMock.flush(true)
  33. await waitFor(() => {
  34. screen.findByRole('table')
  35. })
  36. })
  37. afterEach(function () {
  38. sendSpy.restore()
  39. window.user_id = undefined
  40. fetchMock.reset()
  41. Object.defineProperty(window, 'location', {
  42. value: originalLocation,
  43. })
  44. })
  45. describe('checkboxes', function () {
  46. let allCheckboxes: Array<HTMLInputElement> = []
  47. let actionsToolbar: HTMLElement
  48. let project1Id: string | null, project2Id: string | null
  49. describe('all projects', function () {
  50. beforeEach(function () {
  51. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  52. // first one is the select all checkbox
  53. fireEvent.click(allCheckboxes[1])
  54. fireEvent.click(allCheckboxes[2])
  55. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  56. project2Id = allCheckboxes[2].getAttribute('data-project-id')
  57. actionsToolbar = screen.getAllByRole('toolbar')[0]
  58. })
  59. it('downloads all selected projects and then unselects them', async function () {
  60. const downloadButton = within(actionsToolbar).getByLabelText('Download')
  61. fireEvent.click(downloadButton)
  62. await waitFor(() => {
  63. expect(locationStub).to.have.been.called
  64. })
  65. sinon.assert.calledWithMatch(
  66. locationStub,
  67. `/project/download/zip?project_ids=${project1Id},${project2Id}`
  68. )
  69. const allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  70. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  71. expect(allCheckboxesChecked.length).to.equal(0)
  72. })
  73. it('opens archive modal for all selected projects and archives all', async function () {
  74. fetchMock.post(
  75. `express:/project/${project1Id}/archive`,
  76. {
  77. status: 200,
  78. },
  79. { delay: 0 }
  80. )
  81. fetchMock.post(
  82. `express:/project/${project2Id}/archive`,
  83. {
  84. status: 200,
  85. },
  86. { delay: 0 }
  87. )
  88. const archiveButton = within(actionsToolbar).getByLabelText('Archive')
  89. fireEvent.click(archiveButton)
  90. const confirmBtn = screen.getByText('Confirm') as HTMLButtonElement
  91. fireEvent.click(confirmBtn)
  92. expect(confirmBtn.disabled).to.be.true
  93. await fetchMock.flush(true)
  94. expect(fetchMock.done()).to.be.true
  95. const requests = fetchMock.calls()
  96. const [projectRequest1Url, projectRequest1Headers] = requests[2]
  97. expect(projectRequest1Url).to.equal(`/project/${project1Id}/archive`)
  98. expect(projectRequest1Headers?.method).to.equal('POST')
  99. const [projectRequest2Url, projectRequest2Headers] = requests[3]
  100. expect(projectRequest2Url).to.equal(`/project/${project2Id}/archive`)
  101. expect(projectRequest2Headers?.method).to.equal('POST')
  102. })
  103. it('opens trash modal for all selected projects and trashes all', async function () {
  104. fetchMock.post(
  105. `express:/project/${project1Id}/trash`,
  106. {
  107. status: 200,
  108. },
  109. { delay: 0 }
  110. )
  111. fetchMock.post(
  112. `express:/project/${project2Id}/trash`,
  113. {
  114. status: 200,
  115. },
  116. { delay: 0 }
  117. )
  118. const archiveButton = within(actionsToolbar).getByLabelText('Trash')
  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 fetchMock.flush(true)
  124. expect(fetchMock.done()).to.be.true
  125. const requests = fetchMock.calls()
  126. const [projectRequest1Url, projectRequest1Headers] = requests[2]
  127. expect(projectRequest1Url).to.equal(`/project/${project1Id}/trash`)
  128. expect(projectRequest1Headers?.method).to.equal('POST')
  129. const [projectRequest2Url, projectRequest2Headers] = requests[3]
  130. expect(projectRequest2Url).to.equal(`/project/${project2Id}/trash`)
  131. expect(projectRequest2Headers?.method).to.equal('POST')
  132. })
  133. it('only checks the projects that are viewable when there is a load more button', async function () {
  134. // first one is the select all checkbox
  135. fireEvent.click(allCheckboxes[0])
  136. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  137. let checked = allCheckboxes.filter(c => c.checked)
  138. expect(checked.length).to.equal(21) // max projects viewable by default is 20, and plus one for check all
  139. const loadMoreButton = screen.getByLabelText('Show 17 more projects')
  140. fireEvent.click(loadMoreButton)
  141. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  142. expect(allCheckboxes.length).to.equal(currentList.length + 1)
  143. checked = allCheckboxes.filter(c => c.checked)
  144. expect(checked.length).to.equal(20) // remains same even after showing more
  145. })
  146. it('maintains viewable and selected projects after loading more and then selecting all', async function () {
  147. const loadMoreButton = screen.getByLabelText('Show 17 more projects')
  148. fireEvent.click(loadMoreButton)
  149. // verify button gone
  150. screen.getByText(
  151. `Showing ${currentList.length} out of ${currentList.length} projects.`
  152. )
  153. // first one is the select all checkbox
  154. fireEvent.click(allCheckboxes[0])
  155. // verify button still gone
  156. screen.getByText(
  157. `Showing ${currentList.length} out of ${currentList.length} projects.`
  158. )
  159. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  160. expect(allCheckboxes.length).to.equal(currentList.length + 1)
  161. })
  162. })
  163. describe('archived projects', function () {
  164. beforeEach(function () {
  165. const filterButton = screen.getAllByText('Archived Projects')[0]
  166. fireEvent.click(filterButton)
  167. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  168. expect(allCheckboxes.length === 2).to.be.true
  169. // first one is the select all checkbox
  170. fireEvent.click(allCheckboxes[1])
  171. project1Id = allCheckboxes[1].getAttribute('data-project-id')
  172. actionsToolbar = screen.getAllByRole('toolbar')[0]
  173. })
  174. it('does not show the archive button in toolbar when archive view selected', function () {
  175. expect(screen.queryByLabelText('Archive')).to.be.null
  176. })
  177. it('restores all projects when selected', async function () {
  178. fetchMock.delete(`express:/project/:id/archive`, {
  179. status: 200,
  180. })
  181. const unarchiveButton =
  182. within(actionsToolbar).getByText<HTMLInputElement>('Restore')
  183. fireEvent.click(unarchiveButton)
  184. await fetchMock.flush(true)
  185. expect(fetchMock.done()).to.be.true
  186. screen.getByText('No projects')
  187. })
  188. it('only unarchive the selected projects', async function () {
  189. // beforeEach selected all, so uncheck the 1st project
  190. fireEvent.click(allCheckboxes[1])
  191. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  192. expect(allCheckboxesChecked.length).to.equal(
  193. archivedProjects.length - 1
  194. )
  195. await fetchMock.flush(true)
  196. expect(fetchMock.done()).to.be.true
  197. expect(screen.queryByText('No projects')).to.be.null
  198. })
  199. })
  200. describe('trashed projects', function () {
  201. beforeEach(function () {
  202. const filterButton = screen.getAllByText('Trashed Projects')[0]
  203. fireEvent.click(filterButton)
  204. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  205. // + 1 because of select all
  206. expect(allCheckboxes.length).to.equal(trashedList.length + 1)
  207. // first one is the select all checkbox
  208. fireEvent.click(allCheckboxes[0])
  209. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  210. // + 1 because of select all
  211. expect(allCheckboxesChecked.length).to.equal(trashedList.length + 1)
  212. actionsToolbar = screen.getAllByRole('toolbar')[0]
  213. })
  214. it('only shows the download, archive, and restore buttons in top toolbar', function () {
  215. expect(screen.queryByLabelText('Trash')).to.be.null
  216. within(actionsToolbar).queryByLabelText('Download')
  217. within(actionsToolbar).queryByLabelText('Archive')
  218. within(actionsToolbar).getByText('Restore') // no icon for this button
  219. })
  220. it('clears selected projects when filter changed', function () {
  221. const filterButton = screen.getAllByText('All Projects')[0]
  222. fireEvent.click(filterButton)
  223. const allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  224. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  225. expect(allCheckboxesChecked.length).to.equal(0)
  226. })
  227. it('untrashes all the projects', async function () {
  228. fetchMock.delete(`express:/project/:id/trash`, {
  229. status: 200,
  230. })
  231. const untrashButton =
  232. within(actionsToolbar).getByText<HTMLInputElement>('Restore')
  233. fireEvent.click(untrashButton)
  234. await fetchMock.flush(true)
  235. expect(fetchMock.done()).to.be.true
  236. screen.getByText('No projects')
  237. })
  238. it('only untrashes the selected projects', async function () {
  239. // beforeEach selected all, so uncheck the 1st project
  240. fireEvent.click(allCheckboxes[1])
  241. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  242. expect(allCheckboxesChecked.length).to.equal(trashedList.length - 1)
  243. await fetchMock.flush(true)
  244. expect(fetchMock.done()).to.be.true
  245. expect(screen.queryByText('No projects')).to.be.null
  246. })
  247. it('removes project from view when archiving', async function () {
  248. fetchMock.post(`express:/project/:id/archive`, {
  249. status: 200,
  250. })
  251. const untrashButton =
  252. within(actionsToolbar).getByLabelText<HTMLInputElement>('Archive')
  253. fireEvent.click(untrashButton)
  254. const confirmButton = screen.getByText<HTMLInputElement>('Confirm')
  255. fireEvent.click(confirmButton)
  256. expect(confirmButton.disabled).to.be.true
  257. await fetchMock.flush(true)
  258. expect(fetchMock.done()).to.be.true
  259. screen.getByText('No projects')
  260. })
  261. })
  262. describe('project tools "More" dropdown', function () {
  263. beforeEach(async function () {
  264. const filterButton = screen.getAllByText('All Projects')[0]
  265. fireEvent.click(filterButton)
  266. allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  267. // first one is the select all checkbox
  268. fireEvent.click(allCheckboxes[2])
  269. actionsToolbar = screen.getAllByRole('toolbar')[0]
  270. })
  271. it('does not show the dropdown when more than 1 project is selected', async function () {
  272. await waitFor(() => {
  273. within(actionsToolbar).getByText<HTMLElement>('More')
  274. })
  275. fireEvent.click(allCheckboxes[0])
  276. expect(within(actionsToolbar).queryByText<HTMLElement>('More')).to.be
  277. .null
  278. })
  279. it('opens the rename modal, and can rename the project, and view updated', async function () {
  280. fetchMock.post(`express:/project/:id/rename`, {
  281. status: 200,
  282. })
  283. await waitFor(() => {
  284. const moreDropdown =
  285. within(actionsToolbar).getByText<HTMLElement>('More')
  286. fireEvent.click(moreDropdown)
  287. })
  288. const renameButton = screen.getByText<HTMLInputElement>('Rename')
  289. fireEvent.click(renameButton)
  290. const modal = screen.getAllByRole('dialog')[0]
  291. expect(sendSpy).to.be.calledOnce
  292. expect(sendSpy).calledWith('project-list-page-interaction')
  293. // same name
  294. let confirmButton = within(modal).getByText<HTMLInputElement>('Rename')
  295. expect(confirmButton.disabled).to.be.true
  296. let input = screen.getByLabelText('New Name') as HTMLButtonElement
  297. const oldName = input.value
  298. // no name
  299. let newProjectName = ''
  300. input = screen.getByLabelText('New Name') as HTMLButtonElement
  301. fireEvent.change(input, {
  302. target: { value: newProjectName },
  303. })
  304. confirmButton = within(modal).getByText<HTMLInputElement>('Rename')
  305. expect(confirmButton.disabled).to.be.true
  306. // a valid name
  307. newProjectName = 'A new project name'
  308. input = screen.getByLabelText('New Name') as HTMLButtonElement
  309. fireEvent.change(input, {
  310. target: { value: newProjectName },
  311. })
  312. confirmButton = within(modal).getByText<HTMLInputElement>('Rename')
  313. expect(confirmButton.disabled).to.be.false
  314. fireEvent.click(confirmButton)
  315. await fetchMock.flush(true)
  316. expect(fetchMock.done()).to.be.true
  317. screen.findByText(newProjectName)
  318. expect(screen.queryByText(oldName)).to.be.null
  319. const allCheckboxes = screen.getAllByRole<HTMLInputElement>('checkbox')
  320. const allCheckboxesChecked = allCheckboxes.filter(c => c.checked)
  321. expect(allCheckboxesChecked.length).to.equal(0)
  322. })
  323. it('opens the copy modal, can copy the project, and view updated', async function () {
  324. const tableRows = screen.getAllByRole('row')
  325. const linkForProjectToCopy = within(tableRows[1]).getByRole('link')
  326. const projectNameToCopy = linkForProjectToCopy.textContent || '' // needed for type checking
  327. screen.findByText(projectNameToCopy) // make sure not just empty string
  328. const copiedProjectName = `${projectNameToCopy} (Copy)`
  329. fetchMock.post(`express:/project/:id/clone`, {
  330. status: 200,
  331. body: {
  332. name: copiedProjectName,
  333. lastUpdated: new Date(),
  334. project_id: userId,
  335. owner_ref: userId,
  336. owner,
  337. id: '6328e14abec0df019fce0be5',
  338. lastUpdatedBy: owner,
  339. accessLevel: 'owner',
  340. source: 'owner',
  341. trashed: false,
  342. archived: false,
  343. },
  344. })
  345. await waitFor(() => {
  346. const moreDropdown =
  347. within(actionsToolbar).getByText<HTMLElement>('More')
  348. fireEvent.click(moreDropdown)
  349. })
  350. const copyButton =
  351. within(actionsToolbar).getByText<HTMLInputElement>('Make a copy')
  352. fireEvent.click(copyButton)
  353. // confirm in modal
  354. const copyConfirmButton = document.querySelector(
  355. 'button[type="submit"]'
  356. ) as HTMLElement
  357. fireEvent.click(copyConfirmButton)
  358. await fetchMock.flush(true)
  359. expect(fetchMock.done()).to.be.true
  360. expect(sendSpy).to.be.calledOnce
  361. expect(sendSpy).calledWith('project-list-page-interaction')
  362. screen.findByText(copiedProjectName)
  363. })
  364. })
  365. })
  366. describe('search', function () {
  367. it('shows only projects based on the input', async function () {
  368. const input = screen.getAllByRole('textbox', {
  369. name: /search projects/i,
  370. })[0]
  371. const value = currentList[0].name
  372. fireEvent.change(input, { target: { value } })
  373. const results = screen.getAllByRole('row')
  374. expect(results.length).to.equal(2) // first is header
  375. })
  376. })
  377. describe('copying project', function () {
  378. it('correctly updates the view after copying a shared project', async function () {
  379. const filterButton = screen.getAllByText('Shared with you')[0]
  380. fireEvent.click(filterButton)
  381. const tableRows = screen.getAllByRole('row')
  382. const linkForProjectToCopy = within(tableRows[1]).getByRole('link')
  383. const projectNameToCopy = linkForProjectToCopy.textContent
  384. const copiedProjectName = `${projectNameToCopy} Copy`
  385. fetchMock.post(`express:/project/:id/clone`, {
  386. status: 200,
  387. body: {
  388. name: copiedProjectName,
  389. lastUpdated: new Date(),
  390. project_id: userId,
  391. owner_ref: userId,
  392. owner,
  393. id: '6328e14abec0df019fce0be5',
  394. lastUpdatedBy: owner,
  395. accessLevel: 'owner',
  396. source: 'owner',
  397. trashed: false,
  398. archived: false,
  399. },
  400. })
  401. const copyButton = within(tableRows[1]).getAllByLabelText('Copy')[0]
  402. fireEvent.click(copyButton)
  403. // confirm in modal
  404. const copyConfirmButton = document.querySelector(
  405. 'button[type="submit"]'
  406. ) as HTMLElement
  407. fireEvent.click(copyConfirmButton)
  408. await fetchMock.flush(true)
  409. expect(fetchMock.done()).to.be.true
  410. expect(sendSpy).to.be.calledOnce
  411. expect(sendSpy).calledWith('project-list-page-interaction')
  412. expect(screen.queryByText(copiedProjectName)).to.be.null
  413. const yourProjectFilter = screen.getAllByText('Your Projects')[0]
  414. fireEvent.click(yourProjectFilter)
  415. screen.findByText(copiedProjectName)
  416. })
  417. })
  418. })