Skip to content

Commit

Permalink
fix: updated isLoaded and isReady conditions for mixpanel (#1650)
Browse files Browse the repository at this point in the history
* fix: updated isLoaded and isReady conditions for mixpanel

* chare: addressed review comments

* fix: updated isLoaded and isReady conditions for mixpanel

* fix: added extra tests

* fix: updated tests
  • Loading branch information
sandeepdsvs authored Apr 2, 2024
1 parent da77fff commit 80c76a1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
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 @@ class Mixpanel {
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 @@ class Mixpanel {
if (this.ignoreDnt) {
options.ignore_dnt = true;
}
options.loaded = () => {
this.isNativeSDKLoaded = true;
};
window.mixpanel.init(this.token, options);
window.mixpanel.register({ mp_lib: 'Rudderstack: web' });
}

isLoaded() {
window.mixpanel.register({ mp_lib: 'Rudderstack: web' });
return !!window?.mixpanel?.config;
return this.isNativeSDKLoaded;
}

isReady() {
return !!window?.mixpanel?.config;
return this.isLoaded();
}

/**
Expand Down

0 comments on commit 80c76a1

Please sign in to comment.