editor.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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('link', { name: 'Download' }).click()
  119. cy.findByRole('menuitem', { name: 'Download as source (.zip)' }).click()
  120. const zipName = projectName.replaceAll('-', '_')
  121. cy.task('readFileInZip', {
  122. pathToZip: `cypress/downloads/${zipName}.zip`,
  123. fileToRead: 'main.tex',
  124. }).should('contain', 'Your introduction goes here')
  125. })
  126. it('can download project PDF', function () {
  127. cy.log('ensure project is compiled')
  128. cy.findByRole('region', { name: 'PDF preview' }).should(
  129. 'contain.text',
  130. 'Your Paper'
  131. )
  132. cy.findByRole('button', { name: 'File' }).click()
  133. cy.findByRole('link', { name: 'Download' }).click()
  134. cy.findByRole('menuitem', { name: 'Download as PDF' }).click()
  135. const pdfName = projectName.replaceAll('-', '_')
  136. cy.task('readPdf', `cypress/downloads/${pdfName}.pdf`).should(
  137. 'contain',
  138. 'Your introduction goes here'
  139. )
  140. })
  141. it('word count', function () {
  142. cy.log('ensure project is compiled')
  143. cy.findByRole('region', { name: 'PDF preview' }).should(
  144. 'contain.text',
  145. 'Your Paper'
  146. )
  147. cy.findByRole('button', { name: 'File' }).click()
  148. cy.findByRole('menuitem', { name: 'Word count' }).click()
  149. cy.findByTestId('word-count-modal').within(() => {
  150. cy.findByText('Total Words:')
  151. cy.findByText('607')
  152. cy.findByText('Headers:')
  153. cy.findByText('14')
  154. cy.findByText('Math Inline:')
  155. cy.findByText('6')
  156. cy.findByText('Math Display:')
  157. cy.findByText('1')
  158. })
  159. })
  160. })
  161. describe('cite key search', function () {
  162. it('can insert citation from cite key', function () {
  163. createNewFile()
  164. cy.get('.cm-line').type('\\cite{{}gre')
  165. cy.findByRole('listbox').within(() => {
  166. cy.findByRole('option').should('contain.text', 'greenwade93').click()
  167. })
  168. cy.get('.cm-line').should('have.text', '\\cite{greenwade93}')
  169. })
  170. it('updates citation search when bib file is changed', function () {
  171. createNewFile()
  172. cy.get('.cm-line').type('\\cite{{}new')
  173. // Wait a reasonable time to ensure the autocomplete would've appeared if there were any matches
  174. // eslint-disable-next-line cypress/no-unnecessary-waiting
  175. cy.wait(200)
  176. cy.findByRole('listbox').should('not.exist')
  177. cy.findByRole('treeitem', { name: 'sample.bib' }).click()
  178. cy.get('.cm-line')
  179. .last()
  180. .type(
  181. '\n@article{{}newkey2024,\n author = {{}Doe, John},\n title = {{}A New Article},\n journal = {{}Journal of Testing},\n year = 2024\n}\n'
  182. )
  183. createNewFile()
  184. cy.get('.cm-line').type('\\cite{{}new')
  185. cy.findByRole('listbox').within(() => {
  186. cy.findByRole('option').should('contain.text', 'newkey2024').click()
  187. })
  188. cy.get('.cm-line').should('have.text', '\\cite{newkey2024}')
  189. })
  190. })
  191. describe('layout selector', function () {
  192. it('show editor only and switch between editor and pdf', function () {
  193. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  194. cy.get('.cm-editor').should('be.visible')
  195. cy.findByRole('button', { name: 'Layout options' }).click()
  196. cy.findByRole('menu').within(() => {
  197. cy.findByRole('menuitem', { name: /Editor only/ }).click()
  198. })
  199. cy.findByRole('region', { name: 'PDF preview' }).should('not.be.visible')
  200. cy.get('.cm-editor').should('be.visible')
  201. // force click as tooltip may cover button for some reason
  202. cy.findByRole('button', { name: 'Switch to PDF' }).click({ force: true })
  203. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  204. cy.get('.cm-editor').should('not.be.visible')
  205. cy.findByRole('button', { name: 'Switch to editor' }).click()
  206. cy.findByRole('region', { name: 'PDF preview' }).should('not.be.visible')
  207. cy.get('.cm-editor').should('be.visible')
  208. })
  209. it('show PDF only and go back to split view', function () {
  210. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  211. cy.get('.cm-editor').should('be.visible')
  212. cy.findByRole('button', { name: 'Layout options' }).click()
  213. cy.findByRole('menu').within(() => {
  214. cy.findByRole('menuitem', { name: /PDF only/ }).click()
  215. })
  216. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  217. cy.get('.cm-editor').should('not.be.visible')
  218. cy.findByRole('button', { name: 'Layout options' }).click()
  219. cy.findByRole('menu').within(() => {
  220. cy.findByRole('menuitem', { name: 'Split view' }).click()
  221. })
  222. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  223. cy.get('.cm-editor').should('be.visible')
  224. })
  225. it('PDF in a separate tab (tests editor only)', function () {
  226. cy.findByTestId('pdf-viewer').should('be.visible')
  227. cy.get('.cm-editor').should('be.visible')
  228. cy.findByRole('button', { name: 'Layout options' }).click()
  229. cy.findByRole('menu').within(() => {
  230. cy.findByRole('menuitem', { name: 'Open PDF in separate tab' }).click()
  231. })
  232. cy.findByTestId('pdf-viewer').should('not.exist')
  233. cy.get('.cm-editor').should('be.visible')
  234. })
  235. })
  236. describe('full project search', function () {
  237. it('can search for text in project files', function () {
  238. cy.findByRole('tab', { name: 'Project search' }).click()
  239. cy.findByRole('searchbox', { name: 'Search' })
  240. .should('be.visible')
  241. .type('Some examples to get started')
  242. cy.get('button').contains('Search').click()
  243. cy.findByRole('listbox').within(() => {
  244. cy.findByRole('option', {
  245. name: /Some examples to get started/,
  246. }).should('be.visible')
  247. })
  248. })
  249. })
  250. describe('theming', function () {
  251. it('can change overall theme in settings menu', function () {
  252. cy.findByRole('button', { name: 'Settings' }).click()
  253. cy.findByRole('dialog').within(() => {
  254. cy.findByRole('tab', { name: 'Appearance' }).click()
  255. cy.findByLabelText('Overall theme').select('Dark')
  256. })
  257. cy.get('body').should('have.attr', 'data-theme', 'default')
  258. cy.get('body').type('{esc}')
  259. cy.findByRole('button', { name: 'Settings' }).click()
  260. cy.findByRole('dialog').within(() => {
  261. cy.findByRole('tab', { name: 'Appearance' }).click()
  262. cy.findByLabelText('Overall theme').select('Light')
  263. })
  264. cy.get('body').should('have.attr', 'data-theme', 'light')
  265. cy.get('body').type('{esc}')
  266. })
  267. })
  268. })
  269. function createRandomLetterString() {
  270. const chars = 'abcdefghijklmnopqrstuvwxyz'
  271. let result = ''
  272. for (let i = 0; i < 12; i++) {
  273. result += chars.charAt(Math.floor(Math.random() * chars.length))
  274. }
  275. return result
  276. }