query-calls.spec.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import {
  3. EnumObjectManagerObjects,
  4. type AutocompleteSearchObjectAttributeExternalDataSourceInput,
  5. } from '#shared/graphql/types.ts'
  6. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  7. import {
  8. getGraphQLMockCalls,
  9. mockGraphQLResult,
  10. mockedApolloClient,
  11. } from '../mocks.ts'
  12. import {
  13. TestAutocompleteArrayFirstLevel,
  14. TestAvatarDocument,
  15. TestTicketArticlesMultiple,
  16. TestUserDocument,
  17. } from './queries.ts'
  18. import { getQueryHandler } from './utils.ts'
  19. import type {
  20. TestAutocompleteArrayFirstLevelQuery,
  21. TestAvatarQuery,
  22. TestTicketArticlesMultipleQuery,
  23. TestUserQuery,
  24. TestUserQueryVariables,
  25. } from './queries.ts'
  26. describe('calling queries without mocking document works correctly', () => {
  27. it('client is defined', () => {
  28. expect(mockedApolloClient).toBeDefined()
  29. })
  30. it('query correctly returns data', async () => {
  31. expect(getGraphQLMockCalls(TestAvatarDocument)).toHaveLength(0)
  32. const handler = getQueryHandler<TestAvatarQuery>(TestAvatarDocument)
  33. const { data } = await handler.query()
  34. const { data: mocked } = handler.getMockedData()
  35. // mocked has more fields because it contains the whole object
  36. expect(mocked).toMatchObject(data!)
  37. expect(data).not.toMatchObject(mocked)
  38. expect(mocked).toHaveProperty('userCurrentAvatarActive.updatedBy')
  39. expect(data).not.toHaveProperty('userCurrentAvatarActive.updatedBy')
  40. })
  41. it('when user is already created, return it if variable is referencing it', async () => {
  42. expect(getGraphQLMockCalls(TestAvatarDocument)).toHaveLength(0)
  43. const handler = getQueryHandler<TestUserQuery, TestUserQueryVariables>(
  44. TestUserDocument,
  45. )
  46. const userId = convertToGraphQLId('User', 42)
  47. const { data } = await handler.query({
  48. variables: {
  49. userId,
  50. },
  51. })
  52. const { data: mocked } = handler.getMockedData()
  53. expect(data?.user.id).toBe(userId)
  54. expect(data?.user.id).toBe(mocked.user.id)
  55. expect(data?.user.fullname).toBe(mocked.user.fullname)
  56. const { data: data2 } = await handler.query({
  57. variables: {
  58. userId,
  59. },
  60. })
  61. expect(data2?.user.id).toBe(userId)
  62. expect(data2?.user.id).toBe(data?.user.id)
  63. expect(data2?.user.fullname).toBe(data?.user.fullname)
  64. const userIdNext = convertToGraphQLId('User', 43)
  65. const { data: data3 } = await handler.query({
  66. variables: {
  67. userId: userIdNext,
  68. },
  69. })
  70. expect(data3?.user.id).toBe(userIdNext)
  71. })
  72. })
  73. describe('calling queries with mocked data works correctly', () => {
  74. it('query correctly uses default data when generating a new item each time', async () => {
  75. expect(getGraphQLMockCalls(TestAvatarDocument)).toHaveLength(0)
  76. const exampleImage = 'https://example.com/image.png'
  77. mockGraphQLResult<TestAvatarQuery>(TestAvatarDocument, {
  78. userCurrentAvatarActive: {
  79. imageFull: exampleImage,
  80. },
  81. })
  82. const handler = getQueryHandler<TestAvatarQuery>(TestAvatarDocument)
  83. const { data } = await handler.query()
  84. const { data: mock } = handler.getMockedData()
  85. expect(data).toHaveProperty(
  86. 'userCurrentAvatarActive.imageFull',
  87. exampleImage,
  88. )
  89. expect(mock).toHaveProperty(
  90. 'userCurrentAvatarActive.imageFull',
  91. exampleImage,
  92. )
  93. const exampleImage2 = 'https://example.com/image2.png'
  94. mockGraphQLResult<TestAvatarQuery>(TestAvatarDocument, {
  95. userCurrentAvatarActive: {
  96. imageFull: exampleImage2,
  97. },
  98. })
  99. const { data: data2 } = await handler.query({ fetchPolicy: 'network-only' })
  100. const { data: mock2 } = handler.getMockedData()
  101. expect(mock2).toHaveProperty(
  102. 'userCurrentAvatarActive.imageFull',
  103. exampleImage2,
  104. )
  105. expect(data2).toHaveProperty(
  106. 'userCurrentAvatarActive.imageFull',
  107. exampleImage2,
  108. )
  109. })
  110. it('query correctly uses default data when updating the same object', async () => {
  111. expect(getGraphQLMockCalls(TestAvatarDocument)).toHaveLength(0)
  112. const exampleImage = 'https://example.com/image.png'
  113. mockGraphQLResult<TestAvatarQuery>(TestAvatarDocument, {
  114. userCurrentAvatarActive: {
  115. imageFull: exampleImage,
  116. },
  117. })
  118. const handler = getQueryHandler<TestAvatarQuery>(TestAvatarDocument)
  119. const { data } = await handler.query()
  120. const { data: mock } = handler.getMockedData()
  121. expect(data).toHaveProperty(
  122. 'userCurrentAvatarActive.imageFull',
  123. exampleImage,
  124. )
  125. expect(mock).toHaveProperty(
  126. 'userCurrentAvatarActive.imageFull',
  127. exampleImage,
  128. )
  129. const exampleImage2 = 'https://example.com/image2.png'
  130. mockGraphQLResult<TestAvatarQuery>(TestAvatarDocument, {
  131. userCurrentAvatarActive: {
  132. id: data?.userCurrentAvatarActive.id,
  133. imageFull: exampleImage2,
  134. },
  135. })
  136. const { data: data2 } = await handler.query({ fetchPolicy: 'network-only' })
  137. const { data: mock2 } = handler.getMockedData()
  138. expect(mock2).toHaveProperty(
  139. 'userCurrentAvatarActive.imageFull',
  140. exampleImage2,
  141. )
  142. expect(data2).toHaveProperty(
  143. 'userCurrentAvatarActive.imageFull',
  144. exampleImage2,
  145. )
  146. })
  147. it('when operation requests an array inside the first level, it correctly returns an array', async () => {
  148. const handler = getQueryHandler<
  149. TestAutocompleteArrayFirstLevelQuery,
  150. {
  151. input: AutocompleteSearchObjectAttributeExternalDataSourceInput
  152. }
  153. >(TestAutocompleteArrayFirstLevel)
  154. const { data } = await handler.query({
  155. variables: {
  156. input: {
  157. query: 'test',
  158. attributeName: 'test',
  159. object: EnumObjectManagerObjects.Ticket,
  160. templateRenderContext: {},
  161. },
  162. },
  163. })
  164. const { data: mocked } = handler.getMockedData()
  165. expect(
  166. data?.autocompleteSearchObjectAttributeExternalDataSource,
  167. ).toBeInstanceOf(Array)
  168. expect(
  169. mocked?.autocompleteSearchObjectAttributeExternalDataSource,
  170. ).toBeInstanceOf(Array)
  171. expect(
  172. data?.autocompleteSearchObjectAttributeExternalDataSource.length,
  173. ).toBe(mocked?.autocompleteSearchObjectAttributeExternalDataSource.length)
  174. expect(mocked).toMatchObject(data!)
  175. })
  176. it('query that references itself correctly returns data', async () => {
  177. const handler = getQueryHandler<
  178. TestTicketArticlesMultipleQuery,
  179. {
  180. ticketId: string
  181. }
  182. >(TestTicketArticlesMultiple)
  183. const { data, error } = await handler.query({
  184. variables: {
  185. ticketId: convertToGraphQLId('Ticket', 42),
  186. },
  187. })
  188. const { data: mock } = handler.getMockedData()
  189. expect(error).toBeUndefined()
  190. expect(data).toHaveProperty(
  191. 'description.edges.0.node.bodyWithUrls',
  192. mock.description.edges[0].node.bodyWithUrls,
  193. )
  194. expect(data).toHaveProperty('articles.totalCount', mock.articles.totalCount)
  195. expect(data).toHaveProperty(
  196. 'articles.edges.0.node.bodyWithUrls',
  197. mock.articles.edges[0].node.bodyWithUrls,
  198. )
  199. })
  200. })