index.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { merge } from 'lodash-es'
  3. import initializeStore from '@shared/stores'
  4. import initializeGlobalComponents from '@shared/initializer/globalComponents'
  5. import initializeGlobalProperties from '@shared/initializer/globalProperties'
  6. import initializeForm from '@mobile/form'
  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'
  12. import type { FormSchemaField } from '@shared/components/Form/types'
  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'
  18. import createCache from '@shared/server/apollo/cache'
  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((app: App) => router.install(app))
  35. return cy.mount(component, merge({ global: { plugins } }, options))
  36. }
  37. export const mockApolloClient = () => {
  38. const client = createMockClient({
  39. cache: createCache(cacheInitializerModules),
  40. queryDeduplication: true,
  41. })
  42. provideApolloClient(client)
  43. return client
  44. }
  45. export const mountFormField = (
  46. field: string,
  47. props?: Partial<FormSchemaField> & Record<string, unknown>,
  48. ) => {
  49. return mountComponent(FormKit, {
  50. props: {
  51. name: field,
  52. type: field,
  53. ...props,
  54. },
  55. })
  56. }
  57. interface CheckFormMatchOptions {
  58. subTitle?: string
  59. type?: string
  60. wrapperSelector?: string
  61. }
  62. export const checkFormMatchesSnapshot = (options?: CheckFormMatchOptions) => {
  63. const title = options?.subTitle
  64. ? `${Cypress.currentTest.title} - ${options.subTitle}`
  65. : Cypress.currentTest.title
  66. const wrapperSelector = options?.wrapperSelector || '.formkit-outer'
  67. return cy.wrap(document.fonts.ready).then(() => {
  68. cy.get(wrapperSelector).matchImage({
  69. title,
  70. imagesDir: options?.type
  71. ? `__image_snapshots__/${options.type}`
  72. : undefined,
  73. })
  74. })
  75. }