project.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { login } from './login'
  2. import { openEmail } from './email'
  3. import { v4 as uuid } from 'uuid'
  4. export function createProject(
  5. name: string,
  6. {
  7. type = 'Blank Project',
  8. newProjectButtonMatcher = /new project/i,
  9. open = true,
  10. }: {
  11. type?: 'Blank Project' | 'Example Project'
  12. newProjectButtonMatcher?: RegExp
  13. open?: boolean
  14. } = {}
  15. ): Cypress.Chainable<string> {
  16. cy.url().then(url => {
  17. if (!url.endsWith('/project')) {
  18. cy.visit('/project')
  19. }
  20. })
  21. const interceptId = uuid()
  22. let projectId = ''
  23. if (!open) {
  24. cy.then(() => {
  25. // Register intercept just before creating the project, otherwise we might
  26. // intercept a request from a prior createProject invocation.
  27. cy.intercept(
  28. { method: 'GET', url: /\/project\/[a-fA-F0-9]{24}$/, times: 1 },
  29. req => {
  30. projectId = req.url.split('/').pop()!
  31. // Redirect back to the project dashboard, effectively reload the page.
  32. req.redirect('/project')
  33. }
  34. ).as(interceptId)
  35. })
  36. }
  37. cy.findAllByRole('button').contains(newProjectButtonMatcher).click()
  38. // FIXME: This should only look in the left menu
  39. cy.findAllByText(type).first().click()
  40. cy.findByRole('dialog').within(() => {
  41. cy.get('input').type(name)
  42. cy.findByText('Create').click()
  43. })
  44. if (open) {
  45. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  46. waitForMainDocToLoad()
  47. return cy
  48. .url()
  49. .should('match', /\/project\/[a-fA-F0-9]{24}/)
  50. .then(url => url.split('/').pop())
  51. } else {
  52. const alias = `@${interceptId}` // IDEs do not like computed values in cy.wait().
  53. cy.wait(alias)
  54. return cy.then(() => projectId)
  55. }
  56. }
  57. export function openProjectByName(projectName: string) {
  58. cy.visit('/project')
  59. cy.findByText(projectName).click()
  60. waitForMainDocToLoad()
  61. }
  62. export function openProjectById(projectId: string) {
  63. cy.visit(`/project/${projectId}`)
  64. waitForMainDocToLoad()
  65. }
  66. export function openProjectViaLinkSharingAsAnon(url: string) {
  67. cy.visit(url)
  68. waitForMainDocToLoad()
  69. }
  70. export function openProjectViaLinkSharingAsUser(
  71. url: string,
  72. projectName: string,
  73. email: string
  74. ) {
  75. cy.visit(url)
  76. cy.findByText(projectName) // wait for lazy loading
  77. cy.contains(`as ${email}`)
  78. cy.findByText('OK, join project').click()
  79. waitForMainDocToLoad()
  80. }
  81. export function openProjectViaInviteNotification(projectName: string) {
  82. cy.visit('/project')
  83. cy.findByText(projectName)
  84. .parent()
  85. .parent()
  86. .within(() => {
  87. cy.findByText('Join Project').click()
  88. })
  89. cy.findByText('Open Project').click()
  90. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  91. waitForMainDocToLoad()
  92. }
  93. function shareProjectByEmail(
  94. projectName: string,
  95. email: string,
  96. level: 'Can view' | 'Can edit'
  97. ) {
  98. openProjectByName(projectName)
  99. cy.findByText('Share').click()
  100. cy.findByRole('dialog').within(() => {
  101. cy.findByLabelText('Add people', { selector: 'input' }).type(`${email},`)
  102. cy.findByLabelText('Add people', { selector: 'input' })
  103. .parents('form')
  104. .within(() => cy.findByText('Can edit').parent().select(level))
  105. cy.findByText('Invite').click({ force: true })
  106. cy.findByText('Invite not yet accepted.')
  107. })
  108. }
  109. export function shareProjectByEmailAndAcceptInviteViaDash(
  110. projectName: string,
  111. email: string,
  112. level: 'Can view' | 'Can edit'
  113. ) {
  114. shareProjectByEmail(projectName, email, level)
  115. login(email)
  116. openProjectViaInviteNotification(projectName)
  117. }
  118. export function shareProjectByEmailAndAcceptInviteViaEmail(
  119. projectName: string,
  120. email: string,
  121. level: 'Can view' | 'Can edit'
  122. ) {
  123. shareProjectByEmail(projectName, email, level)
  124. login(email)
  125. openEmail(projectName, frame => {
  126. frame.contains('View project').then(a => {
  127. cy.log(
  128. 'bypass target=_blank and navigate current browser tab/cypress-iframe to project invite'
  129. )
  130. cy.visit(a.attr('href')!)
  131. })
  132. })
  133. cy.url().should('match', /\/project\/[a-f0-9]+\/invite\/token\/[a-f0-9]+/)
  134. cy.findByText(/user would like you to join/)
  135. cy.contains(new RegExp(`You are accepting this invite as ${email}`))
  136. cy.findByText('Join Project').click()
  137. waitForMainDocToLoad()
  138. }
  139. export function enableLinkSharing() {
  140. let linkSharingReadOnly: string
  141. let linkSharingReadAndWrite: string
  142. const origin = new URL(Cypress.config().baseUrl!).origin
  143. waitForMainDocToLoad()
  144. cy.findByText('Share').click()
  145. cy.findByText('Turn on link sharing').click()
  146. cy.findByText('Anyone with this link can view this project')
  147. .next()
  148. .should('contain.text', origin + '/read')
  149. .then(el => {
  150. linkSharingReadOnly = el.text()
  151. })
  152. cy.findByText('Anyone with this link can edit this project')
  153. .next()
  154. .should('contain.text', origin + '/')
  155. .then(el => {
  156. linkSharingReadAndWrite = el.text()
  157. })
  158. return cy.then(() => {
  159. return { linkSharingReadOnly, linkSharingReadAndWrite }
  160. })
  161. }
  162. export function waitForMainDocToLoad() {
  163. cy.log('Wait for main doc to load; it will steal the focus after loading')
  164. cy.get('.cm-content').should('contain.text', 'Introduction')
  165. }
  166. export function openFile(fileName: string, waitFor: string) {
  167. // force: The file-tree pane is too narrow to display the full name.
  168. cy.findByTestId('file-tree').findByText(fileName).click({ force: true })
  169. // wait until we've switched to the selected file
  170. cy.findByText('Loading…').should('not.exist')
  171. cy.findByText(waitFor)
  172. }
  173. export function createNewFile() {
  174. const fileName = `${uuid()}.tex`
  175. cy.log('create new project file')
  176. cy.get('button').contains('New file').click({ force: true })
  177. cy.findByRole('dialog').within(() => {
  178. cy.get('input').clear()
  179. cy.get('input').type(fileName)
  180. cy.findByText('Create').click()
  181. })
  182. // force: The file-tree pane is too narrow to display the full name.
  183. cy.findByTestId('file-tree').findByText(fileName).click({ force: true })
  184. // wait until we've switched to the newly created empty file
  185. cy.findByText('Loading…').should('not.exist')
  186. cy.get('.cm-line').should('have.length', 1)
  187. return fileName
  188. }
  189. export function toggleTrackChanges(state: boolean) {
  190. cy.findByText('Review').click()
  191. cy.get('.rp-tc-state-collapse').then(el => {
  192. // TODO: simplify this in the frontend?
  193. if (el.hasClass('rp-tc-state-collapse-on')) {
  194. // make track-changes switches visible
  195. cy.get('.rp-tc-state-collapse').click()
  196. }
  197. })
  198. cy.findByText('Everyone')
  199. .parent()
  200. .within(() => {
  201. cy.get('.form-check-input').then(el => {
  202. if (el.prop('checked') === state) return
  203. const id = uuid()
  204. const alias = `@${id}`
  205. cy.intercept({
  206. method: 'POST',
  207. url: '**/track_changes',
  208. times: 1,
  209. }).as(id)
  210. if (state) {
  211. cy.get('.form-check-input').check()
  212. } else {
  213. cy.get('.form-check-input').uncheck()
  214. }
  215. cy.wait(alias)
  216. })
  217. })
  218. cy.findByText('Review').click()
  219. }