Skip to content

Commit

Permalink
test: implement tests for resolveSendBeaconRequestParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
hoebbelsB committed Feb 26, 2024
1 parent dae4558 commit a33717a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
26 changes: 26 additions & 0 deletions tests/platform/navigator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@
logSetters: true,
logStackTraces: false,
logScriptExecution: true,
resolveSendBeaconRequestParameters: (url, location) => {
if (url.searchParams.has('withParams')) {
return {
keepalive: false,
mode: 'same-origin',
cache: 'no-cache',
headers: new Headers({
'custom-header': 'custom-value'
})
};
}
}
};
</script>
<script src="/~partytown/debug/partytown.js"></script>
Expand Down Expand Up @@ -83,6 +95,20 @@ <h1>Navigator</h1>
})();
</script>
</li>
<li>
<strong>sendBeacon() resolveParams</strong>
<code id="testSendBeaconResolveParams"></code>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testSendBeaconResolveParams');
const formData = new FormData();
formData.append('name', 'value');
const success = navigator.sendBeacon('api.js?withParams=1', formData);
elm.textContent = success;
elm.className = 'testSendBeaconResolveParams';
})();
</script>
</li>
<li>
<strong>Assign value 5 to navigator.a</strong>
<code id="testNavKey"></code>
Expand Down
20 changes: 19 additions & 1 deletion tests/platform/navigator/navigator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { test, expect } from '@playwright/test';
import { test, expect, Request } from '@playwright/test';

test('navigator', async ({ page }) => {
const sendBeaconWithParamsRequest = new Promise<Request>((resolve) => {
page.on('request', (request) => {
if (request.url().includes('api.js?withParams=1')) {
resolve(request);
}
});
});

await page.goto('/tests/platform/navigator/');

const testUserAgent = await page.waitForSelector('.testUserAgent');
Expand All @@ -11,6 +19,16 @@ test('navigator', async ({ page }) => {
const testSendBeacon = page.locator('#testSendBeacon');
await expect(testSendBeacon).toContainText('true');

await page.waitForSelector('.testSendBeaconResolveParams');
const testSendBeaconResolveParams = page.locator('#testSendBeaconResolveParams');
await expect(testSendBeaconResolveParams).toContainText('true');
const beaconWithParamsRequest = await sendBeaconWithParamsRequest;
const headers = await beaconWithParamsRequest.allHeaders();
expect(headers['cache-control']).toBe('max-age=0');
expect(headers['custom-header']).toBe('custom-value');
expect(headers['sec-fetch-mode']).toBe('same-origin');
expect(headers['sec-fetch-site']).toBe('same-origin');

await page.waitForSelector('.testNavKey');
const testNavKey = page.locator('#testNavKey');
await expect(testNavKey).toContainText('5');
Expand Down

0 comments on commit a33717a

Please sign in to comment.