Skip to content

Commit

Permalink
fix: rewarded ads crash & request configuration (#31)
Browse files Browse the repository at this point in the history
* fix: rewarded ads crash & request configuration
* fix: update tests
  • Loading branch information
dylancom authored Dec 11, 2021
1 parent 7c4812b commit 382f146
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 21 deletions.
21 changes: 17 additions & 4 deletions __tests__/googleAds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ describe('Admob', function () {
describe('setRequestConfiguration()', function () {
it('throws if config is not an object', function () {
// @ts-ignore
expect(() => admob.setRequestConfiguration('123')).toThrowError(
expect(() => admob().setRequestConfiguration('123')).toThrowError(
"setRequestConfiguration(*) 'requestConfiguration' expected an object value",
);
});

describe('maxAdContentRating', function () {
it('throws if maxAdContentRating is invalid', function () {
expect(() =>
admob.setRequestConfiguration({
admob().setRequestConfiguration({
maxAdContentRating:
'Y' as GoogleAdsTypes.MaxAdContentRating[keyof GoogleAdsTypes.MaxAdContentRating],
}),
Expand All @@ -25,7 +25,7 @@ describe('Admob', function () {
describe('tagForChildDirectedTreatment', function () {
it('throws if tagForChildDirectedTreatment not a boolean', function () {
expect(() =>
admob.setRequestConfiguration({
admob().setRequestConfiguration({
// @ts-ignore
tagForChildDirectedTreatment: 'true',
}),
Expand All @@ -38,7 +38,7 @@ describe('Admob', function () {
describe('tagForUnderAgeOfConsent', function () {
it('throws if tagForUnderAgeOfConsent not a boolean', function () {
expect(() =>
admob.setRequestConfiguration({
admob().setRequestConfiguration({
// @ts-ignore
tagForUnderAgeOfConsent: 'false',
}),
Expand All @@ -47,5 +47,18 @@ describe('Admob', function () {
);
});
});

describe('testDeviceIdentifiers', function () {
it('throws if testDeviceIdentifiers not an array', function () {
expect(() =>
admob().setRequestConfiguration({
// @ts-ignore
testDeviceIdentifiers: 'EMULATOR',
}),
).toThrowError(
"setRequestConfiguration(*) 'requestConfiguration.testDeviceIdentifiers' expected an array value",
);
});
});
});
});
1 change: 0 additions & 1 deletion e2e/interstitial.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe('googleAds InterstitialAd', function () {
foo: 'bar',
},
keywords: ['foo', 'bar'],
testDevices: ['abc', 'EMULATOR'],
contentUrl: 'https://invertase.io',
location: [0, 0],
locationAccuracy: 10,
Expand Down
1 change: 0 additions & 1 deletion e2e/rewarded.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ describe('googleAds RewardedAd', function () {
foo: 'bar',
},
keywords: ['foo', 'bar'],
testDevices: ['abc', 'EMULATOR'],
contentUrl: 'https://invertase.io',
location: [0, 0],
locationAccuracy: 10,
Expand Down
4 changes: 2 additions & 2 deletions ios/RNGoogleAds/RNGoogleAdsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ - (void)setRequestConfiguration:(NSDictionary *)requestConfiguration {
}

if (requestConfiguration[@"tagForChildDirectedTreatment"]) {
BOOL tag = (BOOL)requestConfiguration[@"tagForChildDirectedTreatment"];
BOOL tag = [requestConfiguration[@"tagForChildDirectedTreatment"] boolValue];
[GADMobileAds.sharedInstance.requestConfiguration tagForChildDirectedTreatment:tag];
}

if (requestConfiguration[@"tagForUnderAgeOfConsent"]) {
BOOL tag = (BOOL)requestConfiguration[@"tagForUnderAgeOfConsent"];
BOOL tag = [requestConfiguration[@"tagForUnderAgeOfConsent"] boolValue];
[GADMobileAds.sharedInstance.requestConfiguration tagForUnderAgeOfConsent:tag];
}

Expand Down
22 changes: 14 additions & 8 deletions ios/RNGoogleAds/RNGoogleAdsRewardedModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ - (void)invalidate {
rewardedMap[requestId] = rewardedAd;
rewardedDelegateMap[requestId] = fullScreenContentDelegate;
GADAdReward *reward = rewardedAd.adReward;
NSDictionary *data = @{
@"type" : reward.type,
@"amount" : reward.amount,
};
NSMutableDictionary *data = [NSMutableDictionary dictionary];
if (reward.type != nil) {
[data setValue:reward.type forKey:@"type"];
}
if (reward.amount != nil) {
[data setValue:reward.amount forKey:@"amount"];
}
[RNGoogleAdsCommon sendAdEvent:GOOGLE_ADS_EVENT_REWARDED
requestId:requestId
type:GOOGLE_ADS_EVENT_REWARDED_LOADED
Expand All @@ -142,10 +145,13 @@ - (void)invalidate {
presentFromRootViewController:RCTSharedApplication().delegate.window.rootViewController
userDidEarnRewardHandler:^{
GADAdReward *reward = rewardedAd.adReward;
NSDictionary *data = @{
@"type" : reward.type,
@"amount" : reward.amount,
};
NSMutableDictionary *data = [NSMutableDictionary dictionary];
if (reward.type != nil) {
[data setValue:reward.type forKey:@"type"];
}
if (reward.amount != nil) {
[data setValue:reward.amount forKey:@"amount"];
}
[RNGoogleAdsCommon sendAdEvent:GOOGLE_ADS_EVENT_REWARDED
requestId:requestId
type:GOOGLE_ADS_EVENT_REWARDED_EARNED_REWARD
Expand Down
2 changes: 1 addition & 1 deletion lib/ads/InterstitialAd.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class InterstitialAd extends MobileAd {
}

const requestId = _interstitialRequest++;
return new InterstitialAd('interstitial', googleAds, requestId, adUnitId, options);
return new InterstitialAd('interstitial', googleAds(), requestId, adUnitId, options);
}

load() {
Expand Down
2 changes: 1 addition & 1 deletion lib/ads/RewardedAd.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class RewardedAd extends MobileAd {
}

const requestId = _rewardedRequest++;
return new RewardedAd('rewarded', googleAds, requestId, adUnitId, options);
return new RewardedAd('rewarded', googleAds(), requestId, adUnitId, options);
}

load() {
Expand Down
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,6 @@ export const InterstitialAd: typeof GoogleAdsTypes.InterstitialAd;
export const RewardedAd: typeof GoogleAdsTypes.RewardedAd;
export const BannerAd: React.SFC<GoogleAdsTypes.BannerAd>;

declare const defaultExport: GoogleAdsTypes.Module & GoogleAdsTypes.Statics;
declare const defaultExport: () => GoogleAdsTypes.Module & GoogleAdsTypes.Statics;

export default defaultExport;
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ class GoogleAdsModule extends Module {
// import { SDK_VERSION } from '@invertase/react-native-google-ads';
export const SDK_VERSION = version;

export default new GoogleAdsModule('AppName', {
const googleMobileAds = new GoogleAdsModule('AppName', {
statics,
version,
namespace,
nativeModuleName,
nativeEvents: ['google_ads_interstitial_event', 'google_ads_rewarded_event'],
});

export default () => {
return googleMobileAds;
};

export { default as AdsConsentDebugGeography } from './AdsConsentDebugGeography';
export { default as AdsConsentStatus } from './AdsConsentStatus';
export { default as MaxAdContentRating } from './MaxAdContentRating';
Expand Down
10 changes: 9 additions & 1 deletion lib/validateAdRequestConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
*/

import { hasOwnProperty, isBoolean, isObject } from '../lib/common';
import { hasOwnProperty, isArray, isBoolean, isObject } from '../lib/common';
import MaxAdContentRating from './MaxAdContentRating';

export default function validateAdRequestConfiguration(requestConfiguration) {
Expand Down Expand Up @@ -58,5 +58,13 @@ export default function validateAdRequestConfiguration(requestConfiguration) {
out.tagForUnderAgeOfConsent = requestConfiguration.tagForUnderAgeOfConsent;
}

if (hasOwnProperty(requestConfiguration, 'testDeviceIdentifiers')) {
if (!isArray(requestConfiguration.testDeviceIdentifiers)) {
throw new Error("'requestConfiguration.testDeviceIdentifiers' expected an array value");
}

out.testDeviceIdentifiers = requestConfiguration.testDeviceIdentifiers;
}

return out;
}

0 comments on commit 382f146

Please sign in to comment.