Skip to content

Commit

Permalink
test(SDK-2468] - update unit test imports
Browse files Browse the repository at this point in the history
test(SDK-2468] - update unit test imports
  • Loading branch information
JagadeeshKaricherla-branch authored Jul 26, 2024
1 parent 77693c7 commit a05009f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 28 deletions.
11 changes: 11 additions & 0 deletions src/RNBranch.js
Original file line number Diff line number Diff line change
@@ -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;
44 changes: 22 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
};
Expand All @@ -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;
}
Expand Down Expand Up @@ -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(
Expand All @@ -161,7 +161,7 @@ class Branch {
adUserDataUsageConsent
);
} else {
console.warn("setDMAParamsForEEA: Unable to set DMA params.");
console.warn('setDMAParamsForEEA: Unable to set DMA params.');
}
};
}
Expand Down
5 changes: 2 additions & 3 deletions test/BranchEvent.test.js
Original file line number Diff line number Diff line change
@@ -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 ---

Expand Down
5 changes: 2 additions & 3 deletions test/BranchSubscriber.test.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
65 changes: 65 additions & 0 deletions test/RNBranchModule.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit a05009f

Please sign in to comment.