unittests/tests/JSONSerializer.js
author hamidouk
Thu, 13 Oct 2011 17:13:44 +0200
branchpopcorn-port
changeset 75 f5a7299bd0ff
parent 69 3f7a2e8a948f
child 103 2dfd89e91c3a
permissions -rw-r--r--
changes to the unit tests.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
     1
function test_JSONSerializer() {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
     2
  module("JSON Serializer tests", 
69
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
     3
    { setup: function() {      
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
     4
      this.dt = new IriSP.DataLoader();
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
     5
      }
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
     6
    }
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
     7
    );    
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
     8
    
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
     9
    test("should return the correct JSON", function() {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
    10
      var arr = ["ab", {"de" : "fg"}, "lp"];
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
    11
      var serializer = new IriSP.JSONSerializer(this.dt);
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
    12
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
    13
      equal(serializer.serialize(arr), JSON.stringify(arr), "assert that the outputted json is correct");
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
    14
    });
69
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    15
    
75
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 69
diff changeset
    16
    test("sync()", function() {
69
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    17
      this.xhr = this.sandbox.useFakeXMLHttpRequest();
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    18
      this.requests = [];
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    19
      this.xhr.onCreate = function (request) {
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    20
        this.requests.push(request);
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    21
      };
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    22
      
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    23
      var response_array = [{ media: 12, content: "Hey there" }];
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    24
      var response_string = JSON.stringify(response_array);
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    25
  
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    26
      var spy_callback = this.spy();
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    27
      var ser = new IriSP.JSONSerializer(this.dt, "/url");
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    28
      
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    29
      ser.sync(spy_callback);
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    30
      
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    31
      equals(this.xhr.requests.length, 1, "the mock ajax object should have received the request");
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    32
      this.xhr.requests[0].respond(200, { "Content-Type": "application/json" },
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    33
                             response_string);
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    34
        
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    35
      ok(spy_callback.calledOnce, "callback called");
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    36
      ok(spy_callback.calledWith(response_array), "callback called with correct value");
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    37
      deepEqual(ser._data, response_array, "the internal variable is initialized to the correct value");
3f7a2e8a948f added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents: 65
diff changeset
    38
    });
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
    39
75
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 69
diff changeset
    40
    test("currentMedia should return the current media", function() {
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 69
diff changeset
    41
      var ser = new IriSP.JSONSerializer(this.dt, "/url");
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 69
diff changeset
    42
      /* FIXME: actually get something instead of monkey-patching the struct */
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 69
diff changeset
    43
      ser._data = {}
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 69
diff changeset
    44
      ser._data.medias = [0];
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 69
diff changeset
    45
      equal(ser.currentMedia(), 0, "currentMedia() returns the correct value");
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 69
diff changeset
    46
    });
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents:
diff changeset
    47
};