component.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import 'cypress-axe'
  6. /* eslint-disable */
  7. import { mount } from '@cypress/vue2'
  8. // Example use:
  9. // cy.mount(MyComponent)
  10. Cypress.Commands.add('mount', (component, optionsOrProps) => {
  11. let instance = null
  12. const oldMounted = component?.mounted || false
  13. // Override the mounted method to expose
  14. // the component instance to cypress
  15. component.mounted = function() {
  16. // eslint-disable-next-line
  17. instance = this
  18. if (oldMounted) {
  19. oldMounted.call(instance)
  20. }
  21. }
  22. // Expose the component with cy.get('@component')
  23. return mount(component, optionsOrProps).then(() => {
  24. return cy.wrap(instance).as('component')
  25. })
  26. })
  27. Cypress.Commands.add('mockInitialState', (app: string, key: string, value: any) => {
  28. cy.document().then(($document) => {
  29. const input = $document.createElement('input')
  30. input.setAttribute('type', 'hidden')
  31. input.setAttribute('id', `initial-state-${app}-${key}`)
  32. input.setAttribute('value', btoa(JSON.stringify(value)))
  33. $document.body.appendChild(input)
  34. })
  35. })
  36. Cypress.Commands.add('unmockInitialState', (app?: string, key?: string) => {
  37. cy.document().then(($document) => {
  38. $document.querySelectorAll('body > input[type="hidden"]' + (app ? `[id="initial-state-${app}-${key}"]` : ''))
  39. .forEach((node) => $document.body.removeChild(node))
  40. })
  41. })