import { assert } from 'chai' import * as sinon from 'sinon' import { setDebug, register, unregister} from '..' describe('Cef Register & UnRegister', () => { // clear screen // tslint:disable-next-line console.log('\x1Bc' + new Date()) it('should return unwatch function and pass right arguments', async () => { // fake window.__cef_register__ = (data) => { if (!data || !data.callback) return null data.callback({ data: 'test data response', taskId: 666, unwatch: () => undefined }) } const { unwatch } = await register('test', { p: 1 }, (resp) => { assert.equal(resp.taskId, 666) assert.equal(resp.data, 'test data response') }, true) assert.isFunction(unwatch) }) it('should call unwatch with right taskId', async () => { // fake window.__cef_register__ = (data) => { if (data.name === 'test' && data.callback) { data.callback({ data: 'test data response', taskId: 666, unwatch: () => undefined }) return null } assert.equal(data.name, 'unwatch') assert.hasAllKeys(data.params, [ 'taskId' ]) assert.isObject(data.params) if (data.params) { assert.equal(data.params.taskId, 666) } } const { unwatch } = await register('test', { p: 1 }, (resp) => { assert.equal(resp.taskId, 666) }, true) unwatch() }) it('should unwatch with async callback run', async () => { let isCallbackCalled = false // fake window.__cef_register__ = (data) => { if (data.name === 'test') { window.setTimeout(() => { if (data && typeof data.callback === 'function') { data.callback({ data: 'test data response', taskId: 666, unwatch: () => undefined }) } isCallbackCalled = true }, 20) return null } assert.equal(data.name, 'unwatch') assert.hasAllKeys(data.params, [ 'taskId' ]) assert.isObject(data.params) if (data.params) { assert.equal(data.params.taskId, 666) } assert.isTrue(isCallbackCalled, 'callback is called after unwatch') } const { unwatch } = await register('test', { p: 1 }, (resp) => { assert.equal(resp.taskId, 666) }, true) unwatch() }) it('should throw error when callback throws', async () => { // fake window.__cef_register__ = (data) => { if (data && typeof data.callback === 'function') { data.callback({ data: 'test data response', taskId: 666, unwatch: () => undefined }) } } try { const { unwatch } = await register('test', { p: 1 }, (resp) => { throw { code: 401, message: 'test error' } }, true) } catch (err) { assert.ok(err) assert.equal(err.code, 401) assert.equal(err.message, 'test error') } }) it('should throw error when response.error exist', async () => { // fake window.__cef_register__ = (data) => { if (data && typeof data.callback === 'function') { data.callback({ error: { code: 401, message: 'test error' }, taskId: 666, unwatch: () => undefined }) } } try { const { unwatch } = await register('test') } catch (err) { assert.ok(err) assert.equal(err.code, 401) assert.equal(err.message, 'test error') } }) it('should do nothing if no callback', done => { // fake window.__cef_register__ = (data) => { if (data && typeof data.callback === 'function') { data.callback({ data: { msg: 'will not be received' }, taskId: 666, unwatch: () => undefined }) } assert.equal(data.name, 'test') done() } register('test') }) it('should echo arguments when enable debug mode', done => { setDebug(true) register('test', null, (ret) => { const { data, taskId, unwatch } = ret assert.equal(data.name, 'test') assert.equal(taskId, -1) assert.hasAllKeys(ret, ['data', 'taskId', 'unwatch']) assert.ok(unwatch) unwatch() done() }) setDebug(false) }) it('should timeout 5s if got no responding', async function () { this.timeout(5500) window.__cef_register__ = (data) => { // not call data.callbac() here } const { taskId, unwatch } = await register('test') assert.equal(taskId, -1) delete window.__cef_register__ }) it('should throw No Invoker error if no __cef_register__', async () => { delete window.__cef_register__ setDebug(false) try { await register('test') } catch (e) { assert.equal(e.message, 'No Invoker!') } }) it('should return null if not provide taskId to unregister', () => { const ret = unregister(null) assert.isNotOk(ret) }) })