|
| 1 | +// @vitest-environment jsdom |
| 2 | +import React from "react"; |
| 3 | +import { afterEach, describe, expect, it } from "vitest"; |
| 4 | +import { configureStore } from "@reduxjs/toolkit"; |
| 5 | +import type { Playlist } from "@hls-downloader/core/lib/entities"; |
| 6 | +import { rootReducer } from "@hls-downloader/core/lib/store/root-reducer"; |
| 7 | +import { Provider } from "react-redux"; |
| 8 | +import { act } from "react"; |
| 9 | +import { createRoot, Root } from "react-dom/client"; |
| 10 | +import SnifferModule from "./SnifferModule"; |
| 11 | + |
| 12 | +(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true; |
| 13 | + |
| 14 | +const roots: Array<{ root: Root; container: HTMLDivElement }> = []; |
| 15 | + |
| 16 | +const createPlaylist = ( |
| 17 | + id: string, |
| 18 | + uri: string, |
| 19 | + createdAt: number, |
| 20 | + pageTitle: string |
| 21 | +): Playlist => |
| 22 | + ({ |
| 23 | + id, |
| 24 | + uri, |
| 25 | + createdAt, |
| 26 | + pageTitle, |
| 27 | + initiator: "hls.js", |
| 28 | + } as Playlist); |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + for (const { root, container } of roots.splice(0)) { |
| 32 | + act(() => { |
| 33 | + root.unmount(); |
| 34 | + }); |
| 35 | + container.remove(); |
| 36 | + } |
| 37 | +}); |
| 38 | + |
| 39 | +function renderSniffer() { |
| 40 | + const store = configureStore({ |
| 41 | + reducer: rootReducer, |
| 42 | + preloadedState: { |
| 43 | + playlists: { |
| 44 | + playlists: { |
| 45 | + first: createPlaylist( |
| 46 | + "first", |
| 47 | + "https://example.com/first.m3u8", |
| 48 | + 2, |
| 49 | + "First Video" |
| 50 | + ), |
| 51 | + second: createPlaylist( |
| 52 | + "second", |
| 53 | + "https://example.com/second.m3u8", |
| 54 | + 1, |
| 55 | + "Second Video" |
| 56 | + ), |
| 57 | + }, |
| 58 | + playlistsStatus: { |
| 59 | + first: { status: "ready" }, |
| 60 | + second: { status: "ready" }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } as any, |
| 64 | + }); |
| 65 | + |
| 66 | + const container = document.createElement("div"); |
| 67 | + document.body.appendChild(container); |
| 68 | + const root = createRoot(container); |
| 69 | + roots.push({ root, container }); |
| 70 | + |
| 71 | + act(() => { |
| 72 | + root.render( |
| 73 | + <Provider store={store}> |
| 74 | + <SnifferModule /> |
| 75 | + </Provider> |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + return { container, store }; |
| 80 | +} |
| 81 | + |
| 82 | +function click(element: Element) { |
| 83 | + act(() => { |
| 84 | + element.dispatchEvent(new MouseEvent("click", { bubbles: true })); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +describe("SnifferModule", () => { |
| 89 | + it("removes one sniffed playlist without clearing the list", () => { |
| 90 | + const { container, store } = renderSniffer(); |
| 91 | + |
| 92 | + expect(container.textContent).toContain("First Video"); |
| 93 | + expect(container.textContent).toContain("Second Video"); |
| 94 | + |
| 95 | + const toggles = container.querySelectorAll( |
| 96 | + 'button[aria-label="Toggle playlist details"]' |
| 97 | + ); |
| 98 | + click(toggles[0]); |
| 99 | + |
| 100 | + const removeButtons = Array.from( |
| 101 | + container.querySelectorAll("button") |
| 102 | + ).filter((button) => button.textContent?.includes("Remove")); |
| 103 | + click(removeButtons[0]); |
| 104 | + |
| 105 | + expect(container.textContent).not.toContain("First Video"); |
| 106 | + expect(container.textContent).toContain("Second Video"); |
| 107 | + expect(store.getState().playlists.playlists.first).toBeUndefined(); |
| 108 | + expect(store.getState().playlists.playlists.second?.uri).toBe( |
| 109 | + "https://example.com/second.m3u8" |
| 110 | + ); |
| 111 | + }); |
| 112 | +}); |
0 commit comments