editor.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. import {
  2. createNewFile,
  3. createProject,
  4. enableLinkSharing,
  5. openFile,
  6. openProjectById,
  7. openProjectViaLinkSharingAsUser,
  8. toggleTrackChanges,
  9. } from './helpers/project'
  10. import { isExcludedBySharding, startWith } from './helpers/config'
  11. import { ensureUserExists, login } from './helpers/login'
  12. import { v4 as uuid } from 'uuid'
  13. import { beforeWithReRunOnTestRetry } from './helpers/beforeWithReRunOnTestRetry'
  14. import { prepareWaitForNextCompileSlot } from './helpers/compile'
  15. describe('editor', () => {
  16. if (isExcludedBySharding('PRO_DEFAULT_1')) return
  17. startWith({ pro: true })
  18. ensureUserExists({ email: 'user@example.com' })
  19. ensureUserExists({ email: 'collaborator@example.com' })
  20. let projectName: string
  21. let projectId: string
  22. let recompile: () => void
  23. let waitForCompileRateLimitCoolOff: (fn: () => void) => void
  24. beforeWithReRunOnTestRetry(function () {
  25. projectName = `project-${uuid()}`
  26. login('user@example.com')
  27. createProject(projectName, { type: 'Example Project', open: false }).then(
  28. id => (projectId = id)
  29. )
  30. ;({ recompile, waitForCompileRateLimitCoolOff } =
  31. prepareWaitForNextCompileSlot())
  32. })
  33. beforeEach(() => {
  34. login('user@example.com')
  35. waitForCompileRateLimitCoolOff(() => {
  36. openProjectById(projectId)
  37. })
  38. })
  39. it('word dictionary and spelling', () => {
  40. createNewFile()
  41. const word = createRandomLetterString()
  42. cy.log('edit project file')
  43. cy.get('.cm-line').type(word)
  44. cy.get('.ol-cm-spelling-error').should('exist')
  45. cy.log('change project language')
  46. cy.get('button').contains('Menu').click()
  47. cy.get('select[id=settings-menu-spellCheckLanguage]').select('Spanish')
  48. cy.get('[id="left-menu"]').type('{esc}') // close left menu
  49. cy.log('add word to dictionary')
  50. cy.get('.ol-cm-spelling-error').contains(word).rightclick()
  51. cy.findByText('Add to Dictionary').click()
  52. cy.get('.ol-cm-spelling-error').should('not.exist')
  53. cy.log('remove word from dictionary')
  54. cy.get('button').contains('Menu').click()
  55. cy.get('button').contains('Edit').click()
  56. cy.get('[id="dictionary-modal"]').within(() => {
  57. cy.findByText(word)
  58. .parent()
  59. .within(() => cy.get('button').click())
  60. // the modal has 2 close buttons, this ensures the one with the visible label is
  61. // clicked, otherwise it would need `force: true`
  62. cy.get('.btn').contains('Close').click()
  63. })
  64. cy.log('close left panel')
  65. cy.get('[id="left-menu"]').type('{esc}')
  66. cy.log('rewrite word to force spelling error')
  67. cy.get('.cm-line').type('{selectAll}{del}' + word + '{enter}')
  68. cy.get('.ol-cm-spelling-error').should('contain.text', word)
  69. })
  70. describe('collaboration', () => {
  71. beforeWithReRunOnTestRetry(function () {
  72. enableLinkSharing().then(({ linkSharingReadAndWrite }) => {
  73. const email = 'collaborator@example.com'
  74. login(email)
  75. openProjectViaLinkSharingAsUser(
  76. linkSharingReadAndWrite,
  77. projectName,
  78. email
  79. )
  80. })
  81. login('user@example.com')
  82. waitForCompileRateLimitCoolOff(() => {
  83. openProjectById(projectId)
  84. })
  85. })
  86. it('track-changes', () => {
  87. cy.log('disable track-changes before populating doc')
  88. toggleTrackChanges(false)
  89. const fileName = createNewFile()
  90. const oldContent = 'oldContent'
  91. cy.get('.cm-line').type(`static\n${oldContent}`)
  92. cy.log('recompile to force flush')
  93. recompile()
  94. cy.log('enable track-changes for everyone')
  95. toggleTrackChanges(true)
  96. login('collaborator@example.com')
  97. waitForCompileRateLimitCoolOff(() => {
  98. openProjectById(projectId)
  99. })
  100. openFile(fileName, 'static')
  101. cy.log('make changes in main file')
  102. // cy.type() "clicks" in the center of the selected element before typing. This "click" discards the text as selected by the dblclick.
  103. // Go down to the lower level event based typing, the frontend tests in web use similar events.
  104. cy.get('.cm-editor').as('editor')
  105. cy.get('@editor').findByText(oldContent).dblclick()
  106. cy.get('@editor').trigger('keydown', { key: 'Delete' })
  107. cy.get('@editor').trigger('keydown', { key: 'Enter' })
  108. cy.get('@editor').trigger('keydown', { key: 'Enter' })
  109. cy.log('recompile to force flush')
  110. recompile()
  111. login('user@example.com')
  112. waitForCompileRateLimitCoolOff(() => {
  113. openProjectById(projectId)
  114. })
  115. openFile(fileName, 'static')
  116. cy.log('reject changes')
  117. cy.findByText('Review').click()
  118. cy.get('.cm-content').should('not.contain.text', oldContent)
  119. cy.findByText('Reject').click({ force: true })
  120. cy.findByText('Review').click()
  121. cy.log('verify the changes are applied')
  122. cy.get('.cm-content').should('contain.text', oldContent)
  123. cy.log('disable track-changes for everyone again')
  124. toggleTrackChanges(false)
  125. })
  126. it('track-changes rich text', () => {
  127. cy.log('disable track-changes before populating doc')
  128. toggleTrackChanges(false)
  129. const fileName = createNewFile()
  130. const oldContent = 'oldContent'
  131. cy.get('.cm-line').type(`static\n\\section{{}${oldContent}}`)
  132. cy.log('recompile to force flush')
  133. recompile()
  134. cy.log('enable track-changes for everyone')
  135. toggleTrackChanges(true)
  136. login('collaborator@example.com')
  137. waitForCompileRateLimitCoolOff(() => {
  138. openProjectById(projectId)
  139. })
  140. openFile(fileName, 'static')
  141. cy.log('enable visual editor and make changes in main file')
  142. cy.findByText('Visual Editor').click()
  143. // cy.type() "clicks" in the center of the selected element before typing. This "click" discards the text as selected by the dblclick.
  144. // Go down to the lower level event based typing, the frontend tests in web use similar events.
  145. cy.get('.cm-editor').as('editor')
  146. cy.get('@editor').contains(oldContent).dblclick()
  147. cy.get('@editor').trigger('keydown', { key: 'Delete' })
  148. cy.get('@editor').trigger('keydown', { key: 'Enter' })
  149. cy.get('@editor').trigger('keydown', { key: 'Enter' })
  150. cy.log('recompile to force flush')
  151. recompile()
  152. login('user@example.com')
  153. waitForCompileRateLimitCoolOff(() => {
  154. openProjectById(projectId)
  155. })
  156. openFile(fileName, 'static')
  157. cy.log('reject changes')
  158. cy.findByText('Review').click()
  159. cy.get('.cm-content').should('not.contain.text', oldContent)
  160. cy.findAllByText('Reject').first().click({ force: true })
  161. cy.findByText('Review').click()
  162. cy.log('verify the changes are applied in the visual editor')
  163. cy.findByText('Visual Editor').click()
  164. cy.get('.cm-content').should('contain.text', oldContent)
  165. cy.log('disable track-changes for everyone again')
  166. toggleTrackChanges(false)
  167. })
  168. })
  169. describe('editor', () => {
  170. it('renders jpg', () => {
  171. cy.findByTestId('file-tree').findByText('frog.jpg').click()
  172. cy.get('[alt="frog.jpg"]')
  173. .should('be.visible')
  174. .and('have.prop', 'naturalWidth')
  175. .should('be.greaterThan', 0)
  176. })
  177. it('symbol palette', () => {
  178. createNewFile()
  179. cy.get('button[aria-label="Toggle Symbol Palette"]').click({
  180. force: true,
  181. })
  182. cy.get('button').contains('𝜉').click()
  183. cy.get('.cm-content').should('contain.text', '\\xi')
  184. cy.log('recompile to force flush and avoid "unsaved changes" prompt')
  185. recompile()
  186. })
  187. })
  188. describe('add new file to project', () => {
  189. beforeEach(() => {
  190. cy.get('button').contains('New file').click({ force: true })
  191. })
  192. it('can upload file', () => {
  193. const name = `${uuid()}.txt`
  194. const content = `Test File Content ${name}`
  195. cy.get('button').contains('Upload').click({ force: true })
  196. cy.get('input[type=file]')
  197. .first()
  198. .selectFile(
  199. {
  200. contents: Cypress.Buffer.from(content),
  201. fileName: name,
  202. lastModified: Date.now(),
  203. },
  204. { force: true }
  205. )
  206. // force: The file-tree pane is too narrow to display the full name.
  207. cy.findByTestId('file-tree').findByText(name).click({ force: true })
  208. cy.findByText(content)
  209. })
  210. it('should not display import from URL', () => {
  211. cy.findByText('From external URL').should('not.exist')
  212. })
  213. })
  214. describe('left menu', () => {
  215. beforeEach(() => {
  216. cy.get('button').contains('Menu').click()
  217. })
  218. it('can download project sources', () => {
  219. cy.get('a').contains('Source').click()
  220. cy.task('readFileInZip', {
  221. pathToZip: `cypress/downloads/${projectName}.zip`,
  222. fileToRead: 'main.tex',
  223. }).should('contain', 'Your introduction goes here')
  224. })
  225. it('can download project PDF', () => {
  226. cy.log('ensure project is compiled')
  227. cy.get('.pdf-viewer').should('contain.text', 'Your Paper')
  228. cy.get('.nav-downloads').within(() => {
  229. cy.findByText('PDF').click()
  230. const pdfName = projectName.replaceAll('-', '_')
  231. cy.task('readPdf', `cypress/downloads/${pdfName}.pdf`).should(
  232. 'contain',
  233. 'Your introduction goes here'
  234. )
  235. })
  236. })
  237. it('word count', () => {
  238. cy.log('ensure project is compiled')
  239. cy.get('.pdf-viewer').should('contain.text', 'Your Paper')
  240. cy.findByText('Word Count').click()
  241. cy.get('#word-count-modal').within(() => {
  242. cy.findByText('Total Words:')
  243. cy.findByText('607')
  244. cy.findByText('Headers:')
  245. cy.findByText('14')
  246. cy.findByText('Math Inline:')
  247. cy.findByText('6')
  248. cy.findByText('Math Display:')
  249. cy.findByText('1')
  250. })
  251. })
  252. })
  253. describe('layout selector', () => {
  254. it('show editor only and switch between editor and pdf', () => {
  255. cy.get('.pdf-viewer').should('be.visible')
  256. cy.get('.cm-editor').should('be.visible')
  257. cy.findByText('Layout').click()
  258. cy.findByText('Editor only').click()
  259. cy.get('.pdf-viewer').should('not.be.visible')
  260. cy.get('.cm-editor').should('be.visible')
  261. cy.findByText('Switch to PDF').click()
  262. cy.get('.pdf-viewer').should('be.visible')
  263. cy.get('.cm-editor').should('not.be.visible')
  264. cy.findByText('Switch to editor').click()
  265. cy.get('.pdf-viewer').should('not.be.visible')
  266. cy.get('.cm-editor').should('be.visible')
  267. })
  268. it('show PDF only and go back to Editor & PDF', () => {
  269. cy.get('.pdf-viewer').should('be.visible')
  270. cy.get('.cm-editor').should('be.visible')
  271. cy.findByText('Layout').click()
  272. cy.findByText('PDF only').click()
  273. cy.get('.pdf-viewer').should('be.visible')
  274. cy.get('.cm-editor').should('not.be.visible')
  275. cy.findByText('Layout').click()
  276. cy.findByText('Editor & PDF').click()
  277. cy.get('.pdf-viewer').should('be.visible')
  278. cy.get('.cm-editor').should('be.visible')
  279. })
  280. it('PDF in a separate tab (tests editor only)', () => {
  281. cy.get('.pdf-viewer').should('be.visible')
  282. cy.get('.cm-editor').should('be.visible')
  283. cy.findByText('Layout').click()
  284. cy.findByText('PDF in separate tab').click()
  285. cy.get('.pdf-viewer').should('not.exist')
  286. cy.get('.cm-editor').should('be.visible')
  287. })
  288. })
  289. })
  290. function createRandomLetterString() {
  291. const chars = 'abcdefghijklmnopqrstuvwxyz'
  292. let result = ''
  293. for (let i = 0; i < 12; i++) {
  294. result += chars.charAt(Math.floor(Math.random() * chars.length))
  295. }
  296. return result
  297. }