@@ -2,7 +2,8 @@ import CookieSyncManager, {
22 DAYS_IN_MILLISECONDS ,
33 IPixelConfiguration ,
44 CookieSyncDates ,
5- isLastSyncDateExpired
5+ isLastSyncDateExpired ,
6+ isTcfApiAvailable
67} from '../../src/cookieSyncManager' ;
78import { MParticleWebSDK } from '../../src/sdkRuntimeModels' ;
89import { testMPID } from '../src/config/constants' ;
@@ -390,7 +391,8 @@ describe('CookieSyncManager', () => {
390391 filteringConsentRuleValues : {
391392 values : [ 'test' ] ,
392393 } ,
393- } as unknown as IPixelConfiguration ; const loggerSpy = jest . fn ( ) ;
394+ } as unknown as IPixelConfiguration ;
395+ const loggerSpy = jest . fn ( ) ;
394396
395397 const mockMPInstance = ( {
396398 _Store : {
@@ -553,4 +555,138 @@ describe('CookieSyncManager', () => {
553555 expect ( isLastSyncDateExpired ( frequencyCap , lastSyncDate ) ) . toBe ( false ) ;
554556 } ) ;
555557 } ) ;
556- } ) ;
558+
559+ describe ( '#isTcfApiAvailable' , ( ) => {
560+ it ( 'should return true if window.__tcfapi exists on the page' , ( ) => {
561+ window . __tcfapi = jest . fn ( ) ;
562+ expect ( isTcfApiAvailable ( ) ) . toBe ( true )
563+ } ) ;
564+ } ) ;
565+
566+ describe ( '#performCookieSyncWithGDPR' , ( ) => {
567+ const verboseLoggerSpy = jest . fn ( ) ;
568+ const errorLoggerSpy = jest . fn ( ) ;
569+
570+ let cookieSyncManager : any ;
571+ const mockMpInstance = ( {
572+ Logger : {
573+ verbose : verboseLoggerSpy ,
574+ error : errorLoggerSpy
575+ } ,
576+ _Persistence : {
577+ saveUserCookieSyncDatesToPersistence : jest . fn ( ) ,
578+ } ,
579+ Identity : {
580+ getCurrentUser : jest . fn ( ) ,
581+ } ,
582+ } as unknown ) as MParticleWebSDK ;
583+
584+ beforeEach ( ( ) => {
585+ cookieSyncManager = new CookieSyncManager ( mockMpInstance ) ;
586+ global . window . __tcfapi = jest . fn ( ) ;
587+ } )
588+
589+ afterEach ( ( ) => {
590+ jest . clearAllMocks ( ) ;
591+ } )
592+
593+ it ( 'should append GDPR parameters to the URL if __tcfapi callback succeeds' , ( ) => {
594+ const mockCookieSyncDates = { } ;
595+ const mockUrl = 'https://example.com/cookie-sync' ;
596+ const moduleId = 'module1' ;
597+ const mpid = '12345' ;
598+
599+ // Mock __tcfapi to call the callback with success
600+ ( window . __tcfapi as jest . Mock ) . mockImplementation ( (
601+ command ,
602+ version ,
603+ callback
604+ ) => {
605+ expect ( command ) . toBe ( 'getInAppTCData' ) ;
606+ expect ( version ) . toBe ( 2 ) ;
607+ // Simulate a successful response
608+ callback (
609+ { gdprApplies : true , tcString : 'test-consent-string' } ,
610+ true
611+ ) ;
612+ } ) ;
613+
614+ const performCookieSyncSpy = jest . spyOn ( cookieSyncManager , 'performCookieSync' ) ;
615+
616+ // Call the function under test
617+ cookieSyncManager . performCookieSyncWithGDPR (
618+ mockUrl ,
619+ moduleId ,
620+ mpid ,
621+ mockCookieSyncDates
622+ ) ;
623+
624+ expect ( performCookieSyncSpy ) . toHaveBeenCalledWith (
625+ `${ mockUrl } &gdpr=1&gdpr_consent=test-consent-string` ,
626+ moduleId ,
627+ mpid ,
628+ mockCookieSyncDates
629+ ) ;
630+ } ) ;
631+
632+ it ( 'should fall back to performCookieSync if __tcfapi callback fails' , ( ) => {
633+ const mockCookieSyncDates = { } ;
634+ const mockUrl = 'https://example.com/cookie-sync' ;
635+ const moduleId = 'module1' ;
636+ const mpid = '12345' ;
637+
638+ // Mock __tcfapi to call the callback with failure
639+ ( window . __tcfapi as jest . Mock ) . mockImplementation ( ( command , version , callback ) => {
640+ callback ( null , false ) ; // Simulate a failure
641+ } ) ;
642+
643+ // Spy on the `performCookieSync` method
644+ const performCookieSyncSpy = jest . spyOn ( cookieSyncManager , 'performCookieSync' ) ;
645+
646+ // Call the function under test
647+ cookieSyncManager . performCookieSyncWithGDPR (
648+ mockUrl ,
649+ moduleId ,
650+ mpid ,
651+ mockCookieSyncDates
652+ ) ;
653+
654+ // Assert that the fallback method was called with the original URL
655+ expect ( performCookieSyncSpy ) . toHaveBeenCalledWith (
656+ mockUrl ,
657+ moduleId ,
658+ mpid ,
659+ mockCookieSyncDates
660+ ) ;
661+ } ) ;
662+
663+ it ( 'should handle exceptions thrown by __tcfapi gracefully' , ( ) => {
664+ const mockCookieSyncDates = { } ;
665+ const mockUrl = 'https://example.com/cookie-sync' ;
666+ const moduleId = 'module1' ;
667+ const mpid = '12345' ;
668+
669+ // Mock __tcfapi to throw an error
670+ ( window . __tcfapi as jest . Mock ) . mockImplementation ( ( ) => {
671+ throw new Error ( 'Test Error' ) ;
672+ } ) ;
673+
674+ // Spy on the `performCookieSync` method
675+ const performCookieSyncSpy = jest . spyOn ( cookieSyncManager , 'performCookieSync' ) ;
676+
677+ // Call the function under test
678+ cookieSyncManager . performCookieSyncWithGDPR (
679+ mockUrl ,
680+ moduleId ,
681+ mpid ,
682+ mockCookieSyncDates
683+ ) ;
684+
685+ // Assert that the fallback method was called with the original URL
686+ expect ( performCookieSyncSpy ) . not . toHaveBeenCalled ( ) ;
687+
688+ // Ensure the error was logged (if applicable)
689+ expect ( errorLoggerSpy ) . toHaveBeenCalledWith ( 'Test Error' ) ;
690+ } ) ;
691+ } ) ;
692+ } ) ;
0 commit comments