editor.spec.ts 12 KB

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