Skip to content

Commit

Permalink
fix: braze anonymousId tracking with alias details (#1994)
Browse files Browse the repository at this point in the history
  • Loading branch information
utsabc authored Jan 21, 2025
1 parent 09ff745 commit 7215304
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const mockBrazeSDK = () => {
throw new Error('Braze SDK Error: changeUser requires a non-empty userId. (v4.2.1)');
}
}),
addAlias: jest.fn(),
openSession: jest.fn(),
getUser: jest.fn().mockReturnThis(),
setCountry: jest.fn(),
Expand Down Expand Up @@ -141,15 +142,99 @@ describe('isLoaded', () => {
});
});

describe('isLoaded', () => {
it('should get false value with isReady', () => {
const config = {};
const analytics = {};
const destinationInfo = {};
describe('setUserAlias', () => {
let braze;
let config;
let analytics;

const braze = new Braze(config, analytics, destinationInfo);
const isLoaded = braze.isReady();
expect(isLoaded).toBe(false);
beforeEach(() => {
config = {
appKey: 'APP_KEY',
};
analytics = {
getAnonymousId: jest.fn(),
};
braze = new Braze(config, analytics, {});
braze.init();
mockBrazeSDK();
});

it('should successfully set user alias', () => {
analytics.getAnonymousId.mockReturnValue('anon123');
window.braze.getUser().addAlias.mockReturnValue(true);

const result = braze.setUserAlias();
expect(result).toBe(true);
expect(window.braze.getUser().addAlias).toHaveBeenCalledWith('anon123', 'rudder_id');
});

it('should fail when anonymous ID is missing', () => {
analytics.getAnonymousId.mockReturnValue(null);

const result = braze.setUserAlias();
expect(result).toBe(false);
});

it('should fail when user object is not available', () => {
analytics.getAnonymousId.mockReturnValue('anon123');
window.braze.getUser = jest.fn().mockReturnValue(null);

const result = braze.setUserAlias();
expect(result).toBe(false);
});

it('should fail when addAlias returns false', () => {
analytics.getAnonymousId.mockReturnValue('anon123');
window.braze.getUser().addAlias.mockReturnValue(false);

const result = braze.setUserAlias();
expect(result).toBe(false);
});

it('should handle errors gracefully', () => {
analytics.getAnonymousId.mockImplementation(() => {
throw new Error('Test error');
});

const result = braze.setUserAlias();
expect(result).toBe(false);
});
});


describe('isReady', () => {
let braze;
let config;
let analytics;

beforeEach(() => {
config = { appKey: 'APP_KEY' };
analytics = { getAnonymousId: jest.fn() };
braze = new Braze(config, analytics, {});
});

it('should return false when not loaded', () => {
jest.spyOn(braze, 'isLoaded').mockReturnValue(false);

const result = braze.isReady();
expect(result).toBe(false);
expect(braze.isLoaded).toHaveBeenCalled();
});

it('should return true when loaded and alias set successfully', () => {
jest.spyOn(braze, 'isLoaded').mockReturnValue(true);
jest.spyOn(braze, 'setUserAlias').mockReturnValue(true);

const result = braze.isReady();
expect(result).toBe(true);
});

it('should return false when loaded but alias setting fails', () => {
jest.spyOn(braze, 'isLoaded').mockReturnValue(true);
jest.spyOn(braze, 'setUserAlias').mockReturnValue(false);

const result = braze.isReady();
expect(result).toBe(false);
});
});

Expand Down Expand Up @@ -561,11 +646,10 @@ describe('track', () => {
},
};

jest.spyOn(window.braze, 'changeUser');
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Product Reviewed', {
rating: 3,
review_body: 'Good product.',
Expand Down Expand Up @@ -622,11 +706,10 @@ describe('track', () => {
},
};

jest.spyOn(window.braze, 'changeUser');
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logPurchase).toHaveBeenCalledTimes(1);
expect(window.braze.logPurchase).toHaveBeenCalledWith('123454387', 15.99, 'USD', 1, {});
});

Expand Down Expand Up @@ -680,11 +763,10 @@ describe('track', () => {
},
};

jest.spyOn(window.braze, 'changeUser');
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logPurchase).toHaveBeenCalledTimes(1);
expect(window.braze.logPurchase).toHaveBeenCalledWith('123454387', 15.99, 'USD', 1, {
rating: 5,
});
Expand Down Expand Up @@ -734,11 +816,15 @@ describe('track', () => {
},
};

jest.spyOn(window.braze, 'changeUser');
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('anon123');
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Product Reviewed', {
rating: 3,
review_body: 'Good product.',
review_id: '12345',
});
});

it('should call the necessary Braze methods for order completed event wit hreserved properties', () => {
Expand Down Expand Up @@ -791,11 +877,10 @@ describe('track', () => {
},
};

jest.spyOn(window.braze, 'changeUser');
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Product Reviewed', {
products: [{ name: 'Game', price: 15.99, product_id: '123454387', quantity: 1 }],
});
Expand Down Expand Up @@ -827,19 +912,18 @@ describe('page', () => {
name: 'Home',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};

jest.spyOn(window.braze, 'changeUser');
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Home', {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
});
});

Expand All @@ -866,19 +950,18 @@ describe('page', () => {
type: 'page',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};

jest.spyOn(window.braze, 'changeUser');
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
});
});

Expand All @@ -905,19 +988,18 @@ describe('page', () => {
type: 'page',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};

jest.spyOn(window.braze, 'changeUser');
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(0);
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
});
});

Expand All @@ -944,20 +1026,18 @@ describe('page', () => {
type: 'page',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};

jest.spyOn(window.braze, 'changeUser');
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(1);
expect(window.braze.changeUser).toHaveBeenCalledWith('anon123');
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
});
});

Expand All @@ -984,23 +1064,21 @@ describe('page', () => {
type: 'page',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
event_name: 'ABC',
referer: 'index',
currency: 'usd',
},
},
};

jest.spyOn(window.braze, 'changeUser');
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(1);
expect(window.braze.changeUser).toHaveBeenCalledWith('anon123');
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
referer: 'index',
});
});
Expand Down Expand Up @@ -1033,7 +1111,7 @@ describe('hybrid mode', () => {
name: 'Home',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};
Expand All @@ -1042,7 +1120,7 @@ describe('hybrid mode', () => {
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(0);
expect(window.braze.changeUser).toHaveBeenCalledTimes(0);
});

it('should not call the necessary Braze methods for track call', () => {
Expand Down Expand Up @@ -1071,7 +1149,7 @@ describe('hybrid mode', () => {
name: 'Home',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};
Expand All @@ -1080,7 +1158,7 @@ describe('hybrid mode', () => {
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(0);
expect(window.braze.changeUser).toHaveBeenCalledTimes(0);
});

it('should call the necessary Braze methods for identify call', () => {
Expand Down Expand Up @@ -1109,7 +1187,7 @@ describe('hybrid mode', () => {
name: 'Home',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};
Expand All @@ -1118,6 +1196,6 @@ describe('hybrid mode', () => {
braze.identify(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(1);
expect(window.braze.changeUser).toHaveBeenCalledTimes(1);
});
});
Loading

0 comments on commit 7215304

Please sign in to comment.