Browse Source

changes from review

Shelley Vohr 7 years ago
parent
commit
933c7330a5
2 changed files with 21 additions and 16 deletions
  1. 2 3
      lib/common/api/callbacks-registry.js
  2. 19 13
      spec/api-callbacks-registry-spec.js

+ 2 - 3
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) {

+ 19 - 13
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())
   })
 })