editor.spec.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import {
  2. createNewFile,
  3. createProjectAndOpenInNewEditor,
  4. openProjectById,
  5. openProjectByName,
  6. shareProjectByEmailAndAcceptInviteViaDash,
  7. testNewFileUpload,
  8. } from './helpers/project'
  9. import { isExcludedBySharding, startWith } from './helpers/config'
  10. import { ensureUserExists, login } from './helpers/login'
  11. import { v4 as uuid } from 'uuid'
  12. import { beforeWithReRunOnTestRetry } from './helpers/beforeWithReRunOnTestRetry'
  13. import { prepareWaitForNextCompileSlot } from './helpers/compile'
  14. const USER = 'user@example.com'
  15. const COLLABORATOR = 'collaborator@example.com'
  16. describe('editor', function () {
  17. if (isExcludedBySharding('PRO_DEFAULT_1')) return
  18. startWith({ pro: true })
  19. ensureUserExists({ email: USER })
  20. ensureUserExists({ email: COLLABORATOR })
  21. let projectName: string
  22. let projectId: string
  23. let recompile: () => void
  24. let waitForCompile: (fn: () => void) => void
  25. beforeWithReRunOnTestRetry(() => {
  26. projectName = `project-${uuid()}`
  27. login(USER)
  28. createProjectAndOpenInNewEditor(projectName, {
  29. type: 'Example project',
  30. }).then(id => (projectId = id))
  31. ;({ recompile, waitForCompile } = prepareWaitForNextCompileSlot())
  32. })
  33. beforeEach(function () {
  34. login(USER)
  35. waitForCompile(() => {
  36. openProjectById(projectId, true)
  37. })
  38. })
  39. describe('spelling', function () {
  40. function changeSpellCheckLanguageTo(lng: string) {
  41. cy.log(`change project language to '${lng}'`)
  42. cy.findByRole('button', { name: 'Settings' }).click()
  43. cy.findByRole('dialog').within(() => {
  44. cy.findByRole('tab', { name: 'Spelling and language' }).click()
  45. cy.findByLabelText('Spellcheck language').select(lng)
  46. })
  47. cy.get('body').type('{esc}')
  48. }
  49. afterEach(function () {
  50. changeSpellCheckLanguageTo('Off')
  51. })
  52. it('word dictionary and spelling', function () {
  53. changeSpellCheckLanguageTo('English (American)')
  54. createNewFile()
  55. const word = createRandomLetterString()
  56. cy.log('edit project file')
  57. cy.get('.cm-line').type(word)
  58. cy.findByText(word).should('have.class', 'ol-cm-spelling-error')
  59. changeSpellCheckLanguageTo('Off')
  60. cy.findByText(word).should('not.have.class', 'ol-cm-spelling-error')
  61. changeSpellCheckLanguageTo('Spanish')
  62. cy.findByText(word).should('have.class', 'ol-cm-spelling-error')
  63. cy.log('add word to dictionary')
  64. cy.findByText(word).rightclick()
  65. cy.findByRole('menuitem', { name: 'Add to dictionary' }).click()
  66. cy.findByText(word).should('not.have.class', 'ol-cm-spelling-error')
  67. cy.log('remove word from dictionary')
  68. cy.findByRole('button', { name: 'Settings' }).click()
  69. cy.findByRole('dialog').within(() => {
  70. cy.findByRole('tab', { name: 'Spelling and language' }).click()
  71. cy.findByLabelText('Dictionary').click()
  72. })
  73. cy.findByTestId('dictionary-modal').within(() => {
  74. cy.findByText(word)
  75. .parent()
  76. .within(() =>
  77. cy.findByRole('button', { name: 'Remove from dictionary' }).click()
  78. )
  79. cy.findByRole('button', { name: 'Close dialog' }).click()
  80. })
  81. cy.log('close modal')
  82. cy.get('body').type('{esc}')
  83. cy.log('rewrite word to force spelling error')
  84. cy.get('.cm-line').type('{selectAll}{del}' + word + '{enter}')
  85. cy.get('.ol-cm-spelling-error').should('contain.text', word)
  86. })
  87. })
  88. describe('editor', function () {
  89. it('renders jpg', function () {
  90. cy.findByRole('treeitem', { name: 'frog.jpg' }).click({ force: true })
  91. cy.get('[alt="frog.jpg"]')
  92. .should('be.visible')
  93. .and('have.prop', 'naturalWidth')
  94. .should('be.greaterThan', 0)
  95. })
  96. it('symbol palette', function () {
  97. createNewFile()
  98. cy.get('button[aria-label="Insert symbol"]').click({
  99. force: true,
  100. })
  101. cy.get('button').contains('𝜉').click()
  102. cy.findByRole('textbox', { name: 'Source Editor editing' }).should(
  103. 'contain.text',
  104. '\\xi'
  105. )
  106. cy.log('recompile to force flush and avoid "unsaved changes" prompt')
  107. recompile()
  108. })
  109. })
  110. describe('add new file to project', function () {
  111. beforeEach(function () {
  112. cy.findByRole('button', { name: 'New file' }).click()
  113. })
  114. testNewFileUpload()
  115. it('should not display import from URL', function () {
  116. cy.findByRole('button', { name: 'From external URL' }).should('not.exist')
  117. })
  118. })
  119. describe('file menu', function () {
  120. it('can download project sources', function () {
  121. cy.findByRole('button', { name: 'File' }).click()
  122. cy.findByRole('menuitem', { name: 'Download' }).click()
  123. cy.findByRole('menuitem', { name: 'Download as source (.zip)' }).click()
  124. const zipName = projectName.replaceAll('-', '_')
  125. cy.task('readFileInZip', {
  126. pathToZip: `cypress/downloads/${zipName}.zip`,
  127. fileToRead: 'main.tex',
  128. }).should('contain', 'Your introduction goes here')
  129. })
  130. it('can download project PDF', function () {
  131. cy.log('ensure project is compiled')
  132. cy.findByRole('region', { name: 'PDF preview' }).should(
  133. 'contain.text',
  134. 'Your Paper'
  135. )
  136. cy.findByRole('button', { name: 'File' }).click()
  137. cy.findByRole('menuitem', { name: 'Download' }).click()
  138. cy.findByRole('menuitem', { name: 'Download as PDF' }).click()
  139. const pdfName = projectName.replaceAll('-', '_')
  140. cy.task('readPdf', `cypress/downloads/${pdfName}.pdf`).should(
  141. 'contain',
  142. 'Your introduction goes here'
  143. )
  144. })
  145. it('word count', function () {
  146. cy.log('ensure project is compiled')
  147. cy.findByRole('region', { name: 'PDF preview' }).should(
  148. 'contain.text',
  149. 'Your Paper'
  150. )
  151. cy.findByRole('button', { name: 'File' }).click()
  152. cy.findByRole('menuitem', { name: 'Word count' }).click()
  153. cy.findByTestId('word-count-modal').within(() => {
  154. cy.findByText('Total Words:')
  155. cy.findByText('607')
  156. cy.findByText('Headers:')
  157. cy.findByText('14')
  158. cy.findByText('Math Inline:')
  159. cy.findByText('6')
  160. cy.findByText('Math Display:')
  161. cy.findByText('1')
  162. })
  163. })
  164. })
  165. describe('cite key search', function () {
  166. it('can insert citation from cite key', function () {
  167. createNewFile()
  168. cy.get('.cm-line').type('\\cite{{}gre')
  169. cy.findByRole('listbox').within(() => {
  170. cy.findByRole('option').should('contain.text', 'greenwade93').click()
  171. })
  172. cy.get('.cm-line').should('have.text', '\\cite{greenwade93}')
  173. })
  174. it('updates citation search when bib file is changed', function () {
  175. createNewFile()
  176. cy.get('.cm-line').type('\\cite{{}new')
  177. // Wait a reasonable time to ensure the autocomplete would've appeared if there were any matches
  178. // eslint-disable-next-line cypress/no-unnecessary-waiting
  179. cy.wait(200)
  180. cy.findByRole('listbox').should('not.exist')
  181. cy.findByRole('treeitem', { name: 'sample.bib' }).click()
  182. cy.get('.cm-line')
  183. .last()
  184. .type(
  185. '\n@article{{}newkey2024,\n author = {{}Doe, John},\n title = {{}A New Article},\n journal = {{}Journal of Testing},\n year = 2024\n}\n'
  186. )
  187. createNewFile()
  188. cy.get('.cm-line').type('\\cite{{}new')
  189. cy.findByRole('listbox').within(() => {
  190. cy.findByRole('option').should('contain.text', 'newkey2024').click()
  191. })
  192. cy.get('.cm-line').should('have.text', '\\cite{newkey2024}')
  193. })
  194. })
  195. describe('layout selector', function () {
  196. it('show editor only and switch between editor and pdf', function () {
  197. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  198. cy.get('.cm-editor').should('be.visible')
  199. cy.findByRole('button', { name: 'Layout options' }).click()
  200. cy.findByRole('menu').within(() => {
  201. cy.findByRole('menuitem', { name: /Editor only/ }).click()
  202. })
  203. cy.findByRole('region', { name: 'PDF preview' }).should('not.be.visible')
  204. cy.get('.cm-editor').should('be.visible')
  205. // force click as tooltip may cover button for some reason
  206. cy.findByRole('button', { name: 'Switch to PDF' }).click({ force: true })
  207. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  208. cy.get('.cm-editor').should('not.be.visible')
  209. cy.findByRole('button', { name: 'Switch to editor' }).click()
  210. cy.findByRole('region', { name: 'PDF preview' }).should('not.be.visible')
  211. cy.get('.cm-editor').should('be.visible')
  212. })
  213. it('show PDF only and go back to split view', function () {
  214. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  215. cy.get('.cm-editor').should('be.visible')
  216. cy.findByRole('button', { name: 'Layout options' }).click()
  217. cy.findByRole('menu').within(() => {
  218. cy.findByRole('menuitem', { name: /PDF only/ }).click()
  219. })
  220. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  221. cy.get('.cm-editor').should('not.be.visible')
  222. cy.findByRole('button', { name: 'Layout options' }).click()
  223. cy.findByRole('menu').within(() => {
  224. cy.findByRole('menuitem', { name: 'Split view' }).click()
  225. })
  226. cy.findByRole('region', { name: 'PDF preview' }).should('be.visible')
  227. cy.get('.cm-editor').should('be.visible')
  228. })
  229. it('PDF in a separate tab (tests editor only)', function () {
  230. cy.findByTestId('pdf-viewer').should('be.visible')
  231. cy.get('.cm-editor').should('be.visible')
  232. cy.findByRole('button', { name: 'Layout options' }).click()
  233. cy.findByRole('menu').within(() => {
  234. cy.findByRole('menuitem', { name: 'Open PDF in separate tab' }).click()
  235. })
  236. cy.findByTestId('pdf-viewer').should('not.exist')
  237. cy.get('.cm-editor').should('be.visible')
  238. })
  239. })
  240. describe('full project search', function () {
  241. it('can search for text in project files', function () {
  242. cy.findByRole('tab', { name: 'Project search' }).click()
  243. cy.findByRole('searchbox', { name: 'Search' })
  244. .should('be.visible')
  245. .type('Some examples to get started')
  246. cy.get('button').contains('Search').click()
  247. cy.findByRole('listbox').within(() => {
  248. cy.findByRole('option', {
  249. name: /Some examples to get started/,
  250. }).should('be.visible')
  251. })
  252. })
  253. })
  254. describe('theming', function () {
  255. it('can change overall theme in settings menu', function () {
  256. cy.findByRole('button', { name: 'Settings' }).click()
  257. cy.findByRole('dialog').within(() => {
  258. cy.findByRole('tab', { name: 'Appearance' }).click()
  259. cy.findByLabelText('Overall theme').select('Dark')
  260. })
  261. cy.get('body').should('have.attr', 'data-theme', 'default')
  262. cy.get('body').type('{esc}')
  263. cy.findByRole('button', { name: 'Settings' }).click()
  264. cy.findByRole('dialog').within(() => {
  265. cy.findByRole('tab', { name: 'Appearance' }).click()
  266. cy.findByLabelText('Overall theme').select('Light')
  267. })
  268. cy.get('body').should('have.attr', 'data-theme', 'light')
  269. cy.get('body').type('{esc}')
  270. })
  271. })
  272. })
  273. describe('editing mode', function () {
  274. if (isExcludedBySharding('PRO_DEFAULT_1')) return
  275. startWith({ pro: true })
  276. const OWNER = USER
  277. const EDITOR = COLLABORATOR
  278. const REVIEWER = 'reviewer@example.com'
  279. const VIEWER = 'viewer@example.com'
  280. ensureUserExists({ email: OWNER })
  281. ensureUserExists({ email: EDITOR })
  282. ensureUserExists({ email: REVIEWER })
  283. ensureUserExists({ email: VIEWER })
  284. let projectName: string
  285. function openEditingModeMenu() {
  286. cy.findByRole('button', { name: 'View' }).click()
  287. cy.findByRole('menuitem', { name: 'Editing mode' }).click()
  288. }
  289. beforeWithReRunOnTestRetry(() => {
  290. projectName = `project-${uuid()}`
  291. login(OWNER)
  292. createProjectAndOpenInNewEditor(projectName, { type: 'Example project' })
  293. shareProjectByEmailAndAcceptInviteViaDash(
  294. projectName,
  295. EDITOR,
  296. 'Editor',
  297. true
  298. )
  299. login(OWNER)
  300. shareProjectByEmailAndAcceptInviteViaDash(
  301. projectName,
  302. REVIEWER,
  303. 'Reviewer',
  304. true
  305. )
  306. login(OWNER)
  307. shareProjectByEmailAndAcceptInviteViaDash(
  308. projectName,
  309. VIEWER,
  310. 'Viewer',
  311. true
  312. )
  313. })
  314. it('lets an editor switch between editing and reviewing', function () {
  315. login(EDITOR)
  316. openProjectByName(projectName, true)
  317. openEditingModeMenu()
  318. cy.findByRole('menuitem', { name: 'Editing' })
  319. .should('have.attr', 'aria-selected', 'true')
  320. .and('not.have.attr', 'aria-disabled')
  321. cy.findByRole('menuitem', { name: 'Reviewing' })
  322. .should('have.attr', 'aria-selected', 'false')
  323. .and('not.have.attr', 'aria-disabled')
  324. cy.findByRole('menuitem', { name: 'Viewing' }).should('not.exist')
  325. cy.findByRole('menuitem', { name: 'Reviewing' }).click()
  326. cy.findByRole('button', { name: 'Reviewing' }).should('exist')
  327. openEditingModeMenu()
  328. cy.findByRole('menuitem', { name: 'Reviewing' }).should(
  329. 'have.attr',
  330. 'aria-selected',
  331. 'true'
  332. )
  333. cy.findByRole('menuitem', { name: 'Editing' }).should(
  334. 'have.attr',
  335. 'aria-selected',
  336. 'false'
  337. )
  338. cy.findByRole('menuitem', { name: 'Editing' }).click()
  339. cy.findByRole('button', { name: 'Editing' }).should('exist')
  340. })
  341. it('shows a reviewer in reviewing mode with editing disabled', function () {
  342. login(REVIEWER)
  343. openProjectByName(projectName, true)
  344. openEditingModeMenu()
  345. cy.findByRole('menuitem', { name: 'Reviewing' })
  346. .should('have.attr', 'aria-selected', 'true')
  347. .and('not.have.attr', 'aria-disabled')
  348. cy.findByRole('menuitem', { name: 'Editing' }).should(
  349. 'have.attr',
  350. 'aria-disabled',
  351. 'true'
  352. )
  353. cy.findByRole('menuitem', { name: 'Viewing' }).should('not.exist')
  354. })
  355. it('shows a viewer in viewing mode with editing and reviewing disabled', function () {
  356. login(VIEWER)
  357. openProjectByName(projectName, true)
  358. openEditingModeMenu()
  359. cy.findByRole('menuitem', { name: 'Viewing' }).should(
  360. 'have.attr',
  361. 'aria-selected',
  362. 'true'
  363. )
  364. cy.findByRole('menuitem', { name: 'Editing' }).should(
  365. 'have.attr',
  366. 'aria-disabled',
  367. 'true'
  368. )
  369. cy.findByRole('menuitem', { name: 'Reviewing' }).should(
  370. 'have.attr',
  371. 'aria-disabled',
  372. 'true'
  373. )
  374. })
  375. })
  376. function createRandomLetterString() {
  377. const chars = 'abcdefghijklmnopqrstuvwxyz'
  378. let result = ''
  379. for (let i = 0; i < 12; i++) {
  380. result += chars.charAt(Math.floor(Math.random() * chars.length))
  381. }
  382. return result
  383. }