SubscriptionHandler.spec.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { NetworkStatus } from '@apollo/client/core'
  3. import { useSubscription } from '@vue/apollo-composable'
  4. import { createMockSubscription } from 'mock-apollo-client'
  5. import { effectScope } from 'vue'
  6. import { SampleTypedSubscriptionDocument } from '#tests/fixtures/graphqlSampleTypes.ts'
  7. import type {
  8. SampleUpdatedSubscription,
  9. SampleUpdatedSubscriptionVariables,
  10. } from '#tests/fixtures/graphqlSampleTypes.ts'
  11. import createMockClient from '#tests/support/mock-apollo-client.ts'
  12. import { useNotifications } from '#shared/components/CommonNotifications/index.ts'
  13. import SubscriptionHandler from '../SubscriptionHandler.ts'
  14. import type { IMockSubscription } from 'mock-apollo-client'
  15. const subscriptionFunctionCallSpy = vi.fn()
  16. const subscriptionSampleResult = {
  17. sampleUpdated: { id: 1, title: 'Test Title', text: 'Test Text' },
  18. }
  19. const subscriptionSampleResultUpdated = {
  20. sampleUpdated: { id: 1, title: 'Test Title2', text: 'Test Text2' },
  21. }
  22. const subscriptionSampleErrorResult = {
  23. networkStatus: NetworkStatus.error,
  24. errors: [
  25. {
  26. message: 'GraphQL Error',
  27. extensions: { type: 'Exceptions::UnknownError' },
  28. },
  29. ],
  30. }
  31. let mockSubscription: IMockSubscription
  32. const mockClient = () => {
  33. mockSubscription = createMockSubscription()
  34. createMockClient([
  35. {
  36. operationDocument: SampleTypedSubscriptionDocument,
  37. handler: () => mockSubscription,
  38. },
  39. ])
  40. subscriptionFunctionCallSpy.mockClear()
  41. }
  42. const scope = effectScope()
  43. describe('SubscriptionHandler', () => {
  44. const sampleSubscription = (
  45. variables: SampleUpdatedSubscriptionVariables,
  46. options = {},
  47. ) => {
  48. subscriptionFunctionCallSpy()
  49. return useSubscription<
  50. SampleUpdatedSubscription,
  51. SampleUpdatedSubscriptionVariables
  52. >(SampleTypedSubscriptionDocument, variables, options)
  53. }
  54. describe('constructor', () => {
  55. beforeEach(() => {
  56. mockClient()
  57. })
  58. it('instance can be created', () => {
  59. scope.run(() => {
  60. const subscriptionHandlerObject = new SubscriptionHandler(
  61. sampleSubscription({ id: 1 }),
  62. )
  63. expect(subscriptionHandlerObject).toBeInstanceOf(SubscriptionHandler)
  64. })
  65. })
  66. it('default handler options can be changed', () => {
  67. scope.run(() => {
  68. const errorNotificationMessage = 'A test message.'
  69. const subscriptionHandlerObject = new SubscriptionHandler(
  70. sampleSubscription({ id: 1 }),
  71. {
  72. errorNotificationMessage,
  73. },
  74. )
  75. expect(
  76. subscriptionHandlerObject.handlerOptions.errorNotificationMessage,
  77. ).toBe(errorNotificationMessage)
  78. })
  79. })
  80. it('given subscription function was executed', () => {
  81. scope.run(() => {
  82. const subscriptionHandlerObject = new SubscriptionHandler(
  83. sampleSubscription({ id: 1 }),
  84. )
  85. expect(subscriptionFunctionCallSpy).toBeCalled()
  86. expect(subscriptionHandlerObject.operationResult).toBeTruthy()
  87. })
  88. })
  89. })
  90. describe('loading', () => {
  91. beforeEach(() => {
  92. mockClient()
  93. })
  94. it('loading state will be updated', async () => {
  95. await scope.run(async () => {
  96. expect.assertions(2)
  97. const subscriptionHandlerObject = new SubscriptionHandler(
  98. sampleSubscription({ id: 1 }),
  99. )
  100. expect(subscriptionHandlerObject.loading().value).toBe(true)
  101. const subscribed = subscriptionHandlerObject.onSubscribed()
  102. mockSubscription.next({
  103. data: subscriptionSampleResult,
  104. })
  105. await subscribed
  106. expect(subscriptionHandlerObject.loading().value).toBe(false)
  107. })
  108. })
  109. })
  110. describe('result/subscribe', () => {
  111. beforeEach(() => {
  112. mockClient()
  113. })
  114. it('subscribed', async () => {
  115. await scope.run(async () => {
  116. expect.assertions(1)
  117. const subscriptionHandlerObject = new SubscriptionHandler(
  118. sampleSubscription({ id: 1 }),
  119. )
  120. const subscribed = subscriptionHandlerObject.onSubscribed()
  121. mockSubscription.next({
  122. data: subscriptionSampleResult,
  123. })
  124. const result = await subscribed
  125. expect(result).toEqual(subscriptionSampleResult)
  126. })
  127. })
  128. it('watch on result change', async () => {
  129. await scope.run(async () => {
  130. expect.assertions(2)
  131. const subscriptionHandlerObject = new SubscriptionHandler(
  132. sampleSubscription({ id: 1 }),
  133. )
  134. const subscribed = subscriptionHandlerObject.onSubscribed()
  135. mockSubscription.next({
  136. data: subscriptionSampleResult,
  137. })
  138. await subscribed
  139. let watchCount = 0
  140. subscriptionHandlerObject.watchOnResult((result) => {
  141. expect(result).toEqual(
  142. watchCount === 0
  143. ? subscriptionSampleResult
  144. : subscriptionSampleResultUpdated,
  145. )
  146. watchCount += 1
  147. })
  148. mockSubscription.next({
  149. data: subscriptionSampleResultUpdated,
  150. })
  151. })
  152. })
  153. it('register onResult callback', async () => {
  154. await scope.run(async () => {
  155. expect.assertions(1)
  156. const subscriptionHandlerObject = new SubscriptionHandler(
  157. sampleSubscription({ id: 1 }),
  158. )
  159. const resultCallbackSpy = vi.fn()
  160. const subscribed = subscriptionHandlerObject.onSubscribed()
  161. mockSubscription.next({
  162. data: subscriptionSampleResult,
  163. })
  164. await subscribed
  165. subscriptionHandlerObject.onResult((result) => {
  166. resultCallbackSpy(result)
  167. })
  168. mockSubscription.next({
  169. data: subscriptionSampleResultUpdated,
  170. })
  171. expect(resultCallbackSpy).toHaveBeenCalledWith({
  172. data: subscriptionSampleResultUpdated,
  173. })
  174. })
  175. })
  176. })
  177. describe('error handling', () => {
  178. describe('GraphQL errors', () => {
  179. beforeEach(() => {
  180. mockClient()
  181. })
  182. it('notification is triggerd', () => {
  183. scope.run(() => {
  184. const subscriptionHandlerObject = new SubscriptionHandler(
  185. sampleSubscription({ id: 1 }),
  186. )
  187. mockSubscription.next(subscriptionSampleErrorResult)
  188. expect(subscriptionHandlerObject.operationError().value).toBeTruthy()
  189. const { notifications } = useNotifications()
  190. expect(notifications.value.length).toBe(1)
  191. })
  192. })
  193. it('use error callback', () => {
  194. scope.run(() => {
  195. const errorCallbackSpy = vi.fn()
  196. const subscriptionHandlerObject = new SubscriptionHandler(
  197. sampleSubscription({ id: 1 }),
  198. {
  199. errorCallback: (error) => {
  200. errorCallbackSpy(error)
  201. },
  202. },
  203. )
  204. mockSubscription.next(subscriptionSampleErrorResult)
  205. expect(subscriptionHandlerObject.operationError().value).toBeTruthy()
  206. expect(errorCallbackSpy).toHaveBeenCalledWith({
  207. type: 'Exceptions::UnknownError',
  208. message: 'GraphQL Error',
  209. })
  210. })
  211. })
  212. })
  213. })
  214. describe('use operation result wrapper', () => {
  215. beforeEach(() => {
  216. mockClient()
  217. })
  218. it('use returned query options', () => {
  219. scope.run(() => {
  220. const subscriptionHandlerObject = new SubscriptionHandler(
  221. sampleSubscription({ id: 1 }),
  222. )
  223. expect(subscriptionHandlerObject.options()).toBeTruthy()
  224. })
  225. })
  226. })
  227. })