diff --git a/lib/common/api/callbacks-registry.js b/lib/common/api/callbacks-registry.js index 31ba6a08f1..4283961951 100644 --- a/lib/common/api/callbacks-registry.js +++ b/lib/common/api/callbacks-registry.js @@ -20,7 +20,6 @@ class CallbacksRegistry { const regexp = /at (.*)/gi const stackString = (new Error()).stack - let ref let filenameAndLine let match @@ -29,7 +28,7 @@ class CallbacksRegistry { if (location.includes('native')) continue if (location.includes('electron.asar')) continue - ref = /([^/^)]*)\)?$/gi.exec(location) + const ref = /([^/^)]*)\)?$/gi.exec(location) filenameAndLine = ref[1] break } @@ -40,7 +39,7 @@ class CallbacksRegistry { } get (id) { - return this.callbacks[id] != null ? this.callbacks[id] : function () {} + return this.callbacks[id] || function () {} } apply (id, ...args) { diff --git a/spec/api-callbacks-registry-spec.js b/spec/api-callbacks-registry-spec.js index 866a3921e9..33030edd3b 100644 --- a/spec/api-callbacks-registry-spec.js +++ b/spec/api-callbacks-registry-spec.js @@ -1,7 +1,7 @@ -const assert = require('assert') +const {assert} = require('chai') const {CallbacksRegistry} = require('electron') -describe('CallbacksRegistry module', () => { +describe.only('CallbacksRegistry module', () => { let registry = null beforeEach(() => { @@ -10,33 +10,39 @@ describe('CallbacksRegistry module', () => { it('adds a callback to the registry', () => { const cb = () => [1, 2, 3, 4, 5] - const id = registry.add(cb) - assert.equal(id, 1) + const key = registry.add(cb) + + assert.exists(key) }) it('returns a specified callback if it is in the registry', () => { const cb = () => [1, 2, 3, 4, 5] - registry.add(cb) + const key = registry.add(cb) + const callback = registry.get(key) - const callback = registry.get(1) assert.equal(callback.toString(), cb.toString()) }) it('returns an empty function if the cb doesnt exist', () => { const callback = registry.get(1) - assert.equal(callback.toString(), 'function () {}') + + assert.isFunction(callback) }) it('removes a callback to the registry', () => { const cb = () => [1, 2, 3, 4, 5] - const id = registry.add(cb) - assert.equal(id, 1) + const key = registry.add(cb) + + assert.exists(key) + + const beforeCB = registry.get(key) - const beforeCB = registry.get(1) assert.equal(beforeCB.toString(), cb.toString()) - registry.remove(1) - const afterCB = registry.get(1) - assert.equal(afterCB.toString(), 'function () {}') + registry.remove(key) + const afterCB = registry.get(key) + + assert.isFunction(afterCB) + assert.notEqual(afterCB.toString(), cb.toString()) }) })