Skip to content

Commit 6da740f

Browse files
committed
fix(firestore): implement snapshotEqual in modular API
this also quiets all the deprecation warnings in the generic firestore e2e suite as well as the QuerySnapshot suite
1 parent 1cf9f50 commit 6da740f

File tree

6 files changed

+78
-28
lines changed

6 files changed

+78
-28
lines changed

packages/firestore/e2e/DocumentSnapshot/isEqual.e2e.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ describe('firestore.doc() -> snapshot.isEqual()', function () {
6161

6262
describe('modular', function () {
6363
it('throws if other is not a DocumentSnapshot', async function () {
64-
const { getFirestore, doc, getDoc } = firestoreModular;
64+
const { getFirestore, doc, getDoc, snapshotEqual } = firestoreModular;
6565
try {
6666
const docRef = doc(getFirestore(), `${COLLECTION}/baz`);
6767

6868
const docSnapshot = await getDoc(docRef);
69-
docSnapshot.isEqual(123);
69+
snapshotEqual(docSnapshot, 123);
7070
return Promise.reject(new Error('Did not throw an Error.'));
7171
} catch (error) {
7272
error.message.should.containEql("'other' expected a DocumentSnapshot instance");
@@ -75,7 +75,7 @@ describe('firestore.doc() -> snapshot.isEqual()', function () {
7575
});
7676

7777
it('returns false when not equal', async function () {
78-
const { getFirestore, doc, setDoc, getDoc } = firestoreModular;
78+
const { getFirestore, doc, setDoc, getDoc, snapshotEqual } = firestoreModular;
7979
const db = getFirestore();
8080
const docRef = doc(db, `${COLLECTION}/isEqual-false-exists`);
8181
await setDoc(docRef, { foo: 'bar' });
@@ -85,21 +85,21 @@ describe('firestore.doc() -> snapshot.isEqual()', function () {
8585
await setDoc(docRef, { foo: 'baz' });
8686
const docSnapshot3 = await getDoc(docRef);
8787

88-
const eql1 = docSnapshot1.isEqual(docSnapshot2);
89-
const eql2 = docSnapshot1.isEqual(docSnapshot3);
88+
const eql1 = snapshotEqual(docSnapshot1, docSnapshot2);
89+
const eql2 = snapshotEqual(docSnapshot1, docSnapshot3);
9090

9191
eql1.should.be.False();
9292
eql2.should.be.False();
9393
});
9494

9595
it('returns true when equal', async function () {
96-
const { getFirestore, doc, setDoc, getDoc } = firestoreModular;
96+
const { getFirestore, doc, setDoc, getDoc, snapshotEqual } = firestoreModular;
9797
const docRef = doc(getFirestore(), `${COLLECTION}/isEqual-true-exists`);
9898
await setDoc(docRef, { foo: 'bar' });
9999

100100
const docSnapshot = await getDoc(docRef);
101101

102-
const eql1 = docSnapshot.isEqual(docSnapshot);
102+
const eql1 = snapshotEqual(docSnapshot, docSnapshot);
103103

104104
eql1.should.be.True();
105105
});

packages/firestore/e2e/QuerySnapshot.e2e.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ describe('firestore.QuerySnapshot', function () {
2323
});
2424

2525
describe('v8 compatibility', function () {
26+
beforeEach(async function beforeEachTest() {
27+
// @ts-ignore
28+
globalThis.RNFB_SILENCE_MODULAR_DEPRECATION_WARNINGS = true;
29+
});
30+
31+
afterEach(async function afterEachTest() {
32+
// @ts-ignore
33+
globalThis.RNFB_SILENCE_MODULAR_DEPRECATION_WARNINGS = false;
34+
});
35+
2636
it('is returned from a collection get()', async function () {
2737
const snapshot = await firebase.firestore().collection(COLLECTION).get();
2838

@@ -564,11 +574,11 @@ describe('firestore.QuerySnapshot', function () {
564574

565575
describe('isEqual()', function () {
566576
it('throws if other is not a QuerySnapshot', async function () {
567-
const { getFirestore, collection, getDocs } = firestoreModular;
577+
const { getFirestore, collection, getDocs, snapshotEqual } = firestoreModular;
568578

569579
try {
570580
const qs = await getDocs(collection(getFirestore(), COLLECTION));
571-
qs.isEqual(123);
581+
snapshotEqual(qs, 123);
572582
return Promise.reject(new Error('Did not throw an Error.'));
573583
} catch (error) {
574584
error.message.should.containEql("'other' expected a QuerySnapshot instance");
@@ -577,7 +587,7 @@ describe('firestore.QuerySnapshot', function () {
577587
});
578588

579589
it('returns false if not equal (simple checks)', async function () {
580-
const { getFirestore, collection, addDoc, getDocs } = firestoreModular;
590+
const { getFirestore, collection, addDoc, getDocs, snapshotEqual } = firestoreModular;
581591
const db = getFirestore();
582592

583593
const colRef = collection(db, COLLECTION);
@@ -590,13 +600,23 @@ describe('firestore.QuerySnapshot', function () {
590600
collection(db, `${COLLECTION}/querysnapshot/querySnapshotIsEqual`),
591601
);
592602

593-
const eq1 = qs.isEqual(querySnap1);
603+
const eq1 = snapshotEqual(qs, querySnap1);
594604

595605
eq1.should.be.False();
596606
});
597607

598608
it('returns false if not equal (expensive checks)', async function () {
599-
const { getFirestore, collection, doc, setDoc, getDoc, updateDoc } = firestoreModular;
609+
const {
610+
getFirestore,
611+
collection,
612+
doc,
613+
setDoc,
614+
getDoc,
615+
getDocs,
616+
updateDoc,
617+
query,
618+
snapshotEqual,
619+
} = firestoreModular;
600620

601621
const colRef = collection(
602622
getFirestore(),
@@ -621,15 +641,15 @@ describe('firestore.QuerySnapshot', function () {
621641
},
622642
});
623643

624-
const qs2 = await colRef.get();
644+
const qs2 = await getDocs(query(colRef));
625645

626-
const eq1 = qs1.isEqual(qs2);
646+
const eq1 = snapshotEqual(qs1, qs2);
627647

628648
eq1.should.be.False();
629649
});
630650

631651
it('returns true when equal', async function () {
632-
const { getFirestore, collection, addDoc, getDocs } = firestoreModular;
652+
const { getFirestore, collection, addDoc, getDocs, snapshotEqual } = firestoreModular;
633653

634654
const colRef = collection(
635655
getFirestore(),
@@ -649,7 +669,7 @@ describe('firestore.QuerySnapshot', function () {
649669
const qs1 = await getDocs(colRef);
650670
const qs2 = await getDocs(colRef);
651671

652-
const eq = qs1.isEqual(qs2);
672+
const eq = snapshotEqual(qs1, qs2);
653673

654674
eq.should.be.True();
655675
});

packages/firestore/e2e/firestore.e2e.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ const COLLECTION_GROUP = 'collectionGroup';
1919

2020
describe('firestore()', function () {
2121
describe('v8 compatibility', function () {
22+
beforeEach(async function beforeEachTest() {
23+
// @ts-ignore
24+
globalThis.RNFB_SILENCE_MODULAR_DEPRECATION_WARNINGS = true;
25+
});
26+
27+
afterEach(async function afterEachTest() {
28+
// @ts-ignore
29+
globalThis.RNFB_SILENCE_MODULAR_DEPRECATION_WARNINGS = false;
30+
});
31+
2232
describe('namespace', function () {
2333
// removing as pending if module.options.hasMultiAppSupport = true
2434
it('supports multiple apps', async function () {
@@ -422,9 +432,10 @@ describe('firestore()', function () {
422432
describe('getFirestore', function () {
423433
// removing as pending if module.options.hasMultiAppSupport = true
424434
it('supports multiple apps', async function () {
435+
const { getApp } = modular;
425436
const { getFirestore } = firestoreModular;
426437
const db1 = await getFirestore();
427-
const db2 = await getFirestore(firebase.app('secondaryFromNative'));
438+
const db2 = await getFirestore(getApp('secondaryFromNative'));
428439

429440
db1.app.name.should.equal('[DEFAULT]');
430441
db2.app.name.should.equal('secondaryFromNative');
@@ -663,9 +674,9 @@ describe('firestore()', function () {
663674
await deleteDoc(ref);
664675
});
665676

666-
// FIXME: works in isolation but not in suite
667-
xit("handles 'previous'", async function () {
668-
const { initializeFirestore, doc, onSnapshot, setDoc, deleteDoc } = firestoreModular;
677+
it("handles 'previous'", async function () {
678+
const { initializeFirestore, doc, onSnapshot, setDoc, deleteDoc, snapshotEqual } =
679+
firestoreModular;
669680

670681
const db = await initializeFirestore(firebase.app(), {
671682
serverTimestampBehavior: 'previous',
@@ -692,17 +703,17 @@ describe('firestore()', function () {
692703
should(snapshot.get('timestamp')).be.an.instanceOf(
693704
firebase.firestore.Timestamp,
694705
);
695-
should(snapshot.get('timestamp').isEqual(previous.get('timestamp'))).equal(
696-
true,
697-
);
706+
should(
707+
snapshotEqual(snapshot.get('timestamp'), previous.get('timestamp')),
708+
).equal(true);
698709
break;
699710
case 3:
700711
should(snapshot.get('timestamp')).be.an.instanceOf(
701712
firebase.firestore.Timestamp,
702713
);
703-
should(snapshot.get('timestamp').isEqual(previous.get('timestamp'))).equal(
704-
false,
705-
);
714+
should(
715+
snapshotEqual(snapshot.get('timestamp'), previous.get('timestamp')),
716+
).equal(false);
706717
subscription();
707718
resolve();
708719
break;

packages/firestore/lib/FirestoreQuerySnapshot.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ export default class FirestoreQuerySnapshot {
112112
}
113113
}
114114

115-
isEqual(other) {
115+
// send '...args' through as it may contain our namespace deprecation marker
116+
isEqual(other, ...args) {
116117
if (!(other instanceof FirestoreQuerySnapshot)) {
117118
throw new Error(
118119
"firebase.firestore() QuerySnapshot.isEqual(*) 'other' expected a QuerySnapshot instance.",
@@ -134,7 +135,8 @@ export default class FirestoreQuerySnapshot {
134135
const thisDoc = this.docs[i];
135136
const otherDoc = other.docs[i];
136137

137-
if (!thisDoc.isEqual(otherDoc)) {
138+
// send '...args' through as it may contain our namespace deprecation marker
139+
if (!thisDoc.isEqual(otherDoc, ...args)) {
138140
return false;
139141
}
140142
}

packages/firestore/lib/modular/snapshot.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,16 @@ export function onSnapshot<T>(
201201
onError?: (error: FirestoreError) => void,
202202
onCompletion?: () => void,
203203
): Unsubscribe;
204+
205+
/**
206+
* Returns true if the provided snapshots are equal.
207+
*
208+
* @param left DocumentSnapshot<AppModelType, DbModelType> | QuerySnapshot<AppModelType, DbModelType> A snapshot to compare.
209+
* @param right DocumentSnapshot<AppModelType, DbModelType> | QuerySnapshot<AppModelType, DbModelType> A snapshot to compare.
210+
*
211+
* @returns boolean true if the snapshots are equal.
212+
*/
213+
export function snapshotEqual<AppModelType, DbModelType extends DocumentData>(
214+
left: DocumentSnapshot<AppModelType, DbModelType> | QuerySnapshot<AppModelType, DbModelType>,
215+
right: DocumentSnapshot<AppModelType, DbModelType> | QuerySnapshot<AppModelType, DbModelType>,
216+
): boolean;

packages/firestore/lib/modular/snapshot.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ import { MODULAR_DEPRECATION_ARG } from '../../../app/lib/common';
1414
export function onSnapshot(reference, ...args) {
1515
return reference.onSnapshot.call(reference, ...args, MODULAR_DEPRECATION_ARG);
1616
}
17+
18+
export function snapshotEqual(left, right) {
19+
return left.isEqual.call(left, right, MODULAR_DEPRECATION_ARG);
20+
}

0 commit comments

Comments
 (0)