preview-download-button.test.js 3.1 KB

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