@@ -3,7 +3,8 @@ import CookieSyncManager, {
33 IPixelConfiguration ,
44 CookieSyncDates ,
55 isLastSyncDateExpired ,
6- isTcfApiAvailable
6+ isTcfApiAvailable ,
7+ appendGdprConsentUrl
78} from '../../src/cookieSyncManager' ;
89import { MParticleWebSDK } from '../../src/sdkRuntimeModels' ;
910import { testMPID } from '../src/config/constants' ;
@@ -563,9 +564,10 @@ describe('CookieSyncManager', () => {
563564 } ) ;
564565 } ) ;
565566
566- describe ( '#performCookieSyncWithGDPR ' , ( ) => {
567+ describe ( '#appendGdprConsentUrl ' , ( ) => {
567568 const verboseLoggerSpy = jest . fn ( ) ;
568569 const errorLoggerSpy = jest . fn ( ) ;
570+ const mockUrl = 'https://example.com/cookie-sync' ;
569571
570572 let cookieSyncManager : any ;
571573 const mockMpInstance = ( {
@@ -590,12 +592,7 @@ describe('CookieSyncManager', () => {
590592 jest . clearAllMocks ( ) ;
591593 } )
592594
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-
595+ it ( 'should append GDPR parameters to the URL if __tcfapi callback succeeds' , async ( ) => {
599596 // Mock __tcfapi to call the callback with success
600597 ( window . __tcfapi as jest . Mock ) . mockImplementation ( (
601598 command ,
@@ -611,82 +608,77 @@ describe('CookieSyncManager', () => {
611608 ) ;
612609 } ) ;
613610
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- ) ;
611+ const fullUrl = await appendGdprConsentUrl ( mockUrl , mockMpInstance . Logger ) ;
623612
624- expect ( performCookieSyncSpy ) . toHaveBeenCalledWith (
613+ expect ( fullUrl ) . toBe (
625614 `${ mockUrl } &gdpr=1&gdpr_consent=test-consent-string` ,
626- moduleId ,
627- mpid ,
628- mockCookieSyncDates
629615 ) ;
630616 } ) ;
631617
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-
618+ it ( 'should return only the base url if the __tcfapi callback fails to get tcData' , async ( ) => {
638619 // Mock __tcfapi to call the callback with failure
639620 ( window . __tcfapi as jest . Mock ) . mockImplementation ( ( command , version , callback ) => {
640621 callback ( null , false ) ; // Simulate a failure
641622 } ) ;
642623
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-
624+ const fullUrl = await appendGdprConsentUrl ( mockUrl , mockMpInstance . Logger ) ;
654625 // Assert that the fallback method was called with the original URL
655- expect ( performCookieSyncSpy ) . toHaveBeenCalledWith (
656- mockUrl ,
657- moduleId ,
658- mpid ,
659- mockCookieSyncDates
660- ) ;
626+ expect ( fullUrl ) . toBe ( mockUrl ) ;
661627 } ) ;
662628
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-
629+ it ( 'should handle exceptions thrown by __tcfapi gracefully' , async ( ) => {
669630 // Mock __tcfapi to throw an error
670631 ( window . __tcfapi as jest . Mock ) . mockImplementation ( ( ) => {
671632 throw new Error ( 'Test Error' ) ;
672633 } ) ;
673634
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 ( ) ;
635+ try {
636+ await appendGdprConsentUrl ( mockUrl , mockMpInstance . Logger ) ;
637+ } catch ( e ) {
638+ expect ( errorLoggerSpy ) . toHaveBeenCalledWith ( 'Test Error' ) ;
639+ }
640+ } ) ;
687641
688- // Ensure the error was logged (if applicable)
689- expect ( errorLoggerSpy ) . toHaveBeenCalledWith ( 'Test Error' ) ;
642+ describe . only ( '#integration test' , ( ) => {
643+ it ( 'should handle errors properly when calling attemptCookieSync and the callback fails' , ( ) => {
644+ ( window . __tcfapi as jest . Mock ) . mockImplementation ( ( ) => {
645+ throw new Error ( 'Test Error' ) ;
646+ } ) ;
647+ const mockMPInstance = ( {
648+ _Store : {
649+ webviewBridgeEnabled : false ,
650+ pixelConfigurations : [ pixelSettings ] ,
651+ } ,
652+ _Persistence : {
653+ getPersistence : ( ) => ( { testMPID : { } } ) ,
654+ } ,
655+ _Consent : {
656+ isEnabledForUserConsent : jest . fn ( ) . mockReturnValue ( true ) ,
657+ } ,
658+ Identity : {
659+ getCurrentUser : jest . fn ( ) . mockReturnValue ( {
660+ getMPID : ( ) => testMPID ,
661+ } ) ,
662+ } ,
663+ Logger : {
664+ verbose : jest . fn ( ) ,
665+ error : jest . fn ( )
666+ } ,
667+ } as unknown ) as MParticleWebSDK ;
668+
669+ const cookieSyncManager = new CookieSyncManager ( mockMPInstance ) ;
670+ cookieSyncManager . performCookieSync = jest . fn ( ) ;
671+
672+ cookieSyncManager . attemptCookieSync ( testMPID , true ) ;
673+
674+ expect ( cookieSyncManager . performCookieSync ) . toHaveBeenCalledWith (
675+ pixelUrlAndRedirectUrl ,
676+ '5' ,
677+ testMPID ,
678+ { } ,
679+ ) ;
680+ } ) ;
690681 } ) ;
682+
691683 } ) ;
692684} ) ;
0 commit comments