Skip to content

Commit

Permalink
Unit test cases for core/promise (fixed the promise implementation, t…
Browse files Browse the repository at this point in the history
…dd ftw!)
  • Loading branch information
sebdeckers committed Oct 29, 2011
1 parent e76965b commit f082c49
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
</script>
</head>
<body></body>
</html>
</html>
26 changes: 11 additions & 15 deletions core/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ define(function () {
payload = undefined,
pending = 0,
promises = [],
unique = parseInt(Math.random() * 1000000),
execute = function () {
var _callbacks = callbacks;
callbacks = [];
_callbacks.forEach(function (callback) {
callback.apply(this, payload);
});
},
deferred = {
done: function () {
console.log("promise", unique, "done", pending);
isDone = true;
payload = arguments;
payload = arguments || payload;
pending--;
if (pending <= 0) {
console.log("promise", unique, "FEUER FREI!", pending);
callbacks.forEach(function (callback) {
callback.apply(this, payload);
});
isDone = true;
execute();
}
return this;
}
};
if (promisor instanceof Function) {
//setTimeout(function () { promisor(deferred); }, 0);
promisor(deferred);
}
return {
when: function (promise) {
//console.log("promise", unique, "when", pending);
if (promises.indexOf(promise) === -1) {
promises.push(promise);
pending++;
Expand All @@ -36,14 +36,10 @@ define(function () {
return this;
},
then: function (success /* , failure, progress */ ) { // TODO
//console.log("promise", unique, "then", pending);
if (callbacks.indexOf(success) === -1) {
callbacks.push(success);
if(isDone) {
console.log("promise", unique, "FEUER FREI! (THEN!!)", pending);
callbacks.forEach(function (callback) {
callback.apply(this, payload);
});
execute();
}
}
return this;
Expand Down
2 changes: 1 addition & 1 deletion qunit.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ <h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>
</html>
63 changes: 60 additions & 3 deletions tests/core/promise.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,70 @@
define(function () {
define(["core/promise"], function (promise) {
module("promise", {
setup: function () {
},
teardown: function () {
}
});

test("Testing promises", 1, function () {
equal(true, true, "This cannot fail?");
test("Chaining", 2, function () {
var myPromise = new promise;
strictEqual(myPromise.when(new promise), myPromise, "When");
strictEqual(myPromise.then(new Function), myPromise, "Then");
});

test("Then callback", 2, function () {
(new promise(function(deferred) {
deferred.done();
}))
.then(function() {
ok(true, "Synchronous deferred fulfillment");
})
.then(function() {
ok(true, "Multiple then callbacks");
});
});

asyncTest("Asynchronous fulfillment", 1, function () {
(new promise(function(deferred) {
setTimeout(function () {
deferred.done();
}, 0);
}))
.then(function() {
ok(true, "Asynchronous deferred fulfillment");
start();
});
});

test("When condition", 1, function () {
var _deferred,
condition = new promise(function(deferred) {
_deferred = deferred;
});
(new promise())
.when(condition)
.then(function() {
ok(true, "Promise fulfilled by when condition");
});
_deferred.done();
});

test("Multiple when conditions", 1, function () {
var fulfilled = new promise(),
conditions = [];
[1, 2].forEach(function () {
fulfilled.when(
new promise(function (deferred) {
conditions.push(deferred);
})
);
}),
fulfilled.then(function () {
ok(true, "Promise fulfilled by when condition");
});
conditions.forEach(function (condition) {
condition.done();
});
});

return {};
Expand Down

0 comments on commit f082c49

Please sign in to comment.