usersUtils.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @copyright 2023 Christopher Ng <chrng8@gmail.com>
  3. *
  4. * @author Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import type { User } from '@nextcloud/cypress'
  23. /**
  24. * Assert that `element` does not exist or is not visible
  25. * Useful in cases such as when NcModal is opened/closed rapidly
  26. * @param element Element that is inspected
  27. */
  28. export function assertNotExistOrNotVisible(element: JQuery<HTMLElement>) {
  29. const doesNotExist = element.length === 0
  30. const isNotVisible = !element.is(':visible')
  31. // eslint-disable-next-line no-unused-expressions
  32. expect(doesNotExist || isNotVisible, 'does not exist or is not visible').to.be.true
  33. }
  34. /**
  35. * Get the settings users list
  36. * @return Cypress chainable object
  37. */
  38. export function getUserList() {
  39. return cy.get('[data-cy-user-list]')
  40. }
  41. /**
  42. * Get the row entry for given userId within the settings users list
  43. *
  44. * @param userId the user to query
  45. * @return Cypress chainable object
  46. */
  47. export function getUserListRow(userId: string) {
  48. return getUserList().find(`[data-cy-user-row="${userId}"]`)
  49. }
  50. export function waitLoading(selector: string) {
  51. // We need to make sure the element is loading, otherwise the "done loading" will succeed even if we did not start loading.
  52. // But Cypress might also be simply too slow to catch the loading phase. Thats why we need to wait in this case.
  53. // eslint-disable-next-line cypress/no-unnecessary-waiting
  54. cy.get(`${selector}[data-loading]`).if().should('exist').else().wait(1000)
  55. // https://github.com/NoriSte/cypress-wait-until/issues/75#issuecomment-572685623
  56. cy.waitUntil(() => Cypress.$(selector).length > 0 && !Cypress.$(selector).attr('data-loading')?.length, { timeout: 10000 })
  57. }
  58. /**
  59. * Toggle the edit button of the user row
  60. * @param user The user row to edit
  61. * @param toEdit True if it should be switch to edit mode, false to switch to read-only
  62. */
  63. export function toggleEditButton(user: User, toEdit = true) {
  64. // see that the list of users contains the user
  65. getUserListRow(user.userId).should('exist')
  66. // toggle the edit mode for the user
  67. .find('[data-cy-user-list-cell-actions]')
  68. .find(`[data-cy-user-list-action-toggle-edit="${!toEdit}"]`)
  69. .if()
  70. .click({ force: true })
  71. .else()
  72. // otherwise ensure the button is already in edit mode
  73. .then(() => getUserListRow(user.userId)
  74. .find(`[data-cy-user-list-action-toggle-edit="${toEdit}"]`)
  75. .should('exist'),
  76. )
  77. }
  78. /**
  79. * Handle the confirm password dialog (if needed)
  80. * @param adminPassword The admin password for the dialog
  81. */
  82. export function handlePasswordConfirmation(adminPassword = 'admin') {
  83. const handleModal = (context: Cypress.Chainable) => {
  84. return context.contains('.modal-container', 'Confirm your password')
  85. .if()
  86. .within(() => {
  87. cy.get('input[type="password"]').type(adminPassword)
  88. cy.get('button').contains('Confirm').click()
  89. })
  90. }
  91. return cy.get('body')
  92. .if()
  93. .then(() => handleModal(cy.get('body')))
  94. .else()
  95. // Handle if inside a cy.within
  96. .root().closest('body')
  97. .then(($body) => handleModal(cy.wrap($body)))
  98. }