title: How To Test Unstructured Javascript/JQuery
body: <p>I'm faced with trying to add testing to a lot of code like the following. I know I can use mockjax to to intercept the ajax calls. But I don't how to test the <code>$.ajax({...})</code> calls in isolation. I'd appreciate a good refactoring approach, but I'd also like to avoid rewriting the entire app. </p>
  
  <p>I've gotten a start in other areas using qunit, and I like it. But I'm open to other suggestions too. How should I proceed? </p>
  
  <pre><code>function submitSync(repFrom, continuousRep, storedPassword) {
      // var repTriggered = false;
      if (repFrom !== '' &amp;&amp; (storedPassword !== null || storedPassword !== "")) {
          var secureHome = "http://" + homeUser + ":" + storedPassword + "@" + window.location.host + "/" + homeURL;
          var theSource = repFrom.split("/");
          var singleDocumentReplication = (theSource.length === 5);
  
  
  
          /*
          * DELETE existing replications. There will normally be no more than 1.
          * Do not delete replications for application updates.
          * Note that we don't allow the user to create continuous replications.
          */
  
          $.getJSON(currentHost + '/_replicator/_all_docs', function (data) {
              $.each(data.rows, function (i, theRow) {
                  $.ajax({
                      url: currentHost + '/_replicator/' + theRow.id,
                      type: "GET",
                      dataType: 'json',
                      async: false,
                      contentType: "application/json",
                      success: function (doc) {
                          if (doc._id !== "_design/_replicator" &amp;&amp; (typeof doc.source !== 'undefined' &amp;&amp; !doc.source.match(onlineBase + '/' + remoteDB))) {
                              $.ajax({
                                  url: "/_replicator/" + doc._id + "?rev=" + doc._rev,
                                  type: "DELETE",
                                  contentType: "application/json",
                                  success: function () {
                                      console.log('Replication deleted: ' + doc._id + '?rev=' + doc._rev);
                                  }
                              });
                          }
                      }
                  });
              });
          });
  
          if (singleDocumentReplication) {
              var theDoc = theSource[4];
              var repFromBase = repFrom.substr(0, repFrom.indexOf(theDoc) - 1);
  
              $.ajax({
                  url: "/_replicator",
                  type: "POST",
                  data: JSON.stringify({ "source": repFromBase, "target": secureHome,
                      "userCtx": { "name": homeUser, "roles": ["_admin", homeUser] },
                      "continuous": continuousRep,
                      "retries_per_request": 10,
                      "http_connections": 3,
                      "doc_ids": [theDoc]
                  }),
                  contentType: "application/json",
                  error: function () {                    
                      dialog(libLang.noSync);
                  },
                  success: function (message) {
                      if (message) {                        
                          dialog(libLang.synced);
                      }
                      repTriggered = true;
                  }
              });
  
          } else {
  
              $.ajax({
                  url: "/_replicator",
                  type: "POST",
                  data: JSON.stringify({ "source": repFrom, "target": secureHome,
                      "userCtx": { "name": homeUser, "roles": ["_admin", homeUser] },
                      "continuous": continuousRep,
                      "retries_per_request": 10,
                      "http_connections": 3
                  }),
                  contentType: "application/json",
                  error: function () {                    
                      dialog(libLang.noSync);
                  },
                  success: function (message) {
                      if (message) {                        
                          dialog(libLang.synced);
                      }
                      repTriggered = true;
                  }
              });
          }
      }
  }
  </code></pre>