utils.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2012-2024 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(() => { initializeAppName('mobile') })
  35. plugins.push(initializeStore)
  36. plugins.push(initializeGlobalComponentStyles)
  37. plugins.push(initializeGlobalComponents)
  38. plugins.push(initializeGlobalProperties)
  39. plugins.push(initializeMobileIcons)
  40. plugins.push(initializeForm)
  41. plugins.push(initializeFormFields)
  42. plugins.push(initializeMobileVisuals)
  43. plugins.push((app: App) => router.install(app))
  44. return cy.mount(component, merge({ global: { plugins } }, options))
  45. }
  46. export const mockApolloClient = () => {
  47. const client = createMockClient({
  48. cache: createCache(cacheInitializerModules),
  49. queryDeduplication: true,
  50. })
  51. provideApolloClient(client)
  52. return client
  53. }
  54. export const mountFormField = (
  55. field: string,
  56. props?: Partial<FormSchemaField> & Record<string, unknown>,
  57. ) => {
  58. return mountComponent(FormKit, {
  59. props: {
  60. name: field,
  61. type: field,
  62. ...props,
  63. },
  64. })
  65. }
  66. interface CheckFormMatchOptions {
  67. subTitle?: string
  68. type?: string
  69. wrapperSelector?: string
  70. assertion?: (subject: JQuery<HTMLElement>) => void
  71. }
  72. export const checkFormMatchesSnapshot = (
  73. options: CheckFormMatchOptions = {},
  74. ) => {
  75. const title = options?.subTitle
  76. ? `${Cypress.currentTest.title} - ${options.subTitle}`
  77. : Cypress.currentTest.title
  78. const wrapperSelector = options?.wrapperSelector || '.formkit-outer'
  79. return cy.wrap(document.fonts.ready).then(() => {
  80. cy.wrap(new Promise((resolve) => setTimeout(resolve))).then(() => {
  81. cy.get(wrapperSelector)
  82. .should(options?.assertion || (() => {}))
  83. .matchImage({
  84. title,
  85. imagesDir: options?.type
  86. ? `__image_snapshots__/${options.type}`
  87. : undefined,
  88. })
  89. })
  90. })
  91. }