project.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. // The upgrading tests create projects in older versions of Server Pro which used different casing of the project type. Use case-insensitive match.
  40. cy.findAllByText(type, { exact: false }).first().click()
  41. cy.findByRole('dialog').within(() => {
  42. cy.get('input').type(name)
  43. cy.findByText('Create').click()
  44. })
  45. if (open) {
  46. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  47. waitForMainDocToLoad()
  48. return cy
  49. .url()
  50. .should('match', /\/project\/[a-fA-F0-9]{24}/)
  51. .then(url => url.split('/').pop())
  52. } else {
  53. const alias = `@${interceptId}` // IDEs do not like computed values in cy.wait().
  54. cy.wait(alias)
  55. return cy.then(() => projectId)
  56. }
  57. }
  58. export function openProjectByName(projectName: string) {
  59. cy.visit('/project')
  60. cy.findByText(projectName).click()
  61. waitForMainDocToLoad()
  62. }
  63. export function openProjectById(projectId: string) {
  64. cy.visit(`/project/${projectId}`)
  65. waitForMainDocToLoad()
  66. }
  67. export function openProjectViaLinkSharingAsAnon(url: string) {
  68. cy.visit(url)
  69. waitForMainDocToLoad()
  70. }
  71. export function openProjectViaLinkSharingAsUser(
  72. url: string,
  73. projectName: string,
  74. email: string
  75. ) {
  76. cy.visit(url)
  77. cy.findByText(projectName) // wait for lazy loading
  78. cy.contains(`as ${email}`)
  79. cy.findByText('OK, join project').click()
  80. waitForMainDocToLoad()
  81. }
  82. export function openProjectViaInviteNotification(projectName: string) {
  83. cy.visit('/project')
  84. cy.findByText(projectName)
  85. .parent()
  86. .parent()
  87. .within(() => {
  88. cy.findByText('Join Project').click()
  89. })
  90. cy.findByText('Open Project').click()
  91. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  92. waitForMainDocToLoad()
  93. }
  94. function shareProjectByEmail(
  95. projectName: string,
  96. email: string,
  97. level: 'Viewer' | 'Editor'
  98. ) {
  99. openProjectByName(projectName)
  100. cy.findByText('Share').click()
  101. cy.findByRole('dialog').within(() => {
  102. cy.findByLabelText('Add people', { selector: 'input' }).type(`${email},`)
  103. cy.findByLabelText('Add people', { selector: 'input' })
  104. .parents('form')
  105. .within(() => {
  106. cy.findByTestId('add-collaborator-select')
  107. .click()
  108. .then(() => {
  109. cy.findByText(level).click()
  110. })
  111. })
  112. cy.findByText('Invite').click({ force: true })
  113. cy.findByText('Invite not yet accepted.')
  114. })
  115. }
  116. export function shareProjectByEmailAndAcceptInviteViaDash(
  117. projectName: string,
  118. email: string,
  119. level: 'Viewer' | 'Editor'
  120. ) {
  121. shareProjectByEmail(projectName, email, level)
  122. login(email)
  123. openProjectViaInviteNotification(projectName)
  124. }
  125. export function shareProjectByEmailAndAcceptInviteViaEmail(
  126. projectName: string,
  127. email: string,
  128. level: 'Viewer' | 'Editor'
  129. ) {
  130. shareProjectByEmail(projectName, email, level)
  131. login(email)
  132. openEmail(projectName, frame => {
  133. frame.contains('View project').then(a => {
  134. cy.log(
  135. 'bypass target=_blank and navigate current browser tab/cypress-iframe to project invite'
  136. )
  137. cy.visit(a.attr('href')!)
  138. })
  139. })
  140. cy.url().should('match', /\/project\/[a-f0-9]+\/invite\/token\/[a-f0-9]+/)
  141. cy.findByText(/user would like you to join/)
  142. cy.contains(new RegExp(`You are accepting this invite as ${email}`))
  143. cy.findByText('Join Project').click()
  144. waitForMainDocToLoad()
  145. }
  146. export function enableLinkSharing() {
  147. let linkSharingReadOnly: string
  148. let linkSharingReadAndWrite: string
  149. const origin = new URL(Cypress.config().baseUrl!).origin
  150. waitForMainDocToLoad()
  151. cy.findByText('Share').click()
  152. cy.findByText('Turn on link sharing').click()
  153. cy.findByText('Anyone with this link can view this project')
  154. .next()
  155. .should('contain.text', origin + '/read')
  156. .then(el => {
  157. linkSharingReadOnly = el.text()
  158. })
  159. cy.findByText('Anyone with this link can edit this project')
  160. .next()
  161. .should('contain.text', origin + '/')
  162. .then(el => {
  163. linkSharingReadAndWrite = el.text()
  164. })
  165. return cy.then(() => {
  166. return { linkSharingReadOnly, linkSharingReadAndWrite }
  167. })
  168. }
  169. export function waitForMainDocToLoad() {
  170. cy.log('Wait for main doc to load; it will steal the focus after loading')
  171. cy.get('.cm-content').should('contain.text', 'Introduction')
  172. }
  173. export function openFile(fileName: string, waitFor: string) {
  174. // force: The file-tree pane is too narrow to display the full name.
  175. cy.findByTestId('file-tree').findByText(fileName).click({ force: true })
  176. // wait until we've switched to the selected file
  177. cy.findByText('Loading…').should('not.exist')
  178. cy.findByText(waitFor)
  179. }
  180. export function createNewFile() {
  181. const fileName = `${uuid()}.tex`
  182. cy.log('create new project file')
  183. cy.get('button').contains('New file').click({ force: true })
  184. cy.findByRole('dialog').within(() => {
  185. cy.get('input').clear()
  186. cy.get('input').type(fileName)
  187. cy.findByText('Create').click()
  188. })
  189. // force: The file-tree pane is too narrow to display the full name.
  190. cy.findByTestId('file-tree').findByText(fileName).click({ force: true })
  191. // wait until we've switched to the newly created empty file
  192. cy.findByText('Loading…').should('not.exist')
  193. cy.get('.cm-line').should('have.length', 1)
  194. return fileName
  195. }