pdf-synctex-controls.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import '../../helpers/bootstrap-3'
  2. import PdfSynctexControls from '../../../../frontend/js/features/pdf-preview/components/pdf-synctex-controls'
  3. import { cloneDeep } from 'lodash'
  4. import { useDetachCompileContext as useCompileContext } from '../../../../frontend/js/shared/context/detach-compile-context'
  5. import { useFileTreeData } from '../../../../frontend/js/shared/context/file-tree-data-context'
  6. import { useEffect } from 'react'
  7. import { EditorProviders } from '../../helpers/editor-providers'
  8. import { mockScope } from './scope'
  9. import { detachChannel, testDetachChannel } from '../../helpers/detach-channel'
  10. import { FindResult } from '@/features/file-tree/util/path'
  11. const mockHighlights = [
  12. {
  13. page: 1,
  14. h: 85.03936,
  15. v: 509.999878,
  16. width: 441.921265,
  17. height: 8.855677,
  18. },
  19. {
  20. page: 1,
  21. h: 85.03936,
  22. v: 486.089539,
  23. width: 441.921265,
  24. height: 8.855677,
  25. },
  26. ]
  27. type Position = {
  28. page: number
  29. offset: { top: number; left: number }
  30. pageSize: { height: number; width: number }
  31. }
  32. const mockPosition: Position = {
  33. page: 1,
  34. offset: { top: 10, left: 10 },
  35. pageSize: { height: 500, width: 500 },
  36. }
  37. const mockSelectedEntities = [{ type: 'doc' }] as FindResult[]
  38. const WithPosition = ({ mockPosition }: { mockPosition: Position }) => {
  39. const { setPosition } = useCompileContext()
  40. // mock PDF scroll position update
  41. useEffect(() => {
  42. setPosition(mockPosition)
  43. }, [mockPosition, setPosition])
  44. return null
  45. }
  46. // mock PDF scroll position update
  47. const setDetachedPosition = (mockPosition: Position) => {
  48. testDetachChannel.postMessage({
  49. role: 'detacher',
  50. event: 'state-position',
  51. data: { value: mockPosition },
  52. })
  53. }
  54. const WithSelectedEntities = ({
  55. mockSelectedEntities = [],
  56. }: {
  57. mockSelectedEntities: FindResult[]
  58. }) => {
  59. const { setSelectedEntities } = useFileTreeData()
  60. useEffect(() => {
  61. setSelectedEntities(mockSelectedEntities)
  62. }, [mockSelectedEntities, setSelectedEntities])
  63. return null
  64. }
  65. describe('<PdfSynctexControls/>', function () {
  66. beforeEach(function () {
  67. window.metaAttributesCache.set('ol-project_id', 'test-project')
  68. window.metaAttributesCache.set('ol-preventCompileOnLoad', false)
  69. cy.interceptEvents()
  70. })
  71. it('handles clicks on sync buttons', function () {
  72. cy.interceptCompile()
  73. const scope = mockScope()
  74. cy.mount(
  75. <EditorProviders scope={scope}>
  76. <WithPosition mockPosition={mockPosition} />
  77. <WithSelectedEntities mockSelectedEntities={mockSelectedEntities} />
  78. <PdfSynctexControls />
  79. </EditorProviders>
  80. )
  81. cy.waitForCompile()
  82. cy.get('.synctex-control-icon').should('have.length', 2)
  83. // mock editor cursor position update
  84. cy.window().then(win => {
  85. win.dispatchEvent(
  86. new CustomEvent('cursor:editor:update', {
  87. detail: { row: 100, column: 10 },
  88. })
  89. )
  90. })
  91. cy.wrap(null).then(() => {
  92. setDetachedPosition(mockPosition)
  93. })
  94. cy.interceptAsync({ pathname: '/project/*/sync/code' }, 'sync-code').then(
  95. syncCodeResponse => {
  96. cy.findByRole('button', { name: 'Go to code location in PDF' }).click()
  97. cy.findByRole('button', { name: 'Go to code location in PDF' })
  98. .should('be.disabled')
  99. .then(() => {
  100. syncCodeResponse.resolve({
  101. body: { pdf: cloneDeep(mockHighlights) },
  102. })
  103. })
  104. cy.wait('@sync-code')
  105. }
  106. )
  107. cy.interceptAsync({ pathname: '/project/*/sync/pdf' }, 'sync-pdf').then(
  108. syncPdfResponse => {
  109. cy.findByRole('button', { name: /^Go to PDF location in code/ }).click()
  110. cy.findByRole('button', { name: /^Go to PDF location in code/ })
  111. .should('be.disabled')
  112. .then(() => {
  113. syncPdfResponse.resolve({
  114. body: { code: [{ file: 'main.tex', line: 100 }] },
  115. })
  116. })
  117. cy.wait('@sync-pdf')
  118. }
  119. )
  120. })
  121. it('disables button when multiple entities are selected', function () {
  122. cy.interceptCompile()
  123. const scope = mockScope()
  124. cy.mount(
  125. <EditorProviders scope={scope}>
  126. <WithPosition mockPosition={mockPosition} />
  127. <WithSelectedEntities
  128. mockSelectedEntities={
  129. [{ type: 'doc' }, { type: 'doc' }] as FindResult[]
  130. }
  131. />
  132. <PdfSynctexControls />
  133. </EditorProviders>
  134. )
  135. cy.waitForCompile()
  136. cy.findByRole('button', { name: 'Go to code location in PDF' }).should(
  137. 'be.disabled'
  138. )
  139. })
  140. it('disables button when a file is selected', function () {
  141. cy.interceptCompile()
  142. const scope = mockScope()
  143. cy.mount(
  144. <EditorProviders scope={scope}>
  145. <WithPosition mockPosition={mockPosition} />
  146. <WithSelectedEntities
  147. mockSelectedEntities={[{ type: 'fileRef' }] as FindResult[]}
  148. />
  149. <PdfSynctexControls />
  150. </EditorProviders>
  151. )
  152. cy.waitForCompile()
  153. cy.findByRole('button', { name: 'Go to code location in PDF' }).should(
  154. 'be.disabled'
  155. )
  156. })
  157. describe('with detacher role', function () {
  158. beforeEach(function () {
  159. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  160. })
  161. it('does not have go to PDF location button nor arrow icon', function () {
  162. cy.interceptCompile()
  163. const scope = mockScope()
  164. cy.mount(
  165. <EditorProviders scope={scope}>
  166. <WithPosition mockPosition={mockPosition} />
  167. <WithSelectedEntities mockSelectedEntities={mockSelectedEntities} />
  168. <PdfSynctexControls />
  169. </EditorProviders>
  170. )
  171. cy.waitForCompile()
  172. cy.findByRole('button', { name: /^Go to PDF location in code/ }).should(
  173. 'not.exist'
  174. )
  175. cy.get('.synctex-control-icon').should('not.exist')
  176. })
  177. it('send set highlights action', function () {
  178. cy.interceptCompile()
  179. const scope = mockScope()
  180. cy.mount(
  181. <EditorProviders scope={scope}>
  182. <WithPosition mockPosition={mockPosition} />
  183. <WithSelectedEntities mockSelectedEntities={mockSelectedEntities} />
  184. <PdfSynctexControls />
  185. </EditorProviders>
  186. )
  187. cy.waitForCompile()
  188. // mock editor cursor position update
  189. cy.window().then(win => {
  190. win.dispatchEvent(
  191. new CustomEvent('cursor:editor:update', {
  192. detail: { row: 100, column: 10 },
  193. })
  194. )
  195. })
  196. cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
  197. cy.interceptAsync({ pathname: '/project/*/sync/code' }, 'sync-code').then(
  198. syncCodeResponse => {
  199. cy.findByRole('button', {
  200. name: 'Go to code location in PDF',
  201. })
  202. .should('not.be.disabled')
  203. .click()
  204. cy.findByRole('button', {
  205. name: 'Go to code location in PDF',
  206. })
  207. .should('be.disabled')
  208. .then(() => {
  209. syncCodeResponse.resolve({
  210. body: { pdf: cloneDeep(mockHighlights) },
  211. })
  212. })
  213. cy.wait('@sync-code')
  214. }
  215. )
  216. cy.findByRole('button', {
  217. name: 'Go to code location in PDF',
  218. }).should('not.be.disabled')
  219. // synctex is called locally and the result are broadcast for the detached tab
  220. // NOTE: can't use `.to.deep.include({…})` as it doesn't match the nested array
  221. cy.get('@postDetachMessage').should('have.been.calledWith', {
  222. role: 'detacher',
  223. event: 'action-setHighlights',
  224. data: { args: [mockHighlights] },
  225. })
  226. })
  227. it('reacts to sync to code action', function () {
  228. cy.interceptCompile()
  229. const scope = mockScope()
  230. cy.mount(
  231. <EditorProviders scope={scope}>
  232. <WithPosition mockPosition={mockPosition} />
  233. <WithSelectedEntities mockSelectedEntities={mockSelectedEntities} />
  234. <PdfSynctexControls />
  235. </EditorProviders>
  236. )
  237. cy.waitForCompile()
  238. cy.interceptAsync({ pathname: '/project/*/sync/pdf' }, 'sync-pdf')
  239. .then(syncPdfResponse => {
  240. syncPdfResponse.resolve({
  241. body: { code: [{ file: 'main.tex', line: 100 }] },
  242. })
  243. testDetachChannel.postMessage({
  244. role: 'detached',
  245. event: 'action-sync-to-code',
  246. data: {
  247. args: [mockPosition],
  248. },
  249. })
  250. })
  251. .wait('@sync-pdf')
  252. })
  253. })
  254. describe('with detached role', function () {
  255. beforeEach(function () {
  256. window.metaAttributesCache.set('ol-detachRole', 'detached')
  257. })
  258. it('does not have go to code location button nor arrow icon', function () {
  259. cy.interceptCompile()
  260. const scope = mockScope()
  261. cy.mount(
  262. <EditorProviders scope={scope}>
  263. <WithPosition mockPosition={mockPosition} />
  264. <PdfSynctexControls />
  265. </EditorProviders>
  266. )
  267. cy.waitForCompile()
  268. cy.findByRole('button', {
  269. name: 'Go to code location in PDF',
  270. }).should('not.exist')
  271. cy.get('.synctex-control-icon').should('not.exist')
  272. })
  273. it('send go to code line action', function () {
  274. cy.interceptCompile()
  275. const scope = mockScope()
  276. cy.mount(
  277. <EditorProviders scope={scope}>
  278. <PdfSynctexControls />
  279. </EditorProviders>
  280. )
  281. cy.waitForCompile().then(() => {
  282. testDetachChannel.postMessage({
  283. role: 'detacher',
  284. event: `state-position`,
  285. data: { value: mockPosition },
  286. })
  287. })
  288. cy.findByRole('button', {
  289. name: /^Go to PDF location in code/,
  290. })
  291. cy.findByRole('button', { name: /^Go to PDF location in code/ }).should(
  292. 'not.be.disabled'
  293. )
  294. cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
  295. cy.findByRole('button', { name: /^Go to PDF location in code/ }).click()
  296. // the button is only disabled when the state is updated
  297. cy.findByRole('button', { name: /^Go to PDF location in code/ }).should(
  298. 'not.be.disabled'
  299. )
  300. cy.get('.synctex-spin-icon').should('not.exist')
  301. cy.get('@postDetachMessage').should('have.been.calledWith', {
  302. role: 'detached',
  303. event: 'action-sync-to-code',
  304. data: {
  305. args: [mockPosition, 72],
  306. },
  307. })
  308. })
  309. it('update inflight state', function () {
  310. cy.interceptCompile()
  311. const scope = mockScope()
  312. cy.mount(
  313. <EditorProviders scope={scope}>
  314. <WithPosition mockPosition={mockPosition} />
  315. <PdfSynctexControls />
  316. </EditorProviders>
  317. )
  318. cy.waitForCompile().then(() => {
  319. testDetachChannel.postMessage({
  320. role: 'detacher',
  321. event: `state-position`,
  322. data: { value: mockPosition },
  323. })
  324. })
  325. cy.findByRole('button', { name: /^Go to PDF location in code/ }).should(
  326. 'not.be.disabled'
  327. )
  328. cy.get('.synctex-spin-icon').should('not.exist')
  329. cy.wrap(null).then(() => {
  330. testDetachChannel.postMessage({
  331. role: 'detacher',
  332. event: 'state-sync-to-code-inflight',
  333. data: { value: true },
  334. })
  335. })
  336. cy.findByRole('button', { name: /^Go to PDF location in code/ }).should(
  337. 'be.disabled'
  338. )
  339. cy.get('.synctex-spin-icon').should('have.length', 1)
  340. cy.wrap(null).then(() => {
  341. testDetachChannel.postMessage({
  342. role: 'detacher',
  343. event: 'state-sync-to-code-inflight',
  344. data: { value: false },
  345. })
  346. })
  347. cy.findByRole('button', { name: /^Go to PDF location in code/ }).should(
  348. 'not.be.disabled'
  349. )
  350. cy.get('.synctex-spin-icon').should('not.exist')
  351. })
  352. })
  353. })