@@ -62,7 +62,6 @@ jest.mock('../../lib/Mixpanel', () => ({
6262
6363jest . mock ( '../../lib/Braze' , ( ) => ( {
6464 BrazeWrapper : {
65- merge : jest . fn ( ( ) => Promise . resolve ( ) ) ,
6665 identify : jest . fn ( ( ) => Promise . resolve ( ) ) ,
6766 setEmail : jest . fn ( ) ,
6867 setEmailNotificationSubscriptionType : jest . fn ( ) ,
@@ -150,17 +149,19 @@ jest.mock('../../api/bitpay', () => ({
150149
151150import AuthApi from '../../api/auth' ;
152151import UserApi from '../../api/user' ;
152+ import BitPayIdApi from '../../api/bitpay' ;
153153import { getPasskeyStatus , signInWithPasskey } from '../../utils/passkey' ;
154154import * as helperMethods from '../../utils/helper-methods' ;
155155import { isAnonymousBrazeEid } from '../app/app.effects' ;
156- import { BrazeWrapper } from '../../lib/Braze ' ;
156+ import { Analytics } from '../analytics/analytics.effects ' ;
157157
158158const MockAuthApi = AuthApi as jest . Mocked < typeof AuthApi > ;
159159const MockUserApi = UserApi as jest . Mocked < typeof UserApi > ;
160160const MockGetPasskeyStatus = getPasskeyStatus as jest . Mock ;
161161const MockSignInWithPasskey = signInWithPasskey as jest . Mock ;
162162const MockIsAnonymousBrazeEid = isAnonymousBrazeEid as jest . Mock ;
163- const MockBrazeWrapperMerge = BrazeWrapper . merge as jest . Mock ;
163+ const MockBitPayIdApiCall = BitPayIdApi . apiCall as jest . Mock ;
164+ const MockAnalyticsEndMergingUser = Analytics . endMergingUser as jest . Mock ;
164165const MockSleep = jest
165166 . spyOn ( helperMethods , 'sleep' )
166167 . mockResolvedValue ( undefined ) ;
@@ -288,16 +289,16 @@ describe('startBitPayIdAnalyticsInit', () => {
288289 it ( 'does nothing when user is falsy' , async ( ) => {
289290 const store = baseStore ( ) ;
290291 await store . dispatch ( startBitPayIdAnalyticsInit ( null as any ) ) ;
291- expect ( MockBrazeWrapperMerge ) . not . toHaveBeenCalled ( ) ;
292+ expect ( MockBitPayIdApiCall ) . not . toHaveBeenCalled ( ) ;
292293 } ) ;
293294
294- it ( 'calls BrazeWrapper.merge when brazeEid is anonymous and differs from user eid' , async ( ) => {
295+ it ( 'calls the mergeBrazeUser API when brazeEid is anonymous and differs from user eid' , async ( ) => {
295296 MockIsAnonymousBrazeEid . mockReturnValueOnce ( true ) ;
296297
297298 const store = configureTestStore ( {
298299 BITPAY_ID : {
299300 session : makeSession ( ) ,
300- apiToken : { [ Network . mainnet ] : '' } ,
301+ apiToken : { [ Network . mainnet ] : 'token1 ' } ,
301302 } ,
302303 APP : {
303304 network : Network . mainnet ,
@@ -310,14 +311,17 @@ describe('startBitPayIdAnalyticsInit', () => {
310311 const user = makeUser ( { eid : 'new-eid-xyz' } ) ;
311312 await store . dispatch ( startBitPayIdAnalyticsInit ( user ) ) ;
312313
313- expect ( MockBrazeWrapperMerge ) . toHaveBeenCalledWith (
314- 'old-anon-eid' ,
315- 'new-eid-xyz' ,
314+ expect ( MockBitPayIdApiCall ) . toHaveBeenCalledWith (
315+ 'token1' ,
316+ 'mergeBrazeUser' ,
317+ {
318+ userToMerge : 'old-anon-eid' ,
319+ } ,
316320 ) ;
317321 expect ( MockSleep ) . toHaveBeenCalledWith ( 5000 ) ;
318322 } ) ;
319323
320- it ( 'does NOT call BrazeWrapper.merge when brazeEid is not anonymous' , async ( ) => {
324+ it ( 'does NOT call the mergeBrazeUser API when brazeEid is not anonymous' , async ( ) => {
321325 MockIsAnonymousBrazeEid . mockReturnValueOnce ( false ) ;
322326
323327 const store = configureTestStore ( {
@@ -334,7 +338,33 @@ describe('startBitPayIdAnalyticsInit', () => {
334338 } ) ;
335339
336340 await store . dispatch ( startBitPayIdAnalyticsInit ( makeUser ( ) ) ) ;
337- expect ( MockBrazeWrapperMerge ) . not . toHaveBeenCalled ( ) ;
341+ expect ( MockBitPayIdApiCall ) . not . toHaveBeenCalled ( ) ;
342+ } ) ;
343+
344+ it ( 'still completes the merging lifecycle (sleep + endMergingUser) when mergeBrazeUser fails' , async ( ) => {
345+ MockIsAnonymousBrazeEid . mockReturnValueOnce ( true ) ;
346+ MockBitPayIdApiCall . mockRejectedValueOnce ( new Error ( 'merge failed' ) ) ;
347+
348+ const store = configureTestStore ( {
349+ BITPAY_ID : {
350+ session : makeSession ( ) ,
351+ apiToken : { [ Network . mainnet ] : 'token1' } ,
352+ } ,
353+ APP : {
354+ network : Network . mainnet ,
355+ brazeEid : 'old-anon-eid' ,
356+ emailNotifications : { accepted : false } ,
357+ notificationsAccepted : false ,
358+ } ,
359+ } ) ;
360+
361+ const user = makeUser ( { eid : 'new-eid-xyz' } ) ;
362+ await expect (
363+ store . dispatch ( startBitPayIdAnalyticsInit ( user ) ) ,
364+ ) . resolves . toBeUndefined ( ) ;
365+
366+ expect ( MockSleep ) . toHaveBeenCalledWith ( 5000 ) ;
367+ expect ( MockAnalyticsEndMergingUser ) . toHaveBeenCalled ( ) ;
338368 } ) ;
339369
340370 it ( 'derives givenName/familyName from name when they are missing' , async ( ) => {
0 commit comments