Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | // noinspection DuplicatedCode
import type { StoryObj } from '@storybook/web-components';
import { expect, within } from '@storybook/test';
import '../custom-element/custom-element.js';
type TProps = { title: string; tag: string; attributes: string; slot: string; payload:string; };
type Story = StoryObj<TProps>;
function render(args: TProps)
{
const { title, tag, attributes, slot, payload} = args;
return `
<fieldset>
<legend>${ title }</legend>
<custom-element
tag="${ tag }"
hidden
>
${ slot }
</custom-element>
<${ tag } ${attributes}>${ payload }</${ tag }>
</fieldset>
`;
}
const meta =
{ title: 'slots'
, render
};
export default meta;
export const Regular:Story =
{ args:
{ title: 'custom-element'
, tag: 'my-component'
, slot: `Hello World`
, payload: `π`
}
, play: async ({canvasElement}) =>
{
const titleText = Regular.args!.title as string;
const canvas = within(canvasElement);
await canvas.findByText(titleText);
const el = canvasElement;
expect(el.querySelectorAll('my-component').length).to.equal(1);
expect(el.querySelector('my-component').innerHTML).to.include(`Hello World`);
}
};
export const NamedSlot:Story =
{ args:
{ title: 'named slot'
, tag: 'dce-1-slot'
, slot: `<slot name="slot1"> π₯¦</slot>`
, payload: `<i slot="slot1">π₯</i>`
}
, play: async ({canvasElement}) =>
{
const titleText = NamedSlot.args!.title as string;
const canvas = within(canvasElement);
await canvas.findByText(titleText);
const el = canvasElement;
expect(el.querySelectorAll('dce-1-slot').length).to.equal(1);
expect(el.querySelector('dce-1-slot').innerText).to.equal(`π₯`);
}
};
export const DoubleSlot:Story =
{ args:
{ title: 'same slot can be used multiple times in template'
, tag: 'dce-2-slots'
, slot: `<slot name="slot2"> π</slot> and again: <slot name="slot2"> π</slot>`
, payload: `<i slot="slot2">π₯</i>`
}
, play: async ({canvasElement}) =>
{
const titleText = DoubleSlot.args!.title as string;
const canvas = within(canvasElement);
await canvas.findByText(titleText);
const el = canvasElement;
expect(el.querySelectorAll('dce-2-slots').length).to.equal(1);
expect(el.querySelector('dce-2-slots').innerText).to.equal(`π₯ and again: π₯`);
}
};
export const NamedUnnamedDefaultSlot:Story =
{ args:
{ title: 'named and un-named default slot'
, tag: 'dce-4-slot'
, slot: `#1 <slot name=""> π</slot> <br/>
#2 <slot> π</slot>`
, payload: `<i slot="">π₯</i>`
}
, play: async ({canvasElement}) =>
{
const titleText = NamedUnnamedDefaultSlot.args!.title as string;
const canvas = within(canvasElement);
await canvas.findByText(titleText);
const el = canvasElement;
expect(el.querySelectorAll('dce-4-slot').length).to.equal(1);
expect(el.querySelector('dce-4-slot').innerText).to.include(`#1 π₯`);
expect(el.querySelector('dce-4-slot').innerText).to.include(`#2 π₯`);
}
};
export const NamedDefaultSlot:Story =
{ args:
{ title: 'two named default slot'
, tag: 'dce-3-slot'
, slot: `#1 <slot name=""> π</slot> <br/>
#2 <slot name=""> π</slot>`
, payload: `<i slot="">π₯</i>`
}
, play: async ({canvasElement}) =>
{
const titleText = NamedDefaultSlot.args!.title as string;
const canvas = within(canvasElement);
await canvas.findByText(titleText);
const el = canvasElement;
expect(el.querySelectorAll('dce-3-slot').length).to.equal(1);
expect(el.querySelector('dce-3-slot').innerText).to.include(`#1 π₯`);
expect(el.querySelector('dce-3-slot').innerText).to.include(`#2 π₯`);
}
};
export const DefaultSlot:Story =
{ args:
{ title: 'default slot'
, tag: 'greet-element'
, slot: `<slot> Hello </slot> World!`
, payload: `π`
}
, play: async ({canvasElement}) =>
{
const titleText = DefaultSlot.args!.title as string;
const canvas = within(canvasElement);
await canvas.findByText(titleText);
const el = canvasElement;
expect(el.querySelectorAll('greet-element').length).to.equal(1);
expect(el.querySelector('greet-element').innerText).to.include(`π World!`);
}
};
export const TemplateWithAttributesAndCondition:Story =
{ args:
{ title: 'πͺ DCE template'
, tag: 'pokemon-tile'
, attributes: ` title="bulbasaur" data-smile="πΌ" pokemon-id="1" `
, slot: `
<template>
<h3><value-of select="title"></value-of></h3> <!-- title is an attribute in instance
mapped into /*/attributes/title -->
<if test="//smile"> <!-- data-smile DCE instance attribute,
mapped into /*/dataset/smile
used in condition -->
<!-- data-smile DCE instance attribute, used as HTML -->
<div>Smile as: <value-of select="//smile"></value-of></div>
</if>
<!-- image would not be visible in sandbox, see live demo -->
<img src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/{pokemon-id}.svg" alt="{title} image">
<!-- image-src and title are DCE instance attributes,
mapped into /*/attributes/
used within output attribute via curly brackets -->
<!-- \`slot name=xxx\` replaced with elements with \`slot=xxx\` attribute -->
<p><slot name="description"><i>description is not available</i></slot></p>
<for-each select="//*[@pokemon-id]">
<!-- loop over payload elements with \`pokemon-id\` attribute -->
<button>
<img height="32"
src="https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/{@pokemon-id}.svg"
alt="{text()}"/>
<br/>
<value-of select='text()'/>
</button>
</for-each>
</template>
`
, payload: `<p slot="description">Bulbasaur is a cute PokΓ©mon born with a large seed firmly affixed to its back;
the seed grows in size as the PokΓ©mon does.</p>
<ul>
<li pokemon-id="2">ivysaur</li>
<li pokemon-id="3">venusaur</li>
</ul>
`
}
, play: async ({canvasElement}) =>
{
const titleText = TemplateWithAttributesAndCondition.args!.title as string;
const canvas = within(canvasElement);
await canvas.findByText(titleText);
const el = canvasElement;
expect(el.querySelectorAll('pokemon-tile').length).to.equal(1);
const p = el.querySelector('pokemon-tile');
expect(p.querySelector('img').src).to.equal('https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/1.svg');
expect(p.querySelector('h3').innerText).to.equal('bulbasaur');
expect(p.innerText).to.include('Smile as: πΌ');
expect(p.querySelector('[slot="description"]').innerText).to.include('Bulbasaur is a cute PokΓ©mon');
}
};
//#region unit tests
/* istanbul ignore else -- @preserve */
if( 'test' === import.meta.env.MODE &&
!import.meta.url.includes('skiptest') )
{
const mod = await import('./slots.test.stories.ts?skiptest');
const { testStoryBook } = await import('./testStoryBook')
const { describe } = await import('vitest')
describe(meta.title, () => testStoryBook( mod, meta ) );
}
//#endregion
|