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

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