| @@ -0,0 +1,86 @@ |
| 1 | +// <pebble-lite-youtube>: nothing from YouTube until the link is activated, |
| 2 | +// then the privacy-enhanced player in its place, focused; modified clicks and |
| 3 | +// bad ids are left to the link. |
| 4 | +import { afterAll, afterEach, beforeAll, expect, test } from "bun:test"; |
| 5 | +import { VIDEO_ID, embedUrl } from "../packages/lite-youtube/index"; |
| 6 | +import styles from "../packages/lite-youtube/lite-youtube.css"; |
| 7 | + |
| 8 | +const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)); |
| 9 | + |
| 10 | +// happy-dom loads iframes for real by default: the suite never reaches YouTube. |
| 11 | +const settings = (window as unknown as { happyDOM: { settings: { disableIframePageLoading: boolean } } }).happyDOM.settings; |
| 12 | +const loading = settings.disableIframePageLoading; |
| 13 | +beforeAll(() => void (settings.disableIframePageLoading = true)); |
| 14 | +afterAll(() => void (settings.disableIframePageLoading = loading)); |
| 15 | + |
| 16 | +const mount = (video = "dQw4w9WgXcQ", title = "Bande-annonce : Maman, j’ai raté l’avion") => { |
| 17 | + const wrap = document.createElement("div"); |
| 18 | + wrap.innerHTML = `<pebble-lite-youtube video="${video}" title="${title}"> |
| 19 | + <a href="https://www.youtube.com/watch?v=${video}"><img src="/affiches/yt-${video}.jpg" alt="L’affiche"><span class="play" aria-hidden="true">▶</span></a> |
| 20 | + </pebble-lite-youtube>`; |
| 21 | + document.body.appendChild(wrap); |
| 22 | + const host = wrap.querySelector("pebble-lite-youtube") as HTMLElement; |
| 23 | + return { host, link: host.querySelector("a")! }; |
| 24 | +}; |
| 25 | + |
| 26 | +afterEach(async () => { |
| 27 | + document.body.textContent = ""; |
| 28 | + await sleep(0); |
| 29 | +}); |
| 30 | + |
| 31 | +test("before activation the element holds the server's link and nothing from YouTube", () => { |
| 32 | + const { host } = mount(); |
| 33 | + expect(host.querySelector("iframe")).toBeNull(); |
| 34 | + expect(host.querySelector("a")!.getAttribute("href")).toBe("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); |
| 35 | + expect(document.querySelectorAll("link[rel=preconnect], script[src*=youtube]").length).toBe(0); |
| 36 | + expect(host.hasAttribute("data-playing")).toBe(false); |
| 37 | +}); |
| 38 | + |
| 39 | +test("a click replaces the link with the nocookie player, focused", () => { |
| 40 | + const { host, link } = mount(); |
| 41 | + const event = new MouseEvent("click", { bubbles: true, cancelable: true }); |
| 42 | + link.dispatchEvent(event); |
| 43 | + expect(event.defaultPrevented).toBe(true); |
| 44 | + const iframe = host.querySelector("iframe")!; |
| 45 | + expect(host.children.length).toBe(1); |
| 46 | + expect(iframe.getAttribute("src")).toBe("https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?autoplay=1&playsinline=1"); |
| 47 | + expect(iframe.getAttribute("allow")).toBe("autoplay; encrypted-media; picture-in-picture"); |
| 48 | + expect(iframe.hasAttribute("allowfullscreen")).toBe(true); |
| 49 | + expect(iframe.getAttribute("title")).toBe("Bande-annonce : Maman, j’ai raté l’avion"); |
| 50 | + expect(iframe.getAttribute("referrerpolicy")).toBe("strict-origin-when-cross-origin"); |
| 51 | + expect(host.hasAttribute("data-playing")).toBe(true); |
| 52 | + expect(document.activeElement).toBe(iframe); |
| 53 | +}); |
| 54 | + |
| 55 | +test("without a title, the poster's alt names the player", () => { |
| 56 | + const { host, link } = mount("dQw4w9WgXcQ", ""); |
| 57 | + link.click(); |
| 58 | + expect(host.querySelector("iframe")!.getAttribute("title")).toBe("L’affiche"); |
| 59 | +}); |
| 60 | + |
| 61 | +test("a modified click, or a video that is not an id, is left to the link", () => { |
| 62 | + const { host, link } = mount(); |
| 63 | + const ctrl = new MouseEvent("click", { bubbles: true, cancelable: true, ctrlKey: true }); |
| 64 | + link.dispatchEvent(ctrl); |
| 65 | + expect(ctrl.defaultPrevented).toBe(false); |
| 66 | + expect(host.querySelector("iframe")).toBeNull(); |
| 67 | + |
| 68 | + const bad = mount('x" onload="alert(1)'); |
| 69 | + const event = new MouseEvent("click", { bubbles: true, cancelable: true }); |
| 70 | + bad.link.dispatchEvent(event); |
| 71 | + expect(event.defaultPrevented).toBe(false); |
| 72 | + expect(bad.host.querySelector("iframe")).toBeNull(); |
| 73 | +}); |
| 74 | + |
| 75 | +test("ids and the embed address", () => { |
| 76 | + expect(VIDEO_ID.test("dQw4w9WgXcQ")).toBe(true); |
| 77 | + expect(VIDEO_ID.test("dQw4w9WgXc")).toBe(false); |
| 78 | + expect(VIDEO_ID.test("dQw4w9WgXc/")).toBe(false); |
| 79 | + expect(embedUrl("a_b-c")).toBe("https://www.youtube-nocookie.com/embed/a_b-c?autoplay=1&playsinline=1"); |
| 80 | +}); |
| 81 | + |
| 82 | +test("the sheet: 16:9, a 44px badge, dark defaults", () => { |
| 83 | + expect(styles).toMatch(/aspect-ratio:\s*16\s*\/\s*9/); |
| 84 | + expect(styles).toContain("44px"); |
| 85 | + expect(styles).toContain("prefers-color-scheme:dark"); |
| 86 | +}); |