spec: Put protocol registration in beforeEach
authorCheng Zhao <zcbenz@gmail.com>
Thu, 23 Jun 2016 04:11:19 +0000 (13:11 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 23 Jun 2016 04:11:19 +0000 (13:11 +0900)
spec/api-session-spec.js

index 25a09ba..874726d 100644 (file)
@@ -16,10 +16,6 @@ describe('session module', function () {
   var fixtures = path.resolve(__dirname, 'fixtures')
   var w = null
   var url = 'http://127.0.0.1'
-  var partitionName = 'temp'
-  var protocolName = 'sp'
-  const partitionProtocol = session.fromPartition(partitionName).protocol
-  const protocol = session.defaultSession.protocol
 
   beforeEach(function () {
     if (w != null) {
@@ -274,18 +270,25 @@ describe('session module', function () {
   })
 
   describe('session.protocol', function () {
-    beforeEach(function () {
-      if (w != null) {
-        w.destroy()
-      }
+    const partitionName = 'temp'
+    const protocolName = 'sp'
+    const partitionProtocol = session.fromPartition(partitionName).protocol
+    const protocol = session.defaultSession.protocol
+    const handler = function (error, callback) {
+      callback({data: 'test'})
+    }
+
+    beforeEach(function (done) {
+      if (w != null) w.destroy()
       w = new BrowserWindow({
         show: false,
-        width: 400,
-        height: 400,
         webPreferences: {
           partition: partitionName
         }
       })
+      partitionProtocol.registerStringProtocol(protocolName, handler, function (error) {
+        done(error ? error : undefined)
+      })
     })
 
     afterEach(function (done) {
@@ -293,24 +296,14 @@ describe('session module', function () {
     })
 
     it('handles requests from a partition', function (done) {
-      var handler = function (error, callback) {
-        callback({
-          data: 'test'
-        })
-      }
-      partitionProtocol.registerStringProtocol(protocolName, handler, function (error) {
-        if (error) {
-          return done(error)
-        }
-        protocol.isProtocolHandled(protocolName, function (result) {
-          assert.equal(result, false)
-          partitionProtocol.isProtocolHandled(protocolName, function (result) {
-            assert.equal(result, true)
-            w.webContents.on('did-finish-load', function () {
-              done()
-            })
-            w.loadURL(protocolName + "://fake-host")
+      protocol.isProtocolHandled(protocolName, function (result) {
+        assert.equal(result, false)
+        partitionProtocol.isProtocolHandled(protocolName, function (result) {
+          assert.equal(result, true)
+          w.webContents.on('did-finish-load', function () {
+            done()
           })
+          w.loadURL(protocolName + "://fake-host")
         })
       })
     })