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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 without a count when there is a single error', function () {
  67. const logsState = {
  68. nErrors: 1,
  69. nWarnings: 0,
  70. }
  71. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  72. screen.getByText('This project has an error')
  73. })
  74. it('should render an error status message with a count when there are multiple errors', function () {
  75. const logsState = {
  76. nErrors: 10,
  77. nWarnings: 0,
  78. }
  79. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  80. screen.getByText(`This project has errors (${logsState.nErrors})`)
  81. })
  82. it('should render an error status message when there are both errors and warnings', function () {
  83. const logsState = {
  84. nErrors: 1,
  85. nWarnings: 1,
  86. }
  87. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  88. screen.getByText('This project has an error')
  89. })
  90. it('should render a warning status message without a count when there is a single warning and no errors', function () {
  91. const logsState = {
  92. nErrors: 0,
  93. nWarnings: 1,
  94. }
  95. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  96. screen.getByText('View warning')
  97. })
  98. it('should render a warning status message with a count when there are multiple warnings and no errors', function () {
  99. const logsState = {
  100. nErrors: 0,
  101. nWarnings: 10,
  102. }
  103. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  104. screen.getByText(`View warnings (${logsState.nWarnings})`)
  105. })
  106. it('should render 99+ errors when there are more than 99 errors', function () {
  107. const logsState = {
  108. nErrors: 100,
  109. nWarnings: 0,
  110. }
  111. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs)
  112. screen.getByText('This project has errors (99+)')
  113. })
  114. it('should show the button text when prop showText=true', function () {
  115. const logsState = {
  116. nErrors: 0,
  117. nWarnings: 0,
  118. }
  119. const showText = true
  120. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs, showText)
  121. expect(screen.getByText('View logs').getAttribute('style')).to.be.null
  122. })
  123. it('should not show the button text when prop showText=false', function () {
  124. const logsState = {
  125. nErrors: 0,
  126. nWarnings: 0,
  127. }
  128. const showText = false
  129. renderPreviewLogsToggleButton(logsState, onToggleLogs, showLogs, showText)
  130. expect(screen.getByText('View logs').getAttribute('style')).to.equal(
  131. 'position: absolute; right: -100vw;'
  132. )
  133. })
  134. })
  135. })