Skip to content

Commit

Permalink
feat: edit in logic and adding test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Oct 22, 2024
1 parent 11faf13 commit 274007d
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,143 @@ describe('Init tests', () => {
loaded: expect.any(Function),
});
});

test('Session replay configuration', () => {
const analytics = {
loadOnlyIntegrations: {
Mixpanel: {
recordBlockClass: 'block-class',
recordCollectFonts: true,
recordIdleTimeout: 5000,
recordMaskTextClass: 'mask-text',
recordMaskTextSelector: '.sensitive',
recordMaxMs: 30000,
recordMinMs: 1000,
},
},
logLevel: 'debug',
};

mixpanel = new Mixpanel(
{
persistenceType: 'localStorage',
persistenceName: 'test',
sessionReplayPercentage: 50,
},
analytics,
{ logLevel: 'debug' },
);
mixpanel.init();

expect(window.mixpanel._i[0][1]).toEqual({
cross_subdomain_cookie: false,
secure_cookie: false,
persistence: 'localStorage',
persistence_name: 'test',
record_block_class: 'block-class',
record_collect_fonts: true,
record_idle_timeout_ms: 5000,
record_mask_text_class: 'mask-text',
record_mask_text_selector: '.sensitive',
record_max_ms: 30000,
record_min_ms: 1000,
record_sessions_percent: 50,
loaded: expect.any(Function),
});
});

test('Session replay configuration with partial options', () => {
const analytics = {
loadOnlyIntegrations: {
Mixpanel: {
recordBlockClass: 'block-class',
recordCollectFonts: true,
},
},
logLevel: 'debug',
};

mixpanel = new Mixpanel(
{
persistenceType: 'localStorage',
persistenceName: 'test',
},
analytics,
{ logLevel: 'debug' },
);
mixpanel.init();

expect(window.mixpanel._i[0][1]).toEqual({
cross_subdomain_cookie: false,
secure_cookie: false,
persistence: 'localStorage',
persistence_name: 'test',
record_block_class: 'block-class',
record_collect_fonts: true,
loaded: expect.any(Function),
});
});

test('Session replay configuration without loadOnlyIntegrations', () => {
const analytics = {
logLevel: 'debug',
};

mixpanel = new Mixpanel(
{
persistenceType: 'localStorage',
persistenceName: 'test',
sessionReplayPercentage: 75,
},
analytics,
{ logLevel: 'debug' },
);
mixpanel.init();

expect(window.mixpanel._i[0][1]).toEqual({
cross_subdomain_cookie: false,
secure_cookie: false,
persistence: 'localStorage',
persistence_name: 'test',
record_sessions_percent: 75,
loaded: expect.any(Function),
});
});

test('Session replay configuration with invalid percentage', () => {
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});

const analytics = {
logLevel: 'debug',
};

mixpanel = new Mixpanel(
{
persistenceType: 'localStorage',
persistenceName: 'test',
sessionReplayPercentage: '101',
},
analytics,
{ logLevel: 'debug' },
);
mixpanel.init();

expect(window.mixpanel._i[0][1]).toEqual({
cross_subdomain_cookie: false,
secure_cookie: false,
persistence: 'localStorage',
persistence_name: 'test',
loaded: expect.any(Function),
});

expect(consoleSpy).toHaveBeenCalledWith(
'%c RS SDK - Mixpanel %c Invalid sessionReplayPercentage: 101. It should be a string matching the pattern "^(100|[1-9]?[0-9])$"',
'font-weight: bold; background: black; color: white;',
'font-weight: normal;',
);

consoleSpy.mockRestore();
});
});

describe('isLoaded and isReady tests', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
DISPLAY_NAME,
} from '@rudderstack/analytics-js-common/constants/integrations/Mixpanel/constants';
import Logger from '../../utils/logger';
import { pick, removeUndefinedAndNullValues, isNotEmpty } from '../../utils/commonUtils';
import {
pick,
removeUndefinedAndNullValues,
isNotEmpty,
isDefinedAndNotNull,
} from '../../utils/commonUtils';
import {
mapTraits,
unionArrays,
Expand Down Expand Up @@ -101,16 +106,26 @@ class Mixpanel {
// ref : https://docs.mixpanel.com/docs/tracking-methods/sdks/javascript#session-replay
if (mixpanelIntgConfig) {
const sessionReplayConfig = {
record_sessions_percent : this.sessionReplayPercentage,
record_block_class : mixpanelIntgConfig?.recordBlockClass,
record_collect_fonts : mixpanelIntgConfig?.recordCollectFonts,
record_idle_timeout_ms : mixpanelIntgConfig?.recordIdleTimeout,
record_mask_text_class : mixpanelIntgConfig?.recordMaskTextClass,
record_mask_text_selector : mixpanelIntgConfig?.recordMaskTextSelector,
record_max_ms : mixpanelIntgConfig?.recordMaxMs,
record_min_ms : mixpanelIntgConfig?.recordMinMs
record_block_class: mixpanelIntgConfig?.recordBlockClass,
record_collect_fonts: mixpanelIntgConfig?.recordCollectFonts,
record_idle_timeout_ms: mixpanelIntgConfig?.recordIdleTimeout,
record_mask_text_class: mixpanelIntgConfig?.recordMaskTextClass,
record_mask_text_selector: mixpanelIntgConfig?.recordMaskTextSelector,
record_max_ms: mixpanelIntgConfig?.recordMaxMs,
record_min_ms: mixpanelIntgConfig?.recordMinMs,
};
options = {...options,...removeUndefinedAndNullValues(sessionReplayConfig)}
options = { ...options, ...removeUndefinedAndNullValues(sessionReplayConfig) };
}

if (isDefinedAndNotNull(this.sessionReplayPercentage)) {
const percentageInt = parseInt(this.sessionReplayPercentage, 10);
if (percentageInt >= 0 && percentageInt <= 100) {
options.record_sessions_percent = percentageInt;
} else {
logger.warn(
`Invalid sessionReplayPercentage: ${this.sessionReplayPercentage}. It should be a string matching the pattern "^(100|[1-9]?[0-9])$"`,
);
}
}
options.loaded = () => {
this.isNativeSDKLoaded = true;
Expand Down

0 comments on commit 274007d

Please sign in to comment.