Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Change hasHostListenerAttached from var to protoype property #6074

Merged
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
6 changes: 3 additions & 3 deletions src/runtime/bootstrap-custom-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ export const proxyCustomElement = (Cstr: any, compactMeta: d.ComponentRuntimeMet

const originalConnectedCallback = Cstr.prototype.connectedCallback;
const originalDisconnectedCallback = Cstr.prototype.disconnectedCallback;
let hasHostListenerAttached = false;
Object.assign(Cstr.prototype, {
__hasHostListenerAttached: false,
__registerHost() {
registerHost(this, cmpMeta);
},
connectedCallback() {
if (!hasHostListenerAttached) {
if (!this.__hasHostListenerAttached) {
const hostRef = getHostRef(this);
addHostEventListeners(this, hostRef, cmpMeta.$listeners$, false);
hasHostListenerAttached = true;
this.__hasHostListenerAttached = true;
}

connectedCallback(this);
Expand Down
68 changes: 68 additions & 0 deletions test/wdio/event-listener-capture/event-re-register.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,72 @@ describe('event-listener-capture using lazy load components', function () {
// check if event fired 6 times
await expect(reattach).toHaveText(expect.stringContaining('Event fired times: 6'));
});

it('should attach keydown event listener once per component', async () => {
const elem = document.createElement('event-re-register') as HTMLElement;
elem.setAttribute('id', 'elem1');
document.body.appendChild(elem);

const reattach = $('#elem1');
await expect(reattach).toBePresent();

// focus on element 1
await reattach.click();
await browser.action('key').down('a').pause(100).up('a').perform();
await browser.action('key').down('a').pause(100).up('a').perform();
await browser.action('key').down('a').pause(100).up('a').perform();

// check if event fired 3 times on first element
await expect(reattach).toHaveText(expect.stringContaining('Event fired times: 3'), {
message: 'Second element should have fired 3 times',
});

const elem2 = document.createElement('event-re-register') as HTMLElement;
elem2.setAttribute('id', 'elem2');
document.body.appendChild(elem2);

const reattach2 = $('#elem2');
await expect(reattach2).toBePresent();

// focus on element 2
await reattach2.click();
await browser.action('key').down('a').pause(100).up('a').perform();
await browser.action('key').down('a').pause(100).up('a').perform();
await browser.action('key').down('a').pause(100).up('a').perform();

// check if event fired 3 times on second element
await expect(reattach2).toHaveText(expect.stringContaining('Event fired times: 3'), {
message: 'Second element should have fired 3 times',
});

// remove node from DOM
elem.remove();
elem2.remove();

// reattach node to DOM
document.body.appendChild(elem);
document.body.appendChild(elem2);

// retrigger event
await reattach.click();
await browser.action('key').down('a').pause(100).up('a').perform();
await browser.action('key').down('a').pause(100).up('a').perform();
await browser.action('key').down('a').pause(100).up('a').perform();

// check if event fired 6 times on first element
await expect(reattach).toHaveText(expect.stringContaining('Event fired times: 6'), {
message: 'First element should have fired 3 times',
});

// retrigger event on element 2
await reattach2.click();
await browser.action('key').down('a').pause(100).up('a').perform();
await browser.action('key').down('a').pause(100).up('a').perform();
await browser.action('key').down('a').pause(100).up('a').perform();

// check if event fired 3 times on second element
await expect(reattach2).toHaveText(expect.stringContaining('Event fired times: 6'), {
message: 'Second element should have fired 3 times',
});
});
});
Loading