editor.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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('.input-switch').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('.input-switch').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. })
  156. it('renders jpg', () => {
  157. cy.findByTestId('file-tree').findByText('frog.jpg').click()
  158. cy.get('[alt="frog.jpg"]')
  159. .should('be.visible')
  160. .and('have.prop', 'naturalWidth')
  161. .should('be.greaterThan', 0)
  162. })
  163. it('symbol palette', () => {
  164. cy.get('button[aria-label="Toggle Symbol Palette"]').click({
  165. force: true,
  166. })
  167. cy.get('button').contains('𝜉').click()
  168. cy.get('.cm-content').should('contain.text', '\\xi')
  169. })
  170. })
  171. describe('add new file to project', () => {
  172. let projectName: string
  173. beforeEach(() => {
  174. projectName = `project-${uuid()}`
  175. login('user@example.com')
  176. cy.visit(`/project`)
  177. createProject(projectName, { type: 'Example Project' })
  178. cy.get('button').contains('New file').click({ force: true })
  179. })
  180. it('can upload file', () => {
  181. cy.get('button').contains('Upload').click({ force: true })
  182. cy.get('input[type=file]')
  183. .first()
  184. .selectFile(
  185. {
  186. contents: Cypress.Buffer.from('Test File Content'),
  187. fileName: 'file.txt',
  188. lastModified: Date.now(),
  189. },
  190. { force: true }
  191. )
  192. cy.findByTestId('file-tree').findByText('file.txt').click({ force: true })
  193. cy.findByText('Test File Content')
  194. })
  195. it('should not display import from URL', () => {
  196. cy.findByText('From external URL').should('not.exist')
  197. })
  198. })
  199. describe('left menu', () => {
  200. let projectName: string
  201. beforeEach(() => {
  202. projectName = `project-${uuid()}`
  203. login('user@example.com')
  204. cy.visit(`/project`)
  205. createProject(projectName, { type: 'Example Project' })
  206. cy.get('button').contains('Menu').click()
  207. })
  208. it('can download project sources', () => {
  209. cy.get('a').contains('Source').click()
  210. cy.task('readFileInZip', {
  211. pathToZip: `cypress/downloads/${projectName}.zip`,
  212. fileToRead: 'main.tex',
  213. }).should('contain', 'Your introduction goes here')
  214. })
  215. it('can download project PDF', () => {
  216. cy.log('ensure project is compiled')
  217. cy.get('.pdf-viewer').should('contain.text', 'Your Paper')
  218. cy.get('.nav-downloads').within(() => {
  219. cy.findByText('PDF').click()
  220. const pdfName = projectName.replaceAll('-', '_')
  221. cy.task('readPdf', `cypress/downloads/${pdfName}.pdf`).should(
  222. 'contain',
  223. 'Your introduction goes here'
  224. )
  225. })
  226. })
  227. it('word count', () => {
  228. cy.log('ensure project is compiled')
  229. cy.get('.pdf-viewer').should('contain.text', 'Your Paper')
  230. cy.findByText('Word Count').click()
  231. cy.get('#word-count-modal').within(() => {
  232. cy.findByText('Total Words:')
  233. cy.findByText('607')
  234. cy.findByText('Headers:')
  235. cy.findByText('14')
  236. cy.findByText('Math Inline:')
  237. cy.findByText('6')
  238. cy.findByText('Math Display:')
  239. cy.findByText('1')
  240. })
  241. })
  242. })
  243. describe('layout selector', () => {
  244. let projectId: string
  245. beforeEach(() => {
  246. login('user@example.com')
  247. cy.visit(`/project`)
  248. createProject(`project-${uuid()}`, { type: 'Example Project' })
  249. })
  250. it('show editor only and switch between editor and pdf', () => {
  251. cy.get('.pdf-viewer').should('be.visible')
  252. cy.get('.cm-editor').should('be.visible')
  253. cy.findByText('Layout').click()
  254. cy.findByText('Editor only').click()
  255. cy.get('.pdf-viewer').should('not.be.visible')
  256. cy.get('.cm-editor').should('be.visible')
  257. cy.findByText('Switch to PDF').click()
  258. cy.get('.pdf-viewer').should('be.visible')
  259. cy.get('.cm-editor').should('not.be.visible')
  260. cy.findByText('Switch to editor').click()
  261. cy.get('.pdf-viewer').should('not.be.visible')
  262. cy.get('.cm-editor').should('be.visible')
  263. })
  264. it('show PDF only and go back to Editor & PDF', () => {
  265. cy.get('.pdf-viewer').should('be.visible')
  266. cy.get('.cm-editor').should('be.visible')
  267. cy.findByText('Layout').click()
  268. cy.findByText('PDF only').click()
  269. cy.get('.pdf-viewer').should('be.visible')
  270. cy.get('.cm-editor').should('not.be.visible')
  271. cy.findByText('Layout').click()
  272. cy.findByText('Editor & PDF').click()
  273. cy.get('.pdf-viewer').should('be.visible')
  274. cy.get('.cm-editor').should('be.visible')
  275. })
  276. it('PDF in a separate tab (tests editor only)', () => {
  277. cy.get('.pdf-viewer').should('be.visible')
  278. cy.get('.cm-editor').should('be.visible')
  279. cy.findByText('Layout').click()
  280. cy.findByText('PDF in separate tab').click()
  281. cy.get('.pdf-viewer').should('not.exist')
  282. cy.get('.cm-editor').should('be.visible')
  283. })
  284. })
  285. })
  286. function createRandomLetterString() {
  287. const chars = 'abcdefghijklmnopqrstuvwxyz'
  288. let result = ''
  289. for (let i = 0; i < 12; i++) {
  290. result += chars.charAt(Math.floor(Math.random() * chars.length))
  291. }
  292. return result
  293. }