CommonNotifications.spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { nextTick } from 'vue'
  3. import {
  4. renderComponent,
  5. type ExtendedRenderResult,
  6. } from '#tests/support/components/index.ts'
  7. import { waitForTimeout } from '#tests/support/utils.ts'
  8. import CommonNotifications from '../CommonNotifications.vue'
  9. import { NotificationTypes } from '../types.ts'
  10. import { useNotifications } from '../useNotifications.ts'
  11. let wrapper: ExtendedRenderResult
  12. const message = 'Test Notification'
  13. beforeEach(() => {
  14. const { clearAllNotifications } = useNotifications()
  15. clearAllNotifications()
  16. wrapper = renderComponent(CommonNotifications, { shallow: false })
  17. })
  18. describe('CommonNotifications.vue', () => {
  19. it('renders notification with passed message', async () => {
  20. const { notify } = useNotifications()
  21. await notify({
  22. message,
  23. type: NotificationTypes.Warn,
  24. })
  25. expect(wrapper.getByTestId('notification')).toBeInTheDocument()
  26. expect(wrapper.getByTestId('notification')).toHaveTextContent(message)
  27. })
  28. it('automatically removes notification after timeout', async () => {
  29. const { notify } = useNotifications()
  30. await notify({
  31. message,
  32. type: NotificationTypes.Warn,
  33. durationMS: 10,
  34. })
  35. await waitForTimeout(20)
  36. expect(wrapper.queryByTestId('notification')).not.toBeInTheDocument()
  37. })
  38. it('does not remove persistent notifications', async () => {
  39. const { notify } = useNotifications()
  40. await notify({
  41. message,
  42. type: NotificationTypes.Warn,
  43. durationMS: 10,
  44. persistent: true,
  45. })
  46. await waitForTimeout(20)
  47. expect(wrapper.getByTestId('notification')).toBeInTheDocument()
  48. })
  49. it('executes a callback on click', async () => {
  50. expect.assertions(2)
  51. const { notify } = useNotifications()
  52. let test = false
  53. await notify({
  54. message,
  55. type: NotificationTypes.Warn,
  56. callback: () => {
  57. expect(test).toBe(false)
  58. test = true
  59. },
  60. })
  61. await wrapper.events.click(wrapper.getByText(message))
  62. expect(test).toBe(true)
  63. })
  64. it('renders multiple notifications at the same time', async () => {
  65. const { notify, notifications } = useNotifications()
  66. await notify({
  67. message: `${message} - ${notifications.value.length}`,
  68. type: NotificationTypes.Warn,
  69. })
  70. await notify({
  71. message: `${message} - ${notifications.value.length}`,
  72. type: NotificationTypes.Warn,
  73. })
  74. await notify({
  75. message: `${message} - ${notifications.value.length}`,
  76. type: NotificationTypes.Warn,
  77. })
  78. expect(wrapper.getAllByTestId('notification')).toHaveLength(3)
  79. })
  80. it('clears all notifications', async () => {
  81. const { notify, notifications, clearAllNotifications } = useNotifications()
  82. await notify({
  83. message: `${message} - ${notifications.value.length}`,
  84. type: NotificationTypes.Warn,
  85. })
  86. await notify({
  87. message: `${message} - ${notifications.value.length}`,
  88. type: NotificationTypes.Warn,
  89. })
  90. await notify({
  91. message: `${message} - ${notifications.value.length}`,
  92. type: NotificationTypes.Warn,
  93. })
  94. clearAllNotifications()
  95. await nextTick()
  96. expect(notifications.value).toHaveLength(0)
  97. expect(wrapper.queryAllByTestId('notification')).toHaveLength(0)
  98. })
  99. it('renders notification with icon', async () => {
  100. const { notify } = useNotifications()
  101. await notify({
  102. message,
  103. type: NotificationTypes.Warn,
  104. })
  105. expect(wrapper.getByIconName('info')).toBeInTheDocument()
  106. })
  107. })