Skip to content

Instantly share code, notes, and snippets.

@alastairparagas
Created May 28, 2015 23:28
Show Gist options
  • Select an option

  • Save alastairparagas/4bb99873241a63e3ac68 to your computer and use it in GitHub Desktop.

Select an option

Save alastairparagas/4bb99873241a63e3ac68 to your computer and use it in GitHub Desktop.
A sample unit test on an AngularJS factory involving Firebase. The factory being unit tested abstracts Firebase away as a storage service from the rest of the app.
/*global describe, beforeEach, it, expect */
(function (window) {
'use strict';
var angular = window.angular,
MockFirebase = window.MockFirebase,
Firebase = window.Firebase,
jasmine = window.jasmine;
MockFirebase.override();
describe('Storage Access Factory:', function () {
var StorageAccess,
$q,
$rootScope;
beforeEach(function () {
angular.mock.module('myhonorsApp.storage');
window.SETTINGS = {
STORAGE: {
FIREBASE: {
BASE_URL: "https://test.firebaseio.com"
}
}
};
angular.mock.inject(function (_StorageAccess_, _$q_, _$rootScope_) {
StorageAccess = _StorageAccess_;
$q = _$q_;
$rootScope = _$rootScope_;
});
});
it('should have correct preliminary Firebase reference', function () {
expect(StorageAccess.$$storageRef).toBeTruthy();
expect(StorageAccess.$$storageRef).toEqual(jasmine.any(MockFirebase));
});
describe('Set method:', function () {
it('should store data at base reference', function (done) {
var testModel = {
testProp: "someValue",
testObject: {
anotherTestProp: "someAnotherValue"
}
},
storageRef = StorageAccess.$$storageRef;
StorageAccess.set(testModel).then(function () {
expect(storageRef.getData()).toEqual(testModel);
done();
}, function () {
done.fail("Data not set at base reference.");
});
storageRef.flush();
$rootScope.$apply();
});
it('should store data at relative reference', function (done) {
var testModel = {
testProp: "someValue"
},
storageRef = StorageAccess.$$storageRef.child("childPath");
StorageAccess.set(testModel, "childPath").then(function () {
expect(storageRef.getData()).toEqual(testModel);
done();
}, function () {
done.fail("Data not set at relative reference");
});
storageRef.flush();
$rootScope.$apply();
});
it('should wipe out any previous data', function (done) {
var preliminaryData = {
preliminaryProp: "someOtherValue"
},
testModel = {
testProp: "someValue"
},
storageRef = StorageAccess.$$storageRef;
storageRef.set(preliminaryData);
storageRef.flush();
StorageAccess.set(testModel).then(function () {
expect(storageRef.getData()).toEqual(testModel);
done();
}, function () {
done.fail("Data not set");
});
storageRef.flush();
$rootScope.$apply();
});
});
describe('Update method:', function () {
it('should store data at base reference', function (done) {
var testModel = {
testProp: "someValue"
},
storageRef = StorageAccess.$$storageRef;
StorageAccess.update(testModel).then(function () {
expect(storageRef.getData()).toEqual(testModel);
done();
}, function () {
done.fail("Data not set at base reference");
});
storageRef.flush();
$rootScope.$apply();
});
it('should store data at relative reference', function (done) {
var testModel = {
testProp: "someValue"
},
storageRef = StorageAccess.$$storageRef.child("childPath");
StorageAccess.update(testModel, "childPath").then(function () {
expect(storageRef.getData()).toEqual(testModel);
done();
}, function () {
done.fail("Data not set at relative reference");
});
storageRef.flush();
$rootScope.$apply();
});
it('should update explicit properties of data only', function (done) {
var preliminaryData = {
preliminaryProp: "someOtherValue"
},
testModel = {
testProp: "someValue"
},
storageRef = StorageAccess.$$storageRef;
storageRef.set(preliminaryData);
storageRef.flush();
StorageAccess.update(testModel).then(function () {
var combinedData = {};
angular.extend(combinedData, preliminaryData);
angular.extend(combinedData, testModel);
expect(storageRef.getData()).toEqual(combinedData);
done();
}, function () {
done.fail("Data not updated");
});
storageRef.flush();
$rootScope.$apply();
});
});
describe('Push method:', function () {
it('should push data at base reference', function (done) {
var testModel = {
testProp: "someValue"
},
storageRef = StorageAccess.$$storageRef;
StorageAccess.push(testModel).then(function () {
expect(storageRef.getData()).not.toEqual(testModel);
expect(storageRef.getData()).toBeNonEmptyObject();
done();
}, function () {
done.fail("Data not pushed at base reference");
});
storageRef.flush();
$rootScope.$apply();
});
it('should push data at relative reference', function (done) {
var testModel = {
testProp: "someValue"
},
storageRef = StorageAccess.$$storageRef.child("childPath");
StorageAccess.push(testModel, "childPath").then(function () {
expect(storageRef.getData()).not.toEqual(testModel);
expect(storageRef.getData()).toBeNonEmptyObject();
done();
}, function () {
done.fail("Data not pushed at relative reference");
});
storageRef.flush();
$rootScope.$apply();
});
});
describe('Get method:', function () {
it('should get data at base reference', function (done) {
var preliminaryData = {
testProp: "someValue"
},
storageRef = StorageAccess.$$storageRef;
storageRef.set(preliminaryData);
storageRef.flush();
StorageAccess.get().then(function (data) {
expect(data).toEqual(storageRef.getData());
done();
}, function () {
done.fail("Data not obtained at base reference");
});
storageRef.flush();
$rootScope.$apply();
});
it('should get data at relative reference', function (done) {
var preliminaryData = {
testProp: "someValue"
},
storageRef = StorageAccess.$$storageRef.child("childPath");
storageRef.set(preliminaryData);
storageRef.flush();
StorageAccess.get("childPath").then(function (data) {
expect(data).toEqual(storageRef.getData());
done();
}, function () {
done.fail("Data not obtained at relative reference");
});
storageRef.flush();
$rootScope.$apply();
});
});
});
}(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment