diff --git a/__tests__/chimee/event.js b/__tests__/chimee/event.js index babe8cab..ff8b33fc 100644 --- a/__tests__/chimee/event.js +++ b/__tests__/chimee/event.js @@ -46,10 +46,80 @@ describe('chimee event method', () => { expect(() => player.emitSync('click')).not.toThrow(); }); - test('emit', async () => { - expect(() => player.emit(1)).toThrow(); - await expect(() => player.emit('click')).not.toThrow(); - await expect(() => player.emit('play')).not.toThrow(); + describe('emnit', () => { + let fn; + beforeEach(() => { + fn = jest.fn(); + }); + + test('emitSync', async () => { + expect(() => player.emitSync(1)).toThrow(); + expect(() => player.emitSync('click')).not.toThrow(); + expect(() => player.emitSync('play')).not.toThrow(); + }); + + test('normal emitSync test', async () => { + player.on('hello', fn); + player.emitSync('hello', 1); + expect(fn).toHaveBeenCalledTimes(1); + expect(fn).lastCalledWith(1); + }); + + test('emitSync with target', async () => { + const fn1 = jest.fn(); + player.on('hello', fn1, { target: 'kernel' }); + player.on('hello', fn); + player.emitSync('hello', 1); + expect(fn).toHaveBeenCalledTimes(1); + expect(fn).lastCalledWith(1); + expect(fn1).toHaveBeenCalledTimes(0); + player.emitSync({ + name: 'hello', + target: 'kernel', + }, 2); + expect(fn).toHaveBeenCalledTimes(1); + expect(fn).lastCalledWith(1); + expect(fn1).toHaveBeenCalledTimes(1); + expect(fn1).lastCalledWith(2); + }); + }); + + describe('emnit', () => { + let fn; + beforeEach(() => { + fn = jest.fn(); + }); + + test('emit', async () => { + expect(() => player.emit(1)).toThrow(); + await expect(() => player.emit('click')).not.toThrow(); + await expect(() => player.emit('play')).not.toThrow(); + }); + + test('normal emit test', async () => { + player.on('hello', fn); + await player.emit('hello', 1); + expect(fn).toHaveBeenCalledTimes(1); + expect(fn).lastCalledWith(1); + }); + + test('emit with target', async () => { + const fn1 = jest.fn(); + player.on('hello', fn1, { target: 'kernel' }); + player.on('hello', fn); + await player.emit('hello', 1); + expect(fn).toHaveBeenCalledTimes(1); + expect(fn).lastCalledWith(1); + expect(fn1).toHaveBeenCalledTimes(0); + await player.emit({ + name: 'hello', + target: 'kernel', + }, 2); + expect(fn).toHaveBeenCalledTimes(1); + expect(fn).lastCalledWith(1); + expect(fn1).toHaveBeenCalledTimes(1); + expect(fn1).lastCalledWith(2); + }); }); test('once', () => { diff --git a/doc/zh-cn/api/chimee-api.md b/doc/zh-cn/api/chimee-api.md index 1f8744e8..5991b28a 100644 --- a/doc/zh-cn/api/chimee-api.md +++ b/doc/zh-cn/api/chimee-api.md @@ -778,6 +778,18 @@ chimee 作为 video 的映射,自然也是可以监听 video 上的事件。 - fn - 类型:`Function` - 含义:处理函数 + - options + - 类型:`Object` + - 含义:可以穿入一些事件相关的属性 + - 备注:可选参数 + - target + - 类型:'kernel' | 'container' | 'wrapper' | 'video' | 'video-dom' | 'plugin' | 'esFullscreen' + - 含义:标明事件需要绑定的目标对象。 + - 默认:会根据事件名智能判断 + - stage + - 类型:'before' | 'after' | 'main' | '_' + - 含义:标明事件监听的阶段 + - 默认:会根据事件名智能判断 > 利用 on 可以直接监听任何发生在 video 上的事件。 > @@ -800,6 +812,18 @@ chimee 作为 video 的映射,自然也是可以监听 video 上的事件。 - fn - 类型:`Function` - 含义:处理函数 + - options + - 类型:`Object` + - 含义:可以穿入一些事件相关的属性 + - 备注:可选参数 + - target + - 类型:'kernel' | 'container' | 'wrapper' | 'video' | 'video-dom' | 'plugin' | 'esFullscreen' + - 含义:标明事件需要绑定的目标对象。 + - 默认:会根据事件名智能判断 + - stage + - 类型:'before' | 'after' | 'main' | '_' + - 含义:标明事件监听的阶段 + - 默认:会根据事件名智能判断 ### once @@ -811,14 +835,27 @@ chimee 作为 video 的映射,自然也是可以监听 video 上的事件。 - fn - 类型:`Function` - 含义:处理函数 + - options + - 类型:`Object` + - 含义:可以穿入一些事件相关的属性 + - 备注:可选参数 + - target + - 类型:'kernel' | 'container' | 'wrapper' | 'video' | 'video-dom' | 'plugin' | 'esFullscreen' + - 含义:标明事件需要绑定的目标对象。 + - 默认:会根据事件名智能判断 + - stage + - 类型:'before' | 'after' | 'main' | '_' + - 含义:标明事件监听的阶段 + - 默认:会根据事件名智能判断 ### emit - 含义:触发一次由异步函数处理的事件 - 参数: - key - - 类型:`string` + - 类型:`string | { name: string, target: string }` - 含义:事件名称 + - 备注:target 为 'kernel' | 'container' | 'wrapper' | 'video' | 'video-dom' | 'plugin' | 'esFullscreen' - 其余自定义参数 一般用于触发如 play, pause 等行为,和直接调用`play`、`pause`等方法一致。也可以利用此和插件进行沟通。 diff --git a/karma.conf.js b/karma.conf.js index 0e62f75e..d84dace2 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -21,7 +21,7 @@ module.exports = function(config) { // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter - frameworks: [ 'mocha' ], + frameworks: [ 'mocha', 'chai' ], // list of files / patterns to load in the browser files: [ @@ -45,7 +45,7 @@ module.exports = function(config) { rollupPreprocessor: { output: { - format: 'umd', // Helps prevent naming collisions. + format: 'iife', // Helps prevent naming collisions. name: camelize(name), // Required for 'iife' format. }, plugins: rollupConfig.plugins, diff --git a/package.json b/package.json index 5273d326..2f9745a4 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,8 @@ "postmerge": "sh tool/auto-npm-install.sh", "test": "npm run karma && jest --coverage && node env-check.js", "unit": "jest --coverage --watch", - "lint": "eslint . --fix && flow", - "flow": "flow", + "lint": "eslint . --fix && flow check", + "flow": "flow check", "start": "rollup -c build/rollup.config.dev.js -w", "build": "npm run b-common && npm run b-es && npm run b-umd && npm run b-min", "b-common": "rollup -c build/rollup.config.common.js", @@ -79,6 +79,7 @@ "husky": "^0.14.3", "jest": "^22.4.3", "karma": "^2.0.2", + "karma-chai": "^0.1.0", "karma-firefox-launcher": "^1.1.0", "karma-mocha": "^1.3.0", "karma-mocha-reporter": "^2.2.5", diff --git a/src/dispatcher/dom.js b/src/dispatcher/dom.js index bcf319a4..b7133c03 100644 --- a/src/dispatcher/dom.js +++ b/src/dispatcher/dom.js @@ -69,6 +69,8 @@ export default class Dom { this.__dispatcher = dispatcher; if (!isElement(wrapper) && !isString(wrapper)) throw new TypeError(`Wrapper can only be string or HTMLElement, but not ${typeof wrapper}`); const $wrapper = $(wrapper); + // TODO: we have to decalre length for wrapper + // $FlowFixMe: we have to decalre length here if ($wrapper.length === 0) { throw new TypeError('Can not get dom node accroding wrapper. Please check your wrapper'); } diff --git a/src/dispatcher/index.js b/src/dispatcher/index.js index dfaae52a..494e4e11 100644 --- a/src/dispatcher/index.js +++ b/src/dispatcher/index.js @@ -153,7 +153,7 @@ export default class Dispatcher { * @param {Object|string} option you can just set a plugin name or plugin config * @return {Promise} */ - use(option: string | PluginOption) { + use(option: string | PluginOption): Promise<*> { if (isString(option)) option = { name: option, alias: undefined }; if (!isObject(option) || (isObject(option) && !isString(option.name))) { throw new TypeError('pluginConfig do not match requirement'); @@ -464,7 +464,7 @@ export default class Dispatcher { * @param {Array} configs a set of plugin config * @return {Array} a set of Promise indicate the plugin install stage */ - _initUserPlugin(configs: Array = []) { + _initUserPlugin(configs: Array = []): Promise<*>[] { if (!isArray(configs)) { /* istanbul ignore else */ if (process.env.NODE_ENV !== 'production') Log.warn('Dispatcher', `UserConfig.plugin can only by an Array, but not "${configs}" in ${typeof configs}`); diff --git a/src/dispatcher/video-wrapper.js b/src/dispatcher/video-wrapper.js index e7da5d89..b31529e1 100644 --- a/src/dispatcher/video-wrapper.js +++ b/src/dispatcher/video-wrapper.js @@ -236,7 +236,15 @@ export default @autobindClass() class VideoWrapper { * @param {...args} args */ @alias('emit') - $emit(key: string, ...args: any) { + $emit(key: string | { + target: binderTarget, + name: string, + }, ...args: any) { + let target: binderTarget | void; + if (isObject(key) && isString(key.name) && isString(key.target)) { + target = key.target; + key = key.name; + } if (!isString(key)) throw new TypeError('emit key parameter must be String'); /* istanbul ignore else */ if (process.env.NODE_ENV !== 'production' && domEvents.indexOf(key.replace(/^\w_/, '')) > -1) { @@ -245,6 +253,7 @@ export default @autobindClass() class VideoWrapper { this.__dispatcher.binder.emit({ name: key, id: this.__id, + target, }, ...args); } @@ -254,11 +263,20 @@ export default @autobindClass() class VideoWrapper { * @param {...args} args */ @alias('emitSync') - $emitSync(key: string, ...args: any) { + $emitSync(key: string | { + target: binderTarget, + name: string, + }, ...args: any) { + let target; + if (isObject(key) && isString(key.name) && isString(key.target)) { + target = key.target; + key = key.name; + } if (!isString(key)) throw new TypeError('emitSync key parameter must be String'); return this.__dispatcher.binder.emitSync({ name: key, id: this.__id, + target, }, ...args); } diff --git a/tests/switch-kernel.js b/tests/switch-kernel.js index 2f6936ae..494d07e3 100644 --- a/tests/switch-kernel.js +++ b/tests/switch-kernel.js @@ -1,6 +1,4 @@ import Chimee from '../src/index'; -import chai from 'chai'; -const { expect } = chai; describe('check for chimee switch kernel function', () => { it('should not trigger volume change', async function() { this.timeout(10000); @@ -22,6 +20,7 @@ describe('check for chimee switch kernel function', () => { } catch (error) { console.error(error); } + console.warn(expect); expect(count).to.equal(0); }); });