document-diff-viewer.spec.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import DocumentDiffViewer from '../../../../../frontend/js/features/history/components/diff-view/document-diff-viewer'
  2. import { Highlight } from '../../../../../frontend/js/features/history/services/types/doc'
  3. import { FC } from 'react'
  4. import { EditorProviders } from '../../../helpers/editor-providers'
  5. const doc = `\\documentclass{article}
  6. % Language setting
  7. % Replace \`english' with e.g. \`spanish' to change the document language
  8. \\usepackage[english]{babel}
  9. % Set page size and margins
  10. % Replace \`letterpaper' with \`a4paper' for UK/EU standard size
  11. \\usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
  12. % Useful packages
  13. \\usepackage{amsmath}
  14. \\usepackage{graphicx}
  15. \\usepackage[colorlinks=true, allcolors=blue]{hyperref}
  16. \\title{Your Paper}
  17. \\author{You}
  18. \\begin{document}
  19. \\maketitle
  20. \\begin{abstract}
  21. Your abstract.
  22. \\end{abstract}
  23. \\section{Introduction}
  24. Your introduction goes here! Simply start writing your document and use the Recompile button to view the updated PDF preview. Examples of commonly used commands and features are listed below, to help you get started.
  25. Once you're familiar with the editor, you can find various project settings in the Overleaf menu, accessed via the button in the very top left of the editor. To view tutorials, user guides, and further documentation, please visit our \\href{https://www.overleaf.com/learn}{help library}, or head to our plans page to \\href{https://www.overleaf.com/user/subscription/plans}{choose your plan}.
  26. ${'\n'.repeat(200)}
  27. \\end{document}`
  28. const highlights: Highlight[] = [
  29. {
  30. type: 'addition',
  31. range: { from: 15, to: 22 },
  32. hue: 200,
  33. label: 'Added by Wombat on Monday',
  34. },
  35. {
  36. type: 'deletion',
  37. range: { from: 27, to: 35 },
  38. hue: 200,
  39. label: 'Deleted by Wombat on Tuesday',
  40. },
  41. {
  42. type: 'addition',
  43. range: { from: doc.length - 9, to: doc.length - 1 },
  44. hue: 200,
  45. label: 'Added by Wombat on Wednesday',
  46. },
  47. ]
  48. const Container: FC = ({ children }) => (
  49. <div style={{ width: 600, height: 400 }}>{children}</div>
  50. )
  51. const mockScope = () => {
  52. return {
  53. settings: {
  54. fontSize: 12,
  55. fontFamily: 'monaco',
  56. lineHeight: 'normal',
  57. overallTheme: '',
  58. },
  59. }
  60. }
  61. describe('document diff viewer', function () {
  62. it('displays highlights with hover tooltips', function () {
  63. const scope = mockScope()
  64. cy.mount(
  65. <Container>
  66. <EditorProviders scope={scope}>
  67. <DocumentDiffViewer doc={doc} highlights={highlights} />
  68. </EditorProviders>
  69. </Container>
  70. )
  71. cy.get('.ol-cm-addition-marker').should('have.length', 1)
  72. cy.get('.ol-cm-addition-marker').first().as('addition')
  73. cy.get('@addition').should('have.text', 'article')
  74. cy.get('.ol-cm-deletion-marker').should('have.length', 1)
  75. cy.get('.ol-cm-deletion-marker').first().as('deletion')
  76. cy.get('@deletion').should('have.text', 'Language')
  77. // Check hover tooltips
  78. cy.get('@addition').trigger('mouseover')
  79. cy.get('.ol-cm-highlight-tooltip').should('have.length', 1)
  80. cy.get('.ol-cm-highlight-tooltip')
  81. .first()
  82. .should('have.text', 'Added by Wombat on Monday')
  83. cy.get('@deletion').trigger('mouseover')
  84. cy.get('.ol-cm-highlight-tooltip').should('have.length', 1)
  85. cy.get('.ol-cm-highlight-tooltip')
  86. .first()
  87. .should('have.text', 'Deleted by Wombat on Tuesday')
  88. })
  89. it('displays highlights with hover tooltips for empty lines', function () {
  90. const scope = mockScope()
  91. const doc = `1
  92. Addition
  93. End
  94. 2
  95. Deletion
  96. End
  97. 3`
  98. const highlights: Highlight[] = [
  99. {
  100. type: 'addition',
  101. range: { from: 2, to: 16 },
  102. hue: 200,
  103. label: 'Added by Wombat on Monday',
  104. },
  105. {
  106. type: 'deletion',
  107. range: { from: 19, to: 32 },
  108. hue: 200,
  109. label: 'Deleted by Wombat on Tuesday',
  110. },
  111. ]
  112. cy.mount(
  113. <Container>
  114. <EditorProviders scope={scope}>
  115. <DocumentDiffViewer doc={doc} highlights={highlights} />
  116. </EditorProviders>
  117. </Container>
  118. )
  119. cy.get('.ol-cm-empty-line-addition-marker').should('have.length', 2)
  120. cy.get('.ol-cm-empty-line-deletion-marker').should('have.length', 1)
  121. // For an empty line marker, we need to trigger mouseover on the containing
  122. // line beause the marker itself does not trigger mouseover
  123. cy.get('.ol-cm-empty-line-addition-marker')
  124. .first()
  125. .parent()
  126. .as('firstAdditionLine')
  127. cy.get('.ol-cm-empty-line-addition-marker')
  128. .first()
  129. .parent()
  130. .as('lastAdditionLine')
  131. cy.get('.ol-cm-empty-line-deletion-marker')
  132. .last()
  133. .parent()
  134. .as('deletionLine')
  135. // Check hover tooltips
  136. cy.get('@lastAdditionLine').trigger('mouseover')
  137. cy.get('.ol-cm-highlight-tooltip').should('have.length', 1)
  138. cy.get('.ol-cm-highlight-tooltip')
  139. .first()
  140. .should('have.text', 'Added by Wombat on Monday')
  141. cy.get('@lastAdditionLine').trigger('mouseleave')
  142. cy.get('@firstAdditionLine').trigger('mouseover')
  143. cy.get('.ol-cm-highlight-tooltip').should('have.length', 1)
  144. cy.get('.ol-cm-highlight-tooltip')
  145. .first()
  146. .should('have.text', 'Added by Wombat on Monday')
  147. cy.get('@deletionLine').trigger('mouseover')
  148. cy.get('.ol-cm-highlight-tooltip').should('have.length', 1)
  149. cy.get('.ol-cm-highlight-tooltip')
  150. .first()
  151. .should('have.text', 'Deleted by Wombat on Tuesday')
  152. })
  153. it("renders 'More updates' buttons", function () {
  154. const scope = mockScope()
  155. cy.mount(
  156. <Container>
  157. <EditorProviders scope={scope}>
  158. <DocumentDiffViewer doc={doc} highlights={highlights} />
  159. </EditorProviders>
  160. </Container>
  161. )
  162. cy.get('.cm-scroller').first().as('scroller')
  163. // Check the initial state, which should be a "More updates below" button
  164. // but no "More updates above", with the editor scrolled to the top
  165. cy.get('.ol-cm-addition-marker').should('have.length', 1)
  166. cy.get('.ol-cm-deletion-marker').should('have.length', 1)
  167. cy.get('.previous-highlight-button').should('have.length', 0)
  168. cy.get('.next-highlight-button').should('have.length', 1)
  169. cy.get('@scroller').invoke('scrollTop').should('equal', 0)
  170. // Click the "More updates below" button, which should scroll the editor,
  171. // and check the new state
  172. cy.get('.next-highlight-button').first().click()
  173. cy.get('@scroller').invoke('scrollTop').should('not.equal', 0)
  174. cy.get('.previous-highlight-button').should('have.length', 1)
  175. cy.get('.next-highlight-button').should('have.length', 0)
  176. // Click the "More updates above" button, which should scroll the editor up
  177. // but not quite to the top, and check the new state
  178. cy.get('.previous-highlight-button').first().click()
  179. cy.get('@scroller').invoke('scrollTop').should('equal', 0)
  180. cy.get('.previous-highlight-button').should('not.exist')
  181. cy.get('.next-highlight-button').should('have.length', 1)
  182. })
  183. it('scrolls to first change', function () {
  184. const scope = mockScope()
  185. const finalHighlightOnly = highlights.slice(-1)
  186. cy.mount(
  187. <Container>
  188. <EditorProviders scope={scope}>
  189. <DocumentDiffViewer doc={doc} highlights={finalHighlightOnly} />
  190. </EditorProviders>
  191. </Container>
  192. )
  193. cy.get('.cm-scroller').first().invoke('scrollTop').should('not.equal', 0)
  194. cy.get('.ol-cm-addition-marker')
  195. .first()
  196. .then($marker => {
  197. cy.get('.cm-content')
  198. .first()
  199. .then($content => {
  200. const contentRect = $content[0].getBoundingClientRect()
  201. const markerRect = $marker[0].getBoundingClientRect()
  202. expect(markerRect.top).to.be.within(
  203. contentRect.top,
  204. contentRect.bottom
  205. )
  206. expect(markerRect.bottom).to.be.within(
  207. contentRect.top,
  208. contentRect.bottom
  209. )
  210. })
  211. })
  212. })
  213. })