-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
42aa488
commit dfbc340
Showing
9 changed files
with
271 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/end-to-end/src/declarative-shadow-dom/__snapshots__/test.e2e.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renderToString can render a scoped component within a shadow component 1`] = `"<car-list cars=\\"[{"make":"VW","model":"Vento","year":2024},{"make":"VW","model":"Beetle","year":2023}]\\" class=\\"sc-car-list-h\\" custom-hydrate-flag=\\"\\" s-id=\\"1\\"><template shadowrootmode=\\"open\\"><style sty-id=\\"sc-car-list\\">/*!@:host*/.sc-car-list-h{display:block;margin:10px;padding:10px;border:1px solid blue}/*!@ul*/ul.sc-car-list{display:block;margin:0;padding:0}/*!@li*/li.sc-car-list{list-style:none;margin:0;padding:20px}/*[email protected]*/.selected.sc-car-list{font-weight:bold;background:rgb(255, 255, 210)}</style><ul class=\\"sc-car-list\\" c-id=\\"1.0.0.0\\"><li class=\\"sc-car-list\\" c-id=\\"1.1.1.0\\"><car-detail class=\\"sc-car-list\\" custom-hydrate-flag=\\"\\" c-id=\\"1.2.2.0\\" s-id=\\"2\\"><!--r.2--><section c-id=\\"2.0.0.0\\"><!--t.2.1.1.0-->2024 VW Vento</section></car-detail></li><li class=\\"sc-car-list\\" c-id=\\"1.3.1.1\\"><car-detail class=\\"sc-car-list\\" custom-hydrate-flag=\\"\\" c-id=\\"1.4.2.0\\" s-id=\\"3\\"><!--r.3--><section c-id=\\"3.0.0.0\\"><!--t.3.1.1.0-->2023 VW Beetle</section></car-detail></li></ul></template><!--r.1--></car-list>"`; | ||
exports[`renderToString can render a scoped component within a shadow component 1`] = `"<car-list cars=\\"[{"make":"VW","model":"Vento","year":2024},{"make":"VW","model":"Beetle","year":2023}]\\" class=\\"sc-car-list-h\\" custom-hydrate-flag=\\"\\" s-id=\\"1\\"><template shadowrootmode=\\"open\\"><style sty-id=\\"sc-car-list\\">/*!@:host*/.sc-car-list-h{display:block;margin:10px;padding:10px;border:1px solid blue}/*!@ul*/ul.sc-car-list{display:block;margin:0;padding:0}/*!@li*/li.sc-car-list{list-style:none;margin:0;padding:20px}/*[email protected]*/.selected.sc-car-list{font-weight:bold;background:rgb(255, 255, 210)}</style><ul class=\\"sc-car-list\\" c-id=\\"1.0.0.0\\"><li class=\\"sc-car-list\\" c-id=\\"1.1.1.0\\"><car-detail class=\\"sc-car-list\\" custom-hydrate-flag=\\"\\" c-id=\\"1.2.2.0\\" s-id=\\"2\\"><!--r.2--><section class=\\"sc-car-list\\" c-id=\\"2.0.0.0\\"><!--t.2.1.1.0-->2024 VW Vento</section></car-detail></li><li class=\\"sc-car-list\\" c-id=\\"1.3.1.1\\"><car-detail class=\\"sc-car-list\\" custom-hydrate-flag=\\"\\" c-id=\\"1.4.2.0\\" s-id=\\"3\\"><!--r.3--><section class=\\"sc-car-list\\" c-id=\\"3.0.0.0\\"><!--t.3.1.1.0-->2023 VW Beetle</section></car-detail></li></ul></template><!--r.1--></car-list>"`; | ||
|
||
exports[`renderToString can render nested components 1`] = `"<another-car-list cars=\\"[{"make":"VW","model":"Vento","year":2024},{"make":"VW","model":"Beetle","year":2023}]\\" class=\\"sc-another-car-list-h\\" custom-hydrate-flag=\\"\\" s-id=\\"1\\"><template shadowrootmode=\\"open\\"><style sty-id=\\"sc-another-car-list\\">/*!@:host*/.sc-another-car-list-h{display:block;margin:10px;padding:10px;border:1px solid blue}/*!@ul*/ul.sc-another-car-list{display:block;margin:0;padding:0}/*!@li*/li.sc-another-car-list{list-style:none;margin:0;padding:20px}/*[email protected]*/.selected.sc-another-car-list{font-weight:bold;background:rgb(255, 255, 210)}</style><ul class=\\"sc-another-car-list\\" c-id=\\"1.0.0.0\\"><li class=\\"sc-another-car-list\\" c-id=\\"1.1.1.0\\"><another-car-detail class=\\"sc-another-car-list sc-another-car-detail-h\\" custom-hydrate-flag=\\"\\" c-id=\\"1.2.2.0\\" s-id=\\"2\\"><template shadowrootmode=\\"open\\"><style sty-id=\\"sc-another-car-detail\\">/*!@section*/section.sc-another-car-detail{color:green}</style><section class=\\"sc-another-car-detail\\" c-id=\\"2.0.0.0\\"><!--t.2.1.1.0-->2024 VW Vento</section></template><!--r.2--></another-car-detail></li><li class=\\"sc-another-car-list\\" c-id=\\"1.3.1.1\\"><another-car-detail class=\\"sc-another-car-list sc-another-car-detail-h\\" custom-hydrate-flag=\\"\\" c-id=\\"1.4.2.0\\" s-id=\\"3\\"><template shadowrootmode=\\"open\\"><style sty-id=\\"sc-another-car-detail\\">/*!@section*/section.sc-another-car-detail{color:green}</style><section class=\\"sc-another-car-detail\\" c-id=\\"3.0.0.0\\"><!--t.3.1.1.0-->2023 VW Beetle</section></template><!--r.3--></another-car-detail></li></ul></template><!--r.1--></another-car-list>"`; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
test/end-to-end/src/declarative-shadow-dom/scoped-car-detail.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Component, h, Prop } from '@stencil/core'; | ||
|
||
import { CarData } from '../car-list/car-data'; | ||
|
||
@Component({ | ||
tag: 'scoped-car-detail', | ||
styleUrl: 'another-car-detail.css', | ||
scoped: true, | ||
}) | ||
export class CarDetail { | ||
@Prop() car: CarData; | ||
|
||
render() { | ||
if (!this.car) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<section> | ||
{this.car.year} {this.car.make} {this.car.model} | ||
</section> | ||
); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
test/end-to-end/src/declarative-shadow-dom/scoped-car-list.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Component, Event, EventEmitter, h, Prop } from '@stencil/core'; | ||
|
||
import { CarData } from '../car-list/car-data'; | ||
|
||
/** | ||
* Component that helps display a list of cars | ||
* @slot header - The slot for the header content. | ||
* @part car - The shadow part to target to style the car. | ||
*/ | ||
@Component({ | ||
tag: 'scoped-car-list', | ||
styleUrl: 'another-car-list.css', | ||
scoped: true, | ||
}) | ||
export class CarList { | ||
@Prop() cars: CarData[]; | ||
@Prop({ mutable: true }) selected: CarData; | ||
@Event() carSelected: EventEmitter<CarData>; | ||
|
||
componentWillLoad() { | ||
return new Promise((resolve) => setTimeout(resolve, 20)); | ||
} | ||
|
||
selectCar(car: CarData) { | ||
this.selected = car; | ||
this.carSelected.emit(car); | ||
} | ||
|
||
render() { | ||
if (!Array.isArray(this.cars)) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ul> | ||
{this.cars.map((car) => { | ||
return ( | ||
<li class={car === this.selected ? 'selected' : ''}> | ||
<another-car-detail car={car}></another-car-detail> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { CarData } from '../car-list/car-data'; | ||
|
||
const vento = new CarData('VW', 'Vento', 2024); | ||
const beetle = new CarData('VW', 'Beetle', 2023); | ||
|
||
// @ts-ignore may not be existing when project hasn't been built | ||
type HydrateModule = typeof import('../../hydrate'); | ||
let renderToString: HydrateModule['renderToString']; | ||
|
||
describe('renderToString', () => { | ||
beforeAll(async () => { | ||
// @ts-ignore may not be existing when project hasn't been built | ||
const mod = await import('../../hydrate'); | ||
renderToString = mod.renderToString; | ||
}); | ||
|
||
it('allows to hydrate whole HTML page', async () => { | ||
const { html } = await renderToString( | ||
`<html> | ||
<head> | ||
<link rel="stylesheet" href="whatever.css" > | ||
</head> | ||
<body> | ||
<div class="__next"> | ||
<main> | ||
<car-list cars=${JSON.stringify([vento, beetle])}></car-list> | ||
</main> | ||
</div> | ||
<script type="module"> | ||
import { defineCustomElements } from "./static/loader/index.js"; | ||
defineCustomElements().catch(console.error); | ||
</script> | ||
</body> | ||
</html>`, | ||
{ fullDocument: true, serializeShadowRoot: false }, | ||
); | ||
/** | ||
* starts with a DocType and HTML tag | ||
*/ | ||
expect(html.startsWith('<!doctype html><html ')).toBeTruthy(); | ||
/** | ||
* renders hydration styles and custom link tag within the head tag | ||
*/ | ||
expect(html).toContain( | ||
'selected.sc-car-list{font-weight:bold;background:rgb(255, 255, 210)}</style><link rel="stylesheet" href="whatever.css"> </head> <body>', | ||
); | ||
}); | ||
|
||
it('allows to hydrate whole HTML page with using a scoped component', async () => { | ||
const { html } = await renderToString( | ||
`<html> | ||
<head> | ||
<link rel="stylesheet" href="whatever.css" > | ||
</head> | ||
<body> | ||
<div class="__next"> | ||
<main> | ||
<scoped-car-list cars=${JSON.stringify([vento, beetle])}></scoped-car-list> | ||
</main> | ||
</div> | ||
<script type="module"> | ||
import { defineCustomElements } from "./static/loader/index.js"; | ||
defineCustomElements().catch(console.error); | ||
</script> | ||
</body> | ||
</html>`, | ||
{ fullDocument: true, serializeShadowRoot: false }, | ||
); | ||
/** | ||
* starts with a DocType and HTML tag | ||
*/ | ||
expect(html.startsWith('<!doctype html><html ')).toBeTruthy(); | ||
/** | ||
* renders hydration styles and custom link tag within the head tag | ||
*/ | ||
expect(html).toContain( | ||
'.selected.sc-scoped-car-list{font-weight:bold;background:rgb(255, 255, 210)}</style><style sty-id="sc-another-car-detail">/*!@section*/section.sc-another-car-detail{color:green}</style><link rel="stylesheet" href="whatever.css"> </head> <body> <div class="__next"> <main> <scoped-car-list', | ||
); | ||
}); | ||
}); |
Oops, something went wrong.