MutationHandler.spec.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright (C) 2012-2024 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.toEqual(
  136. userErrorObject,
  137. )
  138. })
  139. })
  140. describe('error handling', () => {
  141. describe('GraphQL errors', () => {
  142. it('notification is triggerd', async () => {
  143. mockClient(true)
  144. expect.assertions(1)
  145. const mutationHandlerObject = new MutationHandler(sampleMutation())
  146. await mutationHandlerObject.send().catch(() => {
  147. return null
  148. })
  149. const { notifications } = useNotifications()
  150. expect(notifications.value.length).toBe(1)
  151. })
  152. it('use error callback', async () => {
  153. mockClient(true)
  154. expect.assertions(1)
  155. const errorCallbackSpy = vi.fn()
  156. const mutationHandlerObject = new MutationHandler(sampleMutation(), {
  157. errorCallback: (error) => {
  158. errorCallbackSpy(error)
  159. },
  160. })
  161. await mutationHandlerObject.send().catch(() => {
  162. return null
  163. })
  164. expect(errorCallbackSpy).toHaveBeenCalledWith({
  165. type: 'Exceptions::UnknownError',
  166. message: 'GraphQL Error',
  167. })
  168. })
  169. it('use error callback with known error type', async () => {
  170. errorType = 'Exceptions::Forbidden'
  171. mockClient(true)
  172. expect.assertions(1)
  173. const errorCallbackSpy = vi.fn()
  174. const mutationHandlerObject = new MutationHandler(sampleMutation(), {
  175. errorCallback: (error) => {
  176. errorCallbackSpy(error)
  177. },
  178. })
  179. await mutationHandlerObject.send().catch(() => {
  180. return null
  181. })
  182. expect(errorCallbackSpy).toHaveBeenCalledWith({
  183. type: 'Exceptions::Forbidden',
  184. message: 'GraphQL Error',
  185. })
  186. })
  187. })
  188. describe('Network errors', () => {
  189. beforeEach(() => {
  190. mockClient(true, 'NetworkError')
  191. })
  192. it('use error callback', async () => {
  193. expect.assertions(1)
  194. const mutationHandlerObject = new MutationHandler(sampleMutation(), {
  195. errorCallback: (error) => {
  196. expect(error).toEqual({
  197. type: GraphQLErrorTypes.NetworkError,
  198. })
  199. },
  200. })
  201. await mutationHandlerObject.send().catch(() => {
  202. return null
  203. })
  204. })
  205. })
  206. })
  207. describe('use operation result wrapper', () => {
  208. beforeEach(() => {
  209. mockClient()
  210. })
  211. it('check called', () => {
  212. const mutationHandlerObject = new MutationHandler(sampleMutation())
  213. expect(mutationHandlerObject.called().value).toBe(false)
  214. mutationHandlerObject.send().catch(() => {
  215. return null
  216. })
  217. expect(mutationHandlerObject.called().value).toBe(true)
  218. })
  219. })
  220. })