editor.spec.ts 12 KB

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