Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve create() with WriteResult #135

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,12 @@ MockFirebase.ServerValue = {
}
};

var getServerTime, defaultClock;
getServerTime = defaultClock = function () {
return new Date().getTime();
};

MockFirebase.setClock = function (fn) {
getServerTime = fn;
utils.setServerClock(fn);
};

MockFirebase.restoreClock = function () {
getServerTime = defaultClock;
utils.restoreServerClock();
};

MockFirebase.autoId = function () {
Expand Down Expand Up @@ -449,7 +444,7 @@ MockFirebase.prototype._dataChanged = function (unparsedData) {
var data = utils.cleanData(unparsedData);

if (utils.isServerTimestamp(data)) {
data = getServerTime();
data = utils.getServerTime();
}

if (pri !== this.priority) {
Expand All @@ -475,7 +470,7 @@ MockFirebase.prototype._dataChanged = function (unparsedData) {
keysToChange.forEach(function (key) {
var childData = unparsedData[key];
if (utils.isServerTimestamp(childData)) {
childData = getServerTime();
childData = utils.getServerTime();
}
self._updateOrAdd(key, childData, events);
});
Expand All @@ -493,7 +488,7 @@ MockFirebase.prototype._dataChanged = function (unparsedData) {

MockFirebase.prototype._priChanged = function (newPriority) {
if (utils.isServerTimestamp(newPriority)) {
newPriority = getServerTime();
newPriority = utils.getServerTime();
}
this.priority = newPriority;
if (this.parent) {
Expand Down
8 changes: 6 additions & 2 deletions src/firestore-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ var Promise = require('rsvp').Promise;
var autoId = require('firebase-auto-ids');
var DocumentSnapshot = require('./firestore-document-snapshot');
var Queue = require('./queue').Queue;
var Timestamp = require('./timestamp');
var utils = require('./utils');
var validate = require('./validators');
var WriteResult = require('./write-result');

function MockFirestoreDocument(path, data, parent, name, CollectionReference) {
this.ref = this;
Expand Down Expand Up @@ -108,9 +110,11 @@ MockFirestoreDocument.prototype.create = function (data, callback) {

var base = self._getData();
err = err || self._validateDoesNotExist(base);
if (err === null) {
if (err === null) {
var time = Timestamp.fromMillis(utils.getServerTime());
var result = new WriteResult(time);
self._dataChanged(data);
resolve();
resolve(result);
} else {
if (callback) {
callback(err);
Expand Down
23 changes: 23 additions & 0 deletions src/timestamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

function Timestamp(seconds, nanoseconds) {
this.seconds = seconds;
this.nanoseconds = nanoseconds;
}

Timestamp.fromDate = function(date) {
return Timestamp.fromMillis(date.getTime());
};

Timestamp.fromMillis = function(ms) {
var sec = Math.floor(ms / 1000);
var ns = (ms % 1000) * 1000 * 1000;
return new Timestamp(sec, ns);
};

Timestamp.prototype.toDate = function () {
var millis = this.seconds * 1000 + this.nanoseconds / (1000 * 1000);
return new Date(millis);
};

module.exports = Timestamp;
18 changes: 18 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ exports.priorityComparator = function priorityComparator(a, b) {
return 0;
};

var serverClock, defaultClock;

serverClock = defaultClock = function () {
return new Date().getTime();
};

exports.getServerTime = function getServerTime() {
return serverClock();
};

exports.setServerClock = function setServerTime(fn) {
serverClock = fn;
};

exports.restoreServerClock = function restoreServerTime() {
serverClock = defaultClock;
};

exports.isServerTimestamp = function isServerTimestamp(data) {
return _.isObject(data) && data['.sv'] === 'timestamp';
};
Expand Down
7 changes: 7 additions & 0 deletions src/write-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

function WriteResult(writeTime) {
this.writeTime = writeTime;
}

module.exports = WriteResult;
9 changes: 8 additions & 1 deletion test/unit/firestore-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ chai.use(require('sinon-chai'));
var expect = chai.expect;
var _ = require('../../src/lodash');
var Firestore = require('../../').MockFirestore;
var Firebase = require('../../').MockFirebase;

describe('MockFirestoreDocument', function () {

Expand Down Expand Up @@ -94,9 +95,15 @@ describe('MockFirestoreDocument', function () {

describe('#create', function () {
it('creates a new doc', function (done) {
Firebase.setClock(function() {
return 1234567890123;
});
var createDoc = db.doc('createDoc');

createDoc.create({prop: 'title'});
createDoc.create({prop: 'title'}).then(function (result) {
expect(result).to.have.property('writeTime');
expect(result.writeTime.seconds).to.equal(1234567890);
}).catch(done);

createDoc.get().then(function (snap) {
expect(snap.exists).to.equal(true);
Expand Down
30 changes: 30 additions & 0 deletions test/unit/timestamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var expect = require('chai').use(require('sinon-chai')).expect;
var sinon = require('sinon');
var Timestamp = require('../../src/timestamp');

describe('Timestamp', function () {
describe('fromDate', function () {
it('should convert from date', function () {
var date = new Date('2009-02-13T23:31:30.123456789Z');
var timestamp = Timestamp.fromDate(date);
expect(timestamp.seconds).to.equal(1234567890);
expect(timestamp.nanoseconds).to.equal(123000000);
});
});
describe('fromMillis', function () {
it('should convert from milliseconds', function () {
var timestamp = Timestamp.fromMillis(1234567890123);
expect(timestamp.seconds).to.equal(1234567890);
expect(timestamp.nanoseconds).to.equal(123000000);
});
});
describe('#toDate', function () {
it('should convert to date', function () {
var ts = new Timestamp(1234567890, 123456789);
var date = ts.toDate();
expect(date.toISOString()).to.equal('2009-02-13T23:31:30.123Z');
});
});
});