From a3a60e2f87d989c4e609d6cf638f4d30bbac1f29 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Mon, 21 Nov 2011 19:25:01 +0000
Subject: [PATCH] Cleaning up the channel unit test so that the channel is torn
 down after every test.

* Make the WinMock a function so that we can easily reset its state after every test.
* Do the winMock init in the startup function to reduce cruft.
* Close the channel after every test, causing a channel reset.
---
 resources/static/test/qunit/qunit.js          |  1 +
 .../test/qunit/resources/channel_unit_test.js | 80 +++++++++----------
 2 files changed, 37 insertions(+), 44 deletions(-)

diff --git a/resources/static/test/qunit/qunit.js b/resources/static/test/qunit/qunit.js
index 0142d75e0..91859529a 100644
--- a/resources/static/test/qunit/qunit.js
+++ b/resources/static/test/qunit/qunit.js
@@ -23,6 +23,7 @@ steal.plugins(
       "/shared/validation",
       "/shared/helpers",
 
+      "/dialog/resources/channel",
       "/dialog/resources/helpers",
 
       "/dialog/controllers/page_controller",
diff --git a/resources/static/test/qunit/resources/channel_unit_test.js b/resources/static/test/qunit/resources/channel_unit_test.js
index 108d13d87..026230334 100644
--- a/resources/static/test/qunit/resources/channel_unit_test.js
+++ b/resources/static/test/qunit/resources/channel_unit_test.js
@@ -34,21 +34,20 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
-steal.then("/dialog/resources/channel", function() {
+steal.then(function() {
   "use strict";
 
-  var channel = BrowserID.Channel;
-
-  var navMock = {
-    id: {},
-  };
+  var channel = BrowserID.Channel,
+      winMock,
+      navMock = { id: {} };
 
   // Mock in the window object as well as the frame relay
-  var winMock = {
-    location: {
-      href: "browserid.org/sign_in#1234",
-      hash: "#1234"
-    },
+  var WinMock = function() {
+    this.location.href = "browserid.org/sign_in#1234";
+    this.location.hash = "#1234";
+  }
+  WinMock.prototype = {
+    location: {},
     opener: {
       frames: {
         "1234": {
@@ -57,9 +56,9 @@ steal.then("/dialog/resources/channel", function() {
               registerClient: function(methods) {
                 // mock of the registerClient function in the BrowserID.Channel.
                 methods["get"]("foo.com", {}, function onComplete(success) {
-                  winMock.success = success;
+                  WinMock.success = success;
                 }, function onerror(error) {
-                  winMock.error = error;
+                  WinMock.error = error;
                 });
               },
               unregisterClient: function() {
@@ -73,13 +72,29 @@ steal.then("/dialog/resources/channel", function() {
 
   module("resources/channel", {
     setup: function() {
+      winMock = new WinMock();
+      channel.init({
+        window: winMock,
+        navigator: navMock
+      });
     },
 
     teardown: function() {
-      channel.init({
-        window: window,
-        navigator: navigator
-      });
+      if(channel) {
+        try {
+          channel.close();
+        } catch(e) {
+          if(e.toString() !== "relay frame not found") {
+            // re-throw if the error is other than expected.
+            throw e;
+          }
+        }
+
+        channel.init({
+          window: window,
+          navigator: navigator
+        });
+      }
     }
   });
 
@@ -88,15 +103,10 @@ steal.then("/dialog/resources/channel", function() {
   });
 
   test("IFRAME channel with assertion", function() {
-    channel.init({
-      window: winMock,
-      navigator: navMock
-    });
-
     channel.open({
       getVerifiedEmail: function(origin, onsuccess, onerror) {
         onsuccess("assertion");
-        equal(winMock.success, "assertion", "assertion made it to the relay");
+        equal(WinMock.success, "assertion", "assertion made it to the relay");
         start();
       }
     });
@@ -105,15 +115,10 @@ steal.then("/dialog/resources/channel", function() {
   });
 
   test("IFRAME channel with null assertion", function() {
-    channel.init({
-      window: winMock,
-      navigator: navMock
-    });
-
     channel.open({
       getVerifiedEmail: function(origin, onsuccess, onerror) {
         onsuccess(null);
-        strictEqual(winMock.success, null, "null assertion made it to the relay");
+        strictEqual(WinMock.success, null, "null assertion made it to the relay");
         start();
       }
     });
@@ -122,15 +127,10 @@ steal.then("/dialog/resources/channel", function() {
   });
 
   test("IFRAME channel relaying error", function() {
-    channel.init({
-      window: winMock,
-      navigator: navMock
-    });
-
     channel.open({
       getVerifiedEmail: function(origin, onsuccess, onerror) {
         onerror("error");
-        strictEqual(winMock.error, "error", "error made it to the relay");
+        strictEqual(WinMock.error, "error", "error made it to the relay");
         start();
       }
     });
@@ -139,15 +139,7 @@ steal.then("/dialog/resources/channel", function() {
   });
 
   test("IFRAME channel with error on open", function() {
-    channel.close();
-
-    var winMockWithoutRelay = $.extend(true, {}, winMock);
-    delete winMockWithoutRelay.opener.frames['1234'];
-
-    channel.init({
-      window: winMockWithoutRelay,
-      navigator: navMock
-    });
+    delete winMock.opener.frames['1234'];
 
     // Do this manually so we can test if getVerifiedEmail gets called.
     try {
-- 
GitLab