All files / src/stories external-template.test.stories.ts

24.7% Statements 21/85
100% Branches 3/3
11.76% Functions 2/17
24.39% Lines 20/82

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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261                              12x 12x               1x             1x                                             1x                                   1x                                                         1x                                           1x                                       1x                                     1x                           1x                           1x                             1x                           1x                             1x                                 1x     1x 1x 1x 1x      
// noinspection DuplicatedCode
 
import type { StoryObj }             from '@storybook/web-components';
import {expect, getByTestId, within} from '@storybook/test';
 
import '../custom-element/custom-element.js';
import {Demo, SrcAttribute}          from './location-element.test.stories';
 
type TProps = { title: string; body:string};
 
type Story = StoryObj<TProps>;
function sleep(ms: number) {    return new Promise((resolve) => setTimeout(resolve, ms)); }
 
function render(args: TProps)
{
    const {title,  body} = args;
    return `
        <fieldset>
            <legend>${ title }</legend>
            ${ body }
        </fieldset>
  `;
}
const meta =
{   title:      'external-templates'
,   render
};
 
export default meta;
 
export const TemplateInPage:Story  =
{   args : {title: 'Template in page DOM', body:`
    <template id="template1">
    <slot>Hello</slot> World!
    </template>
 
    <custom-element tag="dce-internal" src="#template1"></custom-element>
    <!-- no need for loading fallback as the template exists -->
 
    <dce-internal data-testid="slot-override">👋</dce-internal>
    <dce-internal  data-testid="slot-default"></dce-internal>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(TemplateInPage.args!.title as string);
        const val = (prop:string)=> canvas.getByTestId(prop).textContent?.trim();
 
        expect(val('slot-override')).toEqual('👋 World!');
        expect(val('slot-default' )).toEqual('Hello World!');
    },
};
 
export const NoTag:Story  =
{   args : {title: 'no tag, template in same DOM', body:`
<template id="template2">
    🏗️ construction
</template>
 
<custom-element src="#template2"></custom-element>
<custom-element src="#template2"></custom-element>`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(NoTag.args!.title as string);
 
        expect(canvasElement.querySelectorAll('custom-element')[0].textContent.trim()).toEqual('🏗️ construction');
        expect(canvasElement.querySelectorAll('custom-element')[1].textContent.trim()).toEqual('🏗️ construction');
    },
};
 
export const ExternalSvg:Story  =
{   args : {title: 'External SVG as template', body:`
    <custom-element tag="dce-external" src="/confused.svg">
        <template><i>loading from SVG ...</i></template>
    </custom-element>
    <dce-external></dce-external>
    <custom-element src="confused.svg">
        <i>inline DCE loading from SVG ...</i>
    </custom-element>
    <custom-element src="no.svg">
        <i>fallback for missing image</i>
    </custom-element>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(ExternalSvg.args!.title as string);
 
        await expect(canvas.getByText('inline DCE loading from SVG ...')).toBeInTheDocument();
 
        // needs separate test
        // await expect( await canvas.findByText('loading from SVG ...')).toBeInTheDocument();
 
        expect(canvasElement.querySelector('[src="confused.svg"]').innerHTML).to.include('loading from SVG ...');
        await expect(await canvas.findByText('fallback for missing image')).toBeInTheDocument();
        await expect(await canvas.findByTitle('Confused')).toBeInTheDocument();
    },
};
 
export const ExternalXsltFile:Story  =
{   args : {title: 'External XSLT file', body:`
    <custom-element tag="dce-external-4" src="/tree.xsl">
        <template><i>loading from XSLT ...</i></template>
    </custom-element>
    <dce-external-4 title="DCE with external XSLT template" data-fruit="🍌">Hi</dce-external-4>
    <hr/>
    <custom-element src="/tree.xsl" data-smile="👼" data-basket="🍒">
        <i>inline DCE loading from XSLT ...</i>
    </custom-element>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(ExternalXsltFile.args!.title as string);
        expect(canvasElement.querySelector('dce-external-4').innerHTML).to.include('Hi');
        expect(canvasElement.querySelector('[data-smile="👼"]').innerHTML).to.include('loading from XSLT ...');
        await expect(await canvas.findByTestId('data-fruit')).toHaveTextContent('🍌');
        await expect(await canvas.findByTestId('data-smile')).toHaveTextContent('👼');
    },
};
 
export const ExternalHtmlFile:Story  =
{   args : {title: 'external HTML template', body:`
    <custom-element tag="dce-external-5" src="/html-template.html">
                <template><i>loading from HTML file ...</i></template>
            </custom-element>
            <dce-external-5 title="DCE with external XSLT template" data-fruit="🍌">Hi</dce-external-5>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(ExternalHtmlFile.args!.title as string);
        await canvas.findByText('👋');
        const t5 = canvasElement.querySelector('dce-external-5').innerHTML;
        expect(t5).to.include('👋');
        expect(t5).to.include('👌');
        expect(t5).to.include('<svg');
        expect(t5).to.include('<math');
    },
};
 
export const ExternalHtmlFileInline:Story  =
{   args : {title: 'external HTML template', body:`
            <custom-element src="/html-template.html" data-smile="👼" data-basket="🍒">
                <i>inline DCE loading from HTML file ...</i>
            </custom-element>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(ExternalHtmlFileInline.args!.title as string);
        await canvas.findByText('👋');
        const t5 = canvasElement.innerHTML;
        expect(t5).to.include('👋');
        expect(t5).to.include('👌');
        expect(t5).to.include('<svg');
        expect(t5).to.include('<math');
    },
};
 
export const HtmlWithinHtmlFile:Story  =
{   args : {title: 'external HTML template - html by id', body:`
        <custom-element src="/html-template.html#wave">
            <template><i>loading HTML from templates file ...</i></template>
        </custom-element>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(HtmlWithinHtmlFile.args!.title as string);
        expect(await canvas.findByText('👋')).toBeInTheDocument();
    },
};
 
export const SvgWithinHtmlFile:Story  =
{   args : {title: 'external HTML template - SVG by id', body:`
        <custom-element src="/html-template.html#dwc-logo">
            <template><i>loading  SVG from templates file ...</i></template>
        </custom-element>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(SvgWithinHtmlFile.args!.title as string);
        expect(await canvas.findByTestId('svg-test')).toBeInTheDocument();
    },
};
 
export const MathMLWithinHtmlFile:Story  =
{   args : {title: 'external HTML template - MathML by id', body:`
        <custom-element src="/html-template.html#sophomores-dream">
            <template><i>loading MathML from HTML file ...</i></template>
        </custom-element>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(MathMLWithinHtmlFile.args!.title as string);
        const ml = await canvas.findByTestId('ml-test');
        expect(ml.firstElementChild.localName).toEqual('mrow');
    },
};
 
export const ByIdWithinXsltFile:Story  =
{   args : {title: 'external XHTML template - xsl by id', body:`
        <custom-element src="/html-template.xhtml#embedded-xsl">
            <template>whole XSLT is  embedded into HTML body</template>
        </custom-element>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(ByIdWithinXsltFile.args!.title as string);
        const ml = await canvas.findByTestId('src');
        expect(ml.textContent).to.include('/html-template.xhtml#embedded-xsl');
    },
};
export const MissingIdWithinXsltFile:Story  =
{   args : {title: 'external HTML template - missing id', body:`
    <custom-element src="/html-template.html#none">
        <template><i data-testid="no-id">element with id=none is missing in template</i></template>
    </custom-element>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(MissingIdWithinXsltFile.args!.title as string);
        await sleep(100);
        const ml = await canvas.findByTestId('no-id');
        expect(ml.textContent).to.include('element with id=none is missing in template');
    },
};
export const EmbeddingInAnotherFile:Story  =
{   args : {title: 'external file with embedding of another external DCE', body:`
    <custom-element src="/embed-1.html">
        loading from embed-1.html ...
    </custom-element>
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(EmbeddingInAnotherFile.args!.title as string);
        await sleep(1);
        expect(await canvas.findByText('embed-1.html')).toBeInTheDocument();
        expect(await canvas.findByText('🖖')).toBeInTheDocument();
    },
};
 
//#region unit tests
/* istanbul ignore else -- @preserve */
if(  'test' === import.meta.env.MODE &&
    !import.meta.url.includes('skiptest') )
{
    const mod = await import('./external-template.test.stories.ts?skiptest');
    const { testStoryBook } = await import('./testStoryBook')
    const { describe } = await import('vitest')
    describe(meta.title, () => testStoryBook( mod, meta ) );
}
//#endregion