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

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