visitView.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useApolloClient } from '@vue/apollo-composable'
  3. import { random } from 'lodash-es'
  4. // import authenticationGuard from '#shared/router/guards/before/authentication.ts'
  5. // import permissionGuard from '#shared/router/guards/before/permission.ts'
  6. import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'
  7. import { useLocaleStore } from '#shared/stores/locale.ts'
  8. import mockApolloClient from '../mock-apollo-client.ts'
  9. import { getTestAppName } from './app.ts'
  10. import LayoutTestDesktopView from './LayoutTestDesktopView.vue'
  11. import LayoutTestMobileView from './LayoutTestMobileView.vue'
  12. import renderComponent, {
  13. getTestRouter,
  14. type ExtendedMountingOptions,
  15. } from './renderComponent.ts'
  16. import type { RouteRecordRaw } from 'vue-router'
  17. Object.defineProperty(window, 'fetch', {
  18. value: (path: string) => {
  19. throw new Error(`calling fetch on ${path}`)
  20. },
  21. writable: true,
  22. configurable: true,
  23. })
  24. const html = String.raw
  25. interface VisitViewOptions extends ExtendedMountingOptions<unknown> {
  26. mockApollo?: boolean
  27. }
  28. const isDesktop = getTestAppName() === 'desktop'
  29. export const visitView = async (
  30. href: string,
  31. // rely on new way to mock apollo in desktop by default
  32. options: VisitViewOptions = { mockApollo: !isDesktop, setLocale: isDesktop },
  33. ) => {
  34. const { routes } = isDesktop
  35. ? await import('#desktop/router/index.ts')
  36. : await import('#mobile/router/index.ts')
  37. if (options.mockApollo) {
  38. vi.mock('#shared/server/apollo/client.ts', () => {
  39. return {
  40. clearApolloClientStore: () => {
  41. return Promise.resolve()
  42. },
  43. getApolloClient: () => {
  44. return {
  45. cache: {
  46. gc: () => [],
  47. },
  48. }
  49. },
  50. }
  51. })
  52. mockApolloClient([])
  53. } else if (isDesktop) {
  54. // automocking is enabled when this file is imported because it happens on the top level
  55. await import('#tests/graphql/builders/mocks.ts')
  56. }
  57. // remove LayoutMain layout, keep only actual content
  58. if (routes.at(-1)?.name === 'Main') {
  59. const [mainRoutes] = routes.splice(routes.length - 1, 1)
  60. routes.push(...(mainRoutes.children as RouteRecordRaw[]), {
  61. path: '/testing-environment',
  62. component: {
  63. template: '<div></div>',
  64. },
  65. })
  66. }
  67. // TODO: disable some stuff for now, because it will break a lot of test.
  68. // const routerBeforeGuards = [authenticationGuard, permissionGuard]
  69. const routerBeforeGuards = []
  70. if (isDesktop) {
  71. // const { default: systemSetupInfo } = await import(
  72. // '#desktop/router/guards/before/systemSetupInfo.ts'
  73. // )
  74. // routerBeforeGuards.push(systemSetupInfo)
  75. const { default: activeTaskbarTab } = await import(
  76. '#desktop/router/guards/before/activeTaskbarTab.ts'
  77. )
  78. routerBeforeGuards.push(activeTaskbarTab)
  79. }
  80. const testKey = random()
  81. useNotifications().clearAllNotifications()
  82. const view = renderComponent(
  83. {
  84. template: html` <LayoutTest${isDesktop
  85. ? 'DesktopView'
  86. : 'MobileView'} />`,
  87. components: {
  88. LayoutTestDesktopView,
  89. LayoutTestMobileView,
  90. },
  91. },
  92. {
  93. store: true,
  94. router: true,
  95. form: true,
  96. unmount: true,
  97. routerRoutes: routes,
  98. routerBeforeGuards,
  99. propsData: {
  100. testKey,
  101. },
  102. ...options,
  103. },
  104. )
  105. const { client } = useApolloClient()
  106. await client.clearStore()
  107. const router = getTestRouter()
  108. await router.replace(href)
  109. if (options.setLocale) {
  110. await useLocaleStore().setLocale()
  111. }
  112. return view
  113. }