Skip to content

Commit 188c86f

Browse files
committed
DocumentReference unit and integration tests
1 parent 44d8045 commit 188c86f

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

packages/firestore/test/lite/integration.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,20 @@ describe('getDoc()', () => {
423423
expect(docSnap.exists()).to.be.true;
424424
});
425425
});
426+
427+
it('can get doc with a deserialized reference', () => {
428+
return withTestDocAndInitialData({ val: 1 }, async docRef => {
429+
const docSnap = await getDoc(docRef);
430+
expect(docSnap.exists()).to.be.true;
431+
const json = docRef.toJSON();
432+
const deserializedDocRef = DocumentReference.fromJSON(
433+
docSnap._firestore,
434+
json
435+
);
436+
const docSnap2 = await getDoc(deserializedDocRef);
437+
expect(docSnap2.exists()).to.be.true;
438+
});
439+
});
426440
});
427441

428442
/**

packages/firestore/test/unit/api/database.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import { expect } from 'chai';
1919

2020
import {
21+
DocumentReference,
2122
connectFirestoreEmulator,
2223
loadBundle,
2324
refEqual,
@@ -71,6 +72,40 @@ describe('DocumentReference', () => {
7172
it('JSON.stringify() does not throw', () => {
7273
JSON.stringify(documentReference('foo/bar'));
7374
});
75+
76+
it('toJSON() does not throw', () => {
77+
expect(() => {
78+
documentReference('foo/bar').toJSON();
79+
}).to.not.throw;
80+
});
81+
82+
it('toJSON() includes correct JSON fields', () => {
83+
const docRef = documentReference('foo/bar');
84+
const json = docRef.toJSON();
85+
expect(json).to.deep.equal({
86+
type: 'firestore/documentReference/1.0',
87+
referencePath: 'foo/bar'
88+
});
89+
});
90+
91+
it('fromJSON() does not throw', () => {
92+
const db = newTestFirestore();
93+
const docRef = documentReference('foo/bar');
94+
const json = docRef.toJSON();
95+
expect(() => {
96+
DocumentReference.fromJSON(db, json);
97+
}).to.not.throw;
98+
});
99+
100+
it('fromJSON() equals original docRef', () => {
101+
const db = newTestFirestore();
102+
const docRef = documentReference('foo/bar');
103+
const json = docRef.toJSON();
104+
const deserializedDocRef = DocumentReference.fromJSON(db, json);
105+
expect(docRef.id).to.equal(deserializedDocRef.id);
106+
expect(docRef.path).to.equal(deserializedDocRef.path);
107+
expect(docRef.toJSON()).to.deep.equal(deserializedDocRef.toJSON());
108+
});
74109
});
75110

76111
describe('DocumentSnapshot', () => {

0 commit comments

Comments
 (0)