notify.test.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { nextTick } from 'vue'
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { rAF } from '@element-plus/test-utils/tick'
  4. import Notification, { closeAll } from '../src/notify'
  5. import { ElNotification } from '..'
  6. import type { NotificationHandle } from '../src/notification'
  7. const selector = '.el-notification'
  8. describe('Notification on command', () => {
  9. afterEach(() => {
  10. closeAll()
  11. })
  12. it('it should get component handle', async () => {
  13. const handle = Notification()
  14. await rAF()
  15. expect(document.querySelector(selector)).toBeDefined()
  16. handle.close()
  17. await rAF()
  18. await nextTick()
  19. expect(document.querySelector(selector)).toBeNull()
  20. expect(
  21. document.querySelector('[class^="container_notification"]')
  22. ).toBeNull()
  23. })
  24. it('it should be able to render vnode', async () => {
  25. const testClassName = 'test-classname'
  26. const { close } = Notification({
  27. duration: 0,
  28. message: <div class={testClassName}>test-content</div>,
  29. })
  30. await rAF()
  31. expect(document.querySelector(`.${testClassName}`)).toBeDefined()
  32. close()
  33. })
  34. it('it should be able to close notification by manually close', async () => {
  35. const { close } = Notification({
  36. duration: 0,
  37. })
  38. await rAF()
  39. const element = document.querySelector(selector)
  40. expect(element).toBeDefined()
  41. close()
  42. await rAF()
  43. await nextTick()
  44. expect(document.querySelector(selector)).toBeNull()
  45. })
  46. it('it should close all notifications', async () => {
  47. const notifications: NotificationHandle[] = []
  48. const onClose = vi.fn()
  49. for (let i = 0; i < 4; i++) {
  50. notifications.push(
  51. Notification({
  52. onClose,
  53. duration: 0,
  54. })
  55. )
  56. }
  57. // vi.runAllTicks()
  58. await rAF()
  59. expect(document.querySelectorAll(selector).length).toBe(4)
  60. closeAll()
  61. // vi.runAllTicks()
  62. await rAF()
  63. expect(onClose).toHaveBeenCalledTimes(notifications.length)
  64. expect(document.querySelectorAll(selector).length).toBe(0)
  65. })
  66. it('it should be able to render all types notification', () => {
  67. for (const type of ['success', 'warning', 'error', 'info'] as const) {
  68. Notification[type]({})
  69. expect(document.querySelector(`.el-icon-${type}`)).toBeDefined()
  70. }
  71. })
  72. it('it should appendTo specified HTMLElement', async () => {
  73. const htmlElement = document.createElement('div')
  74. const handle = Notification({
  75. appendTo: htmlElement,
  76. })
  77. await rAF()
  78. expect(htmlElement.querySelector(selector)).toBeDefined()
  79. handle.close()
  80. await rAF()
  81. await nextTick()
  82. expect(htmlElement.querySelector(selector)).toBeNull()
  83. })
  84. it('it should appendTo specified selector', async () => {
  85. const htmlElement = document.createElement('div')
  86. htmlElement.classList.add('notification-manager')
  87. document.body.appendChild(htmlElement)
  88. const handle = Notification({
  89. appendTo: '.notification-manager',
  90. })
  91. await rAF()
  92. expect(htmlElement.querySelector(selector)).toBeDefined()
  93. handle.close()
  94. await rAF()
  95. await nextTick()
  96. expect(htmlElement.querySelector(selector)).toBeNull()
  97. })
  98. describe('context inheritance', () => {
  99. it('should globally inherit context correctly', () => {
  100. expect(ElNotification._context).toBe(null)
  101. const testContext = {
  102. config: {
  103. globalProperties: {},
  104. },
  105. _context: {},
  106. }
  107. ElNotification.install?.(testContext as any)
  108. expect(ElNotification._context).not.toBe(null)
  109. expect(ElNotification._context).toBe(testContext._context)
  110. // clean up
  111. ElNotification._context = null
  112. })
  113. })
  114. })