Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jest.doMock('react-native', () => {
OS: 'android',
select: () => {},
},
AppRegistry: {
registerHeadlessTask: jest.fn(),
},
NativeModules: {
...ReactNative.NativeModules,
RNFBAnalyticsModule: {
Expand Down
86 changes: 86 additions & 0 deletions packages/app/lib/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,47 @@ const mapOfDeprecationReplacements = {
nanoseconds: NO_REPLACEMENT,
},
},
messaging: {
default: {
isAutoInitEnabled: 'isAutoInitEnabled()',
isDeviceRegisteredForRemoteMessages: 'isDeviceRegisteredForRemoteMessages()',
isNotificationDelegationEnabled: 'isNotificationDelegationEnabled()',
isDeliveryMetricsExportToBigQueryEnabled: 'isDeliveryMetricsExportToBigQueryEnabled()',
setAutoInitEnabled: 'setAutoInitEnabled()',
getInitialNotification: 'getInitialNotification()',
getDidOpenSettingsForNotification: 'getDidOpenSettingsForNotification()',
getIsHeadless: 'getIsHeadless()',
onNotificationOpenedApp: 'onNotificationOpenedApp()',
onTokenRefresh: 'onTokenRefresh()',
requestPermission: 'requestPermission()',
registerDeviceForRemoteMessages: 'registerDeviceForRemoteMessages()',
unregisterDeviceForRemoteMessages: 'unregisterDeviceForRemoteMessages()',
getAPNSToken: 'getAPNSToken()',
setAPNSToken: 'setAPNSToken()',
hasPermission: 'hasPermission()',
onDeletedMessages: 'onDeletedMessages()',
onMessageSent: 'onMessageSent()',
onSendError: 'onSendError()',
setBackgroundMessageHandler: 'setBackgroundMessageHandler()',
setOpenSettingsForNotificationsHandler: 'setOpenSettingsForNotificationsHandler()',
sendMessage: 'sendMessage()',
subscribeToTopic: 'subscribeToTopic()',
unsubscribeFromTopic: 'unsubscribeFromTopic()',
setNotificationDelegationEnabled: 'setNotificationDelegationEnabled()',
// Actual firebase-js-sdk methods
getToken: 'getToken()',
deleteToken: 'deleteToken()',
onMessage: 'onMessage()',
isSupported: 'isSupported()',
setDeliveryMetricsExportToBigQuery:
'experimentalSetDeliveryMetricsExportedToBigQueryEnabled()',
},
statics: {
AuthorizationStatus: 'AuthorizationStatus',
NotificationAndroidPriority: 'NotificationAndroidPriority',
NotificationAndroidVisibility: 'NotificationAndroidVisibility',
},
},
remoteConfig: {
default: {
activate: 'activate()',
Expand Down Expand Up @@ -553,12 +594,44 @@ export function createDeprecationProxy(instance) {
deprecationConsoleWarning('auth', prop, 'statics', false);
}

if (
prop === 'AuthorizationStatus' ||
prop === 'NotificationAndroidPriority' ||
prop === 'NotificationAndroidVisibility'
) {
deprecationConsoleWarning('messaging', prop, 'statics', false);
}

if (prop !== 'setLogLevel') {
// we want to capture setLogLevel function call which we do below
return Reflect.get(target, prop, receiver);
}
}

// Check if it's a getter/setter first
const descriptor =
Object.getOwnPropertyDescriptor(target, prop) ||
Object.getOwnPropertyDescriptor(Object.getPrototypeOf(target), prop);

if (descriptor && (descriptor.get || descriptor.set)) {
const instanceName = getInstanceName(target);
const nameSpace = getNamespace(target);

if (descriptor.get) {
// Handle getter - call it and show deprecation warning
deprecationConsoleWarning(nameSpace, prop, instanceName, _isModularCall);
return descriptor.get.call(target);
}

if (descriptor.set) {
// Handle setter - return a function that calls the setter with deprecation warning
return function (value) {
deprecationConsoleWarning(nameSpace, prop, instanceName, _isModularCall);
descriptor.set.call(target, value);
};
}
}

if (typeof originalMethod === 'function') {
return function (...args) {
const isModularMethod = args.includes(MODULAR_DEPRECATION_ARG);
Expand All @@ -577,6 +650,19 @@ export function createDeprecationProxy(instance) {

export const MODULAR_DEPRECATION_ARG = 'react-native-firebase-modular-method-call';

// Flag to track if we're currently in a modular call
let _isModularCall = false;

export function withModularFlag(fn) {
const previousFlag = _isModularCall;
_isModularCall = true;
try {
return fn();
} finally {
_isModularCall = previousFlag;
}
}

export function filterModularArgument(list) {
return list.filter(arg => arg !== MODULAR_DEPRECATION_ARG);
}
Expand Down
227 changes: 224 additions & 3 deletions packages/messaging/__tests__/messaging.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from '@jest/globals';
import { beforeEach, describe, expect, it, jest } from '@jest/globals';

import {
import messaging, {
getMessaging,
deleteToken,
getToken,
Expand Down Expand Up @@ -35,7 +35,15 @@ import {
NotificationAndroidVisibility,
} from '../lib';

describe('Firestore', function () {
import {
createCheckV9Deprecation,
CheckV9DeprecationFunction,
} from '../../app/lib/common/unitTestUtils';

// @ts-ignore test
import FirebaseModule from '../../app/lib/internal/FirebaseModule';

describe('Messaging', function () {
describe('modular', function () {
it('`getMessaging` function is properly exposed to end user', function () {
expect(getMessaging).toBeDefined();
Expand Down Expand Up @@ -175,4 +183,217 @@ describe('Firestore', function () {
expect(NotificationAndroidVisibility.VISIBILITY_SECRET).toBeDefined();
});
});

describe('test `console.warn` is called for RNFB v8 API & not called for v9 API', function () {
let messagingV9Deprecation: CheckV9DeprecationFunction;
let staticsV9Deprecation: CheckV9DeprecationFunction;

beforeEach(function () {
messagingV9Deprecation = createCheckV9Deprecation(['messaging']);
staticsV9Deprecation = createCheckV9Deprecation(['messaging', 'statics']);

// @ts-ignore test
jest.spyOn(FirebaseModule.prototype, 'native', 'get').mockImplementation(() => {
return new Proxy(
{},
{
get: () =>
jest.fn().mockResolvedValue({
result: true,
} as never),
},
);
});
});

describe('Messaging', function () {
it('isAutoInitEnabled', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => isAutoInitEnabled(messaging),
() => messaging.isAutoInitEnabled,
'isAutoInitEnabled',
);
});

it('isDeviceRegisteredForRemoteMessages', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => isDeviceRegisteredForRemoteMessages(messaging),
() => messaging.isDeviceRegisteredForRemoteMessages,
'isDeviceRegisteredForRemoteMessages',
);
});

it('setAutoInitEnabled', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => setAutoInitEnabled(messaging, true),
() => messaging.setAutoInitEnabled(true),
'setAutoInitEnabled',
);
});

it('getInitialNotification', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => getInitialNotification(messaging),
() => messaging.getInitialNotification(),
'getInitialNotification',
);
});

it('getDidOpenSettingsForNotification', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => getDidOpenSettingsForNotification(messaging),
() => messaging.getDidOpenSettingsForNotification(),
'getDidOpenSettingsForNotification',
);
});

it('getIsHeadless', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => getIsHeadless(messaging),
() => messaging.getIsHeadless(),
'getIsHeadless',
);
});

it('requestPermission', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => requestPermission(messaging),
() => messaging.requestPermission(),
'requestPermission',
);
});

it('registerDeviceForRemoteMessages', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => registerDeviceForRemoteMessages(messaging),
() => messaging.registerDeviceForRemoteMessages(),
'registerDeviceForRemoteMessages',
);
});

it('unregisterDeviceForRemoteMessages', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => unregisterDeviceForRemoteMessages(messaging),
() => messaging.unregisterDeviceForRemoteMessages(),
'unregisterDeviceForRemoteMessages',
);
});

it('getAPNSToken', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => getAPNSToken(messaging),
() => messaging.getAPNSToken(),
'getAPNSToken',
);
});

it('setAPNSToken', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => setAPNSToken(messaging, 'token'),
() => messaging.setAPNSToken('token'),
'setAPNSToken',
);
});

it('hasPermission', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => hasPermission(messaging),
() => messaging.hasPermission(),
'hasPermission',
);
});

it('subscribeToTopic', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => subscribeToTopic(messaging, 'topic'),
() => messaging.subscribeToTopic('topic'),
'subscribeToTopic',
);
});

it('unsubscribeFromTopic', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => unsubscribeFromTopic(messaging, 'topic'),
() => messaging.unsubscribeFromTopic('topic'),
'unsubscribeFromTopic',
);
});

it('getToken', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => getToken(messaging),
() => messaging.getToken(),
'getToken',
);
});

it('deleteToken', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => deleteToken(messaging),
() => messaging.deleteToken(),
'deleteToken',
);
});

it('isSupported', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => isSupported(messaging),
() => messaging.isSupported(),
'isSupported',
);
});

it('experimentalSetDeliveryMetricsExportedToBigQueryEnabled', function () {
const messaging = getMessaging();
messagingV9Deprecation(
() => experimentalSetDeliveryMetricsExportedToBigQueryEnabled(messaging, true),
() => messaging.setDeliveryMetricsExportToBigQuery(true),
'setDeliveryMetricsExportToBigQuery',
);
});

describe('statics', function () {
it('AuthorizationStatus', function () {
staticsV9Deprecation(
() => AuthorizationStatus,
() => messaging.AuthorizationStatus,
'AuthorizationStatus',
);
});

it('NotificationAndroidPriority', function () {
staticsV9Deprecation(
() => NotificationAndroidPriority,
() => messaging.NotificationAndroidPriority,
'NotificationAndroidPriority',
);
});

it('NotificationAndroidVisibility', function () {
staticsV9Deprecation(
() => NotificationAndroidVisibility,
() => messaging.NotificationAndroidVisibility,
'NotificationAndroidVisibility',
);
});
});
});
});
});
Loading
Loading