editor.spec.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import {
  2. createNewFile,
  3. createProject,
  4. openProjectById,
  5. testNewFileUpload,
  6. } from './helpers/project'
  7. import { isExcludedBySharding, startWith } from './helpers/config'
  8. import { ensureUserExists, login } from './helpers/login'
  9. import { v4 as uuid } from 'uuid'
  10. import { beforeWithReRunOnTestRetry } from './helpers/beforeWithReRunOnTestRetry'
  11. import { prepareWaitForNextCompileSlot } from './helpers/compile'
  12. const USER = 'user@example.com'
  13. const COLLABORATOR = 'collaborator@example.com'
  14. describe('editor', () => {
  15. if (isExcludedBySharding('PRO_DEFAULT_1')) return
  16. startWith({ pro: true })
  17. ensureUserExists({ email: USER })
  18. ensureUserExists({ email: COLLABORATOR })
  19. let projectName: string
  20. let projectId: string
  21. let recompile: () => void
  22. let waitForCompileRateLimitCoolOff: (fn: () => void) => void
  23. beforeWithReRunOnTestRetry(function () {
  24. projectName = `project-${uuid()}`
  25. login(USER)
  26. createProject(projectName, { type: 'Example project', open: false }).then(
  27. id => (projectId = id)
  28. )
  29. ;({ recompile, waitForCompileRateLimitCoolOff } =
  30. prepareWaitForNextCompileSlot())
  31. })
  32. beforeEach(() => {
  33. login(USER)
  34. waitForCompileRateLimitCoolOff(() => {
  35. openProjectById(projectId)
  36. })
  37. })
  38. describe('spelling', function () {
  39. function changeSpellCheckLanguageTo(lng: string) {
  40. cy.log(`change project language to '${lng}'`)
  41. cy.findByRole('navigation', {
  42. name: 'Project actions',
  43. })
  44. .findByRole('button', { name: 'Menu' })
  45. .click()
  46. cy.findByRole('dialog').within(() => {
  47. cy.findByLabelText('Spell check').select(lng)
  48. })
  49. cy.get('body').type('{esc}')
  50. }
  51. afterEach(function () {
  52. changeSpellCheckLanguageTo('Off')
  53. })
  54. it('word dictionary and spelling', () => {
  55. changeSpellCheckLanguageTo('English (American)')
  56. createNewFile()
  57. const word = createRandomLetterString()
  58. cy.log('edit project file')
  59. cy.get('.cm-line').type(word)
  60. cy.findByText(word).should('have.class', 'ol-cm-spelling-error')
  61. changeSpellCheckLanguageTo('Off')
  62. cy.findByText(word).should('not.have.class', 'ol-cm-spelling-error')
  63. changeSpellCheckLanguageTo('Spanish')
  64. cy.findByText(word).should('have.class', 'ol-cm-spelling-error')
  65. cy.log('add word to dictionary')
  66. cy.findByText(word).rightclick()
  67. cy.findByRole('menuitem', { name: 'Add to dictionary' }).click()
  68. cy.findByText(word).should('not.have.class', 'ol-cm-spelling-error')
  69. cy.log('remove word from dictionary')
  70. cy.findByRole('navigation', {
  71. name: 'Project actions',
  72. })
  73. .findByRole('button', { name: 'Menu' })
  74. .click()
  75. cy.findByRole('dialog').within(() => {
  76. cy.findByLabelText('Dictionary').click()
  77. })
  78. cy.findByTestId('dictionary-modal').within(() => {
  79. cy.findByText(word)
  80. .parent()
  81. .within(() =>
  82. cy.findByRole('button', { name: 'Remove from dictionary' }).click()
  83. )
  84. // the modal has 2 close buttons, this ensures the one with the visible label is
  85. // clicked, otherwise it would need `force: true`
  86. cy.contains('button', /close/i).click()
  87. })
  88. cy.log('close left panel')
  89. cy.findByTestId('left-menu').type('{esc}')
  90. cy.log('rewrite word to force spelling error')
  91. cy.get('.cm-line').type('{selectAll}{del}' + word + '{enter}')
  92. cy.get('.ol-cm-spelling-error').should('contain.text', word)
  93. })
  94. })
  95. describe('editor', () => {
  96. it('renders jpg', () => {
  97. cy.findByRole('navigation', {
  98. name: 'Project files and outline',
  99. })
  100. .findByRole('treeitem', { name: 'frog.jpg' })
  101. .click()
  102. cy.get('[alt="frog.jpg"]')
  103. .should('be.visible')
  104. .and('have.prop', 'naturalWidth')
  105. .should('be.greaterThan', 0)
  106. })
  107. it('symbol palette', () => {
  108. createNewFile()
  109. cy.get('button[aria-label="Toggle Symbol Palette"]').click({
  110. force: true,
  111. })
  112. cy.get('button').contains('𝜉').click()
  113. cy.findByRole('textbox', { name: 'Source Editor editing' }).should(
  114. 'contain.text',
  115. '\\xi'
  116. )
  117. cy.log('recompile to force flush and avoid "unsaved changes" prompt')
  118. recompile()
  119. })
  120. })
  121. describe('add new file to project', () => {
  122. beforeEach(() => {
  123. cy.findByRole('button', { name: 'New file' }).click()
  124. })
  125. testNewFileUpload()
  126. it('should not display import from URL', () => {
  127. cy.findByRole('button', { name: 'From external URL' }).should('not.exist')
  128. })
  129. })
  130. describe('left menu', () => {
  131. beforeEach(() => {
  132. cy.findByRole('navigation', {
  133. name: 'Project actions',
  134. })
  135. .findByRole('button', { name: 'Menu' })
  136. .click()
  137. })
  138. it('can download project sources', () => {
  139. cy.findByRole('link', { name: 'Source' }).click()
  140. const zipName = projectName.replaceAll('-', '_')
  141. cy.task('readFileInZip', {
  142. pathToZip: `cypress/downloads/${zipName}.zip`,
  143. fileToRead: 'main.tex',
  144. }).should('contain', 'Your introduction goes here')
  145. })
  146. it('can download project PDF', () => {
  147. cy.log('ensure project is compiled')
  148. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  149. 'contain.text',
  150. 'Your Paper'
  151. )
  152. cy.findByRole('dialog').within(() => {
  153. cy.findByRole('link', { name: 'PDF' }).click()
  154. const pdfName = projectName.replaceAll('-', '_')
  155. cy.task('readPdf', `cypress/downloads/${pdfName}.pdf`).should(
  156. 'contain',
  157. 'Your introduction goes here'
  158. )
  159. })
  160. })
  161. it('word count', () => {
  162. cy.log('ensure project is compiled')
  163. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  164. 'contain.text',
  165. 'Your Paper'
  166. )
  167. cy.findByRole('button', { name: 'Word Count' }).click()
  168. cy.findByTestId('word-count-modal').within(() => {
  169. cy.findByText('Total Words:')
  170. cy.findByText('607')
  171. cy.findByText('Headers:')
  172. cy.findByText('14')
  173. cy.findByText('Math Inline:')
  174. cy.findByText('6')
  175. cy.findByText('Math Display:')
  176. cy.findByText('1')
  177. })
  178. })
  179. })
  180. describe('layout selector', () => {
  181. it('show editor only and switch between editor and pdf', () => {
  182. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  183. 'be.visible'
  184. )
  185. cy.get('.cm-editor').should('be.visible')
  186. cy.findByRole('button', { name: 'Layout' }).click()
  187. cy.findByRole('menu').within(() => {
  188. cy.findByRole('menuitem', { name: /Editor only/ }).click()
  189. })
  190. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  191. 'not.be.visible'
  192. )
  193. cy.get('.cm-editor').should('be.visible')
  194. cy.findByRole('button', { name: 'Switch to PDF' }).click()
  195. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  196. 'be.visible'
  197. )
  198. cy.get('.cm-editor').should('not.be.visible')
  199. cy.findByRole('button', { name: 'Switch to editor' }).click()
  200. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  201. 'not.be.visible'
  202. )
  203. cy.get('.cm-editor').should('be.visible')
  204. })
  205. it('show PDF only and go back to Editor & PDF', () => {
  206. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  207. 'be.visible'
  208. )
  209. cy.get('.cm-editor').should('be.visible')
  210. cy.findByRole('button', { name: 'Layout' }).click()
  211. cy.findByRole('menu').within(() => {
  212. cy.findByRole('menuitem', { name: /PDF only/ }).click()
  213. })
  214. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  215. 'be.visible'
  216. )
  217. cy.get('.cm-editor').should('not.be.visible')
  218. cy.findByRole('button', { name: 'Layout' }).click()
  219. cy.findByRole('menu').within(() => {
  220. cy.findByRole('menuitem', { name: 'Editor & PDF' }).click()
  221. })
  222. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  223. 'be.visible'
  224. )
  225. cy.get('.cm-editor').should('be.visible')
  226. })
  227. it('PDF in a separate tab (tests editor only)', () => {
  228. cy.findByTestId('pdf-viewer').should('be.visible')
  229. cy.get('.cm-editor').should('be.visible')
  230. cy.findByRole('button', { name: 'Layout' }).click()
  231. cy.findByRole('menu').within(() => {
  232. cy.findByRole('menuitem', { name: 'PDF in separate tab' }).click()
  233. })
  234. cy.findByTestId('pdf-viewer').should('not.exist')
  235. cy.get('.cm-editor').should('be.visible')
  236. })
  237. })
  238. })
  239. function createRandomLetterString() {
  240. const chars = 'abcdefghijklmnopqrstuvwxyz'
  241. let result = ''
  242. for (let i = 0; i < 12; i++) {
  243. result += chars.charAt(Math.floor(Math.random() * chars.length))
  244. }
  245. return result
  246. }