Commit dd15403f authored by Laura Schlimmer's avatar Laura Schlimmer
Browse files

test and revise setURLQueryString

parent 1c587036
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ FnordMetric.UnitTests = function() {

  var testParseQueryString = function() {
    function test(querystr, expected) {
      results++;
      var result = FnordMetric.util.parseQueryString(querystr);
      result = JSON.stringify(result);
      expected = JSON.stringify(expected);
@@ -52,4 +53,39 @@ FnordMetric.UnitTests = function() {
    test("undefined", {path: "", query_params:{}});
  }();

  var testSetUrlQueryString = function() {
    function test(hash, query_params, encode, push_state, expected) {
      results++;
      FnordMetric.util.setURLQueryString(hash, query_params, encode, push_state);
      var result = window.location.hash;
      if (result != expected) {
        results.bad++;
        console.log("Testing setURLQueryString expected "
          + expected + ", but was " + result);
      }
    }
    test("metric_list", {}, false, false, "#metric_list");
    test(undefined,
        {innerView : "metric",
          innerViewValue : "http_status_codes"}, 
        false,
        false,
        "");

    test("metric_list", 
        {innerView : undefined,
          innerViewValue : "http_status_codes"},
        false,
        false,
        "#metric_list");

    test("query_playground",
          {innerView : "sql_query",
          innerViewValue : "SELECT * FROM `http_status_codes`;"},
          true,
          true,
          "#query_playground?sql_query=SELECT%20*%20FROM%20%60http_status_codes%60%3B");
    test("", {}, false, false, "");
  }();

}
+30 −16
Original line number Diff line number Diff line
@@ -57,10 +57,23 @@ FnordMetric.util.parseQueryString = function(qstr) {
  };
}

/**
  * builds a querystring from the query_params, attachs it to the hash
  * and sets the url
  * @param query_params should be like query_params in parseQueryString
  * @param encode (boolean) determines if innerViewValue should be URIencoded
  * @param push_state (boolena) determines if the url should be
  * added to the browser's history
  */
FnordMetric.util.setURLQueryString = function(hash, query_params, encode, push_state) {
  var path = "#"+hash+"?";
  if (hash === undefined || hash === "undefined") {
    window.location.hash = "";
    return;
  }
  var path = "#" + hash;

  path += query_params.innerView + "=";
  if ("innerView" in query_params && query_params.innerView != undefined) {
    path += "?" + query_params.innerView + "=";
    path += (encode)?
      encodeURIComponent(query_params.innerViewValue) :
      query_params.innerViewValue;
@@ -76,6 +89,7 @@ FnordMetric.util.setURLQueryString = function(hash, query_params, encode, push_s
          "=" + query_params[param];
      }
    }
  }

  if (push_state) {
    window.history.pushState({url:path}, "#", path);