Skip to content

Commit a05009f

Browse files
test(SDK-2468] - update unit test imports
test(SDK-2468] - update unit test imports
1 parent 77693c7 commit a05009f

File tree

5 files changed

+102
-28
lines changed

5 files changed

+102
-28
lines changed

src/RNBranch.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Platform, NativeModules } from 'react-native';
2+
3+
let RNBranch;
4+
5+
if (Platform.OS === 'ios' || Platform.OS === 'android') {
6+
RNBranch = NativeModules.RNBranch;
7+
} else {
8+
throw new Error('Unsupported platform');
9+
}
10+
11+
export default RNBranch;

src/index.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { NativeModules, Platform } from "react-native";
1+
import { Platform } from 'react-native';
22

3-
const { RNBranch } = NativeModules;
3+
import RNBranch from './RNBranch';
44

5-
import createBranchUniversalObject from "./branchUniversalObject";
6-
import BranchEvent from "./BranchEvent";
7-
import BranchSubscriber from "./BranchSubscriber";
5+
import createBranchUniversalObject from './branchUniversalObject';
6+
import BranchEvent from './BranchEvent';
7+
import BranchSubscriber from './BranchSubscriber';
88

9-
const packageFile = require("./../package.json");
9+
const packageFile = require('./../package.json');
1010
export const VERSION = packageFile.version;
1111

1212
class Branch {
@@ -17,11 +17,11 @@ class Branch {
1717
constructor(options = {}) {
1818
if (options.debug) this._debug = true;
1919

20-
console.info("Initializing react-native-branch v. " + VERSION);
20+
console.info('Initializing react-native-branch v. ' + VERSION);
2121
}
2222

2323
subscribe(options) {
24-
if (typeof options === "function") {
24+
if (typeof options === 'function') {
2525
/*
2626
* Support for legacy API, passing a single callback function:
2727
* branch.subscribe(({params, error, uri}) => { ... }). This is
@@ -36,7 +36,7 @@ class Branch {
3636
* You can specify checkCachedEvents in the subscribe options to control
3737
* this per subscriber.
3838
*/
39-
if (!("checkCachedEvents" in options)) {
39+
if (!('checkCachedEvents' in options)) {
4040
options.checkCachedEvents = this._checkCachedEvents;
4141
}
4242
this._checkCachedEvents = false;
@@ -66,19 +66,19 @@ class Branch {
6666
setIdentityAsync = (identity) => RNBranch.setIdentityAsync(identity);
6767
setRequestMetadata = (key, value) => {
6868
console.info(
69-
"[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."
69+
'[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.'
7070
);
7171
return RNBranch.setRequestMetadataKey(key, value);
7272
};
7373
addFacebookPartnerParameter = (name, value) => {
7474
console.info(
75-
"[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."
75+
'[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.'
7676
);
7777
return RNBranch.addFacebookPartnerParameter(name, value);
7878
};
7979
addSnapPartnerParameter = (name, value) => {
8080
console.info(
81-
"[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."
81+
'[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.'
8282
);
8383
return RNBranch.addSnapPartnerParameter(name, value);
8484
};
@@ -92,27 +92,27 @@ class Branch {
9292
})();
9393
};
9494
handleATTAuthorizationStatus = (ATTAuthorizationStatus) => {
95-
if (Platform.OS != "ios") return;
95+
if (Platform.OS != 'ios') return;
9696
let normalizedAttAuthorizationStatus = -1;
9797

9898
switch (ATTAuthorizationStatus) {
99-
case "authorized":
99+
case 'authorized':
100100
normalizedAttAuthorizationStatus = 3;
101101
break;
102-
case "denied":
102+
case 'denied':
103103
normalizedAttAuthorizationStatus = 2;
104104
break;
105-
case "undetermined":
105+
case 'undetermined':
106106
normalizedAttAuthorizationStatus = 0;
107107
break;
108-
case "restricted":
108+
case 'restricted':
109109
normalizedAttAuthorizationStatus = 1;
110110
break;
111111
}
112112

113113
if (normalizedAttAuthorizationStatus < 0) {
114114
console.info(
115-
"[Branch] handleATTAuthorizationStatus received an unrecognized value. Value must be one of; authorized, denied, undetermined, or restricted"
115+
'[Branch] handleATTAuthorizationStatus received an unrecognized value. Value must be one of; authorized, denied, undetermined, or restricted'
116116
);
117117
return;
118118
}
@@ -150,9 +150,9 @@ class Branch {
150150
adUserDataUsageConsent
151151
) => {
152152
const isValid =
153-
validateParam(eeaRegion, "eeaRegion") &&
154-
validateParam(adPersonalizationConsent, "adPersonalizationConsent") &&
155-
validateParam(adUserDataUsageConsent, "adUserDataUsageConsent");
153+
validateParam(eeaRegion, 'eeaRegion') &&
154+
validateParam(adPersonalizationConsent, 'adPersonalizationConsent') &&
155+
validateParam(adUserDataUsageConsent, 'adUserDataUsageConsent');
156156

157157
if (isValid) {
158158
RNBranch.setDMAParamsForEEA(
@@ -161,7 +161,7 @@ class Branch {
161161
adUserDataUsageConsent
162162
);
163163
} else {
164-
console.warn("setDMAParamsForEEA: Unable to set DMA params.");
164+
console.warn('setDMAParamsForEEA: Unable to set DMA params.');
165165
}
166166
};
167167
}

test/BranchEvent.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { NativeModules } from 'react-native'
2-
const { RNBranch } = NativeModules
3-
import { BranchEvent } from 'react-native-branch'
1+
import RNBranch from '../src/RNBranch'
2+
import BranchEvent from '../src/BranchEvent'
43

54
// --- Constant mapping ---
65

test/BranchSubscriber.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { NativeModules } from 'react-native'
2-
const { RNBranch } = NativeModules
3-
import { BranchSubscriber } from 'react-native-branch'
1+
import RNBranch from '../src/RNBranch'
2+
import BranchSubscriber from '../src/BranchSubscriber'
43

54
test('default initializes with no options', () => {
65
const subscriber = new BranchSubscriber(null)

test/RNBranchModule.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Platform, NativeModules } from 'react-native';
2+
3+
describe('RNBranch module', () => {
4+
let originalPlatform;
5+
let originalRNBranch;
6+
7+
beforeEach(() => {
8+
jest.resetModules();
9+
originalPlatform = Platform.OS;
10+
originalRNBranch = NativeModules.RNBranch;
11+
});
12+
13+
afterEach(() => {
14+
Platform.OS = originalPlatform;
15+
NativeModules.RNBranch = originalRNBranch;
16+
jest.restoreAllMocks();
17+
});
18+
19+
describe('Platform-specific tests', () => {
20+
it('should use NativeModules.RNBranch for iOS', () => {
21+
Platform.OS = 'ios';
22+
NativeModules.RNBranch = { mockInit: jest.fn() };
23+
24+
const RNBranch = require('../src/RNBranch').default;
25+
expect(RNBranch).not.toBeNull();
26+
expect(RNBranch).toBe(NativeModules.RNBranch);
27+
});
28+
29+
it('should use NativeModules.RNBranch for Android', () => {
30+
Platform.OS = 'android';
31+
NativeModules.RNBranch = { mockInit: jest.fn() };
32+
33+
const RNBranch = require('../src/RNBranch').default;
34+
expect(RNBranch).not.toBeNull();
35+
expect(RNBranch).toBe(NativeModules.RNBranch);
36+
});
37+
38+
it('should throw an error for unsupported platforms', () => {
39+
Platform.OS = 'unsupportedPlatform';
40+
41+
expect(() => {
42+
require('../src/RNBranch');
43+
}).toThrow('Unsupported platform');
44+
});
45+
});
46+
47+
describe('Null tests', () => {
48+
it('should be null when NativeModules.RNBranch is null for iOS', () => {
49+
Platform.OS = 'ios';
50+
NativeModules.RNBranch = null;
51+
52+
const RNBranch = require('../src/RNBranch').default;
53+
expect(RNBranch).toBeNull();
54+
});
55+
56+
it('should be null when NativeModules.RNBranch is null for Android', () => {
57+
Platform.OS = 'android';
58+
NativeModules.RNBranch = null;
59+
60+
const RNBranch = require('../src/RNBranch').default;
61+
expect(RNBranch).toBeNull();
62+
});
63+
});
64+
});
65+

0 commit comments

Comments
 (0)