/* global FakeBlob tus */

var hasStorage = tus.canStoreURLs;

function expectLocalStorage(key, expectedValue) {
  if (!hasStorage) {
    // Do not evaluate expectations on localStorage in node processes
    return;
  }

  expect(localStorage.getItem(key), expectedValue);
}

function setLocalStorage(key, value) {
  if (!hasStorage) {
    // Do not evaluate expectations on localStorage in node processes
    return;
  }

  localStorage.setItem(key, value);
}

function clearLocalStorage() {
  if (!hasStorage) {
    // Do not evaluate expectations on localStorage in node processes
    return;
  }

  localStorage.clear();
}

describe("tus", function () {
  describe("#Upload", function () {

    beforeEach(function () {
      jasmine.Ajax.install();
      clearLocalStorage();
    });

    afterEach(function () {
      jasmine.Ajax.uninstall();
    });

    it("should resume an upload from a stored url", function (done) {
      setLocalStorage("fingerprinted", "/uploads/resuming");

      var file = new FakeBlob("hello world".split(""));
      var options = {
        endpoint: "/uploads",
        onProgress: function () {},
        fingerprint: function () {}
      };
      spyOn(options, "fingerprint").and.returnValue("fingerprinted");
      spyOn(options, "onProgress");

      var upload = new tus.Upload(file, options);
      upload.start();

      expect(options.fingerprint).toHaveBeenCalledWith(file);

      var req = jasmine.Ajax.requests.mostRecent();
      expect(req.url).toBe("/uploads/resuming");
      expect(req.method).toBe("HEAD");
      expect(req.requestHeaders["Tus-Resumable"]).toBe("1.0.0");

      req.respondWith({
        status: 204,
        responseHeaders: {
          "Upload-Length": 11,
          "Upload-Offset": 3
        }
      });

      expect(upload.url).toBe("/uploads/resuming");

      req = jasmine.Ajax.requests.mostRecent();
      expect(req.url).toBe("/uploads/resuming");
      expect(req.method).toBe("PATCH");
      expect(req.requestHeaders["Tus-Resumable"]).toBe("1.0.0");
      expect(req.requestHeaders["Upload-Offset"]).toBe(3);
      expect(req.contentType()).toBe("application/offset+octet-stream");
      expect(req.params.size).toBe(11 - 3);

      req.respondWith({
        status: 204,
        responseHeaders: {
          "Upload-Offset": 11
        }
      });

      expect(options.onProgress).toHaveBeenCalledWith(11, 11);
      done();
    });

    it("should store upload urls", function (done) {
      var file = new FakeBlob("hello world".split(""));
      var options = {
        endpoint: "/uploads",
        fingerprint: function () {}
      };
      spyOn(options, "fingerprint").and.returnValue("fingerprinted");

      var upload = new tus.Upload(file, options);
      upload.start();

      expect(options.fingerprint).toHaveBeenCalledWith(file);

      var req = jasmine.Ajax.requests.mostRecent();
      expect(req.url).toBe("/uploads");
      expect(req.method).toBe("POST");

      req.respondWith({
        status: 201,
        responseHeaders: {
          Location: "/uploads/blargh"
        }
      });

      expect(upload.url).toBe("/uploads/blargh");
      expectLocalStorage("fingerprinted", "/uploads/blargh");
      done();
    });
  });
});

@@ -24,609 +24,8 @@
 */%0A%0A
-var hasStorage = tus.canStoreURLs;%0A%0Afunction expectLocalStorage(key, expectedValue) %7B%0A  if (!hasStorage) %7B%0A    // Do not evaluate expectations on localStorage in node processes%0A    return;%0A  %7D%0A%0A  expect(localStorage.getItem(key), expectedValue);%0A%7D%0A%0Afunction setLocalStorage(key, value) %7B%0A  if (!hasStorage) %7B%0A    // Do not evaluate expectations on localStorage in node processes%0A    return;%0A  %7D%0A%0A  localStorage.setItem(key, value);%0A%7D%0A%0Afunction clearLocalStorage() %7B%0A  if (!hasStorage) %7B%0A    // Do not evaluate expectations on localStorage in node processes%0A    return;%0A  %7D%0A%0A  localStorage.clear();%0A%7D%0A%0A
 desc
@@ -156,14 +156,9 @@
     
-clearL
+l
 ocal
@@ -164,16 +164,22 @@
 lStorage
+.clear
 ();%0A    
@@ -332,12 +332,9 @@
     
-setL
+l
 ocal
@@ -332,32 +332,40 @@
     localStorage
+.setItem
 (%22fingerprinted%22
@@ -2559,17 +2559,18 @@
   expect
-L
+(l
 ocalStor
@@ -2572,16 +2572,24 @@
 lStorage
+.getItem
 (%22finger
@@ -2588,34 +2588,40 @@
 (%22fingerprinted%22
-, 
+)).toBe(
 %22/uploads/blargh

Remove helpers with localStorage in browser tests