123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- 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)
- })
- })
- })
|