|
| 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