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
46 changes: 19 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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 @@ -92,7 +93,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 @@ -159,6 +159,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 @@ -172,7 +173,7 @@ MockFirestoreDocument.prototype._update = function (changes, opts, callback) {
data = _.assign(_.isObject(base) ? base : {}, utils.updateToFirestoreObject(changes));
}
}
data = utils.removeEmptyFirestoreProperties(data, utils.getServerTime());
data = utils.removeEmptyFirestoreProperties(data, utils.getServerTime(), 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;
8 changes: 7 additions & 1 deletion src/firestore-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ MockFirestoreQuery.prototype.where = function (property, operator, value) {
var path = getPropertyPath(property);

// check if unsupported operator
if (operator !== '==') {
if (['==', 'array-contains'].indexOf(operator) === -1) {
console.warn('Using unsupported where() operator for firebase-mock, returning entire dataset');
return this;
} else {
Expand All @@ -142,6 +142,12 @@ MockFirestoreQuery.prototype.where = function (property, operator, value) {
results[key] = _.cloneDeep(data);
}
break;
case 'array-contains':
var dt = _.get(data, property);
if (Array.isArray(dt) && dt.indexOf(value) > -1) {
results[key] = _.cloneDeep(data);
}
break;
default:
results[key] = _.cloneDeep(data);
break;
Expand Down
3 changes: 2 additions & 1 deletion src/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,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'),
};
21 changes: 17 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,36 @@ exports.removeEmptyRtdbProperties = function removeEmptyRtdbProperties(obj) {
}
};

exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties(obj, serverTime) {
exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties(obj, serverTime, current) {
if (!_.isPlainObject(obj)) {
return obj;
}

var keys = getKeys(obj);
if (keys.length > 0) {
for (var s in obj) {
var value = removeEmptyFirestoreProperties(obj[s], serverTime);
Object.keys(obj).forEach(function(s) {
var value = removeEmptyFirestoreProperties(obj[s], serverTime, current);
if (FieldValue.delete().isEqual(value)) {
delete obj[s];
} else if (FieldValue.serverTimestamp().isEqual(value)) {
obj[s] = new Date(serverTime);
} else if (value instanceof Timestamp) {
obj[s] = value.toDate();
}
}

if (value && typeof value === 'object' && 'arg' in value) {
var replacement = Array.isArray(value.arg) ? value.arg : [value.arg];

if (FieldValue.arrayRemove().isEqual(value)) {
obj[s] = current[s].filter(function(e) {
return replacement.indexOf(e) === -1;
});
}
if (FieldValue.arrayUnion().isEqual(value)) {
obj[s] = _.union(current[s], replacement);
}
}
});
}
return obj;

Expand Down
15 changes: 12 additions & 3 deletions test/unit/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,30 @@
"name_type": "string",
"complex": {
"name": "a"
}
},
"array": [
"a", "b"
]
},
"b": {
"name": "b",
"name_type": "string",
"complex": {
"name": "b"
}
},
"array": [
"a", "c"
]
},
"c": {
"name": "c",
"name_type": "string",
"complex": {
"name": "c"
}
},
"array": [
"b", "c"
]
},
"1": {
"name": 1,
Expand Down
9 changes: 9 additions & 0 deletions test/unit/firestore-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ describe('MockFirestoreCollection', function () {
]);
});

it('returns matched documents for operator "array-contains"', function() {
var results1 = collection.where('array', 'array-contains', 'a').get();
db.flush();

return Promise.all([
expect(results1).to.eventually.have.property('size').to.equal(2),
]);
});

it('returns all documents when using unsupported operator', function() {
var expected = 6;

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 @@ -379,6 +379,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
8 changes: 8 additions & 0 deletions test/unit/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ describe('MockFirebaseSdk', function () {
it('FieldPath.documentId', function () {
expect(firebase.firestore.FieldPath.documentId).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