core.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { describe, expect, test } from 'vitest'
  2. import EmitterCore from '../lib/core/emit/core'
  3. // 使用 vitest 的 describe 方法定义测试套件
  4. describe('EmitterCore', () => {
  5. const emitter = new EmitterCore().getInstance()
  6. test('验证 $on $emit 订阅/发布 参数有效性', () => {
  7. const handler = (params, expands, type) => {
  8. expect(params).toBe(1)
  9. expect(expands.test).toBe(1)
  10. expect(type).toBe('test')
  11. }
  12. emitter.$on('test', handler)
  13. emitter.$emit('test', 1, {
  14. test: 1
  15. })
  16. })
  17. test('验证 $on 多次订阅及 $off 取消订阅', () => {
  18. let count = 0
  19. const handler = (val) => {
  20. count += val
  21. }
  22. emitter.$on('test-on', handler)
  23. emitter.$on('test-on', handler)
  24. emitter.$emit('test-on', 1)
  25. // 1 * 2 = 2
  26. expect(count).toBe(2)
  27. // 2 + 2 * 2 = 6
  28. emitter.$emit('test-on', 2)
  29. expect(count).toBe(6)
  30. emitter.$off('test-on')
  31. emitter.$emit('test-on', 2)
  32. expect(count).toBe(6)
  33. })
  34. test('验证 $once 单次订阅', () => {
  35. let count = 0
  36. const handler = () => {
  37. count++
  38. expect(count).toBe(1)
  39. }
  40. emitter.$once('test-once', handler)
  41. emitter.$emit('test-once')
  42. emitter.$emit('test-once')
  43. })
  44. describe('验证 plugins 插件 hooks 冒泡', () => {
  45. const triggerHooks = []
  46. const testContent = 'test-plugin-hooks'
  47. const emitter = new EmitterCore().getInstance()
  48. test('use', () => {
  49. emitter.use({
  50. use(context) {
  51. triggerHooks.push('use')
  52. expect(emitter).toMatchObject(context)
  53. },
  54. emit(type, event, expands) {
  55. triggerHooks.push('emit')
  56. if (type === '*') {
  57. expect(expands.originEventName).toBe(testContent)
  58. } else {
  59. expect(type).toBe(testContent)
  60. }
  61. expect(event).toBe(testContent)
  62. expect(expands.name).toBe(testContent)
  63. },
  64. once(type) {
  65. triggerHooks.push('once')
  66. expect(type).toBe(testContent)
  67. },
  68. on(type) {
  69. triggerHooks.push('on')
  70. expect(type).toBe(testContent)
  71. },
  72. off(type) {
  73. triggerHooks.push('off')
  74. expect(type).toBe(testContent)
  75. }
  76. })
  77. expect(triggerHooks).toEqual(['use'])
  78. })
  79. // 应该触发两次 emit [*, test-plugin]
  80. test('$emit 应该触发两次 emit [*, test-plugin]', () => {
  81. emitter.$emit(testContent, testContent, {
  82. name: testContent
  83. })
  84. expect(triggerHooks).toEqual(['use', 'emit', 'emit'])
  85. })
  86. test('$once 应该触发 [once, on]', () => {
  87. // 应该触发 on [once, on]
  88. emitter.$once(testContent)
  89. expect(triggerHooks).toEqual(['use', 'emit', 'emit', 'once', 'on'])
  90. })
  91. test('$off', () => {
  92. emitter.$off(testContent)
  93. expect(triggerHooks).toEqual(['use', 'emit', 'emit', 'once', 'on', 'off'])
  94. })
  95. test('$on', () => {
  96. emitter.$on(testContent)
  97. expect(triggerHooks).toEqual([
  98. 'use',
  99. 'emit',
  100. 'emit',
  101. 'once',
  102. 'on',
  103. 'off',
  104. 'on'
  105. ])
  106. })
  107. })
  108. })