online-notification-actions.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 mark all notification as read', async () => {
  37. const mockApi = mockGraphQLApi(OnlineNotificationsDocument).willResolve(
  38. mockOnlineNotificationQuery([
  39. {
  40. seen: false,
  41. },
  42. {
  43. seen: false,
  44. },
  45. {
  46. seen: true,
  47. },
  48. ]),
  49. )
  50. const view = await visitView('/notifications')
  51. await triggerNextOnlineNotificationCount(2)
  52. await waitUntil(() => mockApi.calls.resolve)
  53. expect(view.getAllByLabelText('Notification read')).toHaveLength(1)
  54. mockGraphQLApi(OnlineNotificationMarkAllAsSeenDocument).willResolve({
  55. onlineNotificationMarkAllAsSeen: {
  56. errors: null,
  57. onlineNotifications: [
  58. {
  59. id: '1',
  60. seen: true,
  61. __typename: 'OnlineNotification',
  62. },
  63. {
  64. id: '2',
  65. seen: true,
  66. __typename: 'OnlineNotification',
  67. },
  68. ],
  69. },
  70. })
  71. await view.events.click(view.getByText('Mark all as read'))
  72. expect(view.getAllByLabelText('Notification read')).toHaveLength(3)
  73. await triggerNextOnlineNotificationCount(0)
  74. expect(view.container).not.toHaveTextContent('Mark all as read')
  75. })
  76. it('can mark notification without relation behind (no longer permission) as read', async () => {
  77. const mockApi = mockGraphQLApi(OnlineNotificationsDocument).willResolve(
  78. mockOnlineNotificationQuery([
  79. {
  80. seen: false,
  81. },
  82. {
  83. seen: false,
  84. metaObject: null,
  85. createdBy: null,
  86. },
  87. {
  88. seen: false,
  89. },
  90. ]),
  91. )
  92. const view = await visitView('/notifications')
  93. await triggerNextOnlineNotificationCount(2)
  94. await waitUntil(() => mockApi.calls.resolve)
  95. mockGraphQLApi(OnlineNotificationMarkAllAsSeenDocument).willResolve({
  96. onlineNotificationMarkAllAsSeen: {
  97. errors: null,
  98. onlineNotifications: [
  99. {
  100. id: '2',
  101. seen: true,
  102. __typename: 'OnlineNotification',
  103. },
  104. ],
  105. },
  106. })
  107. const noRelationNotificationItem = view.getByText(
  108. 'You can no longer see the ticket.',
  109. )
  110. await view.events.click(noRelationNotificationItem)
  111. expect(view.getAllByLabelText('Notification read')).toHaveLength(1)
  112. })
  113. it('can delete online notification', async () => {
  114. const mockApi = mockGraphQLApi(OnlineNotificationsDocument).willResolve([
  115. mockOnlineNotificationQuery([
  116. {
  117. id: '111',
  118. seen: true,
  119. },
  120. {
  121. id: '222',
  122. seen: true,
  123. },
  124. ]),
  125. mockOnlineNotificationQuery([
  126. {
  127. id: '222',
  128. seen: true,
  129. },
  130. ]),
  131. ])
  132. const view = await visitView('/notifications')
  133. await waitUntil(() => mockApi.calls.resolve)
  134. let notificationItems = view.getAllByText('Ticket Title', {
  135. exact: false,
  136. })
  137. expect(notificationItems).toHaveLength(2)
  138. mockGraphQLApi(OnlineNotificationDeleteDocument).willResolve({
  139. onlineNotificationDelete: {
  140. errors: null,
  141. success: true,
  142. },
  143. })
  144. await view.events.click(view.getAllByIconName('delete')[0])
  145. notificationItems = view.getAllByText('Ticket Title', {
  146. exact: false,
  147. })
  148. expect(notificationItems).toHaveLength(1)
  149. })
  150. })