Skip to content

Commit b726224

Browse files
authored
Merge pull request #32 from dmurvihill/fieldvalue-array
Fieldvalue array
2 parents 6f4809e + cb3b530 commit b726224

File tree

10 files changed

+396
-164
lines changed

10 files changed

+396
-164
lines changed

package-lock.json

Lines changed: 295 additions & 158 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"lodash.set": "^4.3.2",
6666
"lodash.size": "^4.2.0",
6767
"lodash.toarray": "^4.4.0",
68+
"lodash.union": "^4.6.0",
6869
"rsvp": "^3.6.2"
6970
},
7071
"devDependencies": {
@@ -91,7 +92,7 @@
9192
"karma-mocha": "^1.3.0",
9293
"karma-phantomjs-launcher": "^1.0.4",
9394
"karma-sinon": "^1.0.5",
94-
"mocha": "^5.1.1",
95+
"mocha": "^5.2.0",
9596
"phantomjs-prebuilt": "^2.1.16",
9697
"semver": "^5.5.0",
9798
"sinon": "^4.5.0",

src/firestore-document.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ MockFirestoreDocument.prototype._update = function (changes, opts, callback) {
153153
self._defer('update', _.toArray(arguments), function () {
154154
if (!err) {
155155
var base = self._getData();
156+
var original = _.cloneDeep(base);
156157
var data;
157158
if (_opts.setMerge) {
158159
data = _.merge(_.isObject(base) ? base : {}, changes);
@@ -166,7 +167,7 @@ MockFirestoreDocument.prototype._update = function (changes, opts, callback) {
166167
data = _.assign(_.isObject(base) ? base : {}, utils.updateToFirestoreObject(changes));
167168
}
168169
}
169-
data = utils.removeEmptyFirestoreProperties(data);
170+
data = utils.removeEmptyFirestoreProperties(data, original);
170171
self._dataChanged(data);
171172
resolve(data);
172173
} else {

src/firestore-field-value.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

3-
function MockFirestoreFieldValue(type) {
3+
function MockFirestoreFieldValue(type, arg) {
44
this.type = type;
5+
this.arg = arg;
56
}
67

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

23+
MockFirestoreFieldValue.arrayRemove = function (arg) {
24+
return new MockFirestoreFieldValue('arrayRemove', arg);
25+
};
26+
27+
MockFirestoreFieldValue.arrayUnion = function (arg) {
28+
return new MockFirestoreFieldValue('arrayUnion', arg);
29+
};
30+
2231
module.exports = MockFirestoreFieldValue;

src/firestore-query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ MockFirestoreQuery.prototype.where = function (property, operator, value) {
148148
var query = this.clone();
149149

150150
// check if unsupported operator
151-
if (operator !== '==' && operator != 'array-contains') {
151+
if (operator !== '==' && operator !== 'array-contains') {
152152
console.warn('Using unsupported where() operator for firebase-mock, returning entire dataset');
153153
} else {
154154
if (_.size(this.data) !== 0) {

src/lodash.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ module.exports = {
3333
reduce: require('lodash.reduce'),
3434
remove: require('lodash.remove'),
3535
size: require('lodash.size'),
36-
toArray: require('lodash.toarray')
36+
toArray: require('lodash.toarray'),
37+
union: require('lodash.union'),
3738
};

src/utils.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,39 @@ exports.removeEmptyRtdbProperties = function removeEmptyRtdbProperties(obj) {
108108
}
109109
};
110110

111-
exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties(obj) {
111+
exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties(obj, current) {
112112
var t = typeof obj;
113113
if (t === 'boolean' || t === 'string' || t === 'number' || t === 'undefined') {
114114
return obj;
115115
}
116116
if (obj instanceof Date) return obj;
117117

118118
var keys = getKeys(obj);
119+
120+
const doArrayRemove = function(replacement, sub) {
121+
return current[sub].filter(function(e) {
122+
return replacement.indexOf(e) === -1;
123+
});
124+
};
125+
119126
if (keys.length > 0) {
120127
for (var s in obj) {
128+
121129
var value = removeEmptyFirestoreProperties(obj[s]);
122130
if (FieldValue.delete().isEqual(value)) {
123131
delete obj[s];
124132
}
125133
if (FieldValue.serverTimestamp().isEqual(value)) {
126134
obj[s] = new Date();
127135
}
136+
if (FieldValue.arrayRemove().isEqual(value)) {
137+
const replacement = Array.isArray(value.arg) ? value.arg : [value.arg];
138+
obj[s] = doArrayRemove(replacement, s);
139+
}
140+
if (FieldValue.arrayUnion().isEqual(value)) {
141+
const replacement = Array.isArray(value.arg) ? value.arg : [value.arg];
142+
obj[s] = _.union(current[s], replacement);
143+
}
128144
}
129145
}
130146
return obj;

test/unit/firestore-document.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,40 @@ describe('MockFirestoreDocument', function () {
316316
db.flush();
317317
});
318318

319+
it('updates an array property when using FieldValue.arrayRemove()', function (done) {
320+
doc.set({
321+
titles: ['title1', 'title2']
322+
});
323+
doc.update({
324+
titles: Firestore.FieldValue.arrayRemove('title2')
325+
});
326+
327+
doc.get().then(function (snap) {
328+
expect(snap.exists).to.equal(true);
329+
expect(snap.data()).to.deep.equal({titles: ['title1']});
330+
done();
331+
}).catch(done);
332+
333+
db.flush();
334+
});
335+
336+
it('updates an array property when using FieldValue.arrayUnion()', function (done) {
337+
doc.set({
338+
titles: ['title1']
339+
});
340+
doc.update({
341+
titles: Firestore.FieldValue.arrayUnion('title2')
342+
});
343+
344+
doc.get().then(function (snap) {
345+
expect(snap.exists).to.equal(true);
346+
expect(snap.data()).to.deep.equal({titles: ['title1', 'title2']});
347+
done();
348+
}).catch(done);
349+
350+
db.flush();
351+
});
352+
319353
it('does not merge nested properties recursively by default', function (done) {
320354
doc.set({
321355
nested: {

test/unit/firestore-field-value.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ describe('FieldValue', function () {
3636
});
3737
});
3838

39+
describe('#arrayRemove', function () {
40+
it('should be a function', function () {
41+
expect(FieldValue.arrayRemove).to.be.a('function');
42+
});
43+
it('should return FieldValue', function () {
44+
expect(FieldValue.arrayRemove()).to.be.instanceof(FieldValue);
45+
});
46+
it('should type to "serverTimestamp"', function () {
47+
expect(FieldValue.arrayRemove()).to.have.property('type').to.equal('arrayRemove');
48+
});
49+
});
50+
51+
describe('#arrayUnion', function () {
52+
it('should be a function', function () {
53+
expect(FieldValue.arrayUnion).to.be.a('function');
54+
});
55+
it('should return FieldValue', function () {
56+
expect(FieldValue.arrayUnion()).to.be.instanceof(FieldValue);
57+
});
58+
it('should type to "serverTimestamp"', function () {
59+
expect(FieldValue.arrayUnion()).to.have.property('type').to.equal('arrayUnion');
60+
});
61+
});
62+
3963
describe('#isEqual', function () {
4064
it('should be a function', function () {
4165
expect(FieldValue.delete().isEqual).to.be.a('function');

test/unit/sdk.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ describe('MockFirebaseSdk', function () {
100100
it('FieldValue.serverTimestamp', function () {
101101
expect(firebase.firestore.FieldValue.serverTimestamp).to.be.a('function');
102102
});
103+
104+
it('FieldValue.arrayRemove', function () {
105+
expect(firebase.firestore.FieldValue.arrayRemove).to.be.a('function');
106+
});
107+
108+
it('FieldValue.arrayUnion', function () {
109+
expect(firebase.firestore.FieldValue.arrayUnion).to.be.a('function');
110+
});
111+
103112
});
104113

105114
describe('#auth', function() {

0 commit comments

Comments
 (0)