visitView.ts 3.5 KB

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