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

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