Skip to content

Commit

Permalink
test(integration-karma): add tests for objects with computed key
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-rk9 committed Mar 6, 2025
1 parent 2e5bf3d commit 5a2358b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/@lwc/integration-karma/test/lwc-on/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import Lifecycle from 'x/lifecycle';
import Rerender from 'x/rerender';
import RerenderLoop from 'x/rerenderLoop';
import PublicProp from 'x/publicProp';

// import { catchUnhandledRejectionsAndErrors, spyConsole } from 'test-utils';
import ComputedKey from 'x/computedKey';

describe('lwc:on', () => {
it('adds multiple event listeners', () => {
Expand Down Expand Up @@ -346,4 +345,15 @@ describe('lwc:on', () => {
button.click();
expect(testFn).toHaveBeenCalled();
});

it('works properly with objects whose keys are computed', () => {
const element = createElement('x-computed-key', { is: ComputedKey });
const testFn = jasmine.createSpy('test function');
element.testFn = testFn;
document.body.appendChild(element);
const button = element.shadowRoot.querySelector('button');

button.click();
expect(testFn).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<button lwc:on={eventHandlers}>Click Me</button>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { LightningElement, api } from 'lwc';

let testFn;
const mouseover = 'click';

export default class ComputedKey extends LightningElement {
@api
get testFn() {
return testFn;
}
set testFn(val) {
testFn = val;
}

eventHandlers = {
[mouseover]: function () {
testFn();
},
};
}

0 comments on commit 5a2358b

Please sign in to comment.