preview-pane.test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import React from 'react'
  2. import { screen, render, fireEvent } from '@testing-library/react'
  3. import PreviewPane from '../../../../../frontend/js/features/preview/components/preview-pane'
  4. const { expect } = require('chai')
  5. describe('<PreviewPane />', function() {
  6. const sampleError1 = {
  7. content: 'error 1 content',
  8. file: 'main.tex',
  9. level: 'error',
  10. line: 17,
  11. message: 'Misplaced alignment tab character &.'
  12. }
  13. const sampleError2 = {
  14. content: 'error 1 content',
  15. file: 'main.tex',
  16. level: 'error',
  17. line: 22,
  18. message: 'Extra alignment tab has been changed to cr.'
  19. }
  20. const sampleWarning = {
  21. file: 'main.tex',
  22. level: 'warning',
  23. line: 30,
  24. message: "Reference `idontexist' on page 1 undefined on input line 30."
  25. }
  26. describe('first error pop-up', function() {
  27. it('renders a first error pop-up with the first error', function() {
  28. const propsAfterCompileWithErrors = getProps(false, {
  29. errors: [sampleError1, sampleError2],
  30. warnings: [sampleWarning]
  31. })
  32. render(<PreviewPane {...propsAfterCompileWithErrors} />)
  33. screen.getByRole('alertdialog', {
  34. name: 'Your project has errors. This is the first one.'
  35. })
  36. screen.getByText(sampleError1.message)
  37. })
  38. it('does not render a first error pop-up when there are only warnings', function() {
  39. const propsAfterCompileWithWarningsOnly = getProps(false, {
  40. errors: [],
  41. warnings: [sampleWarning]
  42. })
  43. render(<PreviewPane {...propsAfterCompileWithWarningsOnly} />)
  44. expect(
  45. screen.queryByRole('alertdialog', {
  46. name: 'Your project has errors. This is the first one.'
  47. })
  48. ).to.not.exist
  49. })
  50. it('does not render a first error pop-up when a compile is ongoing', function() {
  51. const propsWhileCompiling = getProps(true, {
  52. errors: [sampleError1, sampleError2],
  53. warnings: [sampleWarning]
  54. })
  55. render(<PreviewPane {...propsWhileCompiling} />)
  56. expect(
  57. screen.queryByRole('alertdialog', {
  58. name: 'Your project has errors. This is the first one.'
  59. })
  60. ).to.not.exist
  61. })
  62. it('does not render a first error pop-up when viewing logs', function() {
  63. const propsWithErrorsViewingLogs = getProps(
  64. false,
  65. {
  66. errors: [sampleError1, sampleError2],
  67. warnings: [sampleWarning]
  68. },
  69. Date.now(),
  70. true
  71. )
  72. render(<PreviewPane {...propsWithErrorsViewingLogs} />)
  73. expect(
  74. screen.queryByRole('alertdialog', {
  75. name: 'Your project has errors. This is the first one.'
  76. })
  77. ).to.not.exist
  78. })
  79. it('does not render a first error pop-up when going back to the PDF view after viewing logs', function() {
  80. const nowTimestamp = Date.now()
  81. const propsWithErrorsViewingLogs = getProps(
  82. false,
  83. {
  84. errors: [sampleError1, sampleError2],
  85. warnings: [sampleWarning]
  86. },
  87. nowTimestamp,
  88. true
  89. )
  90. const propsWithErrorsAfterViewingLogs = getProps(
  91. false,
  92. {
  93. errors: [sampleError1, sampleError2],
  94. warnings: [sampleWarning]
  95. },
  96. nowTimestamp,
  97. false
  98. )
  99. const { rerender } = render(
  100. <PreviewPane {...propsWithErrorsViewingLogs} />
  101. )
  102. rerender(<PreviewPane {...propsWithErrorsAfterViewingLogs} />)
  103. expect(
  104. screen.queryByRole('alertdialog', {
  105. name: 'Your project has errors. This is the first one.'
  106. })
  107. ).to.not.exist
  108. })
  109. it('renders a first error pop-up with updated errors after recompiling', function() {
  110. const nowTimestamp = Date.now()
  111. const laterTimestamp = Date.now() + 1000
  112. const propsWithErrorsAfterFirstCompile = getProps(
  113. false,
  114. {
  115. errors: [sampleError1, sampleError2],
  116. warnings: [sampleWarning]
  117. },
  118. nowTimestamp,
  119. true
  120. )
  121. const propsWithErrorsAfterSecondCompile = getProps(
  122. false,
  123. {
  124. errors: [sampleError2],
  125. warnings: [sampleWarning]
  126. },
  127. laterTimestamp,
  128. false
  129. )
  130. const { rerender } = render(
  131. <PreviewPane {...propsWithErrorsAfterFirstCompile} />
  132. )
  133. rerender(<PreviewPane {...propsWithErrorsAfterSecondCompile} />)
  134. screen.getByRole('alertdialog', {
  135. name: 'Your project has errors. This is the first one.'
  136. })
  137. screen.getByText(sampleError2.message)
  138. })
  139. it('allows dismissing the first error pop-up', function() {
  140. const propsWithErrors = getProps(false, {
  141. errors: [sampleError1, sampleError2],
  142. warnings: [sampleWarning]
  143. })
  144. render(<PreviewPane {...propsWithErrors} />)
  145. const dismissPopUpButton = screen.getByRole('button', {
  146. name: 'Dismiss first error alert'
  147. })
  148. fireEvent.click(dismissPopUpButton)
  149. expect(
  150. screen.queryByRole('alertdialog', {
  151. name: 'Your project has errors. This is the first one.'
  152. })
  153. ).to.not.exist
  154. })
  155. it('does not render the first error pop-up with new recompiles after it being dismissed once', function() {
  156. const nowTimestamp = Date.now()
  157. const laterTimestamp = Date.now() + 1000
  158. const propsWithErrorsForFirstCompile = getProps(
  159. false,
  160. {
  161. errors: [sampleError1, sampleError2],
  162. warnings: [sampleWarning]
  163. },
  164. nowTimestamp
  165. )
  166. const propsWithErrorsForSecondCompile = getProps(
  167. false,
  168. {
  169. errors: [sampleError2],
  170. warnings: [sampleWarning]
  171. },
  172. laterTimestamp
  173. )
  174. const { rerender } = render(
  175. <PreviewPane {...propsWithErrorsForFirstCompile} />
  176. )
  177. const dismissPopUpButton = screen.getByRole('button', {
  178. name: 'Dismiss first error alert'
  179. })
  180. fireEvent.click(dismissPopUpButton)
  181. rerender(<PreviewPane {...propsWithErrorsForSecondCompile} />)
  182. expect(
  183. screen.queryByRole('alertdialog', {
  184. name: 'Your project has errors. This is the first one.'
  185. })
  186. ).to.not.exist
  187. })
  188. })
  189. function getProps(
  190. isCompiling = false,
  191. logEntries = {},
  192. lastCompileTimestamp = Date.now(),
  193. isShowingLogs = false
  194. ) {
  195. return {
  196. compilerState: {
  197. isAutoCompileOn: false,
  198. isCompiling: isCompiling,
  199. isClearingCache: false,
  200. isDraftModeOn: false,
  201. isSyntaxCheckOn: false,
  202. lastCompileTimestamp: lastCompileTimestamp,
  203. logEntries: logEntries
  204. },
  205. onClearCache: () => {},
  206. onLogEntryLocationClick: () => {},
  207. onRecompile: () => {},
  208. onRunSyntaxCheckNow: () => {},
  209. onSetAutoCompile: () => {},
  210. onSetDraftMode: () => {},
  211. onSetSyntaxCheck: () => {},
  212. onToggleLogs: () => {},
  213. showLogs: isShowingLogs
  214. }
  215. }
  216. })