utils.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { merge } from 'lodash-es'
  3. import { initializeAppName } from '#shared/composables/useAppName.ts'
  4. import initializeStore from '#shared/stores/index.ts'
  5. import initializeGlobalComponents from '#shared/initializer/globalComponents.ts'
  6. import initializeGlobalProperties from '#shared/initializer/globalProperties.ts'
  7. import { initializeForm, initializeFormFields } from '#mobile/form/index.ts'
  8. import { initializeGlobalComponentStyles } from '#mobile/initializer/initializeGlobalComponentStyles.ts'
  9. import { initializeMobileIcons } from '#mobile/initializer/initializeMobileIcons.ts'
  10. import { initializeMobileVisuals } from '#mobile/initializer/mobileVisuals.ts'
  11. // imported only for types
  12. // for some reason adding it to tsconfig doesn't work
  13. import '@testing-library/cypress'
  14. import 'cypress-real-events'
  15. import './types/commands.ts'
  16. import type { FormSchemaField } from '#shared/components/Form/types.ts'
  17. import { FormKit } from '@formkit/vue'
  18. import type { mount } from 'cypress/vue'
  19. import { createMemoryHistory, createRouter } from 'vue-router'
  20. import type { App } from 'vue'
  21. import { cacheInitializerModules } from '#mobile/server/apollo/index.ts'
  22. import createCache from '#shared/server/apollo/cache.ts'
  23. import { createMockClient } from 'mock-apollo-client'
  24. import { provideApolloClient } from '@vue/apollo-composable'
  25. const router = createRouter({
  26. history: createMemoryHistory('/'),
  27. routes: [{ path: '/', component: { template: '<div />' } }],
  28. })
  29. export const mountComponent: typeof mount = (
  30. component: any,
  31. options: any,
  32. ): Cypress.Chainable => {
  33. const plugins = []
  34. plugins.push(() => {
  35. initializeAppName('mobile')
  36. })
  37. plugins.push(initializeStore)
  38. plugins.push(initializeGlobalComponentStyles)
  39. plugins.push(initializeGlobalComponents)
  40. plugins.push(initializeGlobalProperties)
  41. plugins.push(initializeMobileIcons)
  42. plugins.push(initializeForm)
  43. plugins.push(initializeFormFields)
  44. plugins.push(initializeMobileVisuals)
  45. plugins.push((app: App) => router.install(app))
  46. return cy.mount(component, merge({ global: { plugins } }, options))
  47. }
  48. export const mockApolloClient = () => {
  49. const client = createMockClient({
  50. cache: createCache(cacheInitializerModules),
  51. queryDeduplication: true,
  52. })
  53. provideApolloClient(client)
  54. return client
  55. }
  56. export const mountFormField = (
  57. field: string,
  58. props?: Partial<FormSchemaField> & Record<string, unknown>,
  59. ) => {
  60. return mountComponent(FormKit, {
  61. props: {
  62. name: field,
  63. type: field,
  64. ...props,
  65. },
  66. })
  67. }
  68. interface CheckFormMatchOptions {
  69. subTitle?: string
  70. type?: string
  71. wrapperSelector?: string
  72. assertion?: (subject: JQuery<HTMLElement>) => void
  73. }
  74. export const checkFormMatchesSnapshot = (
  75. options: CheckFormMatchOptions = {},
  76. ) => {
  77. const title = options?.subTitle
  78. ? `${Cypress.currentTest.title} - ${options.subTitle}`
  79. : Cypress.currentTest.title
  80. const wrapperSelector = options?.wrapperSelector || '.formkit-outer'
  81. return cy.wrap(document.fonts.ready).then(() => {
  82. cy.wrap(new Promise((resolve) => setTimeout(resolve))).then(() => {
  83. cy.get(wrapperSelector)
  84. .should(options?.assertion || (() => {}))
  85. .matchImage({
  86. title,
  87. imagesDir: options?.type
  88. ? `__image_snapshots__/${options.type}`
  89. : undefined,
  90. })
  91. })
  92. })
  93. }