pdf-preview.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import fetchMock from 'fetch-mock'
  4. import { screen, fireEvent, waitFor } from '@testing-library/react'
  5. import PdfPreview from '../../../../../frontend/js/features/pdf-preview/components/pdf-preview'
  6. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  7. const outputFiles = [
  8. {
  9. path: 'output.pdf',
  10. build: '123',
  11. // url: 'about:blank', // TODO: PDF URL to render
  12. type: 'pdf',
  13. },
  14. {
  15. path: 'output.bbl',
  16. build: '123',
  17. url: '/build/output.bbl',
  18. type: 'bbl',
  19. },
  20. {
  21. path: 'output.bib',
  22. build: '123',
  23. url: '/build/output.bib',
  24. type: 'bib',
  25. },
  26. {
  27. path: 'example.txt',
  28. build: '123',
  29. url: '/build/example.txt',
  30. type: 'txt',
  31. },
  32. {
  33. path: 'output.log',
  34. build: '123',
  35. url: '/build/output.log',
  36. type: 'log',
  37. },
  38. {
  39. path: 'output.blg',
  40. build: '123',
  41. url: '/build/output.blg',
  42. type: 'blg',
  43. },
  44. ]
  45. const mockCompile = () =>
  46. fetchMock.post('express:/project/:projectId/compile', {
  47. body: {
  48. status: 'success',
  49. clsiServerId: 'foo',
  50. compileGroup: 'priority',
  51. pdfDownloadDomain: '',
  52. outputFiles,
  53. },
  54. })
  55. const mockCompileError = status =>
  56. fetchMock.post('express:/project/:projectId/compile', {
  57. body: {
  58. status,
  59. clsiServerId: 'foo',
  60. compileGroup: 'priority',
  61. },
  62. })
  63. const mockValidationProblems = validationProblems =>
  64. fetchMock.post('express:/project/:projectId/compile', {
  65. body: {
  66. status: 'validation-problems',
  67. validationProblems,
  68. clsiServerId: 'foo',
  69. compileGroup: 'priority',
  70. },
  71. })
  72. const mockClearCache = () =>
  73. fetchMock.delete('express:/project/:projectId/output', 204)
  74. const defaultFileResponses = {
  75. '/build/output.blg': 'This is BibTeX, Version 4.0', // FIXME
  76. '/build/output.log': `
  77. The LaTeX compiler output
  78. * With a lot of details
  79. Wrapped in an HTML <pre> element with
  80. preformatted text which is to be presented exactly
  81. as written in the HTML file
  82. (whitespace included™)
  83. The text is typically rendered using a non-proportional ("monospace") font.
  84. LaTeX Font Info: External font \`cmex10' loaded for size
  85. (Font) <7> on input line 18.
  86. LaTeX Font Info: External font \`cmex10' loaded for size
  87. (Font) <5> on input line 18.
  88. ! Undefined control sequence.
  89. <recently read> \\Zlpha
  90. main.tex, line 23
  91. `,
  92. }
  93. const mockBuildFile = (responses = defaultFileResponses) =>
  94. fetchMock.get('express:/build/:file', (_url, options, request) => {
  95. const url = new URL(_url, 'https://example.com')
  96. if (url.pathname in responses) {
  97. return responses[url.pathname]
  98. }
  99. return 404
  100. })
  101. const storeAndFireEvent = (key, value) => {
  102. localStorage.setItem(key, value)
  103. fireEvent(window, new StorageEvent('storage', { key }))
  104. }
  105. describe('<PdfPreview/>', function () {
  106. var clock
  107. beforeEach(function () {
  108. clock = sinon.useFakeTimers({
  109. shouldAdvanceTime: true,
  110. now: Date.now(),
  111. })
  112. // xhrMock.setup()
  113. })
  114. afterEach(function () {
  115. clock.runAll()
  116. clock.restore()
  117. // xhrMock.teardown()
  118. fetchMock.reset()
  119. localStorage.clear()
  120. })
  121. it('renders the PDF preview', function () {
  122. renderWithEditorContext(<PdfPreview />)
  123. screen.getByRole('button', { name: 'Recompile' })
  124. })
  125. it('runs a compile only on the first project:joined event', async function () {
  126. mockCompile()
  127. mockBuildFile()
  128. renderWithEditorContext(<PdfPreview />)
  129. // fire a project:joined event => compile
  130. screen.getByRole('button', { name: 'Recompile' })
  131. fireEvent(window, new CustomEvent('project:joined'))
  132. screen.getByRole('button', { name: 'Compiling…' })
  133. await screen.findByRole('button', { name: 'Recompile' })
  134. // fire another project:joined event => no compile
  135. fireEvent(window, new CustomEvent('project:joined'))
  136. screen.getByRole('button', { name: 'Recompile' })
  137. expect(fetchMock.calls()).to.have.length(3)
  138. expect(fetchMock.called('express:/project/:projectId/compile')).to.be.true // TODO: auto_compile query param
  139. expect(fetchMock.called('express:/build/:file')).to.be.true // TODO: actual path
  140. })
  141. it('runs a compile when the Recompile button is pressed', async function () {
  142. mockCompile()
  143. mockBuildFile()
  144. renderWithEditorContext(<PdfPreview />)
  145. // fire a project:joined event => compile
  146. screen.getByRole('button', { name: 'Recompile' })
  147. fireEvent(window, new CustomEvent('project:joined'))
  148. screen.getByRole('button', { name: 'Compiling…' })
  149. await screen.findByRole('button', { name: 'Recompile' })
  150. // press the Recompile button => compile
  151. const button = screen.getByRole('button', { name: 'Recompile' })
  152. button.click()
  153. screen.getByRole('button', { name: 'Compiling…' })
  154. await screen.findByRole('button', { name: 'Recompile' })
  155. expect(fetchMock.calls()).to.have.length(6)
  156. })
  157. it('runs a compile on doc change if autocompile is enabled', async function () {
  158. mockCompile()
  159. mockBuildFile()
  160. renderWithEditorContext(<PdfPreview />)
  161. // fire a project:joined event => compile
  162. screen.getByRole('button', { name: 'Recompile' })
  163. fireEvent(window, new CustomEvent('project:joined'))
  164. screen.getByRole('button', { name: 'Compiling…' })
  165. await screen.findByRole('button', { name: 'Recompile' })
  166. // switch on auto compile
  167. storeAndFireEvent('autocompile_enabled:project123', true)
  168. // fire a doc:changed event => compile
  169. fireEvent(window, new CustomEvent('doc:changed'))
  170. clock.tick(2000) // AUTO_COMPILE_DEBOUNCE
  171. await screen.findByText('Compiling…')
  172. await screen.findByRole('button', { name: 'Recompile' })
  173. expect(fetchMock.calls()).to.have.length(6)
  174. })
  175. it('does not run a compile on doc change if autocompile is disabled', async function () {
  176. mockCompile()
  177. mockBuildFile()
  178. renderWithEditorContext(<PdfPreview />)
  179. // fire a project:joined event => compile
  180. screen.getByRole('button', { name: 'Recompile' })
  181. fireEvent(window, new CustomEvent('project:joined'))
  182. screen.getByRole('button', { name: 'Compiling…' })
  183. await screen.findByRole('button', { name: 'Recompile' })
  184. // make sure auto compile is switched off
  185. storeAndFireEvent('autocompile_enabled:project123', false)
  186. screen.getByRole('button', { name: 'Recompile' })
  187. // fire a doc:changed event => no compile
  188. fireEvent(window, new CustomEvent('doc:changed'))
  189. clock.tick(2000) // AUTO_COMPILE_DEBOUNCE
  190. screen.getByRole('button', { name: 'Recompile' })
  191. expect(fetchMock.calls()).to.have.length(3)
  192. })
  193. it('does not run a compile on doc change if autocompile is blocked by syntax check', async function () {
  194. mockCompile()
  195. mockBuildFile()
  196. renderWithEditorContext(<PdfPreview />, {
  197. scope: {
  198. 'settings.syntaxValidation': true, // enable linting in the editor
  199. hasLintingError: true, // mock a linting error
  200. },
  201. })
  202. // fire a project:joined event => compile
  203. screen.getByRole('button', { name: 'Recompile' })
  204. fireEvent(window, new CustomEvent('project:joined'))
  205. screen.getByRole('button', { name: 'Compiling…' })
  206. await screen.findByRole('button', { name: 'Recompile' })
  207. // switch on auto compile and syntax checking
  208. storeAndFireEvent('autocompile_enabled:project123', true)
  209. storeAndFireEvent('stop_on_validation_error:project123', true)
  210. screen.getByRole('button', { name: 'Recompile' })
  211. // fire a doc:changed event => no compile
  212. fireEvent(window, new CustomEvent('doc:changed'))
  213. clock.tick(2000) // AUTO_COMPILE_DEBOUNCE
  214. screen.getByRole('button', { name: 'Recompile' })
  215. await screen.findByText('Code check failed')
  216. expect(fetchMock.calls()).to.have.length(3)
  217. })
  218. it('displays an error message if there was a compile error', async function () {
  219. mockCompileError('compile-in-progress')
  220. renderWithEditorContext(<PdfPreview />)
  221. // fire a project:joined event => compile
  222. screen.getByRole('button', { name: 'Recompile' })
  223. fireEvent(window, new CustomEvent('project:joined'))
  224. screen.getByRole('button', { name: 'Compiling…' })
  225. await screen.findByRole('button', { name: 'Recompile' })
  226. screen.getByText(
  227. 'Please wait for your other compile to finish before trying again.'
  228. )
  229. expect(fetchMock.called('express:/project/:projectId/compile')).to.be.true // TODO: auto_compile query param
  230. expect(fetchMock.called('express:/build/:file')).to.be.false // TODO: actual path
  231. })
  232. it('disables the view logs button if there is a compile error', async function () {
  233. mockCompileError()
  234. mockBuildFile()
  235. renderWithEditorContext(<PdfPreview />)
  236. const logsButton = screen.getByRole('button', { name: 'View logs' })
  237. expect(logsButton.hasAttribute('disabled')).to.be.false
  238. // fire a project:joined event => compile
  239. screen.getByRole('button', { name: 'Recompile' })
  240. fireEvent(window, new CustomEvent('project:joined'))
  241. screen.getByRole('button', { name: 'Compiling…' })
  242. expect(logsButton.hasAttribute('disabled')).to.be.false
  243. await screen.findByRole('button', { name: 'Recompile' })
  244. expect(logsButton.hasAttribute('disabled')).to.be.true
  245. expect(fetchMock.called('express:/project/:projectId/compile')).to.be.true // TODO: auto_compile query param
  246. expect(fetchMock.called('express:/build/:file')).to.be.false // TODO: actual path
  247. })
  248. it('displays error messages if there were validation problems', async function () {
  249. const validationProblems = {
  250. sizeCheck: {
  251. resources: [
  252. { path: 'foo/bar', kbSize: 76221 },
  253. { path: 'bar/baz', kbSize: 2342 },
  254. ],
  255. },
  256. mainFile: true,
  257. conflictedPaths: [
  258. {
  259. path: 'foo/bar',
  260. },
  261. {
  262. path: 'foo/baz',
  263. },
  264. ],
  265. }
  266. mockValidationProblems(validationProblems)
  267. renderWithEditorContext(<PdfPreview />)
  268. // fire a project:joined event => compile
  269. screen.getByRole('button', { name: 'Recompile' })
  270. fireEvent(window, new CustomEvent('project:joined'))
  271. screen.getByRole('button', { name: 'Compiling…' })
  272. await screen.findByRole('button', { name: 'Recompile' })
  273. screen.getByText('Project too large')
  274. screen.getByText('Unknown main document')
  275. screen.getByText('Conflicting Paths Found')
  276. expect(fetchMock.called('express:/project/:projectId/compile')).to.be.true // TODO: auto_compile query param
  277. expect(fetchMock.called('express:/build/:file')).to.be.false // TODO: actual path
  278. })
  279. it('sends a clear cache request when the button is pressed', async function () {
  280. mockCompile()
  281. mockBuildFile()
  282. mockClearCache()
  283. renderWithEditorContext(<PdfPreview />)
  284. const logsButton = screen.getByRole('button', { name: 'View logs' })
  285. logsButton.click()
  286. let clearCacheButton = screen.getByRole('button', {
  287. name: 'Clear cached files',
  288. })
  289. expect(clearCacheButton.hasAttribute('disabled')).to.be.false
  290. // fire a project:joined event => compile
  291. screen.getByRole('button', { name: 'Recompile' })
  292. fireEvent(window, new CustomEvent('project:joined'))
  293. screen.getByRole('button', { name: 'Compiling…' })
  294. expect(clearCacheButton.hasAttribute('disabled')).to.be.true
  295. await screen.findByRole('button', { name: 'Recompile' })
  296. logsButton.click()
  297. clearCacheButton = await screen.findByRole('button', {
  298. name: 'Clear cached files',
  299. })
  300. expect(clearCacheButton.hasAttribute('disabled')).to.be.false
  301. // click the button
  302. clearCacheButton.click()
  303. expect(clearCacheButton.hasAttribute('disabled')).to.be.true
  304. await waitFor(() => {
  305. expect(clearCacheButton.hasAttribute('disabled')).to.be.false
  306. })
  307. expect(fetchMock.called('express:/project/:projectId/compile')).to.be.true // TODO: auto_compile query param
  308. expect(fetchMock.called('express:/build/:file')).to.be.true // TODO: actual path
  309. })
  310. })