From da7d5103b72294488ec1f477cae8af56fcef4981 Mon Sep 17 00:00:00 2001 From: russellwheatley Date: Fri, 29 Nov 2024 17:52:17 +0000 Subject: [PATCH] FieldValue tests --- packages/app/lib/common/index.js | 26 +++++++++-- .../firestore/__tests__/firestore.test.ts | 43 ++++++++++++++++++- packages/firestore/lib/FirestoreStatics.js | 3 +- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/packages/app/lib/common/index.js b/packages/app/lib/common/index.js index 3276088aa5..33a402394f 100644 --- a/packages/app/lib/common/index.js +++ b/packages/app/lib/common/index.js @@ -152,6 +152,13 @@ const mapOfDeprecationReplacements = { FirestoreDocumentSnapshot: { isEqual: NO_REPLACEMENT, }, + FirestoreFieldValue: { + arrayRemove: 'arrayRemove()', + arrayUnion: 'arrayUnion()', + delete: 'deleteField()', + increment: 'increment()', + serverTimestamp: 'serverTimestamp()', + }, }, }; @@ -208,6 +215,19 @@ function getNamespace(className) { }); } +function getInstanceName(target) { + if (target._config) { + // module class instance, we use default to store map of deprecated methods + return 'default'; + } + if (target.name) { + // It's a function which has a name property unlike classes + return target.name; + } + // It's a class instance + return target.constructor.name; +} + export function createDeprecationProxy(instance) { return new Proxy(instance, { get(target, prop, receiver) { @@ -220,11 +240,9 @@ export function createDeprecationProxy(instance) { if (typeof originalMethod === 'function') { return function (...args) { const isModularMethod = args.includes(MODULAR_DEPRECATION_ARG); - const nameSpace = receiver._config - ? receiver._config.namespace - : getNamespace(target.constructor.name); + const instanceName = getInstanceName(target); + const nameSpace = getNamespace(instanceName); - const instanceName = !receiver._config ? target.constructor.name : 'default'; deprecationConsoleWarning(nameSpace, prop, instanceName, isModularMethod); diff --git a/packages/firestore/__tests__/firestore.test.ts b/packages/firestore/__tests__/firestore.test.ts index 9d00bd932c..afc54c395a 100644 --- a/packages/firestore/__tests__/firestore.test.ts +++ b/packages/firestore/__tests__/firestore.test.ts @@ -714,6 +714,7 @@ describe('Firestore', function () { // let firestoreV9Deprecation: CheckV9DeprecationFunction; let collectionRefV9Deprecation: CheckV9DeprecationFunction; let docRefV9Deprecation: CheckV9DeprecationFunction; + let fieldValueV9Deprecation: CheckV9DeprecationFunction; beforeEach(function () { // firestoreV9Deprecation = createCheckV9Deprecation(['firestore']); @@ -724,7 +725,7 @@ describe('Firestore', function () { docRefV9Deprecation = createCheckV9Deprecation(['firestore', 'FirestoreDocumentReference']); - // queryV9Deprecation = createCheckV9Deprecation(['firestore', 'FirestoreQuery']); + fieldValueV9Deprecation = createCheckV9Deprecation(['firestore', 'FirestoreFieldValue']); // @ts-ignore test jest.spyOn(FirebaseModule.prototype, 'native', 'get').mockImplementation(() => { @@ -1042,5 +1043,45 @@ describe('Firestore', function () { 'isEqual', ); }); + + it('FirestoreFieldValue.delete()', function () { + fieldValueV9Deprecation( + () => deleteField(), + () => firestore.FieldValue.delete(), + 'delete', + ); + }); + + it('FirestoreFieldValue.increment()', function () { + fieldValueV9Deprecation( + () => increment(3), + () => firestore.FieldValue.increment(4), + 'increment', + ); + }); + + it('FirestoreFieldValue.serverTimestamp()', function () { + fieldValueV9Deprecation( + () => serverTimestamp(), + () => firestore.FieldValue.serverTimestamp(), + 'serverTimestamp', + ); + }); + + it('FirestoreFieldValue.arrayUnion()', function () { + fieldValueV9Deprecation( + () => arrayUnion('foo'), + () => firestore.FieldValue.arrayUnion('bar'), + 'arrayUnion', + ); + }); + + it('FirestoreFieldValue.arrayRemove()', function () { + fieldValueV9Deprecation( + () => arrayRemove('foo'), + () => firestore.FieldValue.arrayRemove('bar'), + 'arrayRemove', + ); + }); }); }); diff --git a/packages/firestore/lib/FirestoreStatics.js b/packages/firestore/lib/FirestoreStatics.js index 8f98c714a8..fefd74b0a8 100644 --- a/packages/firestore/lib/FirestoreStatics.js +++ b/packages/firestore/lib/FirestoreStatics.js @@ -15,6 +15,7 @@ * */ +import { createDeprecationProxy } from '@react-native-firebase/app/lib/common'; import { getReactNativeModule } from '@react-native-firebase/app/lib/internal/nativeModule'; import FirestoreBlob from './FirestoreBlob'; import FirestoreFieldPath from './FirestoreFieldPath'; @@ -25,7 +26,7 @@ import { Filter } from './FirestoreFilter'; export default { Blob: FirestoreBlob, FieldPath: FirestoreFieldPath, - FieldValue: FirestoreFieldValue, + FieldValue: createDeprecationProxy(FirestoreFieldValue), GeoPoint: FirestoreGeoPoint, Timestamp: FirestoreTimestamp, Filter: Filter,