utils.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { render } from '@testing-library/vue'
  3. import {
  4. useLazyQuery,
  5. useMutation,
  6. useSubscription,
  7. } from '@vue/apollo-composable'
  8. import MutationHandler from '#shared/server/apollo/handler/MutationHandler.ts'
  9. import QueryHandler from '#shared/server/apollo/handler/QueryHandler.ts'
  10. import SubscriptionHandler from '#shared/server/apollo/handler/SubscriptionHandler.ts'
  11. import {
  12. getGraphQLMockCalls,
  13. getGraphQLSubscriptionHandler,
  14. type TestSubscriptionHandler,
  15. } from '../mocks.ts'
  16. import type { DocumentNode, OperationVariables } from '@apollo/client/core'
  17. import type { OptionsParameter } from '@vue/apollo-composable/dist/useQuery'
  18. interface EnhancedQueryHandler<R, V extends OperationVariables>
  19. extends QueryHandler<R, V> {
  20. getMockedData: () => { data: R }
  21. }
  22. interface EnhancedMutationHandler<R, V extends OperationVariables>
  23. extends MutationHandler<R, V> {
  24. getMockedData: () => { data: R }
  25. }
  26. interface EnhancedSubscriptionHandler<
  27. R extends Record<string, any>,
  28. V extends OperationVariables,
  29. > extends SubscriptionHandler<R, V> {
  30. getTestSubscriptionHandler: () => TestSubscriptionHandler<R>
  31. }
  32. const disposables = new Set<() => void>()
  33. const getHandler = (document: DocumentNode, cb: () => any) => {
  34. let handler: any
  35. const component = render({
  36. render() {
  37. return null
  38. },
  39. setup() {
  40. handler = cb()
  41. handler.getMockedData = () => {
  42. const { result } = getGraphQLMockCalls(document).at(-1) as any
  43. return { data: result }
  44. }
  45. },
  46. })
  47. disposables.add(() => component.unmount())
  48. return handler!
  49. }
  50. export const getQueryHandler = <
  51. R,
  52. V extends OperationVariables = OperationVariables,
  53. >(
  54. document: DocumentNode,
  55. variables?: V,
  56. options?: OptionsParameter<R, V>,
  57. ) => {
  58. return getHandler(
  59. document,
  60. () => new QueryHandler(useLazyQuery(document, variables, options)),
  61. ) as EnhancedQueryHandler<R, V>
  62. }
  63. export const getMutationHandler = <
  64. R,
  65. V extends OperationVariables = OperationVariables,
  66. >(
  67. document: DocumentNode,
  68. ) => {
  69. return getHandler(
  70. document,
  71. () => new MutationHandler(useMutation(document)),
  72. ) as EnhancedMutationHandler<R, V>
  73. }
  74. export const getSubscriptionHandler = <
  75. R extends Record<string, any>,
  76. V extends OperationVariables = OperationVariables,
  77. >(
  78. document: DocumentNode,
  79. variables: V,
  80. ) => {
  81. const handler = getHandler(
  82. document,
  83. () => new SubscriptionHandler(useSubscription<R, V>(document, variables)),
  84. ) as EnhancedSubscriptionHandler<R, V>
  85. handler.getTestSubscriptionHandler = () => {
  86. return getGraphQLSubscriptionHandler(document)
  87. }
  88. return handler
  89. }
  90. beforeEach(() => {
  91. disposables.forEach((dispose) => dispose())
  92. disposables.clear()
  93. })