MutationHandler.spec.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { useMutation } from '@vue/apollo-composable'
  3. import { SampleTypedMutationDocument } from '#tests/fixtures/graphqlSampleTypes.ts'
  4. import type {
  5. SampleUpdateMutation,
  6. SampleUpdateMutationVariables,
  7. } from '#tests/fixtures/graphqlSampleTypes.ts'
  8. import createMockClient from '#tests/support/mock-apollo-client.ts'
  9. import { useNotifications } from '#shared/components/CommonNotifications/index.ts'
  10. import UserError from '#shared/errors/UserError.ts'
  11. import { GraphQLErrorTypes } from '#shared/types/error.ts'
  12. import MutationHandler from '../MutationHandler.ts'
  13. const mutationFunctionCallSpy = vi.fn()
  14. let mutationSampleResult: Record<string, unknown> = {
  15. Sample: {
  16. __typename: 'Sample',
  17. id: 1,
  18. title: 'Test Title',
  19. text: 'Test Text',
  20. errors: null,
  21. },
  22. }
  23. let errorType = 'Exceptions::UnknownError'
  24. const getMutationSampleErrorResult = () => {
  25. return {
  26. errors: [
  27. {
  28. message: 'GraphQL Error',
  29. extensions: { type: errorType },
  30. },
  31. ],
  32. }
  33. }
  34. const mutationSampleNetworkErrorResult = new Error('GraphQL Network Error')
  35. const mockClient = (error = false, errorType = 'GraphQL') => {
  36. createMockClient([
  37. {
  38. operationDocument: SampleTypedMutationDocument,
  39. handler: () => {
  40. if (error) {
  41. return errorType === 'GraphQL'
  42. ? Promise.resolve(getMutationSampleErrorResult())
  43. : Promise.reject(mutationSampleNetworkErrorResult)
  44. }
  45. return Promise.resolve({
  46. data: mutationSampleResult,
  47. })
  48. },
  49. },
  50. ])
  51. mutationFunctionCallSpy.mockClear()
  52. }
  53. describe('MutationHandler', () => {
  54. const sampleMutation = () => {
  55. mutationFunctionCallSpy()
  56. return useMutation<SampleUpdateMutation, SampleUpdateMutationVariables>(
  57. SampleTypedMutationDocument,
  58. )
  59. }
  60. describe('constructor', () => {
  61. beforeEach(() => {
  62. mockClient()
  63. })
  64. it('instance can be created', () => {
  65. const mutationHandlerObject = new MutationHandler(sampleMutation())
  66. expect(mutationHandlerObject).toBeInstanceOf(MutationHandler)
  67. })
  68. it('default handler options can be changed', () => {
  69. const errorNotificationMessage = 'A test message.'
  70. const mutationHandlerObject = new MutationHandler(sampleMutation(), {
  71. errorNotificationMessage,
  72. })
  73. expect(
  74. mutationHandlerObject.handlerOptions.errorNotificationMessage,
  75. ).toBe(errorNotificationMessage)
  76. })
  77. it('given mutation function was executed', () => {
  78. const mutationHandlerObject = new MutationHandler(sampleMutation())
  79. expect(mutationFunctionCallSpy).toBeCalled()
  80. expect(mutationHandlerObject.operationResult).toBeTruthy()
  81. })
  82. })
  83. describe('loading', () => {
  84. beforeEach(() => {
  85. mockClient()
  86. })
  87. it('loading state should be false without called send function', () => {
  88. const mutationHandlerObject = new MutationHandler(sampleMutation())
  89. expect(mutationHandlerObject.loading().value).toBe(false)
  90. })
  91. it('loading state should be changed after called send function', () => {
  92. expect.assertions(1)
  93. const mutationHandlerObject = new MutationHandler(sampleMutation())
  94. mutationHandlerObject.send()
  95. expect(mutationHandlerObject.loading().value).toBe(true)
  96. })
  97. it('loading state will be updated', async () => {
  98. expect.assertions(1)
  99. const mutationHandlerObject = new MutationHandler(sampleMutation())
  100. await mutationHandlerObject.send()
  101. expect(mutationHandlerObject.loading().value).toBe(false)
  102. })
  103. })
  104. describe('send', () => {
  105. beforeEach(() => {
  106. mockClient()
  107. })
  108. it('result is available', async () => {
  109. const mutationHandlerObject = new MutationHandler(sampleMutation())
  110. await expect(
  111. mutationHandlerObject.send({ id: 1, Sample: {} }),
  112. ).resolves.toEqual(mutationSampleResult)
  113. })
  114. it('result with user error', async () => {
  115. const userErrors = [
  116. {
  117. field: null,
  118. message: 'Example error message',
  119. },
  120. {
  121. field: 'id',
  122. message: 'Id field is wrong',
  123. },
  124. ]
  125. const userErrorObject = new UserError(userErrors)
  126. mutationSampleResult = {
  127. Sample: {
  128. id: null,
  129. title: null,
  130. text: null,
  131. errors: userErrors,
  132. },
  133. }
  134. const mutationHandlerObject = new MutationHandler(sampleMutation())
  135. await expect(mutationHandlerObject.send()).rejects.toMatchObject({
  136. message: userErrorObject.message,
  137. errors: userErrorObject.errors,
  138. generalErrors: userErrorObject.generalErrors,
  139. fieldErrors: userErrorObject.fieldErrors,
  140. })
  141. })
  142. })
  143. describe('error handling', () => {
  144. describe('GraphQL errors', () => {
  145. it('notification is triggerd', async () => {
  146. mockClient(true)
  147. expect.assertions(1)
  148. const mutationHandlerObject = new MutationHandler(sampleMutation())
  149. await mutationHandlerObject.send().catch(() => {
  150. return null
  151. })
  152. const { notifications } = useNotifications()
  153. expect(notifications.value.length).toBe(1)
  154. })
  155. it('use error callback', async () => {
  156. mockClient(true)
  157. expect.assertions(1)
  158. const errorCallbackSpy = vi.fn()
  159. const mutationHandlerObject = new MutationHandler(sampleMutation(), {
  160. errorCallback: (error) => {
  161. errorCallbackSpy(error)
  162. },
  163. })
  164. await mutationHandlerObject.send().catch(() => {
  165. return null
  166. })
  167. expect(errorCallbackSpy).toHaveBeenCalledWith({
  168. type: 'Exceptions::UnknownError',
  169. message: 'GraphQL Error',
  170. })
  171. })
  172. it('use error callback with known error type', async () => {
  173. errorType = 'Exceptions::Forbidden'
  174. mockClient(true)
  175. expect.assertions(1)
  176. const errorCallbackSpy = vi.fn()
  177. const mutationHandlerObject = new MutationHandler(sampleMutation(), {
  178. errorCallback: (error) => {
  179. errorCallbackSpy(error)
  180. },
  181. })
  182. await mutationHandlerObject.send().catch(() => {
  183. return null
  184. })
  185. expect(errorCallbackSpy).toHaveBeenCalledWith({
  186. type: 'Exceptions::Forbidden',
  187. message: 'GraphQL Error',
  188. })
  189. })
  190. })
  191. describe('Network errors', () => {
  192. beforeEach(() => {
  193. mockClient(true, 'NetworkError')
  194. })
  195. it('use error callback', async () => {
  196. expect.assertions(1)
  197. const mutationHandlerObject = new MutationHandler(sampleMutation(), {
  198. errorCallback: (error) => {
  199. expect(error).toEqual({
  200. type: GraphQLErrorTypes.NetworkError,
  201. })
  202. },
  203. })
  204. await mutationHandlerObject.send().catch(() => {
  205. return null
  206. })
  207. })
  208. })
  209. })
  210. describe('use operation result wrapper', () => {
  211. beforeEach(() => {
  212. mockClient()
  213. })
  214. it('check called', () => {
  215. const mutationHandlerObject = new MutationHandler(sampleMutation())
  216. expect(mutationHandlerObject.called().value).toBe(false)
  217. mutationHandlerObject.send().catch(() => {
  218. return null
  219. })
  220. expect(mutationHandlerObject.called().value).toBe(true)
  221. })
  222. })
  223. })