Skip to content
Open
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
10 changes: 6 additions & 4 deletions src/Rokt-Kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,11 @@ function extractRoktExtensionConfig(settingsString?: string): RoktExtensionConfi
};
}

function registerLegacyExtensions(legacyExtensions: string[]) {
for (const extension of legacyExtensions) {
window.Rokt?.use(extension);
function registerLegacyExtensions(legacyExtensions: string[], launcher: RoktLauncher | null) {
if (launcher) {
for (const extension of legacyExtensions) {
launcher.use(extension);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
}

Expand Down Expand Up @@ -1107,7 +1109,7 @@ class RoktKit implements KitInterface {
onLoad: () => {
if (this.isLauncherReadyToAttach()) {
this.attachLauncher(accountId, launcherOptions);
registerLegacyExtensions(legacyRoktExtensions);
registerLegacyExtensions(legacyRoktExtensions, this.launcher);
} else {
console.error('Rokt object is not available after script load.');
}
Expand Down
102 changes: 95 additions & 7 deletions test/src/tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3348,11 +3348,18 @@ describe('Rokt Forwarder', () => {
expect(tyeScript.src).toContain('/rokt-elements/rokt-element-thank-you.js');
});

it('should call window.Rokt.use with ThankYouJourney when thank-you-journey extension is provided', async () => {
it('should call launcher.use with ThankYouJourney when thank-you-journey extension is provided', async () => {
document.getElementById('rokt-thank-you-element')?.remove();
document.getElementById('rokt-launcher')?.remove();

const useCalls: string[] = [];
const mockLauncher = {
selectPlacements: () => {},
hashAttributes: () => {},
use: (name: string) => {
useCalls.push(name);
},
};

(window as any).Rokt = undefined;
(window as any).mParticle.Rokt = {
Expand All @@ -3375,17 +3382,98 @@ describe('Rokt Forwarder', () => {
false,
);

// Use a synchronous thenable so this.launcher is set before registerLegacyExtensions runs
(window as any).Rokt = new (MockRoktForwarder as any)();
(window as any).Rokt.use = (name: string) => {
useCalls.push(name);
};
(window as any).Rokt.createLauncher = async () =>
Promise.resolve({ selectPlacements: () => {}, hashAttributes: () => {}, use: () => Promise.resolve() });
(window as any).Rokt.createLauncher = () => ({
then: (onFulfilled: (launcher: typeof mockLauncher) => void) => {
onFulfilled(mockLauncher);
return { catch: () => {} };
},
});

const launcherScript = document.getElementById('rokt-launcher') as HTMLScriptElement;
launcherScript.onload!(new Event('load'));

await waitForCondition(() => useCalls.length > 0);
expect(useCalls).toContain('ThankYouJourney');
});

it('should fetch thank you element resource when thank you element extension is provided', async () => {
document.getElementById('rokt-thank-you-element')?.remove();
document.getElementById('rokt-launcher')?.remove();

(window as any).Rokt = undefined;
(window as any).mParticle.Rokt = {
attachKit: async (kit: any) => {
(window as any).mParticle.Rokt.kit = kit;
},
filters: {
userAttributesFilters: [],
filterUserAttributes: (attrs: any) => attrs,
filteredUser: { getMPID: () => '123' },
},
use: () => Promise.resolve(),
};

await (window as any).mParticle.forwarder.init(
{
accountId: '123456',
roktExtensions: '[{"jsmap":null,"map":null,"maptype":"LegacyExtension","value":"thank-you-journey"}]',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it matters, but there isn't a maptype of LegacyExtension...let's make it staticlist to better match what the server sends back

Suggested change
roktExtensions: '[{"jsmap":null,"map":null,"maptype":"LegacyExtension","value":"thank-you-journey"}]',
roktExtensions: '[{"jsmap":null,"map":null,"maptype":"StaticList","value":"thank-you-journey"}]',

},
reportService.cb,
false,
);

const tyeScript = document.getElementById('rokt-thank-you-element') as HTMLScriptElement;
expect(tyeScript).not.toBeNull();
expect(tyeScript.src).toContain('/rokt-elements/rokt-element-thank-you.js');
});

it('should call launcher.use with ThankYouJourney when thank-you-journey extension is provided', async () => {
document.getElementById('rokt-thank-you-element')?.remove();
document.getElementById('rokt-launcher')?.remove();

const useCalls: string[] = [];

(window as any).Rokt = undefined;
(window as any).mParticle.Rokt = {
attachKit: async (kit: any) => {
(window as any).mParticle.Rokt.kit = kit;
},
filters: {
userAttributesFilters: [],
filterUserAttributes: (attrs: any) => attrs,
filteredUser: { getMPID: () => '123' },
},
};

await (window as any).mParticle.forwarder.init(
{
accountId: '123456',
roktExtensions: '[{"jsmap":null,"map":null,"maptype":"LegacyExtension","value":"thank-you-journey"}]',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
roktExtensions: '[{"jsmap":null,"map":null,"maptype":"LegacyExtension","value":"thank-you-journey"}]',
roktExtensions: '[{"jsmap":null,"map":null,"maptype":"StaticList","value":"thank-you-journey"}]',

},
reportService.cb,
false,
);

const mockLauncher = {
selectPlacements: () => {},
hashAttributes: () => {},
use: (name: string) => {
useCalls.push(name);
},
};

// Use a synchronous thenable so this.launcher is set before registerLegacyExtensions runs
(window as any).Rokt = new (MockRoktForwarder as any)();
(window as any).Rokt.createLauncher = () => ({
then: (onFulfilled: (launcher: typeof mockLauncher) => void) => {
onFulfilled(mockLauncher);
return { catch: () => {} };
},
});

const launcherScript = document.getElementById('rokt-launcher') as HTMLScriptElement;
launcherScript.onload!(new Event('load'));

expect(useCalls).toContain('ThankYouJourney');
});
Expand Down