editor.spec.ts 8.7 KB

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