Skip to content

Commit

Permalink
Merge pull request FirebaseExtended#703 from firebase/de-provider
Browse files Browse the repository at this point in the history
AngularFire 1.2 - firebaseRefProvider and $firebaseAuthService
  • Loading branch information
katowulf committed Mar 22, 2016
2 parents 427d7f9 + eaa688e commit d13a3b7
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/firebaseAuthService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(function() {
"use strict";

function FirebaseAuthService($firebaseAuth, $firebaseRef) {
return $firebaseAuth($firebaseRef.default);
}
FirebaseAuthService.$inject = ['$firebaseAuth', '$firebaseRef'];

angular.module('firebase')
.factory('$firebaseAuthService', FirebaseAuthService);

})();
46 changes: 46 additions & 0 deletions src/firebaseRef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(function() {
"use strict";

function FirebaseRef() {
this.urls = null;
this.registerUrl = function registerUrl(urlOrConfig) {

if (typeof urlOrConfig === 'string') {
this.urls = {};
this.urls.default = urlOrConfig;
}

if (angular.isObject(urlOrConfig)) {
this.urls = urlOrConfig;
}

};

this.$$checkUrls = function $$checkUrls(urlConfig) {
if (!urlConfig) {
return new Error('No Firebase URL registered. Use firebaseRefProvider.registerUrl() in the config phase. This is required if you are using $firebaseAuthService.');
}
if (!urlConfig.default) {
return new Error('No default Firebase URL registered. Use firebaseRefProvider.registerUrl({ default: "https://<my-firebase-app>.firebaseio.com/"}).');
}
};

this.$$createRefsFromUrlConfig = function $$createMultipleRefs(urlConfig) {
var refs = {};
var error = this.$$checkUrls(urlConfig);
if (error) { throw error; }
angular.forEach(urlConfig, function(value, key) {
refs[key] = new Firebase(value);
});
return refs;
};

this.$get = function FirebaseRef_$get() {
return this.$$createRefsFromUrlConfig(this.urls);
};
}

angular.module('firebase')
.provider('$firebaseRef', FirebaseRef);

})();
27 changes: 27 additions & 0 deletions tests/unit/FirebaseAuthService.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
describe('$firebaseAuthService', function () {
var $firebaseRefProvider;
var MOCK_URL = 'https://stub.firebaseio-demo.com/'

beforeEach(module('firebase', function(_$firebaseRefProvider_) {
$firebaseRefProvider = _$firebaseRefProvider_;
$firebaseRefProvider.registerUrl(MOCK_URL);
}));

describe('<constructor>', function() {

var $firebaseAuthService;
beforeEach(function() {
module('firebase');
inject(function (_$firebaseAuthService_) {
$firebaseAuthService = _$firebaseAuthService_;
});
});

it('should exist because we called $firebaseRefProvider.registerUrl()', inject(function() {
expect($firebaseAuthService).not.toBe(null);
}));

});

});
55 changes: 55 additions & 0 deletions tests/unit/firebaseRef.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
describe('firebaseRef', function () {

var $firebaseRefProvider;
var MOCK_URL = 'https://stub.firebaseio-demo.com/'

beforeEach(module('firebase', function(_$firebaseRefProvider_) {
$firebaseRefProvider = _$firebaseRefProvider_;
}));

describe('registerUrl', function() {

it('creates a single reference with a url', inject(function() {
$firebaseRefProvider.registerUrl(MOCK_URL);
expect($firebaseRefProvider.$get().default).toBeAFirebaseRef();
}));

it('creates a default reference with a config object', inject(function() {
$firebaseRefProvider.registerUrl({
default: MOCK_URL
});
var firebaseRef = $firebaseRefProvider.$get();
expect(firebaseRef.default).toBeAFirebaseRef();
}));

it('creates multiple references with a config object', inject(function() {
$firebaseRefProvider.registerUrl({
default: MOCK_URL,
messages: MOCK_URL + 'messages'
});
var firebaseRef = $firebaseRefProvider.$get();
expect(firebaseRef.default).toBeAFirebaseRef();
expect(firebaseRef.messages).toBeAFirebaseRef();
}));

it('should throw an error when no url is provided', inject(function () {
function errorWrapper() {
$firebaseRefProvider.registerUrl();
$firebaseRefProvider.$get();
}
expect(errorWrapper).toThrow();
}));

it('should throw an error when no default url is provided', inject(function() {
function errorWrapper() {
$firebaseRefProvider.registerUrl({ messages: MOCK_URL + 'messages' });
$firebaseRefProvider.$get();
}
expect(errorWrapper).toThrow();
}));


});

});

0 comments on commit d13a3b7

Please sign in to comment.