From 2f68cd9e2b49ce47d0dd2ca7d4bc5ab43b1a12e5 Mon Sep 17 00:00:00 2001 From: Oliver Perez Camargo Date: Thu, 29 Oct 2015 11:10:18 -0500 Subject: [PATCH] Change queue.handler to send null instead of undefined if the queue has no errors This will make Firebase Util compatible with AngularFire, if queue.handler sends undefined instead of null AngularFire won't trigger the callbacks. --- src/common/libs/queue.js | 2 +- test/NormalizedCollection/Record.spec.js | 4 +-- test/common/queue.spec.js | 41 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 test/common/queue.spec.js diff --git a/src/common/libs/queue.js b/src/common/libs/queue.js index e08c6b0..c002b1f 100644 --- a/src/common/libs/queue.js +++ b/src/common/libs/queue.js @@ -66,7 +66,7 @@ Queue.prototype = { handler: function(fn, context) { this._runOrStore(function() { - fn.apply(context, this.getErrors()); + fn.apply(context, this.hasErrors()? this.getErrors() : [null]); }); return this; }, diff --git a/test/NormalizedCollection/Record.spec.js b/test/NormalizedCollection/Record.spec.js index 2357edd..592c6a2 100644 --- a/test/NormalizedCollection/Record.spec.js +++ b/test/NormalizedCollection/Record.spec.js @@ -432,7 +432,7 @@ describe('Record', function() { expect(spy).not.toHaveBeenCalled(); } }); - expect(spy).toHaveBeenCalledWith(); + expect(spy).toHaveBeenCalledWith(null); }); it('triggers callback with correct context', function() { @@ -450,7 +450,7 @@ describe('Record', function() { _.each(refs, function(ref) { try { ref.flush(); } catch(e) {} }); - expect(spy).toHaveBeenCalledWith(); + expect(spy).toHaveBeenCalledWith(null); }); it('returns an error if any path returns an error', function() { diff --git a/test/common/queue.spec.js b/test/common/queue.spec.js new file mode 100644 index 0000000..85046a1 --- /dev/null +++ b/test/common/queue.spec.js @@ -0,0 +1,41 @@ +'use strict'; +var fbutil = require('../../src/common'); + +describe('common/Queue.js', function() { + + describe('#handler', function() { + it('should call handler callback with null if the queue has no errors', function() { + var q = fbutil.queue(); + var handler = q.getHandler(); + + var callback = jasmine.createSpy(); + + q.handler(callback); + + expect(q.hasErrors()).toBe(0); + + handler(); + + expect(q.hasErrors()).toBe(0); + expect(callback).toHaveBeenCalledWith(null); + }); + + it('should call handler callback with the first error if the queue has errors', function() { + var q = fbutil.queue(); + var handler = q.getHandler(); + + var callback = jasmine.createSpy(); + var error = new Error(); + + q.handler(callback); + + expect(q.hasErrors()).toBe(0); + + handler(error); + + expect(q.hasErrors()).toBe(1); + expect(callback).toHaveBeenCalledWith(error); + }); + }); + +}); \ No newline at end of file