Skip to content

Commit

Permalink
[fix] check destroyed before we destroy plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
toxic-johann committed Apr 17, 2018
1 parent c21f872 commit af4c40f
Show file tree
Hide file tree
Showing 8 changed files with 715 additions and 686 deletions.
684 changes: 0 additions & 684 deletions __tests__/dispatcher/plugin.js

This file was deleted.

141 changes: 140 additions & 1 deletion __tests__/dispatcher/plugin/attribute.js
@@ -1,6 +1,6 @@
import Plugin from 'dispatcher/plugin';
import Chimee from 'index';
import { bind } from 'chimee-helper';
import { bind, Log } from 'chimee-helper';

describe("plugin's attributes", () => {
let dispatcher;
Expand Down Expand Up @@ -28,6 +28,7 @@ describe("plugin's attributes", () => {
dispatcher = null;
});


test('penetrate', done => {
const plugin = new Plugin({
id: 'p',
Expand All @@ -43,6 +44,7 @@ describe("plugin's attributes", () => {
plugin.$dom.click();
});


test('set operable to be true and change it', () => {
const plugin = new Plugin({
id: 'o',
Expand All @@ -59,6 +61,7 @@ describe("plugin's attributes", () => {
plugin.$destroy();
});


test('use default operable', () => {
const plugin = new Plugin({
id: 'o',
Expand Down Expand Up @@ -103,5 +106,141 @@ describe("plugin's attributes", () => {
expect(fn).toHaveBeenCalledTimes(0);
plugin.$destroy();
});
test('$fullscreen', () => {
const plugin = new Plugin({ id: 'normal' }, dispatcher);
plugin.$fullscreen();
plugin.$fullscreen(true);
plugin.$fullscreen(false);
plugin.$fullscreen();
plugin.$fullscreen(false, 'container');
plugin.$destroy();
});

test('fullscreen', () => {
const plugin = new Plugin({ id: 'normal' }, dispatcher);
plugin.fullscreen();
plugin.fullscreen(true);
plugin.fullscreen(false);
plugin.fullscreen();
plugin.fullscreen(false, 'container');
plugin.$destroy();
});

describe('$attr & $css', () => {
let plugin;

beforeEach(() => {
plugin = new Plugin({ id: 'normal' }, dispatcher);
});

afterEach(() => {
plugin.$destroy();
});

test('$attr', () => {
dispatcher.videoConfigReady = false;
plugin.$attr('container', 'data-id', 1);
expect(plugin.$attr('container', 'data-id')).toBe(null);
dispatcher.dom.__dispatcher.videoConfigReady = true;
plugin.$attr('data-id', '2');
expect(plugin.$attr('data-id')).toBe('2');
});

test('$css', () => {
expect(plugin.$css('container', 'z-index')).toBe('');
plugin.$css('container', 'z-index', 10);
expect(plugin.$css('container', 'z-index')).toBe('10');
});

test('$attr on video property', () => {
dispatcher.videoConfigReady = false;
plugin.$attr('video', 'controls', true);
expect(plugin.$attr('video', 'controls')).toBe(null);
expect(Log.data.warn[0]).toEqual([ 'chimee',
'normal is tring to set attribute on video before video inited. Please wait until the inited event has benn trigger' ]);
dispatcher.videoConfigReady = true;
plugin.$attr('video', 'controls', true);
expect(plugin.$attr('video', 'controls')).toBe('');
});

test('$attr on video property but it is not in videoconfig', () => {
dispatcher.videoConfigReady = false;
plugin.$attr('video', 'data-controls', true);
expect(plugin.$attr('video', 'data-controls')).toBe(null);
dispatcher.videoConfigReady = true;
dispatcher.dom.__dispatcher.videoConfigReady = true;
plugin.__init(dispatcher.videoConfig);
plugin.$attr('video', 'data-controls', true);
expect(plugin.$attr('video', 'data-controls')).toBe('true');
});
});
});

test('$bump to top', () => {
const level1 = {
name: 'level1',
level: 1,
};
const level2 = {
name: 'level2',
level: 2,
};
const level3 = {
name: 'level3',
level: 3,
};
const olevel1 = {
name: 'olevel1',
level: 1,
inner: false,
};
const olevel2 = {
name: 'olevel2',
level: 2,
inner: false,
};
const olevel3 = {
name: 'olevel3',
level: 3,
inner: false,
};
Chimee.install(level1);
Chimee.install(level2);
Chimee.install(level3);
Chimee.install(olevel1);
Chimee.install(olevel2);
Chimee.install(olevel3);
dispatcher.use('level1');
dispatcher.use('level2');
dispatcher.use('level3');
dispatcher.use('olevel1');
dispatcher.use('olevel2');
dispatcher.use('olevel3');
expect(dispatcher._getTopLevel(true)).toBe(3);
expect(dispatcher._getTopLevel(false)).toBe(3);
expect(dispatcher.plugins.olevel3.$dom.style.zIndex).toBe('4');
expect(dispatcher.plugins.olevel1.$dom.style.zIndex).toBe('2');
expect(dispatcher.plugins.level3.$dom.style.zIndex).toBe('4');
expect(dispatcher.plugins.level1.$dom.style.zIndex).toBe('2');
dispatcher.plugins.level1.$bumpToTop();
dispatcher.plugins.olevel1.$bumpToTop();
expect(dispatcher.plugins.olevel3.$dom.style.zIndex).toBe('3');
expect(dispatcher.plugins.olevel1.$dom.style.zIndex).toBe('4');
expect(dispatcher.plugins.level3.$dom.style.zIndex).toBe('3');
expect(dispatcher.plugins.level1.$dom.style.zIndex).toBe('4');
expect(dispatcher._getTopLevel(true)).toBe(4);
expect(dispatcher._getTopLevel(false)).toBe(4);
});

test('$pluginOrder', () => {
dispatcher.order = [ 'a', 'b', 'c' ];
const plugin = new Plugin({ id: 'p' }, dispatcher);
expect(plugin.$pluginOrder).toBe(dispatcher.order);
});

test('$plugins', () => {
Chimee.install({ name: 'p' });
dispatcher.use('p');
expect(dispatcher.plugins.p.$plugins).toBe(dispatcher.plugins);
});
});
97 changes: 97 additions & 0 deletions __tests__/dispatcher/plugin/error.js
@@ -0,0 +1,97 @@
import Plugin from 'dispatcher/plugin';
import Chimee from 'index';

describe("plugin's error", () => {
let dispatcher;
let player;
let error;

beforeEach(() => {
player = new Chimee({
// 播放地址
src: 'http://cdn.toxicjohann.com/lostStar.mp4',
// 直播:live 点播:vod
isLive: false,
// 编解码容器
box: 'native',
// dom容器
wrapper: 'body',
plugin: [],
events: {},
});

dispatcher = player.__dispatcher;

error = new Error('i am an error');
});

afterEach(() => {
player.destroy();
dispatcher = null;
});

test('beforeCreate', () => {
expect(() => new Plugin({
id: 'err',
beforeCreate() {
throw error;
},
}, dispatcher)).toThrow(error.message);
});

test('create', () => {
expect(() => new Plugin({
id: 'err',
create() {
throw error;
},
}, dispatcher)).toThrow(error.message);
});

test('init', () => {
const plugin = new Plugin({
id: 'err',
init() {
throw error;
},
}, dispatcher);
expect(() => plugin.__init({})).toThrow(error.message);
});

test('inited', () => {
const plugin = new Plugin({
id: 'err',
inited() {
throw error;
},
}, dispatcher);
expect(() => plugin.__inited({})).toThrow(error.message);
});

test('inited with promise', async () => {
const plugin = new Plugin({
id: 'err',
inited() {
return new Promise(() => { throw error; });
},
}, dispatcher);
let catcherr = false;
try {
await plugin.__inited({});
} catch (err) {
catcherr = true;
expect(err).toBe(error);
}
expect(catcherr).toBe(true);
});

test('inited with reject', async () => {
const plugin = new Plugin({
id: 'err',
inited() {
return Promise.reject();
},
}, dispatcher);
expect(plugin.__inited({})).rejects.toBe();
});
});
8 changes: 7 additions & 1 deletion __tests__/dispatcher/plugin/event.js
@@ -1,7 +1,7 @@
import Plugin from 'dispatcher/plugin';
import Chimee from 'index';

describe("plugin's method", () => {
describe("plugin's event", () => {
let dispatcher;
let player;

Expand Down Expand Up @@ -100,4 +100,10 @@ describe("plugin's method", () => {
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).lastCalledWith(1);
});

test('__checkArgsForOnAndOff', () => {
const plugin = new Plugin({ id: 'check' }, dispatcher);
expect(() => plugin.$on()).toThrow('key parameter must be String');
expect(() => plugin.$on('1')).toThrow('fn parameter must be Function');
});
});
59 changes: 59 additions & 0 deletions __tests__/dispatcher/plugin/life.js
Expand Up @@ -126,4 +126,63 @@ describe("plugin's lifecycle", () => {
plugin.$destroy();
expect(createAndDestroyCall).toEqual([ 'create', 'inited', 'destroy' ]);
});

describe('__removeEvents', () => {
let plugin;
beforeEach(() => {
plugin = new Plugin({ id: 're' }, dispatcher);
});
afterEach(() => {
plugin.$destroy();
});

test('empty event', () => {
expect(() => plugin.__removeEvents('wow', () => {})).not.toThrow();
});

test('remove function do not exist', () => {
plugin.__addEvents('wow', () => {});
expect(() => plugin.__removeEvents('wow', () => {})).not.toThrow();
});

test('remove but event is not empty', () => {
const fn = () => {};
plugin.__addEvents('wow', () => {});
plugin.__addEvents('wow', fn);
expect(() => plugin.__removeEvents('wow', fn)).not.toThrow();
});
});

describe('ready and readySync', () => {
test('synchronize', () => {
const plugin = new Plugin({ id: 'a' }, dispatcher);
plugin.__inited();
expect(plugin.readySync).toBe(true);
expect(plugin.ready).resolves.toBe();
});

test('asynchronize with resolve', async () => {
const plugin = new Plugin({
id: 'b',
inited() {
return Promise.resolve(1);
},
}, dispatcher);
plugin.__inited();
await expect(plugin.ready).resolves.toBe(1);
expect(plugin.readySync).toBe(true);
});

test('asynchronize with reject', async () => {
const plugin = new Plugin({
id: 'b',
inited() {
return Promise.reject();
},
}, dispatcher);
plugin.__inited();
await expect(plugin.ready).rejects.toBe();
expect(plugin.readySync).toBe(false);
});
});
});

0 comments on commit af4c40f

Please sign in to comment.