editor.spec.ts 10 KB

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