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
72 changes: 72 additions & 0 deletions __tests__/environment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { NativeModules, Platform } from 'react-native';
import { shouldUseBrowserMode } from '../src/utils/environment';

describe('shouldUseBrowserMode logging', () => {
const originalOS = Platform.OS;
const originalRNPurchases = NativeModules.RNPurchases;
const originalRorkSandbox = NativeModules.RorkSandbox;
const originalExpo = globalThis.expo;

let logSpy: jest.SpyInstance;

beforeEach(() => {
logSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
});

afterEach(() => {
logSpy.mockRestore();
Platform.OS = originalOS;
NativeModules.RNPurchases = originalRNPurchases;
NativeModules.RorkSandbox = originalRorkSandbox;
globalThis.expo = originalExpo;
});

it('prefixes the web platform detection message with [RevenueCat]', () => {
Platform.OS = 'web';
NativeModules.RNPurchases = originalRNPurchases;
NativeModules.RorkSandbox = undefined;

const result = shouldUseBrowserMode();

expect(result).toBe(true);
expect(logSpy).toHaveBeenCalledWith(
'[RevenueCat] Web platform detected. Using RevenueCat in Browser Mode.'
);
});

it('prefixes the Expo Go detection message with [RevenueCat]', () => {
NativeModules.RNPurchases = null;
NativeModules.RorkSandbox = undefined;
globalThis.expo = { modules: { ExpoGo: true } };

const result = shouldUseBrowserMode();

expect(result).toBe(true);
expect(logSpy).toHaveBeenCalledWith(
'[RevenueCat] Expo Go app detected. Using RevenueCat in Browser Mode.'
);
});

it('prefixes the Rork sandbox detection message with [RevenueCat]', () => {
NativeModules.RorkSandbox = {};

const result = shouldUseBrowserMode();

expect(result).toBe(true);
expect(logSpy).toHaveBeenCalledWith(
'[RevenueCat] Rork app detected. Using RevenueCat in Preview API Mode.'
);
});

it('does not log a browser-mode detection message on native platforms', () => {
Platform.OS = 'ios';
NativeModules.RNPurchases = originalRNPurchases;
NativeModules.RorkSandbox = undefined;
globalThis.expo = undefined;

const result = shouldUseBrowserMode();

expect(result).toBe(false);
expect(logSpy).not.toHaveBeenCalled();
});
});
12 changes: 8 additions & 4 deletions src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ import { NativeModules, Platform } from "react-native";
*/
export function shouldUseBrowserMode(): boolean {
if (isExpoGo()) {
console.log('Expo Go app detected. Using RevenueCat in Browser Mode.');
logBrowserModeDetection('Expo Go app detected. Using RevenueCat in Browser Mode.');
return true;
} else if (isRorkSandbox()) {
console.log('Rork app detected. Using RevenueCat in Preview API Mode.');
logBrowserModeDetection('Rork app detected. Using RevenueCat in Preview API Mode.');
return true;
} else if (isWebPlatform()) {
console.log('Web platform detected. Using RevenueCat in Browser Mode.');
logBrowserModeDetection('Web platform detected. Using RevenueCat in Browser Mode.');
return true;
} else {
return false;
}
}

function logBrowserModeDetection(message: string): void {
console.log(`[RevenueCat] ${message}`);
}

declare global {
var expo: {
modules?: {
Expand Down Expand Up @@ -53,4 +57,4 @@ export function isRorkSandbox(): boolean {
*/
function isWebPlatform(): boolean {
return Platform.OS === 'web';
}
}