Skip to content

Commit 73eb8a2

Browse files
committed
Unit tests.
1 parent 65c3d8b commit 73eb8a2

File tree

3 files changed

+98
-7
lines changed

3 files changed

+98
-7
lines changed

packages/firestore/src/api/snapshot.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ export class DocumentSnapshot<
523523
if (this.metadata.hasPendingWrites) {
524524
throw new FirestoreError(
525525
Code.FAILED_PRECONDITION,
526-
'DocumentSnapshot.toJSON attempted to serialize a document with pending writes. ' +
527-
'Await `waitForPendingWrites()` before invoking `toJSON()`.'
526+
'DocumentSnapshot.toJSON() attempted to serialize a document with pending writes. ' +
527+
'Await waitForPendingWrites() before invoking toJSON().'
528528
);
529529
}
530530
builder.addBundleDocument(
@@ -713,8 +713,8 @@ export class QuerySnapshot<
713713
if (this.metadata.hasPendingWrites) {
714714
throw new FirestoreError(
715715
Code.FAILED_PRECONDITION,
716-
'QuerySnapshot.toJSON attempted to serialize a document with pending writes. ' +
717-
'Await `waitForPendingWrites()` before invoking `toJSON()`.'
716+
'QuerySnapshot.toJSON() attempted to serialize a document with pending writes. ' +
717+
'Await waitForPendingWrites() before invoking toJSON().'
718718
);
719719
}
720720
docBundleDataArray.push(

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

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

2020
import {
2121
connectFirestoreEmulator,
22+
loadBundle,
2223
refEqual,
2324
snapshotEqual,
2425
queryEqual
@@ -35,6 +36,15 @@ import {
3536
} from '../../util/api_helpers';
3637
import { keys } from '../../util/helpers';
3738

39+
describe('Bundle', () => {
40+
it('loadBundle silently exits with an empty bundle string)', async () => {
41+
const db = newTestFirestore();
42+
expect(async () => {
43+
await loadBundle(db, '');
44+
}).to.not.throw;
45+
});
46+
});
47+
3848
describe('CollectionReference', () => {
3949
it('support equality checking with isEqual()', () => {
4050
expect(refEqual(collectionReference('foo'), collectionReference('foo'))).to
@@ -107,6 +117,44 @@ describe('DocumentSnapshot', () => {
107117
it('JSON.stringify() does not throw', () => {
108118
JSON.stringify(documentSnapshot('foo/bar', { a: 1 }, true));
109119
});
120+
121+
it('toJSON returns a bundle', () => {
122+
const json = documentSnapshot(
123+
'foo/bar',
124+
{ a: 1 },
125+
/*fromCache=*/ true
126+
).toJSON();
127+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
128+
expect((json as any).bundle).to.exist;
129+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
130+
expect((json as any).bundle.length).to.be.greaterThan(0);
131+
});
132+
133+
it('toJSON returns an empty bundle when there are no documents', () => {
134+
const json = documentSnapshot(
135+
'foo/bar',
136+
/*data=*/ null,
137+
/*fromCache=*/ true
138+
).toJSON();
139+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
140+
expect((json as any).bundle).to.exist;
141+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
142+
expect((json as any).bundle.length).to.equal(0);
143+
});
144+
145+
it('toJSON throws when there are pending writes', () => {
146+
expect(() => {
147+
documentSnapshot(
148+
'foo/bar',
149+
{},
150+
/*fromCache=*/ true,
151+
/*hasPendingWrites=*/ true
152+
).toJSON();
153+
}).to.throw(
154+
`DocumentSnapshot.toJSON() attempted to serialize a document with pending writes. ` +
155+
`Await waitForPendingWrites() before invoking toJSON().`
156+
);
157+
});
110158
});
111159

112160
describe('Query', () => {
@@ -229,6 +277,45 @@ describe('QuerySnapshot', () => {
229277
querySnapshot('foo', {}, { a: { a: 1 } }, keys(), false, false)
230278
);
231279
});
280+
281+
it('toJSON returns a bundle', () => {
282+
const json = querySnapshot(
283+
'foo',
284+
{},
285+
{ a: { a: 1 } },
286+
keys(),
287+
false,
288+
false
289+
).toJSON();
290+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
291+
expect((json as any).bundle).to.exist;
292+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
293+
expect((json as any).bundle.length).to.be.greaterThan(0);
294+
});
295+
296+
it('toJSON returns a bundle when there are no documents', () => {
297+
const json = querySnapshot('foo', {}, {}, keys(), false, false).toJSON();
298+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
299+
expect((json as any).bundle).to.exist;
300+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
301+
expect((json as any).bundle.length).to.be.greaterThan(0);
302+
});
303+
304+
it('toJSON throws when there are pending writes', () => {
305+
expect(() =>
306+
querySnapshot(
307+
'foo',
308+
{},
309+
{ a: { a: 1 } },
310+
keys('foo/a'),
311+
true,
312+
true
313+
).toJSON()
314+
).to.throw(
315+
`QuerySnapshot.toJSON() attempted to serialize a document with pending writes. ` +
316+
`Await waitForPendingWrites() before invoking toJSON().`
317+
);
318+
});
232319
});
233320

234321
describe('SnapshotMetadata', () => {

packages/firestore/test/util/api_helpers.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,12 @@ export function documentReference(path: string): DocumentReference {
7979
export function documentSnapshot(
8080
path: string,
8181
data: JsonObject<unknown> | null,
82-
fromCache: boolean
82+
fromCache: boolean,
83+
hasPendingWrites?: boolean
8384
): DocumentSnapshot {
85+
if (hasPendingWrites === undefined) {
86+
hasPendingWrites = false;
87+
}
8488
const db = firestore();
8589
const userDataWriter = new ExpUserDataWriter(db);
8690
if (data) {
@@ -89,7 +93,7 @@ export function documentSnapshot(
8993
userDataWriter,
9094
key(path),
9195
doc(path, 1, data),
92-
new SnapshotMetadata(/* hasPendingWrites= */ false, fromCache),
96+
new SnapshotMetadata(hasPendingWrites, fromCache),
9397
/* converter= */ null
9498
);
9599
} else {
@@ -98,7 +102,7 @@ export function documentSnapshot(
98102
userDataWriter,
99103
key(path),
100104
null,
101-
new SnapshotMetadata(/* hasPendingWrites= */ false, fromCache),
105+
new SnapshotMetadata(hasPendingWrites, fromCache),
102106
/* converter= */ null
103107
);
104108
}

0 commit comments

Comments
 (0)