Skip to content

Commit

Permalink
Add increment
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlouden committed Feb 20, 2020
1 parent ee51273 commit 2f827dd
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/firestore-field-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ MockFirestoreFieldValue.arrayUnion = function (arg) {
return new MockFirestoreFieldValue('arrayUnion', arg);
};

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

module.exports = MockFirestoreFieldValue;
15 changes: 11 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,21 @@ exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties
obj[s] = new Date(serverTime);
} else if (value instanceof Timestamp) {
obj[s] = value.toDate();
}
if (FieldValue.arrayRemove().isEqual(value)) {
} else 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)) {
} else if (FieldValue.arrayUnion().isEqual(value)) {
const replacement = Array.isArray(value.arg) ? value.arg : [value.arg];
obj[s] = _.union(current[s], replacement);
} else if (FieldValue.increment().isEqual(value)) {
if (current[s] == null) {
// no existing data
obj[s] = value.arg;
console.log(obj[s], value);
} else {
// add to existing data
obj[s] = current[s] + value.arg;
}
}
}
}
Expand Down
67 changes: 65 additions & 2 deletions test/unit/firestore-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ describe('MockFirestoreDocument', function () {

db.flush();
});

it('creates a new doc with value of FieldValue.increment()', function (done) {
var createDoc = db.doc('createDoc');

createDoc.create({titles: Firestore.FieldValue.increment(10)});

createDoc.get().then(function (snap) {
expect(snap.exists).to.equal(true);
expect(snap.get('titles')).to.equal(10);
done();
}).catch(done);

db.flush();
});

});

describe('#set', function () {
Expand Down Expand Up @@ -214,6 +229,23 @@ describe('MockFirestoreDocument', function () {

db.flush();
});

it('overrides existing data when using FieldValue.increment()', function (done) {
doc.set({
titles: 10
});
doc.set({
titles: Firestore.FieldValue.increment(10)
});

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

db.flush();
});
});

describe('#set with {merge: true}', function () {
Expand Down Expand Up @@ -413,6 +445,37 @@ describe('MockFirestoreDocument', function () {
db.flush();
});

it('adds to existing data when using FieldValue.increment()', function (done) {
doc.set({
titles: 10
});
doc.update({
titles: Firestore.FieldValue.increment(10)
});

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

db.flush();
});

it('sets when no existing data exists when using FieldValue.increment()', function (done) {
doc.set({
titles: Firestore.FieldValue.increment(10)
});

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

db.flush();
});

it('does not merge nested properties recursively by default', function (done) {
doc.set({
nested: {
Expand Down Expand Up @@ -577,7 +640,7 @@ describe('MockFirestoreDocument', function () {
if (!first) {
expect(snap.get('newTitle')).to.equal('A new title');
done();
}
}

first = false;
});
Expand All @@ -587,7 +650,7 @@ describe('MockFirestoreDocument', function () {

it('does not call observer when no changes occur', function (done) {
var first = true;

doc.onSnapshot(function(snap) {
if (!first) throw new Error('Observer called unexpectedly!');
first = false;
Expand Down
12 changes: 12 additions & 0 deletions test/unit/firestore-field-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ describe('FieldValue', function () {
});
});

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

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

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

it('FieldPath.documentId', function () {
expect(firebase.firestore.FieldPath.documentId).to.be.a('function');
Expand Down

0 comments on commit 2f827dd

Please sign in to comment.