preview-download-file-list.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { expect } from 'chai'
  2. import { screen, render } from '@testing-library/react'
  3. import PreviewDownloadFileList, {
  4. topFileTypes,
  5. } from '../../../../../frontend/js/features/preview/components/preview-download-file-list'
  6. describe('<PreviewDownloadFileList />', function () {
  7. const projectId = 'projectId123'
  8. function makeFile(fileName, main) {
  9. return {
  10. fileName,
  11. url: `/project/${projectId}/output/${fileName}`,
  12. type: fileName.split('.').pop(),
  13. main: main || false,
  14. }
  15. }
  16. it('should list all output files and group them', function () {
  17. const outputFiles = [
  18. makeFile('output.ind'),
  19. makeFile('output.log'),
  20. makeFile('output.pdf', true),
  21. makeFile('alt.pdf'),
  22. makeFile('output.stderr'),
  23. makeFile('output.stdout'),
  24. makeFile('output.aux'),
  25. makeFile('output.bbl'),
  26. makeFile('output.blg'),
  27. ]
  28. render(<PreviewDownloadFileList fileList={outputFiles} />)
  29. const menuItems = screen.getAllByRole('menuitem')
  30. expect(menuItems.length).to.equal(outputFiles.length - 1) // main PDF is listed separately
  31. const fileTypes = outputFiles.map(file => {
  32. return file.type
  33. })
  34. menuItems.forEach((item, index) => {
  35. // check displayed text
  36. const fileType = item.textContent.split('.').pop()
  37. expect(fileTypes).to.include(fileType)
  38. })
  39. // check grouped correctly
  40. expect(topFileTypes).to.exist
  41. expect(topFileTypes.length).to.be.above(0)
  42. const outputTopFileTypes = outputFiles
  43. .filter(file => {
  44. return topFileTypes.includes(file.type)
  45. })
  46. .map(file => file.type)
  47. const topMenuItems = menuItems.slice(0, outputTopFileTypes.length)
  48. topMenuItems.forEach(item => {
  49. const fileType = item.textContent.split('.').pop().replace(' file', '')
  50. expect(topFileTypes.includes(fileType)).to.be.true
  51. })
  52. })
  53. it('should list all files when there are duplicate types', function () {
  54. const pdfFile = makeFile('output.pdf', true)
  55. const bblFile = makeFile('output.bbl')
  56. const outputFiles = [Object.assign({}, { ...bblFile }), bblFile, pdfFile]
  57. render(<PreviewDownloadFileList fileList={outputFiles} />)
  58. const bblMenuItems = screen.getAllByText((content, element) => {
  59. return content !== '' && element.textContent === 'output.bbl'
  60. })
  61. expect(bblMenuItems.length).to.equal(2)
  62. })
  63. it('should list the non-main PDF in the dropdown', function () {
  64. const pdfFile = makeFile('output.pdf', true)
  65. const pdfAltFile = makeFile('alt.pdf')
  66. const outputFiles = [pdfFile, pdfAltFile]
  67. render(<PreviewDownloadFileList fileList={outputFiles} />)
  68. screen.getAllByRole('menuitem', { name: 'alt.pdf' })
  69. })
  70. describe('list divider and header', function () {
  71. it('should display when there are top files and other files', function () {
  72. const outputFiles = [
  73. makeFile('output.bbl'),
  74. makeFile('output.ind'),
  75. makeFile('output.gls'),
  76. makeFile('output.log'),
  77. ]
  78. render(<PreviewDownloadFileList fileList={outputFiles} />)
  79. screen.getByText('Download other output files')
  80. screen.getByRole('separator')
  81. })
  82. it('should not display when there are top files and no other files', function () {
  83. const outputFiles = [
  84. makeFile('output.bbl'),
  85. makeFile('output.ind'),
  86. makeFile('output.gls'),
  87. ]
  88. render(<PreviewDownloadFileList fileList={outputFiles} />)
  89. expect(screen.queryByText('Other output files')).to.not.exist
  90. expect(screen.queryByRole('separator')).to.not.exist
  91. })
  92. it('should not display when there are other files and no top files', function () {
  93. const outputFiles = [makeFile('output.log')]
  94. render(<PreviewDownloadFileList fileList={outputFiles} />)
  95. expect(screen.queryByText('Other output files')).to.not.exist
  96. expect(screen.queryByRole('separator')).to.not.exist
  97. })
  98. })
  99. })