editor.spec.ts 11 KB

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