Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created June 18, 2011 22:39
Show Gist options
  • Save francescoagati/1033570 to your computer and use it in GitHub Desktop.
Save francescoagati/1033570 to your computer and use it in GitHub Desktop.
jasmine jquery and deferred various examples
checkDeferred=(df,fn) ->
callback = jasmine.createSpy()
df.then(callback)
waitsFor -> callback.callCount > 0
runs ->
#expect(callback).toHaveBeenCalled()
fn.apply @,callback.mostRecentCall.args if fn
checkJson = (path,fn) ->
checkDeferred $.getJSON(path),fn
describe "sum", ->
x=0
beforeEach ->
x=1
describe "mmm",->
it "x==1", ->
expect(x).toEqual(1)
describe "getJson",->
it "should be a==1", ->
checkJson "temporeale.json", (json)->
console.log(json)
expect(json.a).toEqual(1)
it "should load 3 times temporeale.json", ->
checkDeferred $.when($.getJSON("temporeale.json"),$.getJSON("temporeale.json"),$.getJSON("temporeale.json")), (splats...) ->
expect(splats.length).toEqual(3)
describe "chek then and done", ->
next=false
list=[]
beforeEach ->
$.getJSON("temporeale.json")
.then (json1)->
console.log json1
list.push json1
$.getJSON("temporeale.json")
.then (json2)->
console.log json2
list.push json2
$.getJSON("temporeale.json")
.then (json3)->
console.log json3
list.push json3
.done ->
next=true
waitsFor -> next==true
it "should populate list with 3 json", ->
expect(list.length).toEqual(3)
it "shuold 3 element be a with 1", ->
expect(list[2].a).toEqual(1)
(function() {
var checkDeferred, checkJson;
var __slice = Array.prototype.slice;
checkDeferred = function(df, fn) {
var callback;
callback = jasmine.createSpy();
df.then(callback);
waitsFor(function() {
return callback.callCount > 0;
});
return runs(function() {
if (fn) {
return fn.apply(this, callback.mostRecentCall.args);
}
});
};
checkJson = function(path, fn) {
return checkDeferred($.getJSON(path), fn);
};
describe("sum", function() {
var x;
x = 0;
beforeEach(function() {
return x = 1;
});
return describe("mmm", function() {
return it("x==1", function() {
return expect(x).toEqual(1);
});
});
});
describe("getJson", function() {
it("should be a==1", function() {
return checkJson("temporeale.json", function(json) {
console.log(json);
return expect(json.a).toEqual(1);
});
});
it("should load 3 times temporeale.json", function() {
return checkDeferred($.when($.getJSON("temporeale.json"), $.getJSON("temporeale.json"), $.getJSON("temporeale.json")), function() {
var splats;
splats = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return expect(splats.length).toEqual(3);
});
});
return describe("chek then and done", function() {
var list, next;
next = false;
list = [];
beforeEach(function() {
$.getJSON("temporeale.json").then(function(json1) {
console.log(json1);
list.push(json1);
return $.getJSON("temporeale.json");
}).then(function(json2) {
console.log(json2);
list.push(json2);
return $.getJSON("temporeale.json");
}).then(function(json3) {
console.log(json3);
return list.push(json3);
}).done(function() {
return next = true;
});
return waitsFor(function() {
return next === true;
});
});
it("should populate list with 3 json", function() {
return expect(list.length).toEqual(3);
});
return it("shuold 3 element be a with 1", function() {
return expect(list[2].a).toEqual(1);
});
});
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment