pdf-preview.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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: '/build/output.pdf', // 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. const scope = {
  106. settings: {
  107. syntaxValidation: false,
  108. },
  109. editor: {
  110. sharejs_doc: {
  111. doc_id: 'test-doc',
  112. getSnapshot: () => 'some doc content',
  113. },
  114. },
  115. }
  116. describe('<PdfPreview/>', function () {
  117. var clock
  118. beforeEach(function () {
  119. clock = sinon.useFakeTimers({
  120. shouldAdvanceTime: true,
  121. now: Date.now(),
  122. })
  123. // xhrMock.setup()
  124. })
  125. afterEach(function () {
  126. clock.runAll()
  127. clock.restore()
  128. // xhrMock.teardown()
  129. fetchMock.reset()
  130. localStorage.clear()
  131. })
  132. it('renders the PDF preview', async function () {
  133. mockCompile()
  134. mockBuildFile()
  135. renderWithEditorContext(<PdfPreview />, { scope })
  136. // wait for "compile on load" to finish
  137. await screen.findByRole('button', { name: 'Compiling…' })
  138. await screen.findByRole('button', { name: 'Recompile' })
  139. })
  140. it('runs a compile when the Recompile button is pressed', async function () {
  141. mockCompile()
  142. mockBuildFile()
  143. renderWithEditorContext(<PdfPreview />, { scope })
  144. // wait for "compile on load" to finish
  145. await screen.findByRole('button', { name: 'Compiling…' })
  146. await screen.findByRole('button', { name: 'Recompile' })
  147. // press the Recompile button => compile
  148. const button = screen.getByRole('button', { name: 'Recompile' })
  149. button.click()
  150. await screen.findByRole('button', { name: 'Compiling…' })
  151. await screen.findByRole('button', { name: 'Recompile' })
  152. expect(fetchMock.calls()).to.have.length(6)
  153. })
  154. it('runs a compile on doc change if autocompile is enabled', async function () {
  155. mockCompile()
  156. mockBuildFile()
  157. renderWithEditorContext(<PdfPreview />, { scope })
  158. // wait for "compile on load" to finish
  159. await screen.findByRole('button', { name: 'Compiling…' })
  160. await screen.findByRole('button', { name: 'Recompile' })
  161. // switch on auto compile
  162. storeAndFireEvent('autocompile_enabled:project123', true)
  163. // fire a doc:changed event => compile
  164. fireEvent(window, new CustomEvent('doc:changed'))
  165. clock.tick(2000) // AUTO_COMPILE_DEBOUNCE
  166. await screen.findByRole('button', { name: 'Compiling…' })
  167. await screen.findByRole('button', { name: 'Recompile' })
  168. expect(fetchMock.calls()).to.have.length(6)
  169. })
  170. it('does not run a compile on doc change if autocompile is disabled', async function () {
  171. mockCompile()
  172. mockBuildFile()
  173. renderWithEditorContext(<PdfPreview />, { scope })
  174. // wait for "compile on load" to finish
  175. await screen.findByRole('button', { name: 'Compiling…' })
  176. await screen.findByRole('button', { name: 'Recompile' })
  177. // make sure auto compile is switched off
  178. storeAndFireEvent('autocompile_enabled:project123', false)
  179. // fire a doc:changed event => no compile
  180. fireEvent(window, new CustomEvent('doc:changed'))
  181. clock.tick(2000) // AUTO_COMPILE_DEBOUNCE
  182. screen.getByRole('button', { name: 'Recompile' })
  183. expect(fetchMock.calls()).to.have.length(3)
  184. })
  185. it('does not run a compile on doc change if autocompile is blocked by syntax check', async function () {
  186. mockCompile()
  187. mockBuildFile()
  188. renderWithEditorContext(<PdfPreview />, {
  189. scope: {
  190. ...scope,
  191. 'settings.syntaxValidation': true, // enable linting in the editor
  192. hasLintingError: true, // mock a linting error
  193. },
  194. })
  195. // wait for "compile on load" to finish
  196. await screen.findByRole('button', { name: 'Compiling…' })
  197. await screen.findByRole('button', { name: 'Recompile' })
  198. // switch on auto compile and syntax checking
  199. storeAndFireEvent('autocompile_enabled:project123', true)
  200. storeAndFireEvent('stop_on_validation_error:project123', true)
  201. // fire a doc:changed event => no compile
  202. fireEvent(window, new CustomEvent('doc:changed'))
  203. clock.tick(2000) // AUTO_COMPILE_DEBOUNCE
  204. screen.getByRole('button', { name: 'Recompile' })
  205. await screen.findByText('Code check failed')
  206. expect(fetchMock.calls()).to.have.length(3)
  207. })
  208. it('displays an error message if there was a compile error', async function () {
  209. mockCompileError('compile-in-progress')
  210. renderWithEditorContext(<PdfPreview />, { scope })
  211. // wait for "compile on load" to finish
  212. await screen.findByRole('button', { name: 'Compiling…' })
  213. await screen.findByRole('button', { name: 'Recompile' })
  214. screen.getByText(
  215. 'Please wait for your other compile to finish before trying again.'
  216. )
  217. expect(fetchMock.called('express:/project/:projectId/compile')).to.be.true // TODO: auto_compile query param
  218. expect(fetchMock.called('express:/build/:file')).to.be.false // TODO: actual path
  219. })
  220. it('displays error messages if there were validation problems', async function () {
  221. const validationProblems = {
  222. sizeCheck: {
  223. resources: [
  224. { path: 'foo/bar', kbSize: 76221 },
  225. { path: 'bar/baz', kbSize: 2342 },
  226. ],
  227. },
  228. mainFile: true,
  229. conflictedPaths: [
  230. {
  231. path: 'foo/bar',
  232. },
  233. {
  234. path: 'foo/baz',
  235. },
  236. ],
  237. }
  238. mockValidationProblems(validationProblems)
  239. renderWithEditorContext(<PdfPreview />, { scope })
  240. // wait for "compile on load" to finish
  241. await screen.findByRole('button', { name: 'Compiling…' })
  242. await screen.findByRole('button', { name: 'Recompile' })
  243. screen.getByText('Project too large')
  244. screen.getByText('Unknown main document')
  245. screen.getByText('Conflicting Paths Found')
  246. expect(fetchMock.called('express:/project/:projectId/compile')).to.be.true // TODO: auto_compile query param
  247. expect(fetchMock.called('express:/build/:file')).to.be.false // TODO: actual path
  248. })
  249. it('sends a clear cache request when the button is pressed', async function () {
  250. mockCompile()
  251. mockBuildFile()
  252. mockClearCache()
  253. renderWithEditorContext(<PdfPreview />, { scope })
  254. // wait for "compile on load" to finish
  255. await screen.findByRole('button', { name: 'Compiling…' })
  256. await screen.findByRole('button', { name: 'Recompile' })
  257. const logsButton = screen.getByRole('button', {
  258. name: 'View logs',
  259. })
  260. logsButton.click()
  261. const clearCacheButton = await screen.findByRole('button', {
  262. name: 'Clear cached files',
  263. })
  264. expect(clearCacheButton.hasAttribute('disabled')).to.be.false
  265. // click the button
  266. clearCacheButton.click()
  267. expect(clearCacheButton.hasAttribute('disabled')).to.be.true
  268. await waitFor(() => {
  269. expect(clearCacheButton.hasAttribute('disabled')).to.be.false
  270. })
  271. expect(fetchMock.called('express:/project/:projectId/compile')).to.be.true // TODO: auto_compile query param
  272. expect(fetchMock.called('express:/build/:file')).to.be.true // TODO: actual path
  273. })
  274. it('handle "recompile from scratch"', async function () {
  275. mockCompile()
  276. mockBuildFile()
  277. mockClearCache()
  278. renderWithEditorContext(<PdfPreview />, { scope })
  279. // wait for "compile on load" to finish
  280. await screen.findByRole('button', { name: 'Compiling…' })
  281. await screen.findByRole('button', { name: 'Recompile' })
  282. // show the logs UI
  283. const logsButton = screen.getByRole('button', {
  284. name: 'View logs',
  285. })
  286. logsButton.click()
  287. const clearCacheButton = await screen.findByRole('button', {
  288. name: 'Clear cached files',
  289. })
  290. expect(clearCacheButton.hasAttribute('disabled')).to.be.false
  291. const recompileFromScratch = screen.getByRole('menuitem', {
  292. name: 'Recompile from scratch',
  293. hidden: true,
  294. })
  295. recompileFromScratch.click()
  296. expect(clearCacheButton.hasAttribute('disabled')).to.be.true
  297. // wait for compile to finish
  298. await screen.findByRole('button', { name: 'Compiling…' })
  299. await screen.findByRole('button', { name: 'Recompile' })
  300. expect(fetchMock.called('express:/project/:projectId/compile')).to.be.true // TODO: auto_compile query param
  301. expect(fetchMock.called('express:/project/:projectId/output')).to.be.true
  302. expect(fetchMock.called('express:/build/:file')).to.be.true // TODO: actual path
  303. })
  304. })