editor.spec.ts 12 KB

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