pdf-synctex-controls.test.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import PdfSynctexControls from '../../../../../frontend/js/features/pdf-preview/components/pdf-synctex-controls'
  2. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  3. import sysendTestHelper from '../../../helpers/sysend'
  4. import { cloneDeep } from 'lodash'
  5. import fetchMock from 'fetch-mock'
  6. import { fireEvent, screen, waitFor } from '@testing-library/react'
  7. import fs from 'fs'
  8. import path from 'path'
  9. import { expect } from 'chai'
  10. import { useCompileContext } from '../../../../../frontend/js/shared/context/compile-context'
  11. import { useEffect } from 'react'
  12. const examplePDF = path.join(__dirname, '../fixtures/test-example.pdf')
  13. const scope = {
  14. settings: {
  15. syntaxValidation: false,
  16. pdfViewer: 'pdfjs',
  17. },
  18. editor: {
  19. sharejs_doc: {
  20. doc_id: 'test-doc',
  21. getSnapshot: () => 'some doc content',
  22. },
  23. },
  24. }
  25. const outputFiles = [
  26. {
  27. path: 'output.pdf',
  28. build: '123',
  29. url: '/build/output.pdf',
  30. type: 'pdf',
  31. },
  32. {
  33. path: 'output.log',
  34. build: '123',
  35. url: '/build/output.log',
  36. type: 'log',
  37. },
  38. ]
  39. const mockCompile = () =>
  40. fetchMock.post('express:/project/:projectId/compile', {
  41. body: {
  42. status: 'success',
  43. clsiServerId: 'foo',
  44. compileGroup: 'standard',
  45. pdfDownloadDomain: 'https://clsi.test-overleaf.com',
  46. outputFiles: cloneDeep(outputFiles),
  47. },
  48. })
  49. const fileResponses = {
  50. '/build/output.pdf': () => fs.createReadStream(examplePDF),
  51. '/build/output.log': '',
  52. }
  53. const mockBuildFile = () =>
  54. fetchMock.get('begin:https://clsi.test-overleaf.com/', _url => {
  55. const url = new URL(_url, 'https://clsi.test-overleaf.com')
  56. if (url.pathname in fileResponses) {
  57. return fileResponses[url.pathname]
  58. }
  59. return 404
  60. })
  61. const mockHighlights = [
  62. {
  63. page: 1,
  64. h: 85.03936,
  65. v: 509.999878,
  66. width: 441.921265,
  67. height: 8.855677,
  68. },
  69. {
  70. page: 1,
  71. h: 85.03936,
  72. v: 486.089539,
  73. width: 441.921265,
  74. height: 8.855677,
  75. },
  76. ]
  77. const mockSynctex = () =>
  78. fetchMock
  79. .get('express:/project/:projectId/sync/code', () => {
  80. return { pdf: cloneDeep(mockHighlights) }
  81. })
  82. .get('express:/project/:projectId/sync/pdf', () => {
  83. return { code: [{ file: 'main.tex', line: 100 }] }
  84. })
  85. describe('<PdfSynctexControls/>', function () {
  86. beforeEach(function () {
  87. window.showNewPdfPreview = true
  88. window.metaAttributesCache = new Map()
  89. fetchMock.restore()
  90. mockCompile()
  91. mockSynctex()
  92. mockBuildFile()
  93. })
  94. afterEach(function () {
  95. window.showNewPdfPreview = undefined
  96. window.metaAttributesCache = new Map()
  97. fetchMock.restore()
  98. })
  99. it('handles clicks on sync buttons', async function () {
  100. const Inner = () => {
  101. const { setPosition } = useCompileContext()
  102. // mock PDF scroll position update
  103. useEffect(() => {
  104. setPosition({
  105. page: 1,
  106. offset: { top: 10, left: 10 },
  107. pageSize: { height: 500, width: 500 },
  108. })
  109. }, [setPosition])
  110. return null
  111. }
  112. renderWithEditorContext(
  113. <>
  114. <Inner />
  115. <PdfSynctexControls />
  116. </>,
  117. { scope }
  118. )
  119. const syncToPdfButton = await screen.findByRole('button', {
  120. name: 'Go to code location in PDF',
  121. })
  122. const syncToCodeButton = await screen.findByRole('button', {
  123. name: 'Go to PDF location in code',
  124. })
  125. // mock editor cursor position update
  126. fireEvent(
  127. window,
  128. new CustomEvent('cursor:editor:update', {
  129. detail: { row: 100, column: 10 },
  130. })
  131. )
  132. fireEvent.click(syncToPdfButton)
  133. expect(syncToPdfButton.disabled).to.be.true
  134. await waitFor(() => {
  135. expect(fetchMock.called('express:/project/:projectId/sync/code')).to.be
  136. .true
  137. })
  138. fireEvent.click(syncToCodeButton)
  139. expect(syncToCodeButton.disabled).to.be.true
  140. await waitFor(() => {
  141. expect(fetchMock.called('express:/project/:projectId/sync/pdf')).to.be
  142. .true
  143. })
  144. })
  145. describe('with detacher role', async function () {
  146. beforeEach(function () {
  147. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  148. })
  149. it('does not have go to PDF location button', async function () {
  150. renderWithEditorContext(<PdfSynctexControls />, { scope })
  151. expect(
  152. await screen.queryByRole('button', {
  153. name: 'Go to PDF location in code',
  154. })
  155. ).to.not.exist
  156. })
  157. it('send go to PDF location action', async function () {
  158. renderWithEditorContext(<PdfSynctexControls />, { scope })
  159. sysendTestHelper.resetHistory()
  160. const syncToPdfButton = await screen.findByRole('button', {
  161. name: 'Go to code location in PDF',
  162. })
  163. // mock editor cursor position update
  164. fireEvent(
  165. window,
  166. new CustomEvent('cursor:editor:update', {
  167. detail: { row: 100, column: 10 },
  168. })
  169. )
  170. fireEvent.click(syncToPdfButton)
  171. // the button is only disabled when the state is updated via sysend
  172. expect(syncToPdfButton.disabled).to.be.false
  173. expect(sysendTestHelper.getLastBroacastMessage()).to.deep.equal({
  174. role: 'detacher',
  175. event: 'action-go-to-pdf-location',
  176. data: { args: ['file=&line=101&column=10'] },
  177. })
  178. })
  179. it('update inflight state', async function () {
  180. renderWithEditorContext(<PdfSynctexControls />, { scope })
  181. sysendTestHelper.resetHistory()
  182. const syncToPdfButton = await screen.findByRole('button', {
  183. name: 'Go to code location in PDF',
  184. })
  185. // mock editor cursor position update
  186. fireEvent(
  187. window,
  188. new CustomEvent('cursor:editor:update', {
  189. detail: { row: 100, column: 10 },
  190. })
  191. )
  192. sysendTestHelper.receiveMessage({
  193. role: 'detached',
  194. event: 'state-sync-to-pdf-inflight',
  195. data: { value: true },
  196. })
  197. expect(syncToPdfButton.disabled).to.be.true
  198. sysendTestHelper.receiveMessage({
  199. role: 'detached',
  200. event: 'state-sync-to-pdf-inflight',
  201. data: { value: false },
  202. })
  203. expect(syncToPdfButton.disabled).to.be.false
  204. })
  205. })
  206. describe('with detached role', async function () {
  207. beforeEach(function () {
  208. window.metaAttributesCache.set('ol-detachRole', 'detached')
  209. })
  210. it('does not have go to code location button', async function () {
  211. renderWithEditorContext(<PdfSynctexControls />, { scope })
  212. expect(
  213. await screen.queryByRole('button', {
  214. name: 'Go to code location in PDF',
  215. })
  216. ).to.not.exist
  217. })
  218. it('send go to code line action and update inflight state', async function () {
  219. renderWithEditorContext(<PdfSynctexControls />, { scope })
  220. sysendTestHelper.resetHistory()
  221. const syncToCodeButton = await screen.findByRole('button', {
  222. name: 'Go to PDF location in code',
  223. })
  224. sysendTestHelper.resetHistory()
  225. fireEvent.click(syncToCodeButton)
  226. expect(syncToCodeButton.disabled).to.be.true
  227. await waitFor(() => {
  228. expect(fetchMock.called('express:/project/:projectId/sync/pdf')).to.be
  229. .true
  230. })
  231. expect(sysendTestHelper.getLastBroacastMessage()).to.deep.equal({
  232. role: 'detached',
  233. event: 'action-go-to-code-line',
  234. data: { args: ['main.tex', 100] },
  235. })
  236. })
  237. it('sends PDF exists state', async function () {
  238. renderWithEditorContext(<PdfSynctexControls />, { scope })
  239. sysendTestHelper.resetHistory()
  240. await waitFor(() => {
  241. expect(fetchMock.called('express:/project/:projectId/compile')).to.be
  242. .true
  243. })
  244. expect(sysendTestHelper.getLastBroacastMessage()).to.deep.equal({
  245. role: 'detached',
  246. event: 'state-pdf-exists',
  247. data: { value: true },
  248. })
  249. })
  250. it('reacts to go to PDF location action', async function () {
  251. renderWithEditorContext(<PdfSynctexControls />, { scope })
  252. sysendTestHelper.resetHistory()
  253. await waitFor(() => {
  254. expect(fetchMock.called('express:/project/:projectId/compile')).to.be
  255. .true
  256. })
  257. sysendTestHelper.spy.broadcast.resetHistory()
  258. sysendTestHelper.receiveMessage({
  259. role: 'detacher',
  260. event: 'action-go-to-pdf-location',
  261. data: { args: ['file=&line=101&column=10'] },
  262. })
  263. await waitFor(() => {
  264. expect(fetchMock.called('express:/project/:projectId/sync/code')).to.be
  265. .true
  266. })
  267. expect(sysendTestHelper.getLastBroacastMessage()).to.deep.equal({
  268. role: 'detached',
  269. event: 'state-sync-to-pdf-inflight',
  270. data: { value: false },
  271. })
  272. })
  273. })
  274. })