summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test.clear.js25
-rw-r--r--test/test.defer.js84
-rw-r--r--test/test.set.js58
3 files changed, 124 insertions, 43 deletions
diff --git a/test/test.clear.js b/test/test.clear.js
index ee60f2e..f0f60b1 100644
--- a/test/test.clear.js
+++ b/test/test.clear.js
@@ -1,7 +1,7 @@
-suite('Clear', function(){
+suite('clear', function(){
- test("Should not run 'read' job if cleared (sync)", function(done) {
+ test('Should not run "read" job if cleared (sync)', function(done) {
var fastdom = new FastDom();
var read = sinon.spy();
@@ -14,7 +14,7 @@ suite('Clear', function(){
});
});
- test("Should fail silently if job not found in queue", function(done) {
+ test('Should fail silently if job not found in queue', function(done) {
var fastdom = new FastDom();
var read = sinon.spy();
var read2 = sinon.spy();
@@ -28,7 +28,7 @@ suite('Clear', function(){
});
});
- test("Should not run 'write' job if cleared (async)", function(done) {
+ test('Should not run "write" job if cleared (async)', function(done) {
var fastdom = new FastDom();
var read = sinon.spy();
var write = sinon.spy();
@@ -44,7 +44,7 @@ suite('Clear', function(){
});
});
- test("Should not run 'write' job if cleared", function(done) {
+ test('Should not run "write" job if cleared', function(done) {
var fastdom = new FastDom();
var write = sinon.spy();
var id = fastdom.write(write);
@@ -57,10 +57,10 @@ suite('Clear', function(){
});
});
- test("Should not run 'defer' job if cleared", function(done) {
+ test('Should not run "defer" job if cleared', function(done) {
var fastdom = new FastDom();
- var write = sinon.spy();
- var id = fastdom.defer(3, write);
+ var callback = sinon.spy();
+ var id = fastdom.defer(3, callback);
fastdom.clear(id);
@@ -68,7 +68,7 @@ suite('Clear', function(){
raf(function() {
raf(function() {
raf(function() {
- assert(!write.called);
+ assert(!callback.called);
done();
});
});
@@ -76,10 +76,10 @@ suite('Clear', function(){
});
});
- test("Should remove reference to the job if cleared", function(done) {
+ test('Should remove reference to the job if cleared', function(done) {
var fastdom = new FastDom();
var write = sinon.spy();
- var id = fastdom.defer(2, write);
+ var id = fastdom.write(2, write);
fastdom.clear(id);
@@ -87,11 +87,10 @@ suite('Clear', function(){
raf(function() {
raf(function() {
assert(!write.called);
- assert(!fastdom.jobs[id]);
+ assert(!fastdom.batch.hash[id]);
done();
});
});
});
});
-
}); \ No newline at end of file
diff --git a/test/test.defer.js b/test/test.defer.js
index c1ae179..114287f 100644
--- a/test/test.defer.js
+++ b/test/test.defer.js
@@ -1,28 +1,25 @@
suite('defer', function(){
- test("Should run the job after the specified number of frames", function(done) {
+ test('Should run the job after the specified number of frames', function(done) {
var fastdom = new FastDom();
var job = sinon.spy();
- fastdom.defer(4, job);
+ fastdom.defer(3, job);
raf(function() {
assert(!job.called);
raf(function() {
assert(!job.called);
raf(function() {
- assert(!job.called);
- raf(function() {
- assert(job.called);
- done();
- });
+ assert(job.called);
+ done();
});
});
});
});
- test("Should call a deferred callback with the given context", function(done) {
+ test('Should call a deferred callback with the given context', function(done) {
var fastdom = new FastDom();
var cb = sinon.spy();
var ctx = { foo: 'bar' };
@@ -33,18 +30,81 @@ suite('defer', function(){
}, ctx);
});
- test("Should remove the reference to the job once run", function(done) {
+ test('Should run work at next frame if frames argument not supplied.', function(done) {
var fastdom = new FastDom();
- var callback = sinon.spy();
- var id = fastdom.defer(2, callback);
+ var callback1 = sinon.spy();
+ var callback2 = sinon.spy();
+
+ fastdom.defer(callback1);
raf(function() {
+ assert(callback1.called);
+ done();
+ });
+ });
+
+ test('Should run each job on a different frame.', function(done) {
+ var fastdom = new FastDom();
+ var callback1 = sinon.spy();
+ var callback2 = sinon.spy();
+ var callback3 = sinon.spy();
+
+ fastdom.defer(callback1);
+ fastdom.defer(callback2);
+ fastdom.defer(callback3);
+
+ raf(function() {
+ assert(callback1.called);
+ assert(!callback2.called);
+ assert(!callback3.called);
raf(function() {
+ assert(callback2.called);
+ assert(!callback3.called);
raf(function() {
- assert(!fastdom.jobs[id]);
+ assert(callback3.called);
done();
});
});
});
});
+
+ test('Should run fill empty frames before later work is run.', function(done) {
+ var fastdom = new FastDom();
+ var callback1 = sinon.spy();
+ var callback2 = sinon.spy();
+ var callback3 = sinon.spy();
+ var callback4 = sinon.spy();
+
+ // Frame 3
+ fastdom.defer(3, callback3);
+
+ // Frame 1
+ fastdom.defer(callback1);
+
+ // Frame 2
+ fastdom.defer(callback2);
+
+ // Frame 4
+ fastdom.defer(callback4);
+
+ raf(function() {
+ assert(callback1.called);
+ assert(!callback2.called);
+ assert(!callback3.called);
+ assert(!callback4.called);
+ raf(function() {
+ assert(callback2.called);
+ assert(!callback3.called);
+ assert(!callback4.called);
+ raf(function() {
+ assert(callback3.called);
+ assert(!callback4.called);
+ raf(function() {
+ assert(callback4.called);
+ done();
+ });
+ });
+ });
+ });
+ });
});
diff --git a/test/test.set.js b/test/test.set.js
index a1dd83c..a41259a 100644
--- a/test/test.set.js
+++ b/test/test.set.js
@@ -1,7 +1,7 @@
-suite('Set', function() {
+suite('set', function() {
- test("Should run reads before writes", function(done) {
+ test('Should run reads before writes', function(done) {
var fastdom = new FastDom();
var read = sinon.spy(function() {
@@ -17,7 +17,7 @@ suite('Set', function() {
fastdom.write(write);
});
- test("Should call all reads together, followed by all writes", function(done) {
+ test('Should call all reads together, followed by all writes', function(done) {
var fastdom = new FastDom();
var read1 = sinon.spy();
var read2 = sinon.spy();
@@ -41,7 +41,7 @@ suite('Set', function() {
});
});
- test("Should call a read in the same frame if scheduled inside a read callback", function(done) {
+ test('Should call a read in the same frame if scheduled inside a read callback', function(done) {
var fastdom = new FastDom();
var cb = sinon.spy();
@@ -59,11 +59,17 @@ suite('Set', function() {
assert(!cb.called);
done();
});
+
+ // Should not have scheduled a new frame
+ assert(fastdom.frames.length === 0);
});
});
- test("Should call a write in the same frame if scheduled inside a read callback", function(done) {
+ test('Should call a write in the same frame if scheduled inside a read callback', function(done) {
var fastdom = new FastDom();
+
+ fastdom.catchErrors = false;
+
var cb = sinon.spy();
fastdom.read(function() {
@@ -80,10 +86,13 @@ suite('Set', function() {
assert(!cb.called);
done();
});
+
+ // Should not have scheduled a new frame
+ assert(fastdom.frames.length === 0);
});
});
- test("Should call a read in the *next* frame if scheduled inside a write callback", function(done) {
+ test('Should call a read in the *next* frame if scheduled inside a write callback', function(done) {
var fastdom = new FastDom();
var cb = sinon.spy();
@@ -100,10 +109,13 @@ suite('Set', function() {
assert(cb.called);
done();
});
+
+ // Should not have scheduled a new frame
+ assert(fastdom.frames.length === 1);
});
});
- test("Should call a 'read' callback with the given context", function(done) {
+ test('Should call a "read" callback with the given context', function(done) {
var fastdom = new FastDom();
var cb = sinon.spy();
var ctx = { foo: 'bar' };
@@ -114,7 +126,7 @@ suite('Set', function() {
}, ctx);
});
- test("Should call a 'write' callback with the given context", function(done) {
+ test('Should call a "write" callback with the given context', function(done) {
var fastdom = new FastDom();
var cb = sinon.spy();
var ctx = { foo: 'bar' };
@@ -125,7 +137,7 @@ suite('Set', function() {
}, ctx);
});
- test("Should have empty job hash when batch complete", function(done) {
+ test('Should have empty job hash when batch complete', function(done) {
var fastdom = new FastDom();
fastdom.read(function(){});
@@ -134,15 +146,15 @@ suite('Set', function() {
fastdom.write(function(){});
// Check there are four jobs stored
- assert.equal(objectLength(fastdom.jobs), 4);
+ assert.equal(objectLength(fastdom.batch.hash), 4);
raf(function() {
- assert.equal(objectLength(fastdom.jobs), 0);
+ assert.equal(objectLength(fastdom.batch.hash), 0);
done();
});
});
- test("Should maintain correct context if single method is registered twice", function(done) {
+ test('Should maintain correct context if single method is registered twice', function(done) {
var fastdom = new FastDom();
var ctx1 = { foo: 'bar' };
var ctx2 = { bar: 'baz' };
@@ -162,12 +174,12 @@ suite('Set', function() {
});
});
- test("Should call a registered onError handler when an error is thrown inside a job", function(done) {
+ test('Should no error if the `quiet` flag is set', function(done) {
var fastdom = new FastDom();
var err1 = { some: 'error1' };
var err2 = { some: 'error2' };
- fastdom.onError = sinon.spy();
+ fastdom.quiet = true;
fastdom.read(function() {
throw err1;
@@ -178,10 +190,20 @@ suite('Set', function() {
});
raf(function() {
- assert(fastdom.onError.calledTwice);
- assert(fastdom.onError.getCall(0).calledWith(err1));
- assert(fastdom.onError.getCall(1).calledWith(err2));
done();
});
});
-}); \ No newline at end of file
+
+ test('Should stop rAF loop once frame queue is empty', function(done) {
+ var fastdom = new FastDom();
+ var callback = sinon.spy();
+
+ fastdom.read(callback);
+
+ raf(function() {
+ assert(callback.called);
+ assert(fastdom.looping === false);
+ done();
+ });
+ });
+});