Commits

Daniel Roberts committed 967c189 Draft

Wrote test for emit and forEach and fixed the errors I found.

  • Participants
  • Parent commits b6264cc

Comments (0)

Files changed (2)

File dcollection.js

                 f(this.deferred[i], i);
             }
             this.forEachObservers.push(f);
+            return this;
         }
         PromisedCollection.prototype.then = function then(resolve, reject) {
             if (this.deferred.state === "unfulfilled") {
             this.promise = new PromisedCollection(this);
         }
         DeferredCollection.prototype.emit = function emit(element) {
-            for (var i = 0; i < promise.forEachObservers.length; i++) {
-                promise.forEachObservers[i](element, this.data.length);
+            var observers = this.promise.forEachObservers;
+            for (var i = 0; i < observers.length; i++) {
+                observers[i](element, this.data.length);
             }
             this.data.push(element);
         }

File tests/test_dcollection.js

         start();
     });
 });
+
+asyncTest("emit and forEach", function () {
+    var deferred, promise;
+    
+    deferred = new DeferredCollection();
+    promise = deferred.promise;
+
+    promise.forEach(function (element, index) {
+        ok(element == index, "forEach called with correct index and element");
+    });
+
+    deferred.emit(0);
+    deferred.emit(1);
+    deferred.emit(2);
+
+    var previous = true;
+
+    promise.forEach(function (element, index) {
+        if (previous) {
+            ok(element < 3 && index < 3, "forEach called for previously emitted elements.");
+        } else {
+            ok(element == index, "forEach called for previously emitted elements and future emitted elements.");
+        }
+    });
+
+    previous = false;
+
+    deferred.emit(3);
+    deferred.emit(4);
+
+    promise.then(function () {
+        start();
+    });
+    deferred.resolve();
+});