Skip to content

Commit

Permalink
fix(firestore): implement refEqual modular API
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehardy committed Feb 18, 2025
1 parent 4aeae44 commit f30a4fb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
20 changes: 11 additions & 9 deletions packages/firestore/e2e/DocumentReference/isEqual.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ describe('firestore.doc().isEqual()', function () {

describe('modular', function () {
it('throws if other is not a DocumentReference', function () {
const { getFirestore, doc } = firestoreModular;
const { getFirestore, doc, refEqual } = firestoreModular;
try {
doc(getFirestore(), 'bar/baz').isEqual(123);
refEqual(doc(getFirestore(), 'bar/baz'), 123);
return Promise.reject(new Error('Did not throw an Error.'));
} catch (error) {
error.message.should.containEql("'other' expected a DocumentReference instance");
Expand All @@ -74,27 +74,29 @@ describe('firestore.doc().isEqual()', function () {
});

it('returns false when not equal', function () {
const { getFirestore, doc } = firestoreModular;
const { getApp } = modular;
const { getFirestore, doc, refEqual } = firestoreModular;
const db = getFirestore();

const docRef = doc(db, `${COLLECTION}/baz`);

const eql1 = docRef.isEqual(doc(db, `${COLLECTION}/foo`));
const eql2 = docRef.isEqual(
doc(getFirestore(firebase.app('secondaryFromNative')), `${COLLECTION}/baz`),
const eql1 = refEqual(docRef, doc(db, `${COLLECTION}/foo`));
const eql2 = refEqual(
docRef,
doc(getFirestore(getApp('secondaryFromNative')), `${COLLECTION}/baz`),
);

eql1.should.be.False();
eql2.should.be.False();
});

it('returns true when equal', function () {
const { getFirestore, doc } = firestoreModular;
const { getFirestore, doc, refEqual } = firestoreModular;
const db = getFirestore();
const docRef = doc(db, `${COLLECTION}/baz`);

const eql1 = docRef.isEqual(docRef);
const eql2 = docRef.isEqual(doc(db, `${COLLECTION}/baz`));
const eql1 = refEqual(docRef, docRef);
const eql2 = refEqual(docRef, doc(db, `${COLLECTION}/baz`));

eql1.should.be.True();
eql2.should.be.True();
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/e2e/Query/isEqual.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ describe('firestore().collection().isEqual()', function () {

describe('modular', function () {
it('throws if other is not a Query', function () {
const { getFirestore, collection } = firestoreModular;
const { getFirestore, collection, refEqual } = firestoreModular;
try {
collection(getFirestore(), COLLECTION).isEqual(123);
refEqual(collection(getFirestore(), COLLECTION), 123);
return Promise.reject(new Error('Did not throw an Error.'));
} catch (error) {
error.message.should.containEql("'other' expected a Query instance");
Expand Down
16 changes: 16 additions & 0 deletions packages/firestore/lib/modular/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@ export function collection(
...pathSegments: string[]
): CollectionReference<DocumentData>;

/**
*Returns true if the provided references are equal.
*
* @param left DocumentReference<AppModelType, DbModelType> | CollectionReference<AppModelType, DbModelType> A reference to compare.
* @param right DocumentReference<AppModelType, DbModelType> | CollectionReference<AppModelType, DbModelType> A reference to compare.
* @return boolean true if the references point to the same location in the same Firestore database.
*/
export declare function refEqual<AppModelType, DbModelType extends DocumentData>(
left:
| DocumentReference<AppModelType, DbModelType>
| CollectionReference<AppModelType, DbModelType>,
right:
| DocumentReference<AppModelType, DbModelType>
| CollectionReference<AppModelType, DbModelType>,
): boolean;

/**
* Creates and returns a new `Query` instance that includes all documents in the
* database that are contained in a collection or subcollection with the
Expand Down
9 changes: 9 additions & 0 deletions packages/firestore/lib/modular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ export function collection(parent, path, ...pathSegments) {
return parent.collection.call(parent, path, MODULAR_DEPRECATION_ARG);
}

/**
* @param {DocumentReference<AppModelType, DbModelType> | CollectionReference<AppModelType, DbModelType>} left
* @param {DocumentReference<AppModelType, DbModelType> | CollectionReference<AppModelType, DbModelType>} right
* @return boolean true if the two references are equal
*/
export function refEqual(left, right) {
return left.isEqual.call(left, right, MODULAR_DEPRECATION_ARG);
}

/**
* @param {Firestore} firestore
* @param {string} collectionId
Expand Down

0 comments on commit f30a4fb

Please sign in to comment.