usersUtils.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import type { User } from '@nextcloud/cypress'
  6. /**
  7. * Assert that `element` does not exist or is not visible
  8. * Useful in cases such as when NcModal is opened/closed rapidly
  9. * @param element Element that is inspected
  10. */
  11. export function assertNotExistOrNotVisible(element: JQuery<HTMLElement>) {
  12. const doesNotExist = element.length === 0
  13. const isNotVisible = !element.is(':visible')
  14. // eslint-disable-next-line no-unused-expressions
  15. expect(doesNotExist || isNotVisible, 'does not exist or is not visible').to.be.true
  16. }
  17. /**
  18. * Get the settings users list
  19. * @return Cypress chainable object
  20. */
  21. export function getUserList() {
  22. return cy.get('[data-cy-user-list]')
  23. }
  24. /**
  25. * Get the row entry for given userId within the settings users list
  26. *
  27. * @param userId the user to query
  28. * @return Cypress chainable object
  29. */
  30. export function getUserListRow(userId: string) {
  31. return getUserList().find(`[data-cy-user-row="${userId}"]`)
  32. }
  33. export function waitLoading(selector: string) {
  34. // We need to make sure the element is loading, otherwise the "done loading" will succeed even if we did not start loading.
  35. // But Cypress might also be simply too slow to catch the loading phase. Thats why we need to wait in this case.
  36. // eslint-disable-next-line cypress/no-unnecessary-waiting
  37. cy.get(`${selector}[data-loading]`).if().should('exist').else().wait(1000)
  38. // https://github.com/NoriSte/cypress-wait-until/issues/75#issuecomment-572685623
  39. cy.waitUntil(() => Cypress.$(selector).length > 0 && !Cypress.$(selector).attr('data-loading')?.length, { timeout: 10000 })
  40. }
  41. /**
  42. * Toggle the edit button of the user row
  43. * @param user The user row to edit
  44. * @param toEdit True if it should be switch to edit mode, false to switch to read-only
  45. */
  46. export function toggleEditButton(user: User, toEdit = true) {
  47. // see that the list of users contains the user
  48. getUserListRow(user.userId).should('exist')
  49. // toggle the edit mode for the user
  50. .find('[data-cy-user-list-cell-actions]')
  51. .find(`[data-cy-user-list-action-toggle-edit="${!toEdit}"]`)
  52. .if()
  53. .click({ force: true })
  54. .else()
  55. // otherwise ensure the button is already in edit mode
  56. .then(() => getUserListRow(user.userId)
  57. .find(`[data-cy-user-list-action-toggle-edit="${toEdit}"]`)
  58. .should('exist'),
  59. )
  60. }
  61. /**
  62. * Handle the confirm password dialog (if needed)
  63. * @param adminPassword The admin password for the dialog
  64. */
  65. export function handlePasswordConfirmation(adminPassword = 'admin') {
  66. const handleModal = (context: Cypress.Chainable) => {
  67. return context.contains('.modal-container', 'Confirm your password')
  68. .if()
  69. .within(() => {
  70. cy.get('input[type="password"]').type(adminPassword)
  71. cy.get('button').contains('Confirm').click()
  72. })
  73. }
  74. return cy.get('body')
  75. .if()
  76. .then(() => handleModal(cy.get('body')))
  77. .else()
  78. // Handle if inside a cy.within
  79. .root().closest('body')
  80. .then(($body) => handleModal(cy.wrap($body)))
  81. }