diff --git a/packages/analytics-js-common/__tests__/utilities/object.test.ts b/packages/analytics-js-common/__tests__/utilities/object.test.ts index 7c5d65d2c..8902d11a7 100644 --- a/packages/analytics-js-common/__tests__/utilities/object.test.ts +++ b/packages/analytics-js-common/__tests__/utilities/object.test.ts @@ -8,6 +8,7 @@ import { removeUndefinedValues, removeUndefinedAndNullValues, getNormalizedObjectValue, + getNormalizedBooleanValue, } from '../../src/utilities/object'; const identifyTraitsPayloadMock = { @@ -360,4 +361,105 @@ describe('Common Utils - Object', () => { }); }); }); + + describe('getNormalizedBooleanValue', () => { + const tcData = [ + { + input: [true, undefined], + output: true, + }, + { + input: [false, undefined], + output: false, + }, + { + input: [undefined, true], + output: undefined, + }, + { + input: [undefined, false], + output: undefined, + }, + { + input: [true, false], + output: true, + }, + { + input: [false, true], + output: false, + }, + { + input: [undefined, undefined], + output: false, + }, + { + input: [{}, false], + output: false, + }, + { + input: [{}, true], + output: false, + }, + { + input: [{}, undefined], + output: false, + }, + { + input: [[], false], + output: false, + }, + { + input: [[], true], + output: false, + }, + { + input: [[], undefined], + output: false, + }, + { + input: ['string', false], + output: false, + }, + { + input: ['string', true], + output: false, + }, + { + input: ['string', undefined], + output: false, + }, + { + input: [123456, false], + output: false, + }, + { + input: [123456, true], + output: false, + }, + { + input: [123456, undefined], + output: false, + }, + { + input: [new Date(), false], + output: false, + }, + { + input: [new Date(), true], + output: false, + }, + { + input: [new Date(), undefined], + output: false, + }, + ]; + + it.each(tcData)( + 'should return $output for input $input', + ({ input, output }: { input: any; output: any }) => { + const outcome = getNormalizedBooleanValue(input[0], input[1]); + expect(outcome).toEqual(output); + }, + ); + }); }); diff --git a/packages/analytics-js-common/src/utilities/object.ts b/packages/analytics-js-common/src/utilities/object.ts index 0fceee115..6adfbce09 100644 --- a/packages/analytics-js-common/src/utilities/object.ts +++ b/packages/analytics-js-common/src/utilities/object.ts @@ -107,6 +107,14 @@ const getNormalizedObjectValue = (val: any): any => { return removeUndefinedAndNullValues(val); }; +const getNormalizedBooleanValue = (val: any, defVal: boolean | undefined): boolean | undefined => { + if (isDefined(defVal)) { + return isDefined(val) ? val === true : undefined; + } + + return val === true; +}; + export { getValueByPath, hasValueByPath, @@ -119,4 +127,5 @@ export { removeUndefinedAndNullValues, getNormalizedObjectValue, isObject, + getNormalizedBooleanValue, }; diff --git a/packages/analytics-js/src/components/utilities/loadOptions.ts b/packages/analytics-js/src/components/utilities/loadOptions.ts index f71aab507..34aa9a778 100644 --- a/packages/analytics-js/src/components/utilities/loadOptions.ts +++ b/packages/analytics-js/src/components/utilities/loadOptions.ts @@ -1,6 +1,7 @@ /* eslint-disable sonarjs/deprecation */ import { clone } from 'ramda'; import { + getNormalizedBooleanValue, getNormalizedObjectValue, isNonEmptyObject, mergeDeepRight, @@ -31,9 +32,15 @@ const normalizeLoadOptions = ( normalizedLoadOpts.sameSiteCookie = undefined; } - normalizedLoadOpts.secureCookie = normalizedLoadOpts.secureCookie === true; + normalizedLoadOpts.secureCookie = getNormalizedBooleanValue( + normalizedLoadOpts.secureCookie, + loadOptionsFromState.secureCookie, + ); - normalizedLoadOpts.sameDomainCookiesOnly = normalizedLoadOpts.sameDomainCookiesOnly === true; + normalizedLoadOpts.sameDomainCookiesOnly = getNormalizedBooleanValue( + normalizedLoadOpts.sameDomainCookiesOnly, + loadOptionsFromState.sameDomainCookiesOnly, + ); const uaChTrackLevels = ['none', 'default', 'full']; if (!uaChTrackLevels.includes(normalizedLoadOpts.uaChTrackLevel as UaChTrackLevel)) { @@ -44,15 +51,25 @@ const normalizeLoadOptions = ( normalizedLoadOpts.plugins = normalizedLoadOpts.plugins ?? defaultOptionalPluginsList; - normalizedLoadOpts.useGlobalIntegrationsConfigInEvents = - normalizedLoadOpts.useGlobalIntegrationsConfigInEvents === true; + normalizedLoadOpts.useGlobalIntegrationsConfigInEvents = getNormalizedBooleanValue( + normalizedLoadOpts.useGlobalIntegrationsConfigInEvents, + loadOptionsFromState.useGlobalIntegrationsConfigInEvents, + ); - normalizedLoadOpts.bufferDataPlaneEventsUntilReady = - normalizedLoadOpts.bufferDataPlaneEventsUntilReady === true; + normalizedLoadOpts.bufferDataPlaneEventsUntilReady = getNormalizedBooleanValue( + normalizedLoadOpts.bufferDataPlaneEventsUntilReady, + loadOptionsFromState.bufferDataPlaneEventsUntilReady, + ); - normalizedLoadOpts.sendAdblockPage = normalizedLoadOpts.sendAdblockPage === true; + normalizedLoadOpts.sendAdblockPage = getNormalizedBooleanValue( + normalizedLoadOpts.sendAdblockPage, + loadOptionsFromState.sendAdblockPage, + ); - normalizedLoadOpts.useServerSideCookies = normalizedLoadOpts.useServerSideCookies === true; + normalizedLoadOpts.useServerSideCookies = getNormalizedBooleanValue( + normalizedLoadOpts.useServerSideCookies, + loadOptionsFromState.useServerSideCookies, + ); if (!isString(normalizedLoadOpts.dataServiceEndpoint)) { normalizedLoadOpts.dataServiceEndpoint = undefined; @@ -62,7 +79,10 @@ const normalizeLoadOptions = ( normalizedLoadOpts.sendAdblockPageOptions, ); - normalizedLoadOpts.loadIntegration = normalizedLoadOpts.loadIntegration === true; + normalizedLoadOpts.loadIntegration = getNormalizedBooleanValue( + normalizedLoadOpts.loadIntegration, + loadOptionsFromState.loadIntegration, + ); if (!isNonEmptyObject(normalizedLoadOpts.storage)) { normalizedLoadOpts.storage = undefined; @@ -80,9 +100,15 @@ const normalizeLoadOptions = ( normalizedLoadOpts.queueOptions = getNormalizedObjectValue(normalizedLoadOpts.queueOptions); - normalizedLoadOpts.lockIntegrationsVersion = normalizedLoadOpts.lockIntegrationsVersion === true; + normalizedLoadOpts.lockIntegrationsVersion = getNormalizedBooleanValue( + normalizedLoadOpts.lockIntegrationsVersion, + loadOptionsFromState.lockIntegrationsVersion, + ); - normalizedLoadOpts.lockPluginsVersion = normalizedLoadOpts.lockPluginsVersion === true; + normalizedLoadOpts.lockPluginsVersion = getNormalizedBooleanValue( + normalizedLoadOpts.lockPluginsVersion, + loadOptionsFromState.lockPluginsVersion, + ); if (!isNumber(normalizedLoadOpts.dataPlaneEventsBufferTimeout)) { normalizedLoadOpts.dataPlaneEventsBufferTimeout = undefined;