A calm place to write long-form, and publish it to the open social web.
skypress.blog/
1// src/lib/embeds/facade.test.ts
2// @vitest-environment jsdom
3import { beforeEach, describe, expect, it } from 'vitest';
4import { activatePlay } from './facade';
5
6beforeEach( () => {
7 document.body.innerHTML = '';
8} );
9
10function facade( provider: string, id: string ): HTMLButtonElement {
11 const fig = document.createElement( 'figure' );
12 fig.className = 'wp-block-embed skypress-embed skypress-embed--video';
13 const btn = document.createElement( 'button' );
14 btn.className = 'skypress-embed__play';
15 btn.dataset.embedProvider = provider;
16 btn.dataset.embedId = id;
17 fig.appendChild( btn );
18 document.body.appendChild( fig );
19 return btn;
20}
21
22describe( 'activatePlay', () => {
23 it( 'replaces the button with a trusted youtube iframe', () => {
24 const btn = facade( 'youtube', 'dQw4w9WgXcQ' );
25 activatePlay( btn );
26 const iframe = document.querySelector( 'iframe' );
27 expect( iframe ).not.toBeNull();
28 expect( iframe!.getAttribute( 'src' ) ).toBe(
29 'https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?autoplay=1'
30 );
31 expect( document.querySelector( 'button' ) ).toBeNull();
32 } );
33
34 it( 'does nothing for an unknown provider (never builds an iframe)', () => {
35 const btn = facade( 'evil', 'x' );
36 activatePlay( btn );
37 expect( document.querySelector( 'iframe' ) ).toBeNull();
38 } );
39} );