8000 Fieldvalue array by boyko · Pull Request #132 · soumak77/firebase-mock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fieldvalue array #132

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

Open
wants to merge 2 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
3,088 changes: 1,561 additions & 1,527 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 @@ -64,6 +64,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 @@ -90,7 +91,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;
8 changes: 7 additions & 1 deletion src/firestore-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ MockFirestoreQuery.prototype.where = function (property, operator, value) {
var query;

// 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 @@ -139,6 +139,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 @@ -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'),
};
10 changes: 9 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ 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;
Expand All @@ -125,6 +125,14 @@ exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties
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] = current[s].filter(function(e) {return replacement.indexOf(e) === -1});
}
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
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 @@ -228,6 +228,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 @@ -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
0