online-notification-actions.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { visitView } from '#tests/support/components/visitView.ts'
  3. import {
  4. mockGraphQLApi,
  5. mockGraphQLSubscription,
  6. } from '#tests/support/mock-graphql-api.ts'
  7. import type { ExtendedIMockSubscription } from '#tests/support/mock-graphql-api.ts'
  8. import { mockUserCurrent } from '#tests/support/mock-userCurrent.ts'
  9. import { waitUntil } from '#tests/support/utils.ts'
  10. import { mockOnlineNotificationQuery } from '#shared/entities/online-notification/__tests__/mocks/online-notification-mocks.ts'
  11. import { OnlineNotificationDeleteDocument } from '#shared/entities/online-notification/graphql/mutations/delete.api.ts'
  12. import { OnlineNotificationMarkAllAsSeenDocument } from '#shared/entities/online-notification/graphql/mutations/markAllAsSeen.api.ts'
  13. import { OnlineNotificationsDocument } from '#shared/entities/online-notification/graphql/queries/onlineNotifications.api.ts'
  14. import { OnlineNotificationsCountDocument } from '#shared/entities/online-notification/graphql/subscriptions/onlineNotificationsCount.api.ts'
  15. let onlineNotificationCountSubscription: ExtendedIMockSubscription
  16. const triggerNextOnlineNotificationCount = async (newCount = 0) => {
  17. await onlineNotificationCountSubscription.next({
  18. data: {
  19. onlineNotificationsCount: {
  20. __typename: 'OnlineNotificationsCountPayload',
  21. unseenCount: newCount,
  22. },
  23. },
  24. })
  25. }
  26. describe('selecting a online notification', () => {
  27. beforeEach(async () => {
  28. mockUserCurrent({
  29. firstname: 'John',
  30. lastname: 'Doe',
  31. })
  32. onlineNotificationCountSubscription = mockGraphQLSubscription(
  33. OnlineNotificationsCountDocument,
  34. )
  35. })
  36. it('can delete online notification', async () => {
  37. const readQueryStub = vi.fn()
  38. vi.doMock('#shared/server/apollo/client.ts', () => ({
  39. getApolloClient: () => ({
  40. cache: {
  41. readQuery: readQueryStub,
  42. writeQuery: vi.fn(),
  43. },
  44. }),
  45. }))
  46. const mockApi = mockGraphQLApi(OnlineNotificationsDocument).willResolve([
  47. mockOnlineNotificationQuery([
  48. {
  49. id: '111',
  50. seen: true,
  51. },
  52. {
  53. id: '222',
  54. seen: true,
  55. },
  56. ]),
  57. mockOnlineNotificationQuery([
  58. {
  59. id: '222',
  60. seen: true,
  61. },
  62. ]),
  63. ])
  64. const view = await visitView('/notifications')
  65. await waitUntil(() => mockApi.calls.resolve)
  66. const notificationItems = view.getAllByText('Ticket Title', {
  67. exact: false,
  68. })
  69. expect(notificationItems).toHaveLength(2)
  70. mockGraphQLApi(OnlineNotificationDeleteDocument).willResolve({
  71. onlineNotificationDelete: {
  72. errors: null,
  73. success: true,
  74. },
  75. })
  76. await view.events.click(view.getAllByIconName('delete')[0])
  77. expect(readQueryStub).toHaveBeenCalled()
  78. vi.clearAllMocks()
  79. })
  80. it('can mark all notification as read', async () => {
  81. const mockApi = mockGraphQLApi(OnlineNotificationsDocument).willResolve(
  82. mockOnlineNotificationQuery([
  83. {
  84. seen: false,
  85. },
  86. {
  87. seen: false,
  88. },
  89. {
  90. seen: true,
  91. },
  92. ]),
  93. )
  94. const view = await visitView('/notifications')
  95. await triggerNextOnlineNotificationCount(2)
  96. await waitUntil(() => mockApi.calls.resolve)
  97. expect(view.getAllByLabelText('Notification read')).toHaveLength(1)
  98. mockGraphQLApi(OnlineNotificationMarkAllAsSeenDocument).willResolve({
  99. onlineNotificationMarkAllAsSeen: {
  100. errors: null,
  101. onlineNotifications: [
  102. {
  103. id: '1',
  104. seen: true,
  105. __typename: 'OnlineNotification',
  106. },
  107. {
  108. id: '2',
  109. seen: true,
  110. __typename: 'OnlineNotification',
  111. },
  112. ],
  113. },
  114. })
  115. await view.events.click(view.getByText('Mark all as read'))
  116. expect(view.getAllByLabelText('Notification read')).toHaveLength(3)
  117. await triggerNextOnlineNotificationCount(0)
  118. expect(view.container).not.toHaveTextContent('Mark all as read')
  119. })
  120. it('can not mark notification without relation behind (no longer permission) as read', async () => {
  121. const mockApi = mockGraphQLApi(OnlineNotificationsDocument).willResolve(
  122. mockOnlineNotificationQuery([
  123. {
  124. seen: false,
  125. },
  126. {
  127. seen: false,
  128. metaObject: null,
  129. createdBy: null,
  130. },
  131. {
  132. seen: false,
  133. },
  134. ]),
  135. )
  136. const view = await visitView('/notifications')
  137. await triggerNextOnlineNotificationCount(2)
  138. await waitUntil(() => mockApi.calls.resolve)
  139. mockGraphQLApi(OnlineNotificationMarkAllAsSeenDocument).willResolve({
  140. onlineNotificationMarkAllAsSeen: {
  141. errors: null,
  142. onlineNotifications: [
  143. {
  144. id: '2',
  145. seen: true,
  146. __typename: 'OnlineNotification',
  147. },
  148. ],
  149. },
  150. })
  151. const noRelationNotificationItem = view.getByText(
  152. 'You can no longer see the ticket.',
  153. )
  154. await view.events.click(noRelationNotificationItem)
  155. expect(view.getAllByLabelText('Unread notification')).toHaveLength(3)
  156. })
  157. })