Skip to content

Commit 694cd49

Browse files
authored
Merge pull request soumak77#90 from yearofthedan/master
Support firestore.doc.create
2 parents 241af33 + 566fcdb commit 694cd49

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

src/firestore-document.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,40 @@ MockFirestoreDocument.prototype.get = function () {
8787
});
8888
};
8989

90+
MockFirestoreDocument.prototype._validateDoesNotExist = function (data) {
91+
var ALREADY_EXISTS_CODE = 6;
92+
93+
if (data !== null) {
94+
var err = new Error('Cannot create a document which already exists');
95+
err.code = ALREADY_EXISTS_CODE;
96+
return err;
97+
}
98+
return null;
99+
};
100+
101+
MockFirestoreDocument.prototype.create = function (data, callback) {
102+
var err = this._nextErr('create');
103+
data = _.cloneDeep(data);
104+
105+
var self = this;
106+
return new Promise(function (resolve, reject) {
107+
self._defer('create', _.toArray(arguments), function () {
108+
109+
var base = self._getData();
110+
err = err || self._validateDoesNotExist(base);
111+
if (err === null) {
112+
self._dataChanged(data);
113+
resolve();
114+
} else {
115+
if (callback) {
116+
callback(err);
117+
}
118+
reject(err);
119+
}
120+
});
121+
});
122+
};
123+
90124
MockFirestoreDocument.prototype.set = function (data, opts, callback) {
91125
var _opts = _.assign({}, { merge: false }, opts);
92126
if (_opts.merge) {

src/firestore.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ MockFirestore.prototype.batch = function () {
8181
doc.set(data);
8282
}
8383
},
84+
create: function(doc, data) {
85+
doc.create(data);
86+
},
8487
update: function(doc, data) {
8588
doc.update(data);
8689
},

test/unit/firestore-document.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,53 @@ describe('MockFirestoreDocument', function () {
9292
});
9393
});
9494

95+
describe('#create', function () {
96+
it('creates a new doc', function (done) {
97+
var createDoc = db.doc('createDoc');
98+
99+
createDoc.create({prop: 'title'});
100+
101+
createDoc.get().then(function (snap) {
102+
expect(snap.exists).to.equal(true);
103+
expect(snap.get('prop')).to.equal('title');
104+
done();
105+
}).catch(done);
106+
107+
db.flush();
108+
});
109+
110+
it('creates a new doc with null values', function (done) {
111+
var createDoc = db.doc('createDoc');
112+
113+
createDoc.create({prop: null});
114+
115+
createDoc.get().then(function (snap) {
116+
expect(snap.exists).to.equal(true);
117+
expect(snap.get('prop')).to.equal(null);
118+
done();
119+
}).catch(done);
120+
121+
db.flush();
122+
});
123+
124+
it('throws an error when a doc already exists', function (done) {
125+
var createDoc = db.doc('createDoc');
126+
createDoc.create({prop: 'data'});
127+
db.flush();
128+
129+
createDoc.create({otherProp: 'more data'})
130+
.then(function() {
131+
done('should have thrown an error');
132+
})
133+
.catch(function(error) {
134+
expect(error.code).to.equal(6);
135+
done();
136+
});
137+
138+
db.flush();
139+
});
140+
});
141+
95142
describe('#set', function () {
96143
it('sets value of doc', function (done) {
97144
doc.set({

test/unit/firestore.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ describe('MockFirestore', function () {
148148
expect(db.collection('collections').doc('a').get()).to.eventually.have.property('exists').equal(true)
149149
]).then(function() {
150150
var batch = db.batch();
151+
batch.create(db.doc('docToCreate'), {
152+
name: 'abc'
153+
});
151154
batch.update(db.doc('doc'), {
152155
name: 'abc'
153156
});
@@ -157,6 +160,7 @@ describe('MockFirestore', function () {
157160
batch.delete(db.collection('collections').doc('a'));
158161
batch.commit().then(function() {
159162
Promise.all([
163+
expect(db.doc('docToCreate').get()).to.eventually.have.property('exists').equal(true),
160164
expect(db.doc('doc2').get()).to.eventually.have.property('exists').equal(true),
161165
expect(db.collection('collections').doc('a').get()).to.eventually.have.property('exists').equal(false)
162166
]).then(function() {

tutorials/client/firestore.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
When writing unit tests with Firebase Mock, you'll typically want to focus on covering one of two scenarios:
44

55
1. Your client receives data from Firestore using a method like `get`
6-
2. Your client writes data to Firestore using a method like `set` or `update`
6+
2. Your client writes data to Firestore using a method like `set`, `create` or `update`
77

88
While your application almost certainly does both reading and writing to Firestore, each test should try to cover as small a unit of functionality as possible.
99

0 commit comments

Comments
 (0)