Skip to content

Commit 41174bc

Browse files
committed
Use same server time in WriteResult as in docs
1 parent f614189 commit 41174bc

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

src/firestore-document.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ MockFirestoreDocument.prototype.create = function (data, callback) {
111111
var base = self._getData();
112112
err = err || self._validateDoesNotExist(base);
113113
if (err === null) {
114-
var timestamp = Timestamp.fromMillis(utils.getServerTime());
115-
var result = new WriteResult(timestamp);
116-
data = utils.removeEmptyFirestoreProperties(data);
114+
var serverTime = utils.getServerTime();
115+
var result = new WriteResult(Timestamp.fromMillis(serverTime));
116+
data = utils.removeEmptyFirestoreProperties(data, serverTime);
117117
self._dataChanged(data);
118118
resolve(result);
119119
} else {
@@ -137,7 +137,7 @@ MockFirestoreDocument.prototype.set = function (data, opts, callback) {
137137
return new Promise(function (resolve, reject) {
138138
self._defer('set', _.toArray(arguments), function () {
139139
if (err === null) {
140-
data = utils.removeEmptyFirestoreProperties(data);
140+
data = utils.removeEmptyFirestoreProperties(data, utils.getServerTime());
141141
self._dataChanged(data);
142142
resolve();
143143
} else {
@@ -172,7 +172,7 @@ MockFirestoreDocument.prototype._update = function (changes, opts, callback) {
172172
data = _.assign(_.isObject(base) ? base : {}, utils.updateToFirestoreObject(changes));
173173
}
174174
}
175-
data = utils.removeEmptyFirestoreProperties(data);
175+
data = utils.removeEmptyFirestoreProperties(data, utils.getServerTime());
176176
self._dataChanged(data);
177177
resolve(data);
178178
} else {

src/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,19 @@ exports.removeEmptyRtdbProperties = function removeEmptyRtdbProperties(obj) {
127127
}
128128
};
129129

130-
exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties(obj) {
130+
exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties(obj, serverTime) {
131131
if (!_.isPlainObject(obj)) {
132132
return obj;
133133
}
134134

135135
var keys = getKeys(obj);
136136
if (keys.length > 0) {
137137
for (var s in obj) {
138-
var value = removeEmptyFirestoreProperties(obj[s]);
138+
var value = removeEmptyFirestoreProperties(obj[s], serverTime);
139139
if (FieldValue.delete().isEqual(value)) {
140140
delete obj[s];
141141
} else if (FieldValue.serverTimestamp().isEqual(value)) {
142-
obj[s] = new Date(exports.getServerTime());
142+
obj[s] = new Date(serverTime);
143143
} else if (value instanceof Timestamp) {
144144
obj[s] = value.toDate();
145145
}

test/unit/utils.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,56 +56,56 @@ describe('utils', function () {
5656

5757

5858
describe('removeEmptyFirestoreProperties', function () {
59-
59+
var serverTime = Date.now();
6060
it('should return null, when the obj is empty', function () {
61-
expect(removeEmptyFirestoreProperties({})).to.eql({});
61+
expect(removeEmptyFirestoreProperties({}), serverTime).to.eql({});
6262
});
6363

6464
it('should make no changes, when obj does not contain an empty property', function () {
65-
expect(removeEmptyFirestoreProperties({a: 1})).to.eql({a: 1});
65+
expect(removeEmptyFirestoreProperties({a: 1}, serverTime)).to.eql({a: 1});
6666
});
6767

6868
it('should make no changes, when obj is a bool', function () {
69-
expect(removeEmptyFirestoreProperties(true)).to.eql(true);
69+
expect(removeEmptyFirestoreProperties(true, serverTime)).to.eql(true);
7070
});
7171

7272
it('should make no changes, when obj is a string', function () {
73-
expect(removeEmptyFirestoreProperties('hi')).to.eql('hi');
73+
expect(removeEmptyFirestoreProperties('hi', serverTime)).to.eql('hi');
7474
});
7575

7676
it('should make no changes, when obj is a number', function () {
77-
expect(removeEmptyFirestoreProperties(123)).to.eql(123);
77+
expect(removeEmptyFirestoreProperties(123, serverTime)).to.eql(123);
7878
});
7979

8080
it('should make no changes, when obj is a Date', function () {
8181
var date = new Date();
82-
expect(removeEmptyFirestoreProperties(date)).to.eql(date);
82+
expect(removeEmptyFirestoreProperties(date, serverTime)).to.eql(date);
8383
});
8484

8585
it('should make no changes, when obj is a Timestamp', function () {
8686
var ts = new Timestamp(123, 123);
87-
expect(removeEmptyFirestoreProperties(ts)).to.eql(ts);
87+
expect(removeEmptyFirestoreProperties(ts, serverTime)).to.eql(ts);
8888
});
8989

9090
it('should make no changes, when obj is a NaN', function () {
91-
expect(removeEmptyFirestoreProperties(NaN)).to.eql(NaN);
91+
expect(removeEmptyFirestoreProperties(NaN, serverTime)).to.eql(NaN);
9292
});
9393

9494
it('should make no changes, when obj is a undefined', function () {
95-
expect(removeEmptyFirestoreProperties(undefined)).to.eql(undefined);
95+
expect(removeEmptyFirestoreProperties(undefined, serverTime)).to.eql(undefined);
9696
});
9797

9898
it('should remove property, when it is null', function () {
99-
expect(removeEmptyFirestoreProperties({a: 1, b: null})).to.eql({a: 1, b: null});
99+
expect(removeEmptyFirestoreProperties({a: 1, b: null}, serverTime)).to.eql({a: 1, b: null});
100100
});
101101
it('should remove property, when it is an empty object', function () {
102-
expect(removeEmptyFirestoreProperties({a: 1, b: {}})).to.eql({a: 1, b: {}});
102+
expect(removeEmptyFirestoreProperties({a: 1, b: {}}, serverTime)).to.eql({a: 1, b: {}});
103103
});
104104
it('should remove property, when it is an empty array', function () {
105-
expect(removeEmptyFirestoreProperties({a: 1, b: []})).to.eql({a: 1, b: []});
105+
expect(removeEmptyFirestoreProperties({a: 1, b: []}, serverTime)).to.eql({a: 1, b: []});
106106
});
107107
it('should return null, when all properties are null ', function () {
108-
expect(removeEmptyFirestoreProperties({a: {b: null}})).to.eql({a: {b: null}});
108+
expect(removeEmptyFirestoreProperties({a: {b: null}}, serverTime)).to.eql({a: {b: null}});
109109
});
110110
});
111111

0 commit comments

Comments
 (0)