utils.ts 2.8 KB

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