subscription-calls.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { flushPromises } from '@vue/test-utils'
  3. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  4. import { getGraphQLSubscriptionHandler } from '../mocks.ts'
  5. import { TestUserDocument, TestUserUpdatesDocument } from './queries.ts'
  6. import { getQueryHandler, getSubscriptionHandler } from './utils.ts'
  7. import type {
  8. TestUserQuery,
  9. TestUserUpdatesSubscription,
  10. TestUserUpdatesSubscriptionVariables,
  11. } from './queries.ts'
  12. describe('mocked subscription works correctly', () => {
  13. it('subscription returns data correctly when not mocked', async () => {
  14. const userId = convertToGraphQLId('User', 22)
  15. const handler = getSubscriptionHandler<TestUserUpdatesSubscription>(
  16. TestUserUpdatesDocument,
  17. { userId },
  18. )
  19. const results: (TestUserUpdatesSubscription | null | undefined)[] = []
  20. handler.onResult(({ data }) => {
  21. results.push(data)
  22. })
  23. const subscription = handler.getTestSubscriptionHandler()
  24. const mockedData = await subscription.trigger()
  25. expect(results).toHaveLength(1)
  26. const user = results[0]!.userUpdates!.user!
  27. expect(user.id).toBe(userId)
  28. expect(user.fullname).toBeTypeOf('string')
  29. expect(mockedData.userUpdates.user).toMatchObject(user)
  30. expect(user).not.toMatchObject(mockedData.userUpdates.user)
  31. expect(mockedData.userUpdates.user, 'mocked data is filled').toHaveProperty(
  32. 'email',
  33. )
  34. expect(user, 'no email asked').not.toHaveProperty('email')
  35. })
  36. it('subscription returns mocked data correctly', async () => {
  37. const userId = convertToGraphQLId('User', 22)
  38. const fullname = 'John Doe'
  39. const handler = getSubscriptionHandler<TestUserUpdatesSubscription>(
  40. TestUserUpdatesDocument,
  41. { userId },
  42. )
  43. const results: (TestUserUpdatesSubscription | null | undefined)[] = []
  44. handler.onResult(({ data }) => {
  45. results.push(data)
  46. })
  47. const subscription = handler.getTestSubscriptionHandler()
  48. await subscription.trigger({
  49. userUpdates: {
  50. user: {
  51. id: userId,
  52. fullname,
  53. },
  54. },
  55. })
  56. expect(results).toHaveLength(1)
  57. const user = results[0]!.userUpdates!.user!
  58. expect(user.id).toBe(userId)
  59. expect(user.fullname).toBe(fullname)
  60. expect(user, 'no email asked').not.toHaveProperty('email')
  61. })
  62. it('data is updated when query was called and then subscription is triggered', async () => {
  63. const userId = convertToGraphQLId('User', 1)
  64. const queryHandler = getQueryHandler<TestUserQuery>(TestUserDocument, {
  65. userId,
  66. })
  67. queryHandler.subscribeToMore<TestUserUpdatesSubscriptionVariables>({
  68. document: TestUserUpdatesDocument,
  69. variables: { userId },
  70. })
  71. queryHandler.load()
  72. const reactiveResult = queryHandler.result()
  73. await flushPromises()
  74. const { data: mocked } = queryHandler.getMockedData()
  75. expect(reactiveResult.value?.user.id).toBe(userId)
  76. expect(reactiveResult.value?.user.fullname).toBe(mocked.user.fullname)
  77. const mockedSubscription =
  78. getGraphQLSubscriptionHandler<TestUserUpdatesSubscription>('userUpdates')
  79. await mockedSubscription.trigger({
  80. userUpdates: {
  81. user: {
  82. id: userId,
  83. fullname: 'Some New Name',
  84. },
  85. },
  86. })
  87. expect(reactiveResult.value?.user.fullname).toBe('Some New Name')
  88. })
  89. })