visitView.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { useApolloClient } from '@vue/apollo-composable'
  3. import { random } from 'lodash-es'
  4. import type { RouteRecordRaw } from 'vue-router'
  5. import LayoutTest from './LayoutTest.vue'
  6. import mockApolloClient from '../mock-apollo-client'
  7. import renderComponent, { getTestRouter } from './renderComponent'
  8. vi.mock('@shared/server/apollo/client', () => {
  9. return {
  10. clearApolloClientStore: () => {
  11. return Promise.resolve()
  12. },
  13. }
  14. })
  15. Object.defineProperty(window, 'fetch', {
  16. value: (path: string) => {
  17. throw new Error(`calling fetch on ${path}`)
  18. },
  19. writable: true,
  20. configurable: true,
  21. })
  22. const html = String.raw
  23. export const visitView = async (href: string) => {
  24. const { routes } = await import('@mobile/router')
  25. mockApolloClient([])
  26. // remove LayoutMain layout, keep only actual content
  27. if (routes.at(-1)?.name === 'Main') {
  28. const [mainRoutes] = routes.splice(routes.length - 1, 1)
  29. routes.push(...(mainRoutes.children as RouteRecordRaw[]), {
  30. path: '/testing-environment',
  31. component: {
  32. template: '<div></div>',
  33. },
  34. })
  35. }
  36. const testKey = random()
  37. const view = renderComponent(
  38. {
  39. template: html`<LayoutTest />`,
  40. components: { LayoutTest },
  41. },
  42. {
  43. store: true,
  44. router: true,
  45. form: true,
  46. unmount: true,
  47. routerRoutes: routes,
  48. propsData: {
  49. testKey,
  50. },
  51. },
  52. )
  53. const { client } = useApolloClient()
  54. await client.clearStore()
  55. const router = getTestRouter()
  56. await router.replace(href)
  57. return view
  58. }