component.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 { mount } from 'cypress/vue2'
  23. // Augment the Cypress namespace to include type definitions for
  24. // your custom command.
  25. // Alternatively, can be defined in cypress/support/component.d.ts
  26. // with a <reference path="./component" /> at the top of your spec.
  27. declare global {
  28. // eslint-disable-next-line @typescript-eslint/no-namespace
  29. namespace Cypress {
  30. interface Chainable {
  31. mount: typeof mount
  32. }
  33. }
  34. }
  35. // Example use:
  36. // cy.mount(MyComponent)
  37. Cypress.Commands.add('mount', (component, optionsOrProps) => {
  38. let instance = null
  39. const oldMounted = component?.mounted || false
  40. // Override the mounted method to expose
  41. // the component instance to cypress
  42. component.mounted = function() {
  43. // eslint-disable-next-line
  44. instance = this
  45. if (oldMounted) {
  46. oldMounted()
  47. }
  48. }
  49. // Expose the component with cy.get('@component')
  50. return mount(component, optionsOrProps).then(() => {
  51. return cy.wrap(instance).as('component')
  52. })
  53. })