preview-download-button.test.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { expect } from 'chai'
  2. import { screen, render } from '@testing-library/react'
  3. import PreviewDownloadButton from '../../../../../frontend/js/features/preview/components/preview-download-button'
  4. describe('<PreviewDownloadButton />', function () {
  5. const projectId = 'projectId123'
  6. const pdfDownloadUrl = `/download/project/${projectId}/build/17523aaafdf-1ad9063af140f004/output/output.pdf?compileGroup=priority&popupDownload=true`
  7. function renderPreviewDownloadButton(
  8. isCompiling,
  9. outputFiles,
  10. pdfDownloadUrl,
  11. showText
  12. ) {
  13. if (isCompiling === undefined) isCompiling = false
  14. if (showText === undefined) showText = true
  15. render(
  16. <PreviewDownloadButton
  17. isCompiling={isCompiling}
  18. outputFiles={outputFiles || []}
  19. pdfDownloadUrl={pdfDownloadUrl}
  20. showText={showText}
  21. />
  22. )
  23. }
  24. it('should disable the button and dropdown toggle when compiling', function () {
  25. const isCompiling = true
  26. const outputFiles = undefined
  27. renderPreviewDownloadButton(isCompiling, outputFiles)
  28. expect(
  29. screen.getByText('Download PDF').closest('a').getAttribute('disabled')
  30. ).to.exist
  31. const buttons = screen.getAllByRole('button')
  32. expect(buttons.length).to.equal(1) // the dropdown toggle
  33. expect(buttons[0].getAttribute('disabled')).to.exist
  34. expect(buttons[0].getAttribute('aria-label')).to.equal(
  35. 'Toggle output files list'
  36. )
  37. })
  38. it('should disable the PDF button when there is no PDF', function () {
  39. const isCompiling = false
  40. const outputFiles = []
  41. renderPreviewDownloadButton(isCompiling, outputFiles)
  42. expect(
  43. screen.getByText('Download PDF').closest('a').getAttribute('disabled')
  44. ).to.exist
  45. })
  46. it('should enable the PDF button when there is a main PDF', function () {
  47. const isCompiling = false
  48. const outputFiles = []
  49. renderPreviewDownloadButton(isCompiling, outputFiles, pdfDownloadUrl)
  50. expect(
  51. screen.getByText('Download PDF').closest('a').getAttribute('href')
  52. ).to.equal(pdfDownloadUrl)
  53. expect(
  54. screen.getByText('Download PDF').closest('a').getAttribute('disabled')
  55. ).to.not.exist
  56. })
  57. it('should enable the dropdown when not compiling', function () {
  58. const isCompiling = false
  59. const outputFiles = []
  60. renderPreviewDownloadButton(isCompiling, outputFiles, pdfDownloadUrl)
  61. const buttons = screen.getAllByRole('button')
  62. expect(buttons[0]).to.exist
  63. expect(buttons[0].getAttribute('disabled')).to.not.exist
  64. })
  65. it('should show the button text when prop showText=true', function () {
  66. const isCompiling = false
  67. const showText = true
  68. renderPreviewDownloadButton(isCompiling, [], pdfDownloadUrl, showText)
  69. expect(screen.getByText('Download PDF').getAttribute('style')).to.be.null
  70. })
  71. it('should not show the button text when prop showText=false', function () {
  72. const isCompiling = false
  73. const showText = false
  74. renderPreviewDownloadButton(isCompiling, [], pdfDownloadUrl, showText)
  75. expect(screen.getByText('Download PDF').getAttribute('style')).to.equal(
  76. 'position: absolute; right: -100vw;'
  77. )
  78. })
  79. })