Skip to content

Commit 21e23a2

Browse files
author
Luca Forstner
committed
Add integration tests to check for preservation of this
1 parent c8e2ea9 commit 21e23a2

File tree

2 files changed

+48
-0
lines changed
  • packages/integration-tests/suites/public-api/instrumentation/eventListener-this-preservation

2 files changed

+48
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const btn = document.createElement('button');
2+
btn.id = 'btn';
3+
document.body.appendChild(btn);
4+
5+
const functionListener = function () {
6+
functionCallback(this.constructor.name);
7+
};
8+
9+
class EventHandlerClass {
10+
handleEvent() {
11+
classInstanceCallback(this.constructor.name);
12+
}
13+
}
14+
const objectListener = new EventHandlerClass();
15+
16+
// Attach event listeners a few times for good measure
17+
18+
btn.addEventListener('click', functionListener);
19+
btn.addEventListener('click', functionListener);
20+
btn.addEventListener('click', functionListener);
21+
22+
btn.addEventListener('click', objectListener);
23+
btn.addEventListener('click', objectListener);
24+
btn.addEventListener('click', objectListener);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
5+
sentryTest('Event listener instrumentation preserves "this" context', async ({ getLocalTestPath, page }) => {
6+
const url = await getLocalTestPath({ testDir: __dirname });
7+
await page.goto(url);
8+
9+
let assertions = 0;
10+
11+
await page.exposeFunction('functionCallback', (thisInstanceName: unknown) => {
12+
expect(thisInstanceName).toBe('HTMLButtonElement');
13+
assertions = assertions + 1;
14+
});
15+
16+
await page.exposeFunction('classInstanceCallback', (thisInstanceName: unknown) => {
17+
expect(thisInstanceName).toBe('EventHandlerClass');
18+
assertions = assertions + 1;
19+
});
20+
21+
await page.evaluate('document.getElementById("btn").click()');
22+
23+
expect(assertions).toBe(2);
24+
});

0 commit comments

Comments
 (0)