utils.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (C) 2012-2023 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 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((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. assertion?: (subject: JQuery<HTMLElement>) => void
  62. }
  63. export const checkFormMatchesSnapshot = (
  64. options: CheckFormMatchOptions = {},
  65. ) => {
  66. const title = options?.subTitle
  67. ? `${Cypress.currentTest.title} - ${options.subTitle}`
  68. : Cypress.currentTest.title
  69. const wrapperSelector = options?.wrapperSelector || '.formkit-outer'
  70. return cy.wrap(document.fonts.ready).then(() => {
  71. cy.wrap(new Promise((resolve) => setTimeout(resolve))).then(() => {
  72. cy.get(wrapperSelector)
  73. .should(options?.assertion || (() => {}))
  74. .matchImage({
  75. title,
  76. imagesDir: options?.type
  77. ? `__image_snapshots__/${options.type}`
  78. : undefined,
  79. })
  80. })
  81. })
  82. }