Skip to content
Merged
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
453 changes: 295 additions & 158 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"lodash.set": "^4.3.2",
"lodash.size": "^4.2.0",
"lodash.toarray": "^4.4.0",
"lodash.union": "^4.6.0",
"rsvp": "^3.6.2"
},
"devDependencies": {
Expand All @@ -91,7 +92,7 @@
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-sinon": "^1.0.5",
"mocha": "^5.1.1",
"mocha": "^5.2.0",
"phantomjs-prebuilt": "^2.1.16",
"semver": "^5.5.0",
"sinon": "^4.5.0",
Expand Down
3 changes: 2 additions & 1 deletion src/firestore-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ MockFirestoreDocument.prototype._update = function (changes, opts, callback) {
self._defer('update', _.toArray(arguments), function () {
if (!err) {
var base = self._getData();
var original = _.cloneDeep(base);
var data;
if (_opts.setMerge) {
data = _.merge(_.isObject(base) ? base : {}, changes);
Expand All @@ -166,7 +167,7 @@ MockFirestoreDocument.prototype._update = function (changes, opts, callback) {
data = _.assign(_.isObject(base) ? base : {}, utils.updateToFirestoreObject(changes));
}
}
data = utils.removeEmptyFirestoreProperties(data);
data = utils.removeEmptyFirestoreProperties(data, original);
self._dataChanged(data);
resolve(data);
} else {
Expand Down
11 changes: 10 additions & 1 deletion src/firestore-field-value.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

function MockFirestoreFieldValue(type) {
function MockFirestoreFieldValue(type, arg) {
this.type = type;
this.arg = arg;
}

MockFirestoreFieldValue.prototype.isEqual = function (other) {
Expand All @@ -19,4 +20,12 @@ MockFirestoreFieldValue.serverTimestamp = function () {
return new MockFirestoreFieldValue('serverTimestamp');
};

MockFirestoreFieldValue.arrayRemove = function (arg) {
return new MockFirestoreFieldValue('arrayRemove', arg);
};

MockFirestoreFieldValue.arrayUnion = function (arg) {
return new MockFirestoreFieldValue('arrayUnion', arg);
};

module.exports = MockFirestoreFieldValue;
2 changes: 1 addition & 1 deletion src/firestore-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ MockFirestoreQuery.prototype.where = function (property, operator, value) {
var query = this.clone();

// check if unsupported operator
if (operator !== '==' && operator != 'array-contains') {
if (operator !== '==' && operator !== 'array-contains') {
console.warn('Using unsupported where() operator for firebase-mock, returning entire dataset');
} else {
if (_.size(this.data) !== 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ module.exports = {
reduce: require('lodash.reduce'),
remove: require('lodash.remove'),
size: require('lodash.size'),
toArray: require('lodash.toarray')
toArray: require('lodash.toarray'),
union: require('lodash.union'),
};
18 changes: 17 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,39 @@ exports.removeEmptyRtdbProperties = function removeEmptyRtdbProperties(obj) {
}
};

exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties(obj) {
exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties(obj, current) {
var t = typeof obj;
if (t === 'boolean' || t === 'string' || t === 'number' || t === 'undefined') {
return obj;
}
if (obj instanceof Date) return obj;

var keys = getKeys(obj);

const doArrayRemove = function(replacement, sub) {
return current[sub].filter(function(e) {
return replacement.indexOf(e) === -1;
});
};

if (keys.length > 0) {
for (var s in obj) {

var value = removeEmptyFirestoreProperties(obj[s]);
if (FieldValue.delete().isEqual(value)) {
delete obj[s];
}
if (FieldValue.serverTimestamp().isEqual(value)) {
obj[s] = new Date();
}
if (FieldValue.arrayRemove().isEqual(value)) {
const replacement = Array.isArray(value.arg) ? value.arg : [value.arg];
obj[s] = doArrayRemove(replacement, s);
}
if (FieldValue.arrayUnion().isEqual(value)) {
const replacement = Array.isArray(value.arg) ? value.arg : [value.arg];
obj[s] = _.union(current[s], replacement);
}
}
}
return obj;
Expand Down
34 changes: 34 additions & 0 deletions test/unit/firestore-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,40 @@ describe('MockFirestoreDocument', function () {
db.flush();
});

it('updates an array property when using FieldValue.arrayRemove()', function (done) {
doc.set({
titles: ['title1', 'title2']
});
doc.update({
titles: Firestore.FieldValue.arrayRemove('title2')
});

doc.get().then(function (snap) {
expect(snap.exists).to.equal(true);
expect(snap.data()).to.deep.equal({titles: ['title1']});
done();
}).catch(done);

db.flush();
});

it('updates an array property when using FieldValue.arrayUnion()', function (done) {
doc.set({
titles: ['title1']
});
doc.update({
titles: Firestore.FieldValue.arrayUnion('title2')
});

doc.get().then(function (snap) {
expect(snap.exists).to.equal(true);
expect(snap.data()).to.deep.equal({titles: ['title1', 'title2']});
done();
}).catch(done);

db.flush();
});

it('does not merge nested properties recursively by default', function (done) {
doc.set({
nested: {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/firestore-field-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ describe('FieldValue', function () {
});
});

describe('#arrayRemove', function () {
it('should be a function', function () {
expect(FieldValue.arrayRemove).to.be.a('function');
});
it('should return FieldValue', function () {
expect(FieldValue.arrayRemove()).to.be.instanceof(FieldValue);
});
it('should type to "serverTimestamp"', function () {
expect(FieldValue.arrayRemove()).to.have.property('type').to.equal('arrayRemove');
});
});

describe('#arrayUnion', function () {
it('should be a function', function () {
expect(FieldValue.arrayUnion).to.be.a('function');
});
it('should return FieldValue', function () {
expect(FieldValue.arrayUnion()).to.be.instanceof(FieldValue);
});
it('should type to "serverTimestamp"', function () {
expect(FieldValue.arrayUnion()).to.have.property('type').to.equal('arrayUnion');
});
});

describe('#isEqual', function () {
it('should be a function', function () {
expect(FieldValue.delete().isEqual).to.be.a('function');
Expand Down
9 changes: 9 additions & 0 deletions test/unit/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ describe('MockFirebaseSdk', function () {
it('FieldValue.serverTimestamp', function () {
expect(firebase.firestore.FieldValue.serverTimestamp).to.be.a('function');
});

it('FieldValue.arrayRemove', function () {
expect(firebase.firestore.FieldValue.arrayRemove).to.be.a('function');
});

it('FieldValue.arrayUnion', function () {
expect(firebase.firestore.FieldValue.arrayUnion).to.be.a('function');
});

});

describe('#auth', function() {
Expand Down