Skip to content

Commit 79b813a

Browse files
Add tests
1 parent 1f178d5 commit 79b813a

File tree

8 files changed

+213
-17
lines changed

8 files changed

+213
-17
lines changed

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/demo/
22
/.eslintrc.cjs
3+
/babel.config.cjs
4+
/jest.config.js
5+
/test.js

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ In debug mode SDK will log debugging information into console:
106106
UserReport.setDebug(true);
107107
```
108108

109-
#### IDFA and iOS 14
109+
### IDFA and iOS 14
110110

111111
Starting iOS 14.5 you’ll need to [receive user’s permission](https://developer.apple.com/app-store/user-privacy-and-data-use/) to access device advertising identifier. That behavior can be disabled for older iOS versions:
112112

babel.config.cjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
presets: [
3+
['@babel/preset-env', {
4+
targets: {
5+
node: 'current',
6+
},
7+
}],
8+
],
9+
};

demo/__tests__/App-test.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
transform: {
3+
'react-native-userreport-sdk.js': 'babel-jest',
4+
},
5+
};

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@
1010
"author": "AudienceProject",
1111
"license": "Apache-2.0",
1212
"homepage": "https://github.com/audienceproject/userreport-react-native-sdk",
13+
"scripts": {
14+
"test": "jest"
15+
},
1316
"peerDependencies": {
1417
"react-native": "^0.64.0",
1518
"react-native-device-info": "^8.0.6"
1619
},
1720
"devDependencies": {
21+
"@babel/core": "^7.15.0",
22+
"@babel/preset-env": "^7.15.0",
23+
"babel-jest": "^27.1.0",
1824
"eslint": "^7.14.0",
1925
"eslint-config-airbnb-base": "^14.2.1",
20-
"eslint-plugin-import": "^2.22.1"
26+
"eslint-plugin-import": "^2.22.1",
27+
"jest": "^27.1.0"
2128
},
2229
"type": "module"
2330
}

react-native-userreport-sdk.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ const debugError = (value) => {
1313
};
1414

1515
export const setDebug = (value) => {
16-
useDebug = value;
16+
if (value) { // log if debug enabled
17+
useDebug = value;
18+
}
1719
debugInfo(`Debug ${value ? 'enabled' : 'disabled'}`);
20+
if (!value) { // log if debug disabled
21+
useDebug = value;
22+
}
1823
};
1924

2025
/* dnt */

test.js

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/* eslint-env jest */
2+
3+
/* mocks */
4+
5+
jest.mock('react-native', () => ({
6+
__esModule: true,
7+
8+
default: {
9+
Platform: {
10+
OS: 'ios',
11+
},
12+
13+
Dimensions: {
14+
get: (type) => {
15+
if (type === 'screen') {
16+
return {
17+
width: 100,
18+
height: 200,
19+
scale: 1.5,
20+
};
21+
}
22+
23+
return '__NativeDimensions__';
24+
},
25+
},
26+
27+
NativeModules: {
28+
RNAdvertisingId: {
29+
getAdvertisingId: () => ({
30+
advertisingId: '__NativeAdvertisingId__',
31+
}),
32+
33+
getAdvertisingIdLegacy: () => ({
34+
advertisingId: '__NativeAdvertisingLegacyId__',
35+
}),
36+
},
37+
},
38+
},
39+
}), { virtual: true });
40+
41+
jest.mock('react-native-device-info', () => ({
42+
__esModule: true,
43+
44+
default: {
45+
getUniqueId: () => '__DeviceInfoUniqueId__',
46+
getBundleId: () => '__DeviceInfoBundleId__',
47+
48+
getSystemName: () => '__DeviceInfoSystemName__',
49+
getSystemVersion: () => '__DeviceInfoSystemVersion__',
50+
51+
getBrand: () => '__DeviceInfoBrand__',
52+
getDeviceId: () => '__DeviceInfoDeviceId__',
53+
},
54+
}), { virtual: true });
55+
56+
global.fetch = jest.fn((url) => Promise.resolve({
57+
json: () => Promise.resolve(
58+
url.startsWith('https://sak.') ? { // eslint-disable-line no-nested-ternary
59+
kitTcode: '__FetchMediaCode__',
60+
sections: {
61+
sectionId: '__FetchSectionCode__',
62+
},
63+
hardcodedConsent: '__FetchConsent__',
64+
} : {},
65+
),
66+
}));
67+
68+
beforeEach(() => {
69+
jest.spyOn(global.Math, 'random').mockReturnValue(0.123456789);
70+
});
71+
72+
afterEach(() => {
73+
jest.clearAllMocks();
74+
});
75+
76+
/* imports */
77+
78+
const ReactNative = require('react-native'); // eslint-disable-line import/no-unresolved
79+
const UserReport = require('.');
80+
81+
/* tests */
82+
83+
test('regular mode', async () => {
84+
await Promise.all([
85+
UserReport.configure('publisherId', 'mediaId'),
86+
UserReport.trackScreenView(),
87+
UserReport.trackSectionScreenView('sectionId'),
88+
]);
89+
90+
expect(fetch).toHaveBeenCalledTimes(3);
91+
92+
expect(fetch).toHaveBeenNthCalledWith(1,
93+
'https://sak.userreport.com/publisherId/media/mediaId/ios.json');
94+
95+
expect(fetch).toHaveBeenNthCalledWith(2,
96+
'https://visitanalytics.userreport.com/hit.gif'
97+
+ '?t=__FetchMediaCode__'
98+
+ '&r=4fzzzxjylrx'
99+
+ '&d=__NativeAdvertisingId__'
100+
+ '&idfv=__DeviceInfoUniqueId__'
101+
+ '&med=__DeviceInfoBundleId__'
102+
+ '&os=__DeviceInfoSystemName__'
103+
+ '&osv=__DeviceInfoSystemVersion__'
104+
+ '&dn=__DeviceInfoBrand__%20__DeviceInfoDeviceId__'
105+
+ '&dr=150x300'
106+
+ '&iab_consent=__FetchConsent__');
107+
108+
expect(fetch).toHaveBeenNthCalledWith(3,
109+
'https://visitanalytics.userreport.com/hit.gif'
110+
+ '?t=__FetchSectionCode__'
111+
+ '&r=4fzzzxjylrx'
112+
+ '&d=__NativeAdvertisingId__'
113+
+ '&idfv=__DeviceInfoUniqueId__'
114+
+ '&med=__DeviceInfoBundleId__'
115+
+ '&os=__DeviceInfoSystemName__'
116+
+ '&osv=__DeviceInfoSystemVersion__'
117+
+ '&dn=__DeviceInfoBrand__%20__DeviceInfoDeviceId__'
118+
+ '&dr=150x300'
119+
+ '&iab_consent=__FetchConsent__');
120+
});
121+
122+
test('legacy idfa', async () => {
123+
UserReport.setIdfaDialog(false);
124+
125+
await Promise.all([
126+
UserReport.configure('publisherId', 'mediaId'),
127+
UserReport.trackScreenView(),
128+
]);
129+
130+
expect(fetch).toHaveBeenNthCalledWith(2,
131+
'https://visitanalytics.userreport.com/hit.gif'
132+
+ '?t=__FetchMediaCode__'
133+
+ '&r=4fzzzxjylrx'
134+
+ '&d=__NativeAdvertisingLegacyId__'
135+
+ '&idfv=__DeviceInfoUniqueId__'
136+
+ '&med=__DeviceInfoBundleId__'
137+
+ '&os=__DeviceInfoSystemName__'
138+
+ '&osv=__DeviceInfoSystemVersion__'
139+
+ '&dn=__DeviceInfoBrand__%20__DeviceInfoDeviceId__'
140+
+ '&dr=150x300'
141+
+ '&iab_consent=__FetchConsent__');
142+
});
143+
144+
test('dnt mode', async () => {
145+
ReactNative.default.Platform.OS = 'android';
146+
147+
UserReport.setAnonymousTracking(true);
148+
149+
await Promise.all([
150+
UserReport.configure('publisherId', 'mediaId'),
151+
UserReport.trackScreenView(),
152+
UserReport.trackSectionScreenView('sectionId'),
153+
]);
154+
155+
expect(fetch).toHaveBeenCalledTimes(3);
156+
157+
expect(fetch).toHaveBeenNthCalledWith(1,
158+
'https://sak.dnt-userreport.com/publisherId/media/mediaId/android.json');
159+
160+
expect(fetch).toHaveBeenNthCalledWith(2,
161+
'https://visitanalytics.dnt-userreport.com/hit.gif'
162+
+ '?t=__FetchMediaCode__'
163+
+ '&r=4fzzzxjylrx'
164+
+ '&med=__DeviceInfoBundleId__'
165+
+ '&os=__DeviceInfoSystemName__'
166+
+ '&osv=__DeviceInfoSystemVersion__'
167+
+ '&dn=__DeviceInfoBrand__%20__DeviceInfoDeviceId__'
168+
+ '&dr=150x300'
169+
+ '&iab_consent=__FetchConsent__');
170+
171+
expect(fetch).toHaveBeenNthCalledWith(3,
172+
'https://visitanalytics.dnt-userreport.com/hit.gif'
173+
+ '?t=__FetchSectionCode__'
174+
+ '&r=4fzzzxjylrx'
175+
+ '&med=__DeviceInfoBundleId__'
176+
+ '&os=__DeviceInfoSystemName__'
177+
+ '&osv=__DeviceInfoSystemVersion__'
178+
+ '&dn=__DeviceInfoBrand__%20__DeviceInfoDeviceId__'
179+
+ '&dr=150x300'
180+
+ '&iab_consent=__FetchConsent__');
181+
});

0 commit comments

Comments
 (0)