All files / src/stories frame.canvas.ts

84.61% Statements 11/13
66.66% Branches 4/6
100% Functions 3/3
84.61% Lines 11/13

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                                                  5x 5x   5x 5x 5x   5x   5x 5x 5x   5x   5x        
import {within} from "@storybook/test";
/*
    example of use
 
import {frameCanvas} from "./frame.canvas";
 
export const ModuleByName:Story  =
{   args : {title: '4. module path by symbolic name', body:`
    <iframe src="../demo/module-url-sb-4.html" name="sb" data-testid="fr"></iframe>
 
`}
,   play: async ({canvasElement}) =>
    {
        const canvas = within(canvasElement);
        await canvas.findByText(ModuleByName.args!.title as string);
        const frCanvas = await frameCanvas('fr',canvas);
 
        await expect(await frCanvas.findByText('👌 from embed-relative-hash invoking')).toBeInTheDocument();
    },
};
 */
    export async function
frameCanvas(frameTestId: string, canvas: ReturnType<typeof within>)
    : Promise<ReturnType<typeof within>>
{
    const frEl: HTMLIFrameElement = await canvas.findByTestId(frameTestId);
    return new Promise(resolve =>
    {
        const frameUrl = new URL(frEl.getAttribute('src')!, document.baseURI).href;
        let isResolved = false;
        const resolveCanvas = () =>
        {
            Iif( isResolved )
                return;
            isResolved = true;
            frEl.removeEventListener('load', resolveCanvas);
            resolve(within(frEl.contentWindow?.document.documentElement!));
        };
        frEl.addEventListener('load', resolveCanvas);
 
        Iif( frEl.contentDocument?.readyState === 'complete' && frEl.contentWindow?.location.href === frameUrl )
            resolveCanvas();
    });
}