Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updated isLoaded and isReady conditions for mixpanel #1650

Merged
merged 7 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ afterEach(() => {
});

describe('Init tests', () => {
beforeEach(() => {
window.mixpanel = [];
});
let mixpanel;

test('Persistence type is missing', () => {
Expand All @@ -27,6 +30,7 @@ describe('Init tests', () => {
cross_subdomain_cookie: false,
secure_cookie: false,
persistence: 'cookie',
loaded: expect.any(Function),
});
});

Expand All @@ -40,6 +44,7 @@ describe('Init tests', () => {
cross_subdomain_cookie: false,
secure_cookie: false,
persistence: 'cookie',
loaded: expect.any(Function),
});
});

Expand All @@ -54,6 +59,7 @@ describe('Init tests', () => {
secure_cookie: false,
persistence: 'localStorage',
persistence_name: 'abc',
loaded: expect.any(Function),
});
});

Expand All @@ -67,11 +73,57 @@ describe('Init tests', () => {
cross_subdomain_cookie: false,
secure_cookie: false,
disable_persistence: true,
loaded: expect.any(Function),
});
});
});

describe('isLoaded and isReady tests', () => {
let mixpanel;

const loadSDK = () => {
setTimeout(() => {
mixpanel.isNativeSDKLoaded = true; // Change to true after 5 seconds
}, 5000); // 5 seconds
};

test('isLoaded test', () => {
mixpanel = new Mixpanel({ persistence: 'none' }, { logLevel: 'debug' });

loadSDK(); // Call loadSDK to set isNativeSDKLoaded after 5 seconds

return new Promise(resolve => {
const interval = setInterval(() => {
if (mixpanel.isLoaded()) {
clearInterval(interval);
expect(mixpanel.isLoaded()).toBe(true);
resolve(); // Resolve the promise once the expectation is met
}
}, 1000);
});
});

test('isReady test', () => {
mixpanel = new Mixpanel({ persistence: 'none' }, { logLevel: 'debug' });

loadSDK(); // Call loadSDK to set isNativeSDKLoaded after 5 seconds

return new Promise(resolve => {
const interval = setInterval(() => {
if (mixpanel.isReady()) {
clearInterval(interval);
expect(mixpanel.isReady()).toBe(true);
resolve(); // Resolve the promise once the expectation is met
}
}, 1000);
});
});
});

describe('Page tests', () => {
beforeEach(() => {
window.mixpanel = [];
});
let mixpanel;
test('should return a custom generated event name when useUserDefinedPageEventName setting is enabled and event template is provided', () => {
mixpanel = new Mixpanel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
this.ignoreDnt = config.ignoreDnt || false;
this.useUserDefinedPageEventName = config.useUserDefinedPageEventName || false;
this.userDefinedPageEventTemplate = config.userDefinedPageEventTemplate;
this.isNativeSDKLoaded = false;
}

init() {
Expand Down Expand Up @@ -93,16 +94,19 @@
if (this.ignoreDnt) {
options.ignore_dnt = true;
}
options.loaded = () => {
this.isNativeSDKLoaded = true;

Check warning on line 98 in packages/analytics-js-integrations/src/integrations/Mixpanel/browser.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-integrations/src/integrations/Mixpanel/browser.js#L98

Added line #L98 was not covered by tests
};
window.mixpanel.init(this.token, options);
window.mixpanel.register({ mp_lib: 'Rudderstack: web' });
}

isLoaded() {
sandeepdsvs marked this conversation as resolved.
Show resolved Hide resolved
window.mixpanel.register({ mp_lib: 'Rudderstack: web' });
return !!window?.mixpanel?.config;
return this.isNativeSDKLoaded;
}

isReady() {
sandeepdsvs marked this conversation as resolved.
Show resolved Hide resolved
return !!window?.mixpanel?.config;
return this.isLoaded();
}

/**
Expand Down