editor.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import {
  2. createNewFile,
  3. createProjectAndOpenInNewEditor,
  4. openProjectById,
  5. testNewFileUpload,
  6. } from './helpers/project'
  7. import { isExcludedBySharding, startWith } from './helpers/config'
  8. import { ensureUserExists, login } from './helpers/login'
  9. import { v4 as uuid } from 'uuid'
  10. import { beforeWithReRunOnTestRetry } from './helpers/beforeWithReRunOnTestRetry'
  11. import { prepareWaitForNextCompileSlot } from './helpers/compile'
  12. const USER = 'user@example.com'
  13. const COLLABORATOR = 'collaborator@example.com'
  14. describe('editor', function () {
  15. if (isExcludedBySharding('PRO_DEFAULT_1')) return
  16. startWith({ pro: true })
  17. ensureUserExists({ email: USER })
  18. ensureUserExists({ email: COLLABORATOR })
  19. let projectName: string
  20. let projectId: string
  21. let recompile: () => void
  22. let waitForCompile: (fn: () => void) => void
  23. beforeWithReRunOnTestRetry(() => {
  24. projectName = `project-${uuid()}`
  25. login(USER)
  26. createProjectAndOpenInNewEditor(projectName, {
  27. type: 'Example project',
  28. }).then(id => (projectId = id))
  29. ;({ recompile, waitForCompile } = prepareWaitForNextCompileSlot())
  30. })
  31. beforeEach(function () {
  32. login(USER)
  33. waitForCompile(() => {
  34. openProjectById(projectId, true)
  35. })
  36. })
  37. describe('spelling', function () {
  38. function changeSpellCheckLanguageTo(lng: string) {
  39. cy.log(`change project language to '${lng}'`)
  40. cy.findByRole('button', { name: 'Settings' }).click()
  41. cy.findByRole('dialog').within(() => {
  42. cy.findByLabelText('Spellcheck language').select(lng)
  43. })
  44. cy.get('body').type('{esc}')
  45. }
  46. afterEach(function () {
  47. changeSpellCheckLanguageTo('Off')
  48. })
  49. it('word dictionary and spelling', function () {
  50. changeSpellCheckLanguageTo('English (American)')
  51. createNewFile()
  52. const word = createRandomLetterString()
  53. cy.log('edit project file')
  54. cy.get('.cm-line').type(word)
  55. cy.findByText(word).should('have.class', 'ol-cm-spelling-error')
  56. changeSpellCheckLanguageTo('Off')
  57. cy.findByText(word).should('not.have.class', 'ol-cm-spelling-error')
  58. changeSpellCheckLanguageTo('Spanish')
  59. cy.findByText(word).should('have.class', 'ol-cm-spelling-error')
  60. cy.log('add word to dictionary')
  61. cy.findByText(word).rightclick()
  62. cy.findByRole('menuitem', { name: 'Add to dictionary' }).click()
  63. cy.findByText(word).should('not.have.class', 'ol-cm-spelling-error')
  64. cy.log('remove word from dictionary')
  65. cy.findByRole('button', { name: 'Settings' }).click()
  66. cy.findByRole('dialog').within(() => {
  67. cy.findByLabelText('Dictionary').click()
  68. })
  69. cy.findByTestId('dictionary-modal').within(() => {
  70. cy.findByText(word)
  71. .parent()
  72. .within(() =>
  73. cy.findByRole('button', { name: 'Remove from dictionary' }).click()
  74. )
  75. cy.findByRole('button', { name: 'Close dialog' }).click()
  76. })
  77. cy.log('close modal')
  78. cy.get('body').type('{esc}')
  79. cy.log('rewrite word to force spelling error')
  80. cy.get('.cm-line').type('{selectAll}{del}' + word + '{enter}')
  81. cy.get('.ol-cm-spelling-error').should('contain.text', word)
  82. })
  83. })
  84. describe('editor', function () {
  85. it('renders jpg', function () {
  86. cy.findByRole('treeitem', { name: 'frog.jpg' }).click({ force: true })
  87. cy.get('[alt="frog.jpg"]')
  88. .should('be.visible')
  89. .and('have.prop', 'naturalWidth')
  90. .should('be.greaterThan', 0)
  91. })
  92. it('symbol palette', function () {
  93. createNewFile()
  94. cy.get('button[aria-label="Insert symbol"]').click({
  95. force: true,
  96. })
  97. cy.get('button').contains('𝜉').click()
  98. cy.findByRole('textbox', { name: 'Source Editor editing' }).should(
  99. 'contain.text',
  100. '\\xi'
  101. )
  102. cy.log('recompile to force flush and avoid "unsaved changes" prompt')
  103. recompile()
  104. })
  105. })
  106. describe('add new file to project', function () {
  107. beforeEach(function () {
  108. cy.findByRole('button', { name: 'New file' }).click()
  109. })
  110. testNewFileUpload()
  111. it('should not display import from URL', function () {
  112. cy.findByRole('button', { name: 'From external URL' }).should('not.exist')
  113. })
  114. })
  115. describe('file menu', function () {
  116. it('can download project sources', function () {
  117. cy.findByRole('button', { name: 'File' }).click()
  118. cy.findByRole('menuitem', { name: 'Download as source (.zip)' }).click()
  119. const zipName = projectName.replaceAll('-', '_')
  120. cy.task('readFileInZip', {
  121. pathToZip: `cypress/downloads/${zipName}.zip`,
  122. fileToRead: 'main.tex',
  123. }).should('contain', 'Your introduction goes here')
  124. })
  125. it('can download project PDF', function () {
  126. cy.log('ensure project is compiled')
  127. cy.findByRole('region', { name: 'PDF preview' }).should(
  128. 'contain.text',
  129. 'Your Paper'
  130. )
  131. cy.findByRole('button', { name: 'File' }).click()
  132. cy.findByRole('menuitem', { name: 'Download as PDF' }).click()
  133. const pdfName = projectName.replaceAll('-', '_')
  134. cy.task('readPdf', `cypress/downloads/${pdfName}.pdf`).should(
  135. 'contain',
  136. 'Your introduction goes here'
  137. )
  138. })
  139. it('word count', function () {
  140. cy.log('ensure project is compiled')
  141. cy.findByRole('region', { name: 'PDF preview' }).should(
  142. 'contain.text',
  143. 'Your Paper'
  144. )
  145. cy.findByRole('button', { name: 'File' }).click()
  146. cy.findByRole('menuitem', { name: 'Word count' }).click()
  147. cy.findByTestId('word-count-modal').within(() => {
  148. cy.findByText('Total Words:')
  149. cy.findByText('607')
  150. cy.findByText('Headers:')
  151. cy.findByText('14')
  152. cy.findByText('Math Inline:')
  153. cy.findByText('6')
  154. cy.findByText('Math Display:')
  155. cy.findByText('1')
  156. })
  157. })
  158. })
  159. describe('cite key search', function () {
  160. it('can insert citation from cite key', function () {
  161. createNewFile()
  162. cy.get('.cm-line').type('\\cite{{}gre')
  163. cy.findByRole('listbox').within(() => {
  164. cy.findByRole('option').should('contain.text', 'greenwade93').click()
  165. })
  166. cy.get('.cm-line').should('have.text', '\\cite{greenwade93}')
  167. })
  168. it('updates citation search when bib file is changed', function () {
  169. createNewFile()
  170. cy.get('.cm-line').type('\\cite{{}new')
  171. // Wait a reasonable time to ensure the autocomplete would've appeared if there were any matches
  172. // eslint-disable-next-line cypress/no-unnecessary-waiting
  173. cy.wait(200)
  174. cy.findByRole('listbox').should('not.exist')
  175. cy.findByRole('treeitem', { name: 'sample.bib' }).click()
  176. cy.get('.cm-line')
  177. .last()
  178. .type(
  179. '\n@article{{}newkey2024,\n author = {{}Doe, John},\n title = {{}A New Article},\n journal = {{}Journal of Testing},\n year = 2024\n}\n'
  180. )
  181. createNewFile()
  182. cy.get('.cm-line').type('\\cite{{}new')
  183. cy.findByRole('listbox').within(() => {
  184. cy.findByRole('option').should('contain.text', 'newkey2024').click()
  185. })
  186. cy.get('.cm-line').should('have.text', '\\cite{newkey2024}')
  187. })
  188. })
  189. describe('layout selector', function () {
  190. it('show editor only and switch between editor and pdf', function () {
  191. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  192. cy.get('.cm-editor').should('be.visible')
  193. cy.findByRole('button', { name: 'Layout options' }).click()
  194. cy.findByRole('menu').within(() => {
  195. cy.findByRole('menuitem', { name: /Editor only/ }).click()
  196. })
  197. cy.findByRole('region', { name: 'PDF preview' }).should('not.be.visible')
  198. cy.get('.cm-editor').should('be.visible')
  199. // force click as tooltip may cover button for some reason
  200. cy.findByRole('button', { name: 'Switch to PDF' }).click({ force: true })
  201. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  202. cy.get('.cm-editor').should('not.be.visible')
  203. cy.findByRole('button', { name: 'Switch to editor' }).click()
  204. cy.findByRole('region', { name: 'PDF preview' }).should('not.be.visible')
  205. cy.get('.cm-editor').should('be.visible')
  206. })
  207. it('show PDF only and go back to split view', function () {
  208. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  209. cy.get('.cm-editor').should('be.visible')
  210. cy.findByRole('button', { name: 'Layout options' }).click()
  211. cy.findByRole('menu').within(() => {
  212. cy.findByRole('menuitem', { name: /PDF only/ }).click()
  213. })
  214. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  215. cy.get('.cm-editor').should('not.be.visible')
  216. cy.findByRole('button', { name: 'Layout options' }).click()
  217. cy.findByRole('menu').within(() => {
  218. cy.findByRole('menuitem', { name: 'Split view' }).click()
  219. })
  220. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  221. cy.get('.cm-editor').should('be.visible')
  222. })
  223. it('PDF in a separate tab (tests editor only)', function () {
  224. cy.findByTestId('pdf-viewer').should('be.visible')
  225. cy.get('.cm-editor').should('be.visible')
  226. cy.findByRole('button', { name: 'Layout options' }).click()
  227. cy.findByRole('menu').within(() => {
  228. cy.findByRole('menuitem', { name: 'Open PDF in separate tab' }).click()
  229. })
  230. cy.findByTestId('pdf-viewer').should('not.exist')
  231. cy.get('.cm-editor').should('be.visible')
  232. })
  233. })
  234. describe('full project search', function () {
  235. it('can search for text in project files', function () {
  236. cy.findByRole('tab', { name: 'Project search' }).click()
  237. cy.findByRole('searchbox', { name: 'Search' })
  238. .should('be.visible')
  239. .type('Some examples to get started')
  240. cy.get('button').contains('Search').click()
  241. cy.findByRole('listbox').within(() => {
  242. cy.findByRole('option', {
  243. name: /Some examples to get started/,
  244. }).should('be.visible')
  245. })
  246. })
  247. })
  248. })
  249. function createRandomLetterString() {
  250. const chars = 'abcdefghijklmnopqrstuvwxyz'
  251. let result = ''
  252. for (let i = 0; i < 12; i++) {
  253. result += chars.charAt(Math.floor(Math.random() * chars.length))
  254. }
  255. return result
  256. }