preview-download-button.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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
  31. .getByText('Download PDF')
  32. .closest('a')
  33. .getAttribute('disabled')
  34. ).to.exist
  35. const buttons = screen.getAllByRole('button')
  36. expect(buttons.length).to.equal(1) // the dropdown toggle
  37. expect(buttons[0].getAttribute('disabled')).to.exist
  38. expect(buttons[0].getAttribute('aria-label')).to.equal(
  39. 'Toggle output files list'
  40. )
  41. })
  42. it('should disable the PDF button when there is no PDF', function() {
  43. const isCompiling = false
  44. const outputFiles = []
  45. renderPreviewDownloadButton(isCompiling, outputFiles)
  46. expect(
  47. screen
  48. .getByText('Download PDF')
  49. .closest('a')
  50. .getAttribute('disabled')
  51. ).to.exist
  52. })
  53. it('should enable the PDF button when there is a main PDF', function() {
  54. const isCompiling = false
  55. const outputFiles = []
  56. renderPreviewDownloadButton(isCompiling, outputFiles, pdfDownloadUrl)
  57. expect(
  58. screen
  59. .getByText('Download PDF')
  60. .closest('a')
  61. .getAttribute('href')
  62. ).to.equal(pdfDownloadUrl)
  63. expect(
  64. screen
  65. .getByText('Download PDF')
  66. .closest('a')
  67. .getAttribute('disabled')
  68. ).to.not.exist
  69. })
  70. it('should enable the dropdown when not compiling', function() {
  71. const isCompiling = false
  72. const outputFiles = []
  73. renderPreviewDownloadButton(isCompiling, outputFiles, pdfDownloadUrl)
  74. const buttons = screen.getAllByRole('button')
  75. expect(buttons[0]).to.exist
  76. expect(buttons[0].getAttribute('disabled')).to.not.exist
  77. })
  78. it('should show the button text when prop showText=true', function() {
  79. const isCompiling = false
  80. const showText = true
  81. renderPreviewDownloadButton(isCompiling, [], pdfDownloadUrl, showText)
  82. expect(screen.getByText('Download PDF').getAttribute('style')).to.be.null
  83. })
  84. it('should not show the button text when prop showText=false', function() {
  85. const isCompiling = false
  86. const showText = false
  87. renderPreviewDownloadButton(isCompiling, [], pdfDownloadUrl, showText)
  88. expect(screen.getByText('Download PDF').getAttribute('style')).to.equal(
  89. 'position: absolute; right: -100vw;'
  90. )
  91. })
  92. })