Skip to content

Commit 1a97ed2

Browse files
gaurav-rk9divmain
andauthored
feat(ssr): make SSR compiler recognize lwc:on directive (#5377)
Make SSR compiler recognize lwc:on directive. Also adds snapshot and hydration tests to verify lwc:on used in SSR context Co-authored-by: Dale Bustad <dbustad@salesforce.com>
1 parent c266816 commit 1a97ed2

File tree

12 files changed

+77
-0
lines changed

12 files changed

+77
-0
lines changed

packages/@lwc/engine-server/src/__tests__/fixtures.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ async function compileFixture({
7979
pluginVirtual(`export { default } from "${entry}";`, input),
8080
lwcRollupPlugin({
8181
enableDynamicComponents: true,
82+
enableLwcOn: true,
8283
experimentalDynamicComponent: {
8384
loader: path.join(__dirname, './utils/custom-loader.js'),
8485
strictSpecifier: false,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"entry": "x/lwc-on"
3+
}

packages/@lwc/engine-server/src/__tests__/fixtures/lwc-on/error.txt

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<fixture-test>
2+
<template shadowrootmode="open">
3+
<button>
4+
Button which listens for multiple events
5+
</button>
6+
</template>
7+
</fixture-test>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<button lwc:on={eventListeners}>Button which listens for multiple events</button>
3+
</template>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { LightningElement } from 'lwc';
2+
3+
export default class LwcOn extends LightningElement {
4+
eventListeners = {
5+
click: () => {
6+
console.log('click');
7+
},
8+
foo: () => {
9+
console.log('foo');
10+
},
11+
};
12+
}

packages/@lwc/integration-karma/scripts/karma-plugins/hydration-tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ async function getCompiledModule(dirName, compileForSSR) {
7171
strict: true,
7272
},
7373
enableDynamicComponents: true,
74+
enableLwcOn: true,
7475
enableStaticContentOptimization: !DISABLE_STATIC_CONTENT_OPTIMIZATION,
7576
experimentalDynamicDirective: true,
7677
}),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default {
2+
props: {},
3+
snapshot(target) {
4+
const div = target.shadowRoot.querySelector('div');
5+
6+
return {
7+
div,
8+
};
9+
},
10+
test(target, snapshots) {
11+
const snapshotAfterHydration = this.snapshot(target);
12+
expect(snapshotAfterHydration.div).toBe(snapshots.div);
13+
14+
// verify handler
15+
snapshotAfterHydration.div.click();
16+
expect(target.timesClickedHandlerIsExecuted).toBe(1);
17+
snapshotAfterHydration.div.dispatchEvent(new CustomEvent('foo'));
18+
expect(target.timesFooHandlerIsExecuted).toBe(1);
19+
},
20+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div lwc:on={eventListeners}>I listen for events specified in the eventListeners object</div>
3+
</template>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { LightningElement, api } from 'lwc';
2+
3+
export default class Main extends LightningElement {
4+
_clickedHandlerCounter = 0;
5+
_fooHandlerCounter = 0;
6+
7+
@api
8+
get timesClickedHandlerIsExecuted() {
9+
return this._clickedHandlerCounter;
10+
}
11+
12+
@api
13+
get timesFooHandlerIsExecuted() {
14+
return this._fooHandlerCounter;
15+
}
16+
17+
eventListeners = {
18+
click: function () {
19+
this._clickedHandlerCounter++;
20+
},
21+
foo: function () {
22+
this._fooHandlerCounter++;
23+
},
24+
};
25+
}

0 commit comments

Comments
 (0)