Last active
December 19, 2015 02:29
-
-
Save haiiro-shimeji/5883400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TestCase("Applicative style on jQuery Deferred", { | |
setUp: function() { | |
Function.prototype.$ = function() { | |
return $.when.apply($, arguments) | |
.then(this.bind(null)); | |
}; | |
Function.prototype.$$ = function(obj) { | |
var f = this; | |
return function() { | |
return $.when.apply($, arguments) | |
.then(f.bind(obj)); | |
}; | |
}; | |
var origDefer = $.Deferred; | |
$.Deferred = function(func) { | |
var d = origDefer(func); | |
d.$ = function(name) { | |
return $.when(this) | |
.then(function(obj) { return obj[name]; }); | |
}; | |
return d; | |
}; | |
this.add3 = function(a, b, c) { | |
return a + b + c; | |
}; | |
this.fetchObjectA = function() { | |
return $.Deferred().resolve({ | |
value: 5 | |
}); | |
}; | |
this.fetchObjectB = function(a) { | |
return $.Deferred().resolve({ | |
value: a.value + 3 | |
}); | |
}; | |
this.makeResult = function(a, b) { | |
return a.value + b.value; | |
}; | |
}, | |
teaDown: function() { | |
delete Function.prototype.$; | |
}, | |
'test usual jQuery Deferred': function() { | |
$.when(1, 2, 3) | |
.then(this.add3) | |
.done(function(result) { | |
assertEquals(result, 6); | |
}) | |
.fail(function() { | |
assertTrue(false); | |
}); | |
}, | |
'test of applicative style': function() { | |
assertEquals.$(6, this.add3.$(1, 2, 3)); | |
}, | |
'test of applicative style\'': function() { | |
var a0 = $.Deferred().resolve(1); | |
var a1 = $.Deferred().resolve(2); | |
var a2 = $.Deferred().resolve(3); | |
assertEquals.$(6, this.add3.$(a0, a1, a2)); | |
}, | |
'test of error caused': function() { | |
var a0 = $.Deferred().resolve(1); | |
var a1 = $.Deferred().resolve(2); | |
var a2 = $.Deferred().reject(); | |
//assertEquals() does not run because of some arguments is failed. | |
assertEquals.$(-1, this.add3.$(a0, a1, a2)) | |
.done(function() { | |
assertTrue(false); | |
}) | |
.fail(function() { | |
assertTrue(true); | |
}); | |
}, | |
//more complicated example | |
'test of usual async fetch mothods': function() { | |
this.fetchObjectA() | |
.then((function(a) { | |
return $.when(a, this.fetchObjectB(a)); | |
}).bind(this)) | |
.then((function(a, b) { | |
assertEquals(13, this.makeResult(a, b)); | |
}).bind(this)) | |
.fail(function() { | |
assertTrue(false); | |
}); | |
//or | |
var a = this.fetchObjectA(); | |
var b = $.when(a).then(this.fetchObjectB); | |
$.when(13, $.when(a, b).then(this.makeResult)).then(assertEquals); | |
}, | |
'test of applicative style for async fetch methods': function() { | |
var a = this.fetchObjectA(); | |
var b = this.fetchObjectB.$(a); | |
assertEquals.$(13, this.makeResult.$(a, b)) | |
.fail(function() { | |
assertTrue(false); | |
}); | |
}, | |
'test of member function': function() { | |
var obj = { | |
hoge: function(a) { return this.fuga(a); }, | |
fuga: function(a) { return 2*a; } | |
}; | |
assertEquals.$(10, obj.hoge.$$(obj)($.Deferred().resolve(5))); | |
}, | |
'test of object member': function() { | |
var obj = $.Deferred().resolve({ | |
hoge: 10 | |
}); | |
assertEquals.$(10, obj.$("hoge")); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment