From a05009f752d57ee6b5b66ed3d6f6b78f647a6e2a Mon Sep 17 00:00:00 2001 From: Jagadeesh Branch <102190347+JagadeeshKaricherla-branch@users.noreply.github.com> Date: Fri, 26 Jul 2024 11:08:12 -0700 Subject: [PATCH] test(SDK-2468] - update unit test imports test(SDK-2468] - update unit test imports --- src/RNBranch.js | 11 ++++++ src/index.js | 44 ++++++++++++------------ test/BranchEvent.test.js | 5 ++- test/BranchSubscriber.test.js | 5 ++- test/RNBranchModule.test.js | 65 +++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 28 deletions(-) create mode 100644 src/RNBranch.js create mode 100644 test/RNBranchModule.test.js diff --git a/src/RNBranch.js b/src/RNBranch.js new file mode 100644 index 000000000..d113d1e54 --- /dev/null +++ b/src/RNBranch.js @@ -0,0 +1,11 @@ +import { Platform, NativeModules } from 'react-native'; + +let RNBranch; + +if (Platform.OS === 'ios' || Platform.OS === 'android') { + RNBranch = NativeModules.RNBranch; +} else { + throw new Error('Unsupported platform'); +} + +export default RNBranch; diff --git a/src/index.js b/src/index.js index 835a59432..438c399a2 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,12 @@ -import { NativeModules, Platform } from "react-native"; +import { Platform } from 'react-native'; -const { RNBranch } = NativeModules; +import RNBranch from './RNBranch'; -import createBranchUniversalObject from "./branchUniversalObject"; -import BranchEvent from "./BranchEvent"; -import BranchSubscriber from "./BranchSubscriber"; +import createBranchUniversalObject from './branchUniversalObject'; +import BranchEvent from './BranchEvent'; +import BranchSubscriber from './BranchSubscriber'; -const packageFile = require("./../package.json"); +const packageFile = require('./../package.json'); export const VERSION = packageFile.version; class Branch { @@ -17,11 +17,11 @@ class Branch { constructor(options = {}) { if (options.debug) this._debug = true; - console.info("Initializing react-native-branch v. " + VERSION); + console.info('Initializing react-native-branch v. ' + VERSION); } subscribe(options) { - if (typeof options === "function") { + if (typeof options === 'function') { /* * Support for legacy API, passing a single callback function: * branch.subscribe(({params, error, uri}) => { ... }). This is @@ -36,7 +36,7 @@ class Branch { * You can specify checkCachedEvents in the subscribe options to control * this per subscriber. */ - if (!("checkCachedEvents" in options)) { + if (!('checkCachedEvents' in options)) { options.checkCachedEvents = this._checkCachedEvents; } this._checkCachedEvents = false; @@ -66,19 +66,19 @@ class Branch { setIdentityAsync = (identity) => RNBranch.setIdentityAsync(identity); setRequestMetadata = (key, value) => { console.info( - "[Branch] setRequestMetadata has limitations when called from JS. Some network calls are made prior to the JS layer being available, those calls will not have the metadata." + '[Branch] setRequestMetadata has limitations when called from JS. Some network calls are made prior to the JS layer being available, those calls will not have the metadata.' ); return RNBranch.setRequestMetadataKey(key, value); }; addFacebookPartnerParameter = (name, value) => { console.info( - "[Branch] addFacebookPartnerParameter has limitations when called from JS. Some network calls are made prior to the JS layer being available, those calls will not have the partner parameters." + '[Branch] addFacebookPartnerParameter has limitations when called from JS. Some network calls are made prior to the JS layer being available, those calls will not have the partner parameters.' ); return RNBranch.addFacebookPartnerParameter(name, value); }; addSnapPartnerParameter = (name, value) => { console.info( - "[Branch] addSnapPartnerParameter has limitations when called from JS. Some network calls are made prior to the JS layer being available, those calls will not have the partner parameters." + '[Branch] addSnapPartnerParameter has limitations when called from JS. Some network calls are made prior to the JS layer being available, those calls will not have the partner parameters.' ); return RNBranch.addSnapPartnerParameter(name, value); }; @@ -92,27 +92,27 @@ class Branch { })(); }; handleATTAuthorizationStatus = (ATTAuthorizationStatus) => { - if (Platform.OS != "ios") return; + if (Platform.OS != 'ios') return; let normalizedAttAuthorizationStatus = -1; switch (ATTAuthorizationStatus) { - case "authorized": + case 'authorized': normalizedAttAuthorizationStatus = 3; break; - case "denied": + case 'denied': normalizedAttAuthorizationStatus = 2; break; - case "undetermined": + case 'undetermined': normalizedAttAuthorizationStatus = 0; break; - case "restricted": + case 'restricted': normalizedAttAuthorizationStatus = 1; break; } if (normalizedAttAuthorizationStatus < 0) { console.info( - "[Branch] handleATTAuthorizationStatus received an unrecognized value. Value must be one of; authorized, denied, undetermined, or restricted" + '[Branch] handleATTAuthorizationStatus received an unrecognized value. Value must be one of; authorized, denied, undetermined, or restricted' ); return; } @@ -150,9 +150,9 @@ class Branch { adUserDataUsageConsent ) => { const isValid = - validateParam(eeaRegion, "eeaRegion") && - validateParam(adPersonalizationConsent, "adPersonalizationConsent") && - validateParam(adUserDataUsageConsent, "adUserDataUsageConsent"); + validateParam(eeaRegion, 'eeaRegion') && + validateParam(adPersonalizationConsent, 'adPersonalizationConsent') && + validateParam(adUserDataUsageConsent, 'adUserDataUsageConsent'); if (isValid) { RNBranch.setDMAParamsForEEA( @@ -161,7 +161,7 @@ class Branch { adUserDataUsageConsent ); } else { - console.warn("setDMAParamsForEEA: Unable to set DMA params."); + console.warn('setDMAParamsForEEA: Unable to set DMA params.'); } }; } diff --git a/test/BranchEvent.test.js b/test/BranchEvent.test.js index 05c037473..d4dd6d98c 100644 --- a/test/BranchEvent.test.js +++ b/test/BranchEvent.test.js @@ -1,6 +1,5 @@ -import { NativeModules } from 'react-native' -const { RNBranch } = NativeModules -import { BranchEvent } from 'react-native-branch' +import RNBranch from '../src/RNBranch' +import BranchEvent from '../src/BranchEvent' // --- Constant mapping --- diff --git a/test/BranchSubscriber.test.js b/test/BranchSubscriber.test.js index 4be0e0bf9..f8916446a 100644 --- a/test/BranchSubscriber.test.js +++ b/test/BranchSubscriber.test.js @@ -1,6 +1,5 @@ -import { NativeModules } from 'react-native' -const { RNBranch } = NativeModules -import { BranchSubscriber } from 'react-native-branch' +import RNBranch from '../src/RNBranch' +import BranchSubscriber from '../src/BranchSubscriber' test('default initializes with no options', () => { const subscriber = new BranchSubscriber(null) diff --git a/test/RNBranchModule.test.js b/test/RNBranchModule.test.js new file mode 100644 index 000000000..1968ad67e --- /dev/null +++ b/test/RNBranchModule.test.js @@ -0,0 +1,65 @@ +import { Platform, NativeModules } from 'react-native'; + +describe('RNBranch module', () => { + let originalPlatform; + let originalRNBranch; + + beforeEach(() => { + jest.resetModules(); + originalPlatform = Platform.OS; + originalRNBranch = NativeModules.RNBranch; + }); + + afterEach(() => { + Platform.OS = originalPlatform; + NativeModules.RNBranch = originalRNBranch; + jest.restoreAllMocks(); + }); + + describe('Platform-specific tests', () => { + it('should use NativeModules.RNBranch for iOS', () => { + Platform.OS = 'ios'; + NativeModules.RNBranch = { mockInit: jest.fn() }; + + const RNBranch = require('../src/RNBranch').default; + expect(RNBranch).not.toBeNull(); + expect(RNBranch).toBe(NativeModules.RNBranch); + }); + + it('should use NativeModules.RNBranch for Android', () => { + Platform.OS = 'android'; + NativeModules.RNBranch = { mockInit: jest.fn() }; + + const RNBranch = require('../src/RNBranch').default; + expect(RNBranch).not.toBeNull(); + expect(RNBranch).toBe(NativeModules.RNBranch); + }); + + it('should throw an error for unsupported platforms', () => { + Platform.OS = 'unsupportedPlatform'; + + expect(() => { + require('../src/RNBranch'); + }).toThrow('Unsupported platform'); + }); + }); + + describe('Null tests', () => { + it('should be null when NativeModules.RNBranch is null for iOS', () => { + Platform.OS = 'ios'; + NativeModules.RNBranch = null; + + const RNBranch = require('../src/RNBranch').default; + expect(RNBranch).toBeNull(); + }); + + it('should be null when NativeModules.RNBranch is null for Android', () => { + Platform.OS = 'android'; + NativeModules.RNBranch = null; + + const RNBranch = require('../src/RNBranch').default; + expect(RNBranch).toBeNull(); + }); + }); +}); +