Skip to content

Commit

Permalink
update: test RUM is fired before redirect by using waitForFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
FentPams committed Aug 20, 2024
1 parent e4e2257 commit a949eb3
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions tests/experiments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,38 @@ test.describe('Page-level experiments', () => {
]);
});

test('Track RUM is fired before redirect.', async ({ page }) => {
test('Track RUM is fired before redirect', async ({ page }) => {
const rumCalls = [];
await page.exposeFunction('logRumCall', (...args) => rumCalls.push(args));
await page.addInitScript(() => {
window.hlx = { rum: { sampleRUM: (...args) => window.logRumCall(args) } };
});
await page.goto('/tests/fixtures/experiments/page-level--redirect?experiment=foo/challenger-1');
await page.waitForURL('/tests/fixtures/experiments/page-level-v1');
expect(await page.evaluate(() => window.document.body.innerText)).toMatch(/Hello v1!/);
await page.goto('/tests/fixtures/experiments/page-level--redirect');
await page.waitForFunction(() => window.hlx.rum.sampleRUM);
expect(rumCalls[0]).toContainEqual([
'experiment',
{
source: 'foo',
target: 'challenger-1',
target: expect.stringMatching(/control|challenger-1|challenger-2/),
},
]);

const expectedContent = {
v1: 'Hello v1!',
v2: 'Hello v2!',
redirect: 'Hello World!',
};
const expectedUrlPath = {
v1: '/tests/fixtures/experiments/page-level-v1',
v2: '/tests/fixtures/experiments/page-level-v2',
redirect: '/tests/fixtures/experiments/page-level--redirect',
};
const url = new URL(page.url());
const variant = Object.keys(expectedUrlPath).find((k) => url.pathname.endsWith(k));
expect(await page.evaluate(() => window.document.body.innerText)).toMatch(
new RegExp(expectedContent[variant]),
);
expect(expectedUrlPath[variant]).toBe(url.pathname);
});

test('Exposes the experiment in a JS API.', async ({ page }) => {
Expand Down

0 comments on commit a949eb3

Please sign in to comment.