Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions apps/web/__tests__/unit/trimming-tool-effect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// @vitest-environment jsdom

import { createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { TrimmingTool } from "@/components/tools/TrimmingTool";

vi.mock("@cap/ui", () => ({
Button: () => null,
}));
vi.mock("@/app/utils/analytics", () => ({ trackEvent: vi.fn() }));

describe("TrimmingTool effects", () => {
let root: Root;
let container: HTMLDivElement;
let revoked: string[];
let originalCreate: typeof URL.createObjectURL;
let originalRevoke: typeof URL.revokeObjectURL;

beforeEach(() => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", false);
revoked = [];
originalCreate = URL.createObjectURL;
originalRevoke = URL.revokeObjectURL;
URL.createObjectURL = () => "blob:trim-1";
URL.revokeObjectURL = (url: string) => {
revoked.push(url);
};
container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
});

afterEach(() => {
root.unmount();
container.remove();
Comment thread
MinitJain marked this conversation as resolved.
URL.createObjectURL = originalCreate;
URL.revokeObjectURL = originalRevoke;
vi.unstubAllGlobals();
});

it("keeps the selected video source and revokes it on unmount", async () => {
root.render(createElement(TrimmingTool));
await vi.waitFor(() =>
expect(container.querySelector("input[type=file]")).not.toBeNull(),
);
const input = container.querySelector(
"input[type=file]",
) as HTMLInputElement;
const file = new File(["x"], "clip.mp4", { type: "video/mp4" });
Object.defineProperty(input, "files", { value: [file] });

input.dispatchEvent(new Event("change", { bubbles: true }));
await vi.waitFor(() =>
expect(container.querySelector("video")?.getAttribute("src")).toBe(
"blob:trim-1",
),
);

await new Promise<void>((resolve) => setTimeout(resolve, 50));
expect(container.querySelector("video")?.getAttribute("src")).toBe(
"blob:trim-1",
);
expect(revoked).not.toContain("blob:trim-1");

root.unmount();
expect(revoked).toContain("blob:trim-1");
});
});
26 changes: 21 additions & 5 deletions apps/web/components/tools/TrimmingTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@
try {
if (
(video as any).mozHasAudio ||
Boolean((video as any).webkitAudioDecodedByteCount) ||

Check notice on line 415 in apps/web/components/tools/TrimmingTool.tsx

View workflow job for this annotation

GitHub Actions / Lint (Biome)

lint/complexity/noExtraBooleanCast

Avoid redundant `Boolean` call
Boolean((video as any).audioTracks?.length)

Check notice on line 416 in apps/web/components/tools/TrimmingTool.tsx

View workflow job for this annotation

GitHub Actions / Lint (Biome)

lint/complexity/noExtraBooleanCast

Avoid redundant `Boolean` call
) {
const audioContext = new (
window.AudioContext || (window as any).webkitAudioContext
Expand Down Expand Up @@ -715,14 +715,30 @@
window.addEventListener("drop", preventDefaults);

return () => {
cleanupResources();
if (processingState.outputUrl) {
URL.revokeObjectURL(processingState.outputUrl);
}
window.removeEventListener("dragover", preventDefaults);
window.removeEventListener("drop", preventDefaults);
};
}, [cleanupResources, processingState.outputUrl]);
}, []);

const latestVideoSrcRef = useRef<string | null>(null);
latestVideoSrcRef.current = fileState.videoSrc;

useEffect(() => {
return () => {
if (latestVideoSrcRef.current) {
URL.revokeObjectURL(latestVideoSrcRef.current);
}
};
}, []);

useEffect(() => {
const outputUrl = processingState.outputUrl;
return () => {
if (outputUrl) {
URL.revokeObjectURL(outputUrl);
}
};
}, [processingState.outputUrl]);

return (
<div className="w-full">
Expand Down
Loading