Skip to content

Commit

Permalink
fix: allow undefined configuration properties (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartozzz authored May 10, 2022
1 parent c6aea8b commit 6a083fd
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 13 deletions.
12 changes: 12 additions & 0 deletions e2e/admob.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ describe('googleAds', function () {
maxAdContentRating: googleAds.MaxAdContentRating.G,
});
});

it('accepts undefined', async function () {
await googleAds.setRequestConfiguration({ maxAdContentRating: undefined });
});
});

describe('tagForChildDirectedTreatment', function () {
Expand All @@ -71,6 +75,10 @@ describe('googleAds', function () {
tagForChildDirectedTreatment: false,
});
});

it('accepts undefined', async function () {
await googleAds.setRequestConfiguration({ tagForChildDirectedTreatment: undefined });
});
});

describe('tagForUnderAgeOfConsent', function () {
Expand All @@ -93,6 +101,10 @@ describe('googleAds', function () {
tagForUnderAgeOfConsent: false,
});
});

it('accepts undefined', async function () {
await googleAds.setRequestConfiguration({ tagForUnderAgeOfConsent: undefined });
});
});
});
});
10 changes: 10 additions & 0 deletions e2e/consent.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ describe('googleAds AdsConsent', function () {
}
});

it('accepts undefined properties', function () {
AdsConsent.requestInfoUpdate({
debugGeography: undefined,
tagForUnderAgeOfConsent: undefined,
testDeviceIdentifiers: undefined,
});

return Promise.resolve();
});

it('throws if debugGeography is not a AdsConsentDebugGeography value', function () {
try {
AdsConsent.requestInfoUpdate({
Expand Down
26 changes: 26 additions & 0 deletions e2e/requestOptions.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ describe('googleAds requestOptions', function () {
}
});

it('accepts undefined properties', function () {
const v = validator({
requestNonPersonalizedAdsOnly: undefined,
networkExtras: undefined,
keywords: undefined,
testDevices: undefined,
contentUrl: undefined,
location: undefined,
locationAccuracy: undefined,
requestAgent: undefined,
serverSideVerificationOptions: undefined,
});

v.requestNonPersonalizedAdsOnly.should.eql(undefined);
v.networkExtras.should.eql(undefined);
v.keywords.should.eql(undefined);
v.testDevices.should.eql(undefined);
v.contentUrl.should.eql(undefined);
v.location.should.eql(undefined);
v.locationAccuracy.should.eql(undefined);
v.requestAgent.should.eql(undefined);
v.serverSideVerificationOptions.should.eql(undefined);

return Promise.resolve();
});

describe('requestNonPersonalizedAdsOnly', function () {
it('throws if requestNonPersonalizedAdsOnly is not a boolean', function () {
try {
Expand Down
8 changes: 8 additions & 0 deletions e2e/showOptions.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ describe('googleAds showOptions', function () {
}
});

it('accepts undefined properties', function () {
const v = validator({
immersiveModeEnabled: undefined,
});

v.should.eql(jet.contextify({}));
});

it('throws if immersiveModeEnabled is not a boolean', function () {
try {
validator({
Expand Down
8 changes: 4 additions & 4 deletions src/AdsConsent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { NativeModules } from 'react-native';
import { AdsConsentDebugGeography } from './AdsConsentDebugGeography';
import { AdsConsentPurposes } from './AdsConsentPurposes';
import { AdsConsentSpecialFeatures } from './AdsConsentSpecialFeatures';
import { hasOwnProperty, isArray, isBoolean, isObject, isString } from './common';
import { isPropertySet, isArray, isBoolean, isObject, isString } from './common';
import {
AdsConsentFormResult,
AdsConsentInfo,
Expand All @@ -38,7 +38,7 @@ export const AdsConsent: AdsConsentInterface = {
}

if (
hasOwnProperty(options, 'debugGeography') &&
isPropertySet(options, 'debugGeography') &&
options.debugGeography !== AdsConsentDebugGeography.DISABLED &&
options.debugGeography !== AdsConsentDebugGeography.EEA &&
options.debugGeography !== AdsConsentDebugGeography.NOT_EEA
Expand All @@ -49,15 +49,15 @@ export const AdsConsent: AdsConsentInterface = {
}

if (
hasOwnProperty(options, 'tagForUnderAgeOfConsent') &&
isPropertySet(options, 'tagForUnderAgeOfConsent') &&
!isBoolean(options.tagForUnderAgeOfConsent)
) {
throw new Error(
"AdsConsent.requestInfoUpdate(*) 'options.tagForUnderAgeOfConsent' expected a boolean value.",
);
}

if (hasOwnProperty(options, 'testDeviceIdentifiers')) {
if (isPropertySet(options, 'testDeviceIdentifiers')) {
if (!isArray(options.testDeviceIdentifiers)) {
throw new Error(
"AdsConsent.requestInfoUpdate(*) 'options.testDeviceIdentifiers' expected an array of string values.",
Expand Down
9 changes: 8 additions & 1 deletion src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { Platform } from 'react-native';
import * as Base64 from './Base64';
import { isString } from './validate';
import { isString, isUndefined } from './validate';

export * from './id';
export * from './path';
Expand Down Expand Up @@ -69,6 +69,13 @@ export function hasOwnProperty(target: unknown, property: PropertyKey) {
return Object.hasOwnProperty.call(target, property);
}

export function isPropertySet(target: unknown, property: PropertyKey) {
return (
hasOwnProperty(target, property) &&
!isUndefined((target as Record<PropertyKey, unknown>)[property])
);
}

/**
* Remove a trailing forward slash from a string if it exists
*
Expand Down
8 changes: 4 additions & 4 deletions src/validateAdRequestConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
*/

import { hasOwnProperty, isArray, isBoolean, isObject } from './common';
import { isPropertySet, isArray, isBoolean, isObject } from './common';
import { MaxAdContentRating } from './MaxAdContentRating';
import { RequestConfiguration } from './types/RequestConfiguration';

Expand All @@ -41,7 +41,7 @@ export function validateAdRequestConfiguration(requestConfiguration: RequestConf
out.maxAdContentRating = requestConfiguration.maxAdContentRating;
}

if (hasOwnProperty(requestConfiguration, 'tagForChildDirectedTreatment')) {
if (isPropertySet(requestConfiguration, 'tagForChildDirectedTreatment')) {
if (!isBoolean(requestConfiguration.tagForChildDirectedTreatment)) {
throw new Error(
"'requestConfiguration.tagForChildDirectedTreatment' expected a boolean value",
Expand All @@ -51,15 +51,15 @@ export function validateAdRequestConfiguration(requestConfiguration: RequestConf
out.tagForChildDirectedTreatment = requestConfiguration.tagForChildDirectedTreatment;
}

if (hasOwnProperty(requestConfiguration, 'tagForUnderAgeOfConsent')) {
if (isPropertySet(requestConfiguration, 'tagForUnderAgeOfConsent')) {
if (!isBoolean(requestConfiguration.tagForUnderAgeOfConsent)) {
throw new Error("'requestConfiguration.tagForUnderAgeOfConsent' expected a boolean value");
}

out.tagForUnderAgeOfConsent = requestConfiguration.tagForUnderAgeOfConsent;
}

if (hasOwnProperty(requestConfiguration, 'testDeviceIdentifiers')) {
if (isPropertySet(requestConfiguration, 'testDeviceIdentifiers')) {
if (!isArray(requestConfiguration.testDeviceIdentifiers)) {
throw new Error("'requestConfiguration.testDeviceIdentifiers' expected an array value");
}
Expand Down
4 changes: 2 additions & 2 deletions src/validateAdRequestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import {
hasOwnProperty,
isPropertySet,
isArray,
isBoolean,
isObject,
Expand All @@ -37,7 +37,7 @@ export function validateAdRequestOptions(options?: RequestOptions) {
throw new Error("'options' expected an object value");
}

if (hasOwnProperty(options, 'requestNonPersonalizedAdsOnly')) {
if (isPropertySet(options, 'requestNonPersonalizedAdsOnly')) {
if (!isBoolean(options.requestNonPersonalizedAdsOnly)) {
throw new Error("'options.requestNonPersonalizedAdsOnly' expected a boolean value");
}
Expand Down
4 changes: 2 additions & 2 deletions src/validateAdShowOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
*/

import { hasOwnProperty, isBoolean, isObject, isUndefined } from './common';
import { isPropertySet, isBoolean, isObject, isUndefined } from './common';
import { AdShowOptions } from './types/AdShowOptions';

export function validateAdShowOptions(options?: AdShowOptions) {
Expand All @@ -29,7 +29,7 @@ export function validateAdShowOptions(options?: AdShowOptions) {
throw new Error("'options' expected an object value");
}

if (hasOwnProperty(options, 'immersiveModeEnabled')) {
if (isPropertySet(options, 'immersiveModeEnabled')) {
if (!isBoolean(options.immersiveModeEnabled)) {
throw new Error("'options.immersiveModeEnabled' expected a boolean value");
}
Expand Down

0 comments on commit 6a083fd

Please sign in to comment.