changeset 69:51c1829956d9

Added mock XHR object and a simple bugzilla ajax test
author Atul Varma <avarma@mozilla.com>
date Sun, 25 Apr 2010 11:41:55 -0700
parents 6f5b5b404066
children fbaf7857e7c3
files js/bugzilla.js js/modules/mocks.js js/tests/test-bugzilla.js tests.html
diffstat 4 files changed, 96 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/js/bugzilla.js	Sun Apr 25 10:58:25 2010 -0700
+++ b/js/bugzilla.js	Sun Apr 25 11:41:55 2010 -0700
@@ -30,7 +30,7 @@
       options.success(JSON.parse(xhr.responseText));
     }
 
-    var xhr = new XMLHttpRequest();
+    var xhr = options.xhr ? options.xhr : new XMLHttpRequest();
     var url = this.BASE_URL + options.url;
 
     if (options.data)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/js/modules/mocks.js	Sun Apr 25 11:41:55 2010 -0700
@@ -0,0 +1,56 @@
+Require.modules["mocks"] = function(exports, require) {
+  function MockXMLHttpRequest(delegate) {
+    var self = this;
+
+    var listeners = {
+      "load": [],
+      "progress": [],
+      "error": [],
+      "abort": []
+    };
+
+    function verifyEventType(eventType) {
+      if (!(eventType in listeners))
+        throw new Error("unknown event type: " + eventType);
+    }
+
+    self.addEventListener = function(eventType, handler, useCapture) {
+      verifyEventType(eventType);
+      listeners[eventType].push(handler);
+      delegate("addEventListener", [eventType, handler, useCapture]);
+    };
+
+    self.removeEventListener = function(eventType, handler, useCapture) {
+      verifyEventType(eventType);
+      var index = listeners[eventType].indexOf(handler);
+      if (index == -1)
+        throw new Error("handler not registered for event: " + eventType);
+      listeners[eventType].splice(index, 1);
+      delegate("removeEventListener", [eventType, handler, useCapture]);
+    };
+
+    self.setRequestHeader = function(header, value) {
+      delegate("setRequestHeader", [header, value]);
+    };
+
+    self.send = function(data) {
+      delegate("send", [data]);
+    };
+
+    self.open = function open(method, url) {
+      delegate("open", [method, url]);
+    };
+
+    self.mockTriggerEvent = function(event) {
+      verifyEventType(event.type);
+      listeners.forEach(
+        function(listener) {
+          listener(event);
+        });
+    };
+  }
+
+  exports.xhr = function xhr(delegate) {
+    return new MockXMLHttpRequest(delegate);
+  };
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/js/tests/test-bugzilla.js	Sun Apr 25 11:41:55 2010 -0700
@@ -0,0 +1,36 @@
+function testBugzillaAjax() {
+  var require = Require.build();
+  var actual = [];
+  var expected = [
+    ["open",["GET",
+             "https://api-dev.bugzilla.mozilla.org/latest/configuration"]],
+    ["setRequestHeader",["Accept","application/json"]],
+    ["setRequestHeader",["Content-Type","application/json"]],
+    ["addEventListener",["load",false]],
+    ["send",[null]]
+  ];
+
+  expect(expected.length);
+
+  var xhr = require("mocks").xhr(
+    function xhrDelegate(methodName, args) {
+      var jsonableArgs = [];
+      args.forEach(
+        function(arg) {
+          if (typeof(arg) != "function")
+            jsonableArgs.push(arg);
+        });
+      ;
+      same([methodName, jsonableArgs], expected.splice(0, 1)[0]);
+    });
+
+  var options = {
+    xhr: xhr,
+    url: "/configuration",
+    success: function(result) {
+      console.log("success!");
+    }
+  };
+
+  Bugzilla.ajax(options);
+}
--- a/tests.html	Sun Apr 25 10:58:25 2010 -0700
+++ b/tests.html	Sun Apr 25 11:41:55 2010 -0700
@@ -21,12 +21,15 @@
 <script src="js/jquery.js"></script>
 <script src="js/qunit.js"></script>
 <script src="js/require.js"></script>
+<script src="js/bugzilla.js"></script>
 <script src="js/tests.js"></script>
 
 <!-- CommonJS Modules -->
 <script src="js/modules/date-utils.js"></script>
+<script src="js/modules/mocks.js"></script>
 
 <!-- Test Suites -->
 <script src="js/tests/test-date-utils.js"></script>
 <script src="js/tests/test-require.js"></script>
+<script src="js/tests/test-bugzilla.js"></script>
 </html>