Skip to content

Commit

Permalink
Fix slot attribute inside expressions (withastro#3837)
Browse files Browse the repository at this point in the history
* fix: use slots inside expressions

* test: add test for conditional named slots

* test: fix incorrect test fixture

* chore: update `@astrojs/compiler`

* chore: add test coverage for `switch`

Co-authored-by: Nate Moore <nate@astro.build>
  • Loading branch information
natemoo-re and natemoo-re authored Jul 7, 2022
1 parent 15cd894 commit eb05491
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"test:e2e:match": "playwright test -g"
},
"dependencies": {
"@astrojs/compiler": "^0.17.1",
"@astrojs/compiler": "^0.18.0",
"@astrojs/language-server": "^0.13.4",
"@astrojs/markdown-remark": "^0.11.3",
"@astrojs/prism": "0.4.1",
Expand Down
13 changes: 13 additions & 0 deletions src/runtime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ export async function renderSlot(_result: any, slotted: string, fallback?: any):
return fallback;
}

export function mergeSlots(...slotted: unknown[]) {
const slots: Record<string, () => any> = {};
for (const slot of slotted) {
if (!slot) continue;
if (typeof slot === 'object') {
Object.assign(slots, slot);
} else if (typeof slot === 'function') {
Object.assign(slots, mergeSlots(slot()));
}
}
return slots;
}

export const Fragment = Symbol('Astro.Fragment');

function guessRenderers(componentUrl?: string): string[] {
Expand Down
9 changes: 9 additions & 0 deletions test/astro-expr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,13 @@ describe('Expressions', () => {

expect($('#single-escape').html()).to.equal('Astro &amp; Vite');
});

it('Handles switch statements', async () => {
const html = await fixture.readFile('/switch/index.html');
const $ = cheerio.load(html);

expect($('#red').length).to.equal(0);
expect($('#yellow').length).to.equal(1);
expect($('#blue').length).to.equal(0);
});
});
10 changes: 10 additions & 0 deletions test/astro-slots.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ describe('Slots', () => {
expect($('#default').text().trim()).to.equal('Default');
});

it('Conditional named slots work', async () => {
const html = await fixture.readFile('/conditional/index.html');
const $ = cheerio.load(html);

expect($('#a').text().trim()).to.equal('A');
expect($('#b').text().trim()).to.equal('B');
expect($('#c').text().trim()).to.equal('C');
expect($('#default').text().trim()).to.equal('Default');
});

it('Slots render fallback content by default', async () => {
const html = await fixture.readFile('/fallback/index.html');
const $ = cheerio.load(html);
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/astro-expr/src/pages/switch.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
let title = 'Switch';
let colors = ['red', 'yellow', 'blue'];
---

<html>
<head>
<title>{title}</title>
</head>
<body>
{() => {
const color = colors[1];
switch (color) {
case 'red': return <div id="red">red</div>;
case 'yellow': return <div id="yellow">yellow</div>
case 'blue': return <div id="blue">blue</div>
}
}}
</body>
</html>
27 changes: 27 additions & 0 deletions test/fixtures/astro-slots/src/pages/conditional.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
import Slotted from '../components/Slotted.astro';
---

<html>
<head>
<!-- Head Stuff -->
</head>
<body>
<Slotted>
{true && <span slot="a">A</span>}
{true ? <span slot="b">B</span> : null}
{() => <span slot="c">C</span>}
{() => {
const value = 0.33;
if (value > 0.25) {
return <span>Default</span>
} else if (value > 0.5) {
return <span>Another</span>
} else if (value > 0.75) {
return <span>Other</span>
}
return <span>Yet Another</span>
}}
</Slotted>
</body>
</html>

0 comments on commit eb05491

Please sign in to comment.