aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/AngularSpec.js36
-rw-r--r--test/ResourceSpec.js159
-rw-r--r--test/testabilityPatch.js7
3 files changed, 200 insertions, 2 deletions
diff --git a/test/AngularSpec.js b/test/AngularSpec.js
index 65a32279..043f7bf3 100644
--- a/test/AngularSpec.js
+++ b/test/AngularSpec.js
@@ -7,4 +7,38 @@ describe('Angular', function(){
scope.updateView();
expect(onUpdateView).wasCalled();
});
-}); \ No newline at end of file
+});
+
+describe("copy", function(){
+ it("should return same object", function (){
+ var obj = {};
+ var arr = [];
+ assertSame(obj, copy({}, obj));
+ assertSame(arr, copy([], arr));
+ });
+
+ it("should copy array", function(){
+ var src = [1, {name:"value"}];
+ var dst = [{key:"v"}];
+ assertSame(dst, copy(src, dst));
+ assertEquals([1, {name:"value"}], dst);
+ assertEquals({name:"value"}, dst[1]);
+ assertNotSame(src[1], dst[1]);
+ });
+
+ it('should copy empty array', function() {
+ var src = [];
+ var dst = [{key: "v"}];
+ assertEquals([], copy(src, dst));
+ assertEquals([], dst);
+ });
+
+ it("should copy object", function(){
+ var src = {a:{name:"value"}};
+ var dst = {b:{key:"v"}};
+ assertSame(dst, copy(src, dst));
+ assertEquals({a:{name:"value"}}, dst);
+ assertEquals(src.a, dst.a);
+ assertNotSame(src.a, dst.a);
+ });
+});
diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js
new file mode 100644
index 00000000..562d6500
--- /dev/null
+++ b/test/ResourceSpec.js
@@ -0,0 +1,159 @@
+function MockXHR(){
+ this.expectations = {
+ 'GET': {},
+ 'POST': {},
+ 'DELETE': {}
+ };
+ this.queue = [];
+}
+MockXHR.prototype = {
+ method: function(verb, url, data, callback) {
+ if (verb == 'POST')
+ url += '|' + angular.toJson(data);
+ var response = this.expectations[verb][url];
+ if (!response)
+ throw "No expectation for " + verb + " on '" + url + "'.";
+ this.queue.push(function(){
+ callback(response);
+ });
+ },
+
+ expectGET: function(url) {
+ var self = this;
+ return {
+ respond: function(response){
+ self.expectations.GET[url] = response;
+ }
+ };
+ },
+
+ expectDELETE: function(url) {
+ var self = this;
+ return {
+ respond: function(response){
+ self.expectations.DELETE[url] = response;
+ }
+ };
+ },
+
+ expectPOST: function(url) {
+ var self = this;
+ return {
+ data: function(data){
+ return {
+ respond: function(response){
+ self.expectations.POST[url + '|' + angular.toJson(data)] = response;
+ }
+ };
+ }
+ };
+ },
+
+ flush: function(){
+ while(this.queue.length) {
+ this.queue.shift()();
+ }
+ }
+};
+
+describe("resource", function() {
+ var xhr, resource, CreditCard, callback;
+
+ beforeEach(function(){
+ xhr = new MockXHR();
+ resource = new ResourceFactory(xhr);
+ CreditCard = resource.route('/CreditCard/:id:verb', {id:'id.key'}, {
+ charge:{
+ method:'POST',
+ params:{verb:'!charge'}
+ }
+ });
+ callback = jasmine.createSpy();
+ });
+
+ it("should build resource", function(){
+ expect(typeof CreditCard).toBe('function');
+ expect(typeof CreditCard.get).toBe('function');
+ expect(typeof CreditCard.save).toBe('function');
+ expect(typeof CreditCard.remove).toBe('function');
+ expect(typeof CreditCard['delete']).toBe('function');
+ expect(typeof CreditCard.query).toBe('function');
+ });
+
+ it("should create resource", function(){
+ xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123, name:'misko'});
+
+ var cc = CreditCard.save({name:'misko'}, callback);
+ nakedExpect(cc).toEqual({name:'misko'});
+ expect(callback).wasNotCalled();
+ xhr.flush();
+ nakedExpect(cc).toEqual({id:123, name:'misko'});
+ expect(callback).wasCalledWith(cc);
+ });
+
+ it("should read resource", function(){
+ xhr.expectGET("/CreditCard/123").respond({id:123, number:'9876'});
+ var cc = CreditCard.get({id:123}, callback);
+ expect(cc instanceof CreditCard).toBeTruthy();
+ nakedExpect(cc).toEqual({});
+ expect(callback).wasNotCalled();
+ xhr.flush();
+ nakedExpect(cc).toEqual({id:123, number:'9876'});
+ expect(callback).wasCalledWith(cc);
+ });
+
+ it("should update resource", function(){
+ xhr.expectPOST('/CreditCard/123').data({id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'});
+
+ var cc = CreditCard.save({id:{key:123}, name:'misko'}, callback);
+ nakedExpect(cc).toEqual({id:{key:123}, name:'misko'});
+ expect(callback).wasNotCalled();
+ xhr.flush();
+ nakedExpect(cc).toEqual({id:{key:123}, name:'rama'});
+ expect(callback).wasCalledWith(cc);
+ });
+
+ it("should query resource", function(){
+ xhr.expectGET("/CreditCard?key=value").respond([{id:1}, {id:2}]);
+
+ var ccs = CreditCard.query({key:'value'}, callback);
+ expect(ccs).toEqual([]);
+ expect(callback).wasNotCalled();
+ xhr.flush();
+ nakedExpect(ccs).toEqual([{id:1}, {id:2}]);
+ expect(callback).wasCalledWith(ccs);
+ });
+
+ it('should delete resource', function(){
+ xhr.expectDELETE("/CreditCard/123").respond({});
+
+ CreditCard.remove({id:123}, callback);
+ expect(callback).wasNotCalled();
+ xhr.flush();
+ nakedExpect(callback.mostRecentCall.args).toEqual([{}]);
+ });
+
+ it('should post charge verb', function(){
+ xhr.expectPOST('/CreditCard/123!charge?amount=10').data({auth:'abc'}).respond({success:'ok'});
+
+ CreditCard.charge({id:123, amount:10},{auth:'abc'}, callback);
+ });
+
+ it('should create on save', function(){
+ xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123});
+ var cc = new CreditCard();
+ expect(cc.$get).not.toBeDefined();
+ expect(cc.$query).not.toBeDefined();
+ expect(cc.$remove).toBeDefined();
+ expect(cc.$save).toBeDefined();
+
+ cc.name = 'misko';
+ cc.$save(callback);
+ nakedExpect(cc).toEqual({name:'misko'});
+ xhr.flush();
+ nakedExpect(cc).toEqual({id:123});
+ expect(callback).wasCalledWith(cc);
+ });
+
+
+});
diff --git a/test/testabilityPatch.js b/test/testabilityPatch.js
index 293553da..e7ebb386 100644
--- a/test/testabilityPatch.js
+++ b/test/testabilityPatch.js
@@ -10,6 +10,11 @@ msie = jQuery.browser.msie;
function noop(){}
jstd = jstestdriver;
+dump = _(jstd.console.log).bind(jstd.console);
+
+function nakedExpect(obj) {
+ return expect(angular.fromJson(angular.toJson(obj)));
+};
swfobject = {
createSwf:function() {
@@ -137,4 +142,4 @@ function assertThrows(error, fn){
}
log = noop;
-error = noop; \ No newline at end of file
+error = noop;