index.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { describe, expect, test, it } from 'vitest'
  2. import Emitter from '../lib/core/emit'
  3. describe('Emitter', () => {
  4. function testOptions(options) {
  5. const emitter = Emitter(options)
  6. for (const key in options) {
  7. if (options[key]) {
  8. it(`enable options.${key}`, () => {
  9. expect(emitter.$plugins).toHaveProperty(key)
  10. })
  11. } else {
  12. it(`no enable options.${key}`, () => {
  13. expect(emitter.$plugins).not.toHaveProperty(key)
  14. })
  15. }
  16. }
  17. }
  18. describe('options 配置启用插件验证', () => {
  19. testOptions({
  20. replay: true,
  21. logger: false,
  22. cross: true,
  23. customEvent: true
  24. })
  25. testOptions({
  26. replay: false,
  27. logger: true,
  28. cross: false,
  29. customEvent: false
  30. })
  31. })
  32. describe('注册插件验证', () => {
  33. it('options.plugins 自定义注册插件验证', () => {
  34. const pluginName = 'addPlugin'
  35. const emitter = Emitter({
  36. plugins: [
  37. {
  38. name: pluginName,
  39. use() {
  40. expect(true).toBe(true)
  41. }
  42. }
  43. ],
  44. replay: false,
  45. logger: false,
  46. cross: false,
  47. customEvent: false
  48. })
  49. expect(emitter.$plugins).toHaveProperty(pluginName)
  50. })
  51. it('use 自定义注册插件验证', () => {
  52. const pluginName = 'addPlugin'
  53. const emitter = Emitter({
  54. replay: false,
  55. logger: false,
  56. cross: false,
  57. customEvent: false
  58. })
  59. emitter.use({
  60. name: pluginName,
  61. use() {
  62. expect(true).toBe(true)
  63. }
  64. })
  65. expect(emitter.$plugins).toHaveProperty(pluginName)
  66. })
  67. it('重复注册插件拦截验证', () => {
  68. const emitter = Emitter({
  69. replay: true,
  70. logger: false,
  71. cross: false,
  72. customEvent: false
  73. })
  74. expect(() => {
  75. return emitter.use({
  76. name: 'replay',
  77. use() {
  78. expect(false).toBe(true)
  79. }
  80. })
  81. }).toThrowError('Plugin')
  82. })
  83. })
  84. describe('插件 replay 功能验证', () => {
  85. test('$emit(..., { replay: true }) 重放功能验证', () => {
  86. const emitter = Emitter({
  87. replay: true,
  88. logger: false,
  89. cross: false,
  90. customEvent: false
  91. })
  92. const testContent = 'test-replay'
  93. let result = null
  94. emitter.$emit(testContent, testContent, { replay: true })
  95. emitter.$on(testContent, (event, expands) => {
  96. result = event
  97. expect(expands.replay).toBe(true)
  98. expect(expands.fromReplay).toBe(true)
  99. })
  100. expect(result).toBe(testContent)
  101. emitter.$off(testContent)
  102. })
  103. describe('$on(..., { replay: false }) 禁用重放功能验证', () => {
  104. const emitter = Emitter({
  105. replay: true,
  106. logger: false,
  107. cross: false,
  108. customEvent: false
  109. })
  110. const testContent = 'test-replay'
  111. let result = null
  112. emitter.$emit(testContent, testContent, { replay: true })
  113. emitter.$on(
  114. testContent,
  115. (event, expands = {}) => {
  116. result = event
  117. expect(expands.replay).not.toBe(true)
  118. expect(expands.fromReplay).not.toBe(true)
  119. },
  120. {
  121. replay: false
  122. }
  123. )
  124. it('设置 replay: false 后不触发 handler', () => {
  125. expect(result).toBe(null)
  126. })
  127. it('正常触发后续 $emit handler', () => {
  128. emitter.$emit(testContent, testContent)
  129. expect(result).toBe(testContent)
  130. })
  131. })
  132. })
  133. describe('插件 customEvent 功能验证', () => {
  134. const realEventName = 'custom-event-emitter'
  135. function commonTest(testContent, event, expands, type) {
  136. test('应该正常获取 event, type', () => {
  137. expect(type).toBe(testContent)
  138. expect(event).toBe(testContent)
  139. })
  140. test('[*] expands.customEvent === true', () => {
  141. expect(expands.customEvent).toBe(true)
  142. })
  143. test('[!] expands.fromCustomEvent === undefined', () => {
  144. expect(expands.fromCustomEvent).toBe(undefined)
  145. })
  146. }
  147. describe('$emit(..., { customEvent: true }) 发送及 $on 附加参数 功能验证', () => {
  148. const emitter = Emitter({
  149. replay: false,
  150. logger: false,
  151. cross: false,
  152. customEvent: true
  153. })
  154. const testContent = 'test-custom-event'
  155. emitter.$on(testContent, (...args) => {
  156. commonTest(testContent, ...args)
  157. })
  158. emitter.$emit(testContent, testContent, { customEvent: true })
  159. emitter.$off(testContent)
  160. })
  161. describe('window.addEventListener 接收 $emit(...) 发送 功能验证', () => {
  162. const emitter = Emitter({
  163. replay: false,
  164. logger: false,
  165. cross: false,
  166. customEvent: true
  167. })
  168. const testContent = 'test-custom-event-listeners'
  169. let result = 0
  170. const handler = (e) => {
  171. result++
  172. const { type, event, expands } = e.detail
  173. if (type !== '*') {
  174. commonTest(testContent, event, expands, type)
  175. }
  176. }
  177. window.addEventListener(realEventName, handler)
  178. emitter.$emit(testContent, testContent, { customEvent: true })
  179. test('应该触发两次 handler [*, type]', () => {
  180. expect(result).toBe(2)
  181. })
  182. window.removeEventListener(realEventName, handler)
  183. })
  184. describe('window.dispatchEvent 发送 $on(...) 接收 功能验证', () => {
  185. const emitter = Emitter({
  186. replay: false,
  187. logger: false,
  188. cross: false,
  189. customEvent: true
  190. })
  191. const testContent = 'test-custom-event-dispatchEvent'
  192. emitter.$on(testContent, (event, expands, type) => {
  193. test('应该正常获取 event, type', () => {
  194. expect(type).toBe(testContent)
  195. expect(event).toBe(testContent)
  196. })
  197. test('[!] expands.customEvent === undefined', () => {
  198. expect(expands.customEvent).toBe(undefined)
  199. })
  200. test('[*] expands.fromCustomEvent === true', () => {
  201. expect(expands.fromCustomEvent).toBe(true)
  202. })
  203. })
  204. window.dispatchEvent(
  205. new CustomEvent(realEventName, {
  206. detail: {
  207. type: testContent,
  208. event: testContent,
  209. expands: {}
  210. }
  211. })
  212. )
  213. emitter.$off(testContent)
  214. })
  215. })
  216. })