component.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.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 'cypress-axe'
  23. /* eslint-disable */
  24. import { mount } from '@cypress/vue2'
  25. // Example use:
  26. // cy.mount(MyComponent)
  27. Cypress.Commands.add('mount', (component, optionsOrProps) => {
  28. let instance = null
  29. const oldMounted = component?.mounted || false
  30. // Override the mounted method to expose
  31. // the component instance to cypress
  32. component.mounted = function() {
  33. // eslint-disable-next-line
  34. instance = this
  35. if (oldMounted) {
  36. oldMounted.call(instance)
  37. }
  38. }
  39. // Expose the component with cy.get('@component')
  40. return mount(component, optionsOrProps).then(() => {
  41. return cy.wrap(instance).as('component')
  42. })
  43. })
  44. Cypress.Commands.add('mockInitialState', (app: string, key: string, value: any) => {
  45. cy.document().then(($document) => {
  46. const input = $document.createElement('input')
  47. input.setAttribute('type', 'hidden')
  48. input.setAttribute('id', `initial-state-${app}-${key}`)
  49. input.setAttribute('value', btoa(JSON.stringify(value)))
  50. $document.body.appendChild(input)
  51. })
  52. })
  53. Cypress.Commands.add('unmockInitialState', (app?: string, key?: string) => {
  54. cy.document().then(($document) => {
  55. $document.querySelectorAll('body > input[type="hidden"]' + (app ? `[id="initial-state-${app}-${key}"]` : ''))
  56. .forEach((node) => $document.body.removeChild(node))
  57. })
  58. })