project.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { login } from './login'
  2. import { openEmail } from './email'
  3. export function createProject(
  4. name: string,
  5. {
  6. type = 'Blank Project',
  7. newProjectButtonMatcher = /new project/i,
  8. }: {
  9. type?: 'Blank Project' | 'Example Project'
  10. newProjectButtonMatcher?: RegExp
  11. } = {}
  12. ): Cypress.Chainable<string> {
  13. cy.url().then(url => {
  14. if (!url.endsWith('/project')) {
  15. cy.visit('/project')
  16. }
  17. })
  18. cy.findAllByRole('button').contains(newProjectButtonMatcher).click()
  19. // FIXME: This should only look in the left menu
  20. cy.findAllByText(type).first().click()
  21. cy.findByRole('dialog').within(() => {
  22. cy.get('input').type(name)
  23. cy.findByText('Create').click()
  24. })
  25. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  26. waitForMainDocToLoad()
  27. return cy
  28. .url()
  29. .should('match', /\/project\/[a-fA-F0-9]{24}/)
  30. .then(url => url.split('/').pop())
  31. }
  32. export function openProjectByName(projectName: string) {
  33. cy.visit('/project')
  34. cy.findByText(projectName).click()
  35. waitForMainDocToLoad()
  36. }
  37. export function openProjectViaLinkSharingAsAnon(url: string) {
  38. cy.visit(url)
  39. waitForMainDocToLoad()
  40. }
  41. export function openProjectViaLinkSharingAsUser(
  42. url: string,
  43. projectName: string,
  44. email: string
  45. ) {
  46. cy.visit(url)
  47. cy.findByText(projectName) // wait for lazy loading
  48. cy.findByText(email)
  49. cy.findByText('OK, join project').click()
  50. waitForMainDocToLoad()
  51. }
  52. export function openProjectViaInviteNotification(projectName: string) {
  53. cy.visit('/project')
  54. cy.findByText(projectName)
  55. .parent()
  56. .parent()
  57. .within(() => {
  58. cy.findByText('Join Project').click()
  59. })
  60. cy.findByText('Open Project').click()
  61. cy.url().should('match', /\/project\/[a-fA-F0-9]{24}/)
  62. }
  63. function shareProjectByEmail(
  64. projectName: string,
  65. email: string,
  66. level: 'Can view' | 'Can edit'
  67. ) {
  68. openProjectByName(projectName)
  69. cy.findByText('Share').click()
  70. cy.findByRole('dialog').within(() => {
  71. cy.get('input').type(`${email},`)
  72. cy.get('input')
  73. .parents('form')
  74. .within(() => cy.findByText('Can edit').parent().select(level))
  75. cy.findByText('Invite').click({ force: true })
  76. cy.findByText('Invite not yet accepted.')
  77. })
  78. }
  79. export function shareProjectByEmailAndAcceptInviteViaDash(
  80. projectName: string,
  81. email: string,
  82. level: 'Can view' | 'Can edit'
  83. ) {
  84. shareProjectByEmail(projectName, email, level)
  85. login(email)
  86. openProjectViaInviteNotification(projectName)
  87. }
  88. export function shareProjectByEmailAndAcceptInviteViaEmail(
  89. projectName: string,
  90. email: string,
  91. level: 'Can view' | 'Can edit'
  92. ) {
  93. shareProjectByEmail(projectName, email, level)
  94. login(email)
  95. openEmail(projectName, frame => {
  96. frame.contains('View project').then(a => {
  97. cy.log(
  98. 'bypass target=_blank and navigate current browser tab/cypress-iframe to project invite'
  99. )
  100. cy.visit(a.attr('href')!)
  101. })
  102. })
  103. cy.url().should('match', /\/project\/[a-f0-9]+\/invite\/token\/[a-f0-9]+/)
  104. cy.findByText(/user would like you to join/)
  105. cy.contains(new RegExp(`You are accepting this invite as ${email}`))
  106. cy.findByText('Join Project').click()
  107. }
  108. export function enableLinkSharing() {
  109. let linkSharingReadOnly: string
  110. let linkSharingReadAndWrite: string
  111. waitForMainDocToLoad()
  112. cy.findByText('Share').click()
  113. cy.findByText('Turn on link sharing').click()
  114. cy.findByText('Anyone with this link can view this project')
  115. .next()
  116. .should('contain.text', 'http://sharelatex/')
  117. .then(el => {
  118. linkSharingReadOnly = el.text()
  119. })
  120. cy.findByText('Anyone with this link can edit this project')
  121. .next()
  122. .should('contain.text', 'http://sharelatex/')
  123. .then(el => {
  124. linkSharingReadAndWrite = el.text()
  125. })
  126. return cy.then(() => {
  127. return { linkSharingReadOnly, linkSharingReadAndWrite }
  128. })
  129. }
  130. export function waitForMainDocToLoad() {
  131. cy.log('Wait for main doc to load; it will steal the focus after loading')
  132. cy.get('.cm-content').should('contain.text', 'Introduction')
  133. }