editor.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. describe('editor', () => {
  6. if (isExcludedBySharding('PRO_DEFAULT_1')) return
  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 projectId: string
  56. beforeEach(() => {
  57. login('user@example.com')
  58. cy.visit(`/project`)
  59. createProject('test-editor', { type: 'Example Project' }).then(
  60. (id: string) => {
  61. projectId = id
  62. cy.log('make project shareable')
  63. cy.findByText('Share').click()
  64. cy.findByText('Turn on link sharing').click()
  65. cy.log('accept project invitation')
  66. cy.findByText('Anyone with this link can edit this project')
  67. .next()
  68. .should('contain.text', 'http://') // wait for the link to appear
  69. .then(el => {
  70. const linkSharingReadAndWrite = el.text()
  71. login('collaborator@example.com')
  72. cy.visit(linkSharingReadAndWrite)
  73. cy.get('button').contains('Join Project').click()
  74. cy.log(
  75. 'navigate to project dashboard to avoid cross session requests from editor'
  76. )
  77. cy.visit('/project')
  78. })
  79. login('user@example.com')
  80. cy.visit(`/project/${projectId}`)
  81. }
  82. )
  83. })
  84. it('track-changes', () => {
  85. cy.log('enable track-changes for everyone')
  86. cy.findByText('Review').click()
  87. cy.get('.review-panel-toolbar-collapse-button').click() // make track-changes switches visible
  88. cy.intercept('POST', '**/track_changes').as('enableTrackChanges')
  89. cy.findByText('Everyone')
  90. .parent()
  91. .within(() => cy.get('.form-check-input').click())
  92. cy.wait('@enableTrackChanges')
  93. login('collaborator@example.com')
  94. cy.visit(`/project/${projectId}`)
  95. cy.log('make changes in main file')
  96. // cy.type() "clicks" in the center of the selected element before typing. This "click" discards the text as selected by the dblclick.
  97. // Go down to the lower level event based typing, the frontend tests in web use similar events.
  98. cy.get('.cm-editor').as('editor')
  99. cy.get('@editor').findByText('\\maketitle').dblclick()
  100. cy.get('@editor').trigger('keydown', { key: 'Delete' })
  101. cy.get('@editor').trigger('keydown', { key: 'Enter' })
  102. cy.get('@editor').trigger('keydown', { key: 'Enter' })
  103. cy.log('recompile to force flush')
  104. cy.findByText('Recompile').click()
  105. login('user@example.com')
  106. cy.visit(`/project/${projectId}`)
  107. cy.log('reject changes')
  108. cy.findByText('Review').click()
  109. cy.get('.cm-content').should('not.contain.text', '\\maketitle')
  110. cy.findByText('Reject').click({ force: true })
  111. cy.log('verify the changes are applied')
  112. cy.get('.cm-content').should('contain.text', '\\maketitle')
  113. })
  114. it('track-changes rich text', () => {
  115. cy.log('enable track-changes for everyone')
  116. cy.findByText('Visual Editor').click()
  117. cy.findByText('Review').click()
  118. cy.get('.review-panel-toolbar-collapse-button').click() // make track-changes switches visible
  119. cy.intercept('POST', '**/track_changes').as('enableTrackChanges')
  120. cy.findByText('Everyone')
  121. .parent()
  122. .within(() => cy.get('.form-check-input').click())
  123. cy.wait('@enableTrackChanges')
  124. login('collaborator@example.com')
  125. cy.visit(`/project/${projectId}`)
  126. cy.log('enable visual editor and make changes in main file')
  127. cy.findByText('Visual Editor').click()
  128. // cy.type() "clicks" in the center of the selected element before typing. This "click" discards the text as selected by the dblclick.
  129. // Go down to the lower level event based typing, the frontend tests in web use similar events.
  130. cy.get('.cm-editor').as('editor')
  131. cy.get('@editor').contains('Introduction').dblclick()
  132. cy.get('@editor').trigger('keydown', { key: 'Delete' })
  133. cy.get('@editor').trigger('keydown', { key: 'Enter' })
  134. cy.get('@editor').trigger('keydown', { key: 'Enter' })
  135. cy.log('recompile to force flush')
  136. cy.findByText('Recompile').click()
  137. login('user@example.com')
  138. cy.visit(`/project/${projectId}`)
  139. cy.log('reject changes')
  140. cy.findByText('Review').click()
  141. cy.get('.cm-content').should('not.contain.text', 'Introduction')
  142. cy.findAllByText('Reject').first().click({ force: true })
  143. cy.log('verify the changes are applied in the visual editor')
  144. cy.findByText('Visual Editor').click()
  145. cy.get('.cm-content').should('contain.text', 'Introduction')
  146. })
  147. })
  148. describe('editor', () => {
  149. beforeEach(() => {
  150. login('user@example.com')
  151. cy.visit(`/project`)
  152. createProject(`project-${uuid()}`, { type: 'Example Project' })
  153. // wait until the main document is rendered
  154. cy.findByText(/Loading/).should('not.exist')
  155. cy.findByText(/Your Paper/)
  156. })
  157. it('renders jpg', () => {
  158. cy.findByTestId('file-tree').findByText('frog.jpg').click()
  159. cy.get('[alt="frog.jpg"]')
  160. .should('be.visible')
  161. .and('have.prop', 'naturalWidth')
  162. .should('be.greaterThan', 0)
  163. })
  164. it('symbol palette', () => {
  165. cy.get('button[aria-label="Toggle Symbol Palette"]').click({
  166. force: true,
  167. })
  168. cy.get('button').contains('𝜉').click()
  169. cy.get('.cm-content').should('contain.text', '\\xi')
  170. })
  171. })
  172. describe('add new file to project', () => {
  173. let projectName: string
  174. beforeEach(() => {
  175. projectName = `project-${uuid()}`
  176. login('user@example.com')
  177. cy.visit(`/project`)
  178. createProject(projectName, { type: 'Example Project' })
  179. cy.get('button').contains('New file').click({ force: true })
  180. })
  181. it('can upload file', () => {
  182. cy.get('button').contains('Upload').click({ force: true })
  183. cy.get('input[type=file]')
  184. .first()
  185. .selectFile(
  186. {
  187. contents: Cypress.Buffer.from('Test File Content'),
  188. fileName: 'file.txt',
  189. lastModified: Date.now(),
  190. },
  191. { force: true }
  192. )
  193. cy.findByTestId('file-tree').findByText('file.txt').click({ force: true })
  194. cy.findByText('Test File Content')
  195. })
  196. it('should not display import from URL', () => {
  197. cy.findByText('From external URL').should('not.exist')
  198. })
  199. })
  200. describe('left menu', () => {
  201. let projectName: string
  202. beforeEach(() => {
  203. projectName = `project-${uuid()}`
  204. login('user@example.com')
  205. cy.visit(`/project`)
  206. createProject(projectName, { type: 'Example Project' })
  207. cy.get('button').contains('Menu').click()
  208. })
  209. it('can download project sources', () => {
  210. cy.get('a').contains('Source').click()
  211. cy.task('readFileInZip', {
  212. pathToZip: `cypress/downloads/${projectName}.zip`,
  213. fileToRead: 'main.tex',
  214. }).should('contain', 'Your introduction goes here')
  215. })
  216. it('can download project PDF', () => {
  217. cy.log('ensure project is compiled')
  218. cy.get('.pdf-viewer').should('contain.text', 'Your Paper')
  219. cy.get('.nav-downloads').within(() => {
  220. cy.findByText('PDF').click()
  221. const pdfName = projectName.replaceAll('-', '_')
  222. cy.task('readPdf', `cypress/downloads/${pdfName}.pdf`).should(
  223. 'contain',
  224. 'Your introduction goes here'
  225. )
  226. })
  227. })
  228. it('word count', () => {
  229. cy.log('ensure project is compiled')
  230. cy.get('.pdf-viewer').should('contain.text', 'Your Paper')
  231. cy.findByText('Word Count').click()
  232. cy.get('#word-count-modal').within(() => {
  233. cy.findByText('Total Words:')
  234. cy.findByText('607')
  235. cy.findByText('Headers:')
  236. cy.findByText('14')
  237. cy.findByText('Math Inline:')
  238. cy.findByText('6')
  239. cy.findByText('Math Display:')
  240. cy.findByText('1')
  241. })
  242. })
  243. })
  244. describe('layout selector', () => {
  245. let projectId: string
  246. beforeEach(() => {
  247. login('user@example.com')
  248. cy.visit(`/project`)
  249. createProject(`project-${uuid()}`, { type: 'Example Project' })
  250. })
  251. it('show editor only and switch between editor and pdf', () => {
  252. cy.get('.pdf-viewer').should('be.visible')
  253. cy.get('.cm-editor').should('be.visible')
  254. cy.findByText('Layout').click()
  255. cy.findByText('Editor only').click()
  256. cy.get('.pdf-viewer').should('not.be.visible')
  257. cy.get('.cm-editor').should('be.visible')
  258. cy.findByText('Switch to PDF').click()
  259. cy.get('.pdf-viewer').should('be.visible')
  260. cy.get('.cm-editor').should('not.be.visible')
  261. cy.findByText('Switch to editor').click()
  262. cy.get('.pdf-viewer').should('not.be.visible')
  263. cy.get('.cm-editor').should('be.visible')
  264. })
  265. it('show PDF only and go back to Editor & PDF', () => {
  266. cy.get('.pdf-viewer').should('be.visible')
  267. cy.get('.cm-editor').should('be.visible')
  268. cy.findByText('Layout').click()
  269. cy.findByText('PDF only').click()
  270. cy.get('.pdf-viewer').should('be.visible')
  271. cy.get('.cm-editor').should('not.be.visible')
  272. cy.findByText('Layout').click()
  273. cy.findByText('Editor & PDF').click()
  274. cy.get('.pdf-viewer').should('be.visible')
  275. cy.get('.cm-editor').should('be.visible')
  276. })
  277. it('PDF in a separate tab (tests editor only)', () => {
  278. cy.get('.pdf-viewer').should('be.visible')
  279. cy.get('.cm-editor').should('be.visible')
  280. cy.findByText('Layout').click()
  281. cy.findByText('PDF in separate tab').click()
  282. cy.get('.pdf-viewer').should('not.exist')
  283. cy.get('.cm-editor').should('be.visible')
  284. })
  285. })
  286. })
  287. function createRandomLetterString() {
  288. const chars = 'abcdefghijklmnopqrstuvwxyz'
  289. let result = ''
  290. for (let i = 0; i < 12; i++) {
  291. result += chars.charAt(Math.floor(Math.random() * chars.length))
  292. }
  293. return result
  294. }