|
@@ -0,0 +1,241 @@
|
|
|
|
+import { describe, expect, test, it } from 'vitest'
|
|
|
|
+import Emitter from '../lib/core/emit'
|
|
|
|
+
|
|
|
|
+describe('Emitter', () => {
|
|
|
|
+ function testOptions(options) {
|
|
|
|
+ const emitter = Emitter(options)
|
|
|
|
+
|
|
|
|
+ for (const key in options) {
|
|
|
|
+ if (options[key]) {
|
|
|
|
+ it(`enable options.${key}`, () => {
|
|
|
|
+ expect(emitter.$plugins).toHaveProperty(key)
|
|
|
|
+ })
|
|
|
|
+ } else {
|
|
|
|
+ it(`no enable options.${key}`, () => {
|
|
|
|
+ expect(emitter.$plugins).not.toHaveProperty(key)
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ describe('options 配置启用插件验证', () => {
|
|
|
|
+ testOptions({
|
|
|
|
+ replay: true,
|
|
|
|
+ logger: false,
|
|
|
|
+ cross: true,
|
|
|
|
+ customEvent: true
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ testOptions({
|
|
|
|
+ replay: false,
|
|
|
|
+ logger: true,
|
|
|
|
+ cross: false,
|
|
|
|
+ customEvent: false
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ describe('注册插件验证', () => {
|
|
|
|
+ it('options.plugins 自定义注册插件验证', () => {
|
|
|
|
+ const pluginName = 'addPlugin'
|
|
|
|
+ const emitter = Emitter({
|
|
|
|
+ plugins: [
|
|
|
|
+ {
|
|
|
|
+ name: pluginName,
|
|
|
|
+ use() {
|
|
|
|
+ expect(true).toBe(true)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ],
|
|
|
|
+ replay: false,
|
|
|
|
+ logger: false,
|
|
|
|
+ cross: false,
|
|
|
|
+ customEvent: false
|
|
|
|
+ })
|
|
|
|
+ expect(emitter.$plugins).toHaveProperty(pluginName)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ it('use 自定义注册插件验证', () => {
|
|
|
|
+ const pluginName = 'addPlugin'
|
|
|
|
+ const emitter = Emitter({
|
|
|
|
+ replay: false,
|
|
|
|
+ logger: false,
|
|
|
|
+ cross: false,
|
|
|
|
+ customEvent: false
|
|
|
|
+ })
|
|
|
|
+ emitter.use({
|
|
|
|
+ name: pluginName,
|
|
|
|
+ use() {
|
|
|
|
+ expect(true).toBe(true)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ expect(emitter.$plugins).toHaveProperty(pluginName)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ it('重复注册插件拦截验证', () => {
|
|
|
|
+ const emitter = Emitter({
|
|
|
|
+ replay: true,
|
|
|
|
+ logger: false,
|
|
|
|
+ cross: false,
|
|
|
|
+ customEvent: false
|
|
|
|
+ })
|
|
|
|
+ expect(() => {
|
|
|
|
+ return emitter.use({
|
|
|
|
+ name: 'replay',
|
|
|
|
+ use() {
|
|
|
|
+ expect(false).toBe(true)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }).toThrowError('Plugin')
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ describe('插件 replay 功能验证', () => {
|
|
|
|
+ test('$emit(..., { replay: true }) 重放功能验证', () => {
|
|
|
|
+ const emitter = Emitter({
|
|
|
|
+ replay: true,
|
|
|
|
+ logger: false,
|
|
|
|
+ cross: false,
|
|
|
|
+ customEvent: false
|
|
|
|
+ })
|
|
|
|
+ const testContent = 'test-replay'
|
|
|
|
+ let result = null
|
|
|
|
+
|
|
|
|
+ emitter.$emit(testContent, testContent, { replay: true })
|
|
|
|
+ emitter.$on(testContent, (event, expands) => {
|
|
|
|
+ result = event
|
|
|
|
+ expect(expands.replay).toBe(true)
|
|
|
|
+ expect(expands.fromReplay).toBe(true)
|
|
|
|
+ })
|
|
|
|
+ expect(result).toBe(testContent)
|
|
|
|
+ emitter.$off(testContent)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ describe('$on(..., { replay: false }) 禁用重放功能验证', () => {
|
|
|
|
+ const emitter = Emitter({
|
|
|
|
+ replay: true,
|
|
|
|
+ logger: false,
|
|
|
|
+ cross: false,
|
|
|
|
+ customEvent: false
|
|
|
|
+ })
|
|
|
|
+ const testContent = 'test-replay'
|
|
|
|
+ let result = null
|
|
|
|
+
|
|
|
|
+ emitter.$emit(testContent, testContent, { replay: true })
|
|
|
|
+ emitter.$on(
|
|
|
|
+ testContent,
|
|
|
|
+ (event, expands = {}) => {
|
|
|
|
+ result = event
|
|
|
|
+ expect(expands.replay).not.toBe(true)
|
|
|
|
+ expect(expands.fromReplay).not.toBe(true)
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ replay: false
|
|
|
|
+ }
|
|
|
|
+ )
|
|
|
|
+ it('设置 replay: false 后不触发 handler', () => {
|
|
|
|
+ expect(result).toBe(null)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ it('正常触发后续 $emit handler', () => {
|
|
|
|
+ emitter.$emit(testContent, testContent)
|
|
|
|
+ expect(result).toBe(testContent)
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ describe('插件 customEvent 功能验证', () => {
|
|
|
|
+ const realEventName = 'custom-event-emitter'
|
|
|
|
+
|
|
|
|
+ function commonTest(testContent, event, expands, type) {
|
|
|
|
+ test('应该正常获取 event, type', () => {
|
|
|
|
+ expect(type).toBe(testContent)
|
|
|
|
+ expect(event).toBe(testContent)
|
|
|
|
+ })
|
|
|
|
+ test('[*] expands.customEvent === true', () => {
|
|
|
|
+ expect(expands.customEvent).toBe(true)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ test('[!] expands.fromCustomEvent === undefined', () => {
|
|
|
|
+ expect(expands.fromCustomEvent).toBe(undefined)
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ describe('$emit(..., { customEvent: true }) 发送及 $on 附加参数 功能验证', () => {
|
|
|
|
+ const emitter = Emitter({
|
|
|
|
+ replay: false,
|
|
|
|
+ logger: false,
|
|
|
|
+ cross: false,
|
|
|
|
+ customEvent: true
|
|
|
|
+ })
|
|
|
|
+ const testContent = 'test-custom-event'
|
|
|
|
+
|
|
|
|
+ emitter.$on(testContent, (...args) => {
|
|
|
|
+ commonTest(testContent, ...args)
|
|
|
|
+ })
|
|
|
|
+ emitter.$emit(testContent, testContent, { customEvent: true })
|
|
|
|
+ emitter.$off(testContent)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ describe('window.addEventListener 接收 $emit(...) 发送 功能验证', () => {
|
|
|
|
+ const emitter = Emitter({
|
|
|
|
+ replay: false,
|
|
|
|
+ logger: false,
|
|
|
|
+ cross: false,
|
|
|
|
+ customEvent: true
|
|
|
|
+ })
|
|
|
|
+ const testContent = 'test-custom-event-listeners'
|
|
|
|
+ let result = 0
|
|
|
|
+ const handler = (e) => {
|
|
|
|
+ result++
|
|
|
|
+ const { type, event, expands } = e.detail
|
|
|
|
+ if (type !== '*') {
|
|
|
|
+ commonTest(testContent, event, expands, type)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ window.addEventListener(realEventName, handler)
|
|
|
|
+
|
|
|
|
+ emitter.$emit(testContent, testContent, { customEvent: true })
|
|
|
|
+
|
|
|
|
+ test('应该触发两次 handler [*, type]', () => {
|
|
|
|
+ expect(result).toBe(2)
|
|
|
|
+ })
|
|
|
|
+ window.removeEventListener(realEventName, handler)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ describe('window.dispatchEvent 发送 $on(...) 接收 功能验证', () => {
|
|
|
|
+ const emitter = Emitter({
|
|
|
|
+ replay: false,
|
|
|
|
+ logger: false,
|
|
|
|
+ cross: false,
|
|
|
|
+ customEvent: true
|
|
|
|
+ })
|
|
|
|
+ const testContent = 'test-custom-event-dispatchEvent'
|
|
|
|
+
|
|
|
|
+ emitter.$on(testContent, (event, expands, type) => {
|
|
|
|
+ test('应该正常获取 event, type', () => {
|
|
|
|
+ expect(type).toBe(testContent)
|
|
|
|
+ expect(event).toBe(testContent)
|
|
|
|
+ })
|
|
|
|
+ test('[!] expands.customEvent === undefined', () => {
|
|
|
|
+ expect(expands.customEvent).toBe(undefined)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ test('[*] expands.fromCustomEvent === true', () => {
|
|
|
|
+ expect(expands.fromCustomEvent).toBe(true)
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ window.dispatchEvent(
|
|
|
|
+ new CustomEvent(realEventName, {
|
|
|
|
+ detail: {
|
|
|
|
+ type: testContent,
|
|
|
|
+ event: testContent,
|
|
|
|
+ expands: {}
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ emitter.$off(testContent)
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+})
|