Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
Add support firebase@4 and firebase-admin@5
Browse files Browse the repository at this point in the history
  • Loading branch information
dinoboff committed Sep 1, 2017
1 parent a37fc6a commit 28222cb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 19 deletions.
57 changes: 41 additions & 16 deletions src/index.mjs
Expand Up @@ -13,22 +13,47 @@ import syncList from './synclist.mjs';
*/
export function extend(firebase, Observable) {

/**
* Create an observable emitting user status change.
*
* Note: This is creating a cold observable; it will only start watching auth
* when subscribing to it.
*
* @example
* firebaseApp.auth().observeAuthState().subscribe(
* user => console.log('UID: ' + user.uid)
* );
*
* @return {Observable}
*/
firebase.auth.Auth.prototype.observeAuthState = function() {
return new Observable(this.onAuthStateChanged.bind(this));
};
if (firebase.auth.Auth.prototype.onAuthStateChanged) {

/**
* Create an observable emitting user status change.
*
* Note: This is creating a cold observable; it will only start watching auth
* when subscribing to it.
*
* @example
* firebaseApp.auth().observeAuthState().subscribe(
* user => console.log('UID: ' + user.uid)
* );
*
* @return {Observable}
*/
firebase.auth.Auth.prototype.observeAuthState = function() {
return new Observable(this.onAuthStateChanged.bind(this));
};

}

if (firebase.auth.Auth.prototype.onIdTokenChanged) {

/**
* Create an observable emitting user id token change.
*
* Note: This is creating a cold observable; it will only start watching auth
* when subscribing to it.
*
* @example
* firebaseApp.auth().observeIdTokenState().subscribe(
* user => console.log('UID: ' + user.uid)
* );
*
* @return {Observable}
*/
firebase.auth.Auth.prototype.observeIdTokenState = function() {
return new Observable(this.onIdTokenChanged.bind(this));
};

}

/**
* Create a (cold) observable emitting changes over a firebase data reference
Expand Down
50 changes: 49 additions & 1 deletion test/test_rxfirebase.mjs
Expand Up @@ -17,7 +17,8 @@ describe('extend', function() {
};

firebase.auth.Auth.prototype = {
onAuthStateChanged: sinon.stub()
onAuthStateChanged: sinon.stub(),
onIdTokenChanged: sinon.stub()
};
firebase.database.Query.prototype = {
on: sinon.stub(),
Expand All @@ -29,6 +30,7 @@ describe('extend', function() {

it('should extend firebase auth', function() {
expect(firebase.auth.Auth.prototype.observeAuthState).to.be.a('function');
expect(firebase.auth.Auth.prototype.observeIdTokenState).to.be.a('function');
});

describe('Auth.observeAuthState', function() {
Expand Down Expand Up @@ -77,6 +79,52 @@ describe('extend', function() {

});

describe('Auth.observeIdTokenState', function() {
let unsub, observable;

beforeEach(function() {
unsub = sinon.spy();
firebase.auth.Auth.prototype.onIdTokenChanged.returns(unsub);

observable = new firebase.auth.Auth().observeIdTokenState();
});

it('should return an observable', function() {
expect(observable.subscribe).to.be.a('function');
});

it('should subscribe to onIdTokenChanged on subscription', function() {
const sub = observable.subscribe();
const authObserver = firebase.auth.Auth.prototype.onIdTokenChanged.lastCall.args[0];

expect(firebase.auth.Auth.prototype.onIdTokenChanged).to.have.been.calledOnce;
expect(authObserver.next).to.be.a('function');
expect(authObserver.error).to.be.a('function');
expect(authObserver.complete).to.be.a('function');
expect(sub.unsubscribe).to.be.a('function');
});

it('should emit auth changes', function() {
const user = {uid: 'bob'};
const promise = observable.take(3).toArray().toPromise();
const authObserver = firebase.auth.Auth.prototype.onIdTokenChanged.lastCall.args[0];

authObserver.next(null);
authObserver.next(user);
authObserver.next(null);

return promise.then(
result => expect(result).to.eql([null, user, null])
);
});

it('should clean up on unsubscribe', function() {
observable.subscribe().unsubscribe();
expect(unsub).to.have.been.calledOnce;
});

});

describe('Query.observe', function() {
let query, observable;

Expand Down
5 changes: 3 additions & 2 deletions tools/assets/dist/package.json
Expand Up @@ -14,8 +14,9 @@
"rx-firebase.umd.min.js"
],
"registry": "npm",
"peerDependencies": {
"firebase": "^3.0.0"
"optionalDependencies": {
"firebase": ">=3.0.0",
"firebase-admin": ">=5.0.0"
},
"jspmPackage": true,
"format": "amd",
Expand Down

0 comments on commit 28222cb

Please sign in to comment.