Skip to content

Commit

Permalink
FieldValue tests
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley committed Nov 29, 2024
1 parent 3c728e2 commit da7d510
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
26 changes: 22 additions & 4 deletions packages/app/lib/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ const mapOfDeprecationReplacements = {
FirestoreDocumentSnapshot: {
isEqual: NO_REPLACEMENT,
},
FirestoreFieldValue: {
arrayRemove: 'arrayRemove()',
arrayUnion: 'arrayUnion()',
delete: 'deleteField()',
increment: 'increment()',
serverTimestamp: 'serverTimestamp()',
},
},
};

Expand Down Expand Up @@ -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) {
Expand All @@ -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';

Check failure on line 246 in packages/app/lib/common/index.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`

Check failure on line 246 in packages/app/lib/common/index.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`

Check failure on line 246 in packages/app/lib/common/index.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`
deprecationConsoleWarning(nameSpace, prop, instanceName, isModularMethod);

Expand Down
43 changes: 42 additions & 1 deletion packages/firestore/__tests__/firestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ describe('Firestore', function () {
// let firestoreV9Deprecation: CheckV9DeprecationFunction;
let collectionRefV9Deprecation: CheckV9DeprecationFunction;
let docRefV9Deprecation: CheckV9DeprecationFunction;
let fieldValueV9Deprecation: CheckV9DeprecationFunction;

beforeEach(function () {
// firestoreV9Deprecation = createCheckV9Deprecation(['firestore']);
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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',
);
});
});
});
3 changes: 2 additions & 1 deletion packages/firestore/lib/FirestoreStatics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down

0 comments on commit da7d510

Please sign in to comment.