preview-logs-toggle-button.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { expect } from 'chai'
  2. import { screen, render } from '@testing-library/react'
  3. import PreviewLogsToggleButton from '../../../../../frontend/js/features/preview/components/preview-logs-toggle-button'
  4. describe('<PreviewLogsToggleButton />', function () {
  5. function renderPreviewLogsToggleButton(
  6. logsState,
  7. onToggleLogs,
  8. showLogs,
  9. showText = false,
  10. autoCompileLintingError = false,
  11. compileFailed = false
  12. ) {
  13. render(
  14. <PreviewLogsToggleButton
  15. logsState={logsState}
  16. onToggle={onToggleLogs}
  17. showLogs={showLogs}
  18. showText={showText}
  19. autoCompileLintingError={autoCompileLintingError}
  20. compileFailed={compileFailed}
  21. />
  22. )
  23. }
  24. describe('basic toggle functionality', function () {
  25. const logsState = {
  26. nErrors: 0,
  27. nWarnings: 0,
  28. }
  29. const onToggleLogs = () => {}
  30. it('should render a view logs button when previewing the PDF', function () {
  31. const showLogs = false
  32. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  33. screen.getByText('View logs')
  34. })
  35. it('should render a view PDF button when viewing logs', function () {
  36. const showLogs = true
  37. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  38. screen.getByText('View PDF')
  39. })
  40. })
  41. describe('compile status indicator', function () {
  42. const showLogs = false
  43. const onToggleLogs = () => {}
  44. it('should render a view logs button by default', function () {
  45. const logsState = {
  46. nErrors: 0,
  47. nWarnings: 0,
  48. }
  49. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  50. screen.getByText('View logs')
  51. })
  52. it('should render the code check failed notice', function () {
  53. const logsState = {
  54. nErrors: 1,
  55. nWarnings: 0,
  56. }
  57. renderPreviewLogsToggleButton(
  58. logsState,
  59. onToggleLogs,
  60. showLogs,
  61. false,
  62. true
  63. )
  64. screen.getByText('Code check failed')
  65. })
  66. it('should render an error status message when there are errors', function () {
  67. const logsState = {
  68. nErrors: 1,
  69. nWarnings: 0,
  70. }
  71. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  72. screen.getByText(`This project has errors (${logsState.nErrors})`)
  73. })
  74. it('should render an error status message when there are both errors and warnings', function () {
  75. const logsState = {
  76. nErrors: 1,
  77. nWarnings: 1,
  78. }
  79. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  80. screen.getByText(`This project has errors (${logsState.nErrors})`)
  81. })
  82. it('should render a warning status message when there are warnings but no errors', function () {
  83. const logsState = {
  84. nErrors: 0,
  85. nWarnings: 1,
  86. }
  87. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  88. screen.getByText(`View warnings (${logsState.nWarnings})`)
  89. })
  90. it('should render 99+ errors when there are more than 99 errors', function () {
  91. const logsState = {
  92. nErrors: 100,
  93. nWarnings: 0,
  94. }
  95. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  96. screen.getByText('This project has errors (99+)')
  97. })
  98. it('should show the button text when prop showText=true', function () {
  99. const logsState = {
  100. nErrors: 0,
  101. nWarnings: 0,
  102. }
  103. const showText = true
  104. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs, showText)
  105. expect(screen.getByText('View logs').getAttribute('style')).to.be.null
  106. })
  107. it('should not show the button text when prop showText=false', function () {
  108. const logsState = {
  109. nErrors: 0,
  110. nWarnings: 0,
  111. }
  112. const showText = false
  113. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs, showText)
  114. expect(screen.getByText('View logs').getAttribute('style')).to.equal(
  115. 'position: absolute; right: -100vw;'
  116. )
  117. })
  118. })
  119. })