Permalink
Browse files

implemented callback version and promise version

  • Loading branch information...
1 parent 72a4258 commit 24d40fa1a3b40a8f94e67cd4478e8d35803d12d5 @dominykas dominykas committed Oct 1, 2012
Showing with 59 additions and 21 deletions.
  1. +12 −2 02-promiseAndCallback.js
  2. +47 −19 02-promiseAndCallback.test.js
View
@@ -9,15 +9,25 @@ define(["fs", "path", "q"], function(fs, path, Q){
return path.join("testdata", pageId+"."+lang+".html");
};
- var getHtml = function (lang, pageId)
+ var getHtml = function (lang, pageId, callback)
{
var promise = Q.ninvoke(fs, "readFile", getFn(lang, pageId), UTF);
var onError = (lang == defaultLang
? Q.reject
: function() { return getHtml(defaultLang, pageId); });
- return promise.then(Q.resolve, onError);
+ promise = promise.then(Q.resolve, onError);
+
+ if (callback) {
+ promise.then(function(data){
+ callback(null, data);
+ }).fail(function(error){
+ callback(error, null);
+ }).end();
+ } else {
+ return promise;
+ }
};
return {
@@ -62,25 +62,53 @@ define("02-promiseAndCallback.test", ["buster", "fs", "./02-promiseAndCallback"]
},
"withCallback": {
-// "should read file (de, exists)": function(done)
-// {
-// throw new Error("Not implemented");
-// },
-//
-// "should read file (de, fallback)": function(done)
-// {
-// throw new Error("Not implemented");
-// },
-//
-// "should return error (en, na)": function(done)
-// {
-// throw new Error("Not implemented");
-// },
-//
-// "should return error (de, na)": function(done)
-// {
-// throw new Error("Not implemented");
-// }
+ "should read file (de, exists)": function(done)
+ {
+ html.getHtml("de", "one", function(e, data){
+
+ expect(this.fsStub).toHaveBeenCalledOnce();
+ expect(e).toBeNull();
+ expect(data).toEqual("one german");
+ done();
+
+ }.bind(this));
+ },
+
+ "should read file (de, fallback)": function(done)
+ {
+ html.getHtml("de", "two", function(e, data){
+
+ expect(this.fsStub).toHaveBeenCalledTwice();
+ expect(e).toBeNull();
+ expect(data).toEqual("two english");
+ done();
+
+ }.bind(this));
+ },
+
+ "should return error (en, na)": function(done)
+ {
+ html.getHtml("en", "na", function(e, data){
+
+ expect(this.fsStub).toHaveBeenCalledOnce();
+ expect(e).not.toBeNull();
+ expect(data).toBeNull();
+ done();
+
+ }.bind(this));
+ },
+
+ "should return error (de, na)": function(done)
+ {
+ html.getHtml("de", "na", function(e, data){
+
+ expect(this.fsStub).toHaveBeenCalledTwice();
+ expect(e).not.toBeNull();
+ expect(data).toBeNull();
+ done();
+
+ }.bind(this));
+ }
}
});

0 comments on commit 24d40fa

Please sign in to comment.